<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>0branch.com :: blog - RSS feed</title>
    <link href="http://newblog.0branch.com/rss.xml" rel="self" />
    <link href="http://newblog.0branch.com" />
    <id>http://newblog.0branch.com/rss.xml</id>
    <author>
        <name>Marc Simpson</name>
        <email>marc@0branch.com</email>
    </author>
    <updated>2013-02-24T00:00:00Z</updated>
    <entry>
    <title>Batteries 2.0: Composition and application</title>
    <link href="http://newblog.0branch.com/posts/2013-02-24-batteries-v2-composition.html" />
    <id>http://newblog.0branch.com/posts/2013-02-24-batteries-v2-composition.html</id>
    <published>2013-02-24T00:00:00Z</published>
    <updated>2013-02-24T00:00:00Z</updated>
    <summary type="html"><![CDATA[<div id="blog_post">
  <div class="span-20">
    <div id="post_title" class="span-10"><h1>Batteries 2.0: Composition and application</h1></div>
    <div style="text-align: right; font-style: italic;"
      class="span-10 last" id="post_date"><a href="/index.html">Posted</a> on February 24, 2013</div>
  </div>

  <hr/>

  <div id="post_body">
    <p>A <a href="/posts/2012-04-17-haskell-application-ocaml.html">while back</a>, I discussed an implementation of the application operator (Haskell’s <code>$</code>) in OCaml. In the closing section of that post, a couple of problems were raised regarding treatment of associativity and composition in <em>OCaml Batteries</em>. These issues have been addressed in <em>Batteries</em> 2.0, released in January 2013; the improvements are outlined here.</p>
<h2 id="quick-recap">Quick recap</h2>
<p>Batteries <a href="http://ocaml-batteries-team.github.com/batteries-included/hdoc/BatPervasives.html">1.x</a> defines the following operators for composition and application:</p>
<ul>
<li><code>val ( -| ) : ('a -&gt; 'b) -&gt; ('c -&gt; 'a) -&gt; 'c -&gt; 'b</code>
<ul>
<li>“Function composition. <code>f -| g</code> is <code>fun x -&gt; f (g x)</code>. Mathematically, this is operator <code>o</code>.”</li>
</ul></li>
<li><code>val ( **&gt; ) : ('a -&gt; 'b) -&gt; 'a -&gt; 'b</code>
<ul>
<li>“Function application. <code>f **&gt; x</code> is equivalent to <code>f x</code>. This [operator] may be useful for composing sequences of function calls without too many parenthesis.”</li>
</ul></li>
</ul>
<p>The problem pointed out in the comments section (now the closing <em>Update</em>) of the <a href="/posts/2012-04-17-haskell-application-ocaml.html">post</a> was that</p>
<blockquote>
<p>the precedence you’d expect coming from Haskell is inverted. We’d need to define a new application operator to address this problem as the commenter suggested…</p>
</blockquote>
<p>Specifically, the following code sample was shown to exhibit surprising behaviour for anyone familiar with Haskell’s <code>.</code> and <code>$</code>,</p>
<pre><code># print_endline -| string_of_int **&gt; succ **&gt; sum [1; 2; 3];;
Error: This expression has type string but an expression was expected of type &#39;a -&gt; string</code></pre>
<h2 id="whats-up-doc">What’s up, doc?</h2>
<p>In Batteries <a href="http://ocaml-batteries-team.github.com/batteries-included/hdoc2/BatPervasives.html">2.0</a>, both operators have been renamed:</p>
<ul>
<li>Composition:
<ul>
<li><code>(-|)</code> is now <code>(%)</code></li>
</ul></li>
<li>Application:
<ul>
<li><code>(**&gt;)</code> is now <code>(@@)</code></li>
</ul></li>
</ul>
<p>To appreciate the behaviour of the new operators, we can once again consult (a subsection of) the operator associativity table from the <a href="http://caml.inria.fr/pub/docs/manual-ocaml/expr.html">language manual</a>:</p>
<table>
<thead>
<tr class="header">
<th align="left">Construction or operator</th>
<th align="left">Associativity</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left"><code>*...</code> <code>/...</code> <code>%...</code> <code>mod</code> <code>land</code> <code>lor</code> <code>lxor</code></td>
<td align="left">left</td>
</tr>
<tr class="even">
<td align="left"><code>+...</code> <code>-...</code></td>
<td align="left">left</td>
</tr>
<tr class="odd">
<td align="left"><code>::</code></td>
<td align="left">right</td>
</tr>
<tr class="even">
<td align="left"><code>@...</code> <code>^...</code></td>
<td align="left">right</td>
</tr>
<tr class="odd">
<td align="left"><code>=...</code> <code>&lt;...</code> <code>&gt;...</code> <code>|...</code> <code>&amp;...</code> <code>$...</code></td>
<td align="left">left</td>
</tr>
</tbody>
</table>
<p>A couple of things are worth noting:</p>
<ul>
<li>The associativity of the application operator has changed.
<ul>
<li><code>(**&gt;)</code> is left associative, <code>(@@)</code> right.</li>
<li>As discussed in the original post, right associativity is an integral feature of the application operator.</li>
</ul></li>
<li>In Batteries 1.x, application had higher precedence than composition.
<ul>
<li><code>(**&gt;)</code> is covered by the first row of the table, <code>(-|)</code> by the second.</li>
<li>As of Batteries 2.0, this precedence has been inverted.</li>
</ul></li>
</ul>
<h2 id="no-surprises">No surprises</h2>
<p>Reworking the above code sample for Batteries 2.0 is trivial—simply substitute <code>(%)</code> for <code>(-|)</code> and <code>(@@)</code> for <code>(**&gt;)</code>. With these changes in place, the code behaves as follows:</p>
<pre><code># print_endline % string_of_int @@ succ @@ sum [1; 2; 3];;
7
- : unit = ()</code></pre>
<p>Crucially, the application operator has lower precedence than the composition operator and is right associative.</p>
<p>Returning to the <a href="http://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Prelude.html#v:-36-">definition</a> of <code>$</code> referenced at the outset of the original post,</p>
<blockquote>
<p>This operator is redundant, since ordinary application <code>(f x)</code> means the same as <code>(f $ x)</code>. However, <code>$</code> has low, right-associative binding precedence, so it sometimes allows parentheses to be omitted.</p>
</blockquote>
<p>it should be apparent that these new operators closely conform to Haskell’s treatment of application and composition (in particular, associativity and precedence), allowing for a straightforward translation of the above expression:</p>
<pre><code>Prelude&gt; putStrLn . show $ succ $ sum [1, 2, 3]
7</code></pre>
  </div>
</div>

<hr/>

<div id="disqus_thread"></div>
<script type="text/javascript">
  var disqus_shortname = '0branch'; // required: replace example with your forum shortname
  /* * * DON'T EDIT BELOW THIS LINE * * */
  (function() {
  var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
  dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></summary>
</entry>
<entry>
    <title>Migration from Posterous</title>
    <link href="http://newblog.0branch.com/posts/2013-02-17-posterous-migration.html" />
    <id>http://newblog.0branch.com/posts/2013-02-17-posterous-migration.html</id>
    <published>2013-02-17T00:00:00Z</published>
    <updated>2013-02-17T00:00:00Z</updated>
    <summary type="html"><![CDATA[<div id="blog_post">
  <div class="span-20">
    <div id="post_title" class="span-10"><h1>Migration from Posterous</h1></div>
    <div style="text-align: right; font-style: italic;"
      class="span-10 last" id="post_date"><a href="/index.html">Posted</a> on February 17, 2013</div>
  </div>

  <hr/>

  <div id="post_body">
    <p>I’ve migrated this blog from <a href="https://posterous.com/">Posterous</a> to <a href="http://jaspervdj.be/hakyll/">Hakyll</a> following the (long anticipated) <a href="http://blog.posterous.com/thanks-from-posterous">announcement</a> that Posterous will be shut down in April. Unfortunately, comments for old posts have been lost.</p>
<p>Hopefully folks like the new layout; mobile stylesheet coming soon.</p>
  </div>
</div>

<hr/>

<div id="disqus_thread"></div>
<script type="text/javascript">
  var disqus_shortname = '0branch'; // required: replace example with your forum shortname
  /* * * DON'T EDIT BELOW THIS LINE * * */
  (function() {
  var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
  dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></summary>
</entry>
<entry>
    <title>The application operator in Standard ML</title>
    <link href="http://newblog.0branch.com/posts/2012-04-22-application-operator.html" />
    <id>http://newblog.0branch.com/posts/2012-04-22-application-operator.html</id>
    <published>2012-04-22T00:00:00Z</published>
    <updated>2012-04-22T00:00:00Z</updated>
    <summary type="html"><![CDATA[<div id="blog_post">
  <div class="span-20">
    <div id="post_title" class="span-10"><h1>The application operator in Standard ML</h1></div>
    <div style="text-align: right; font-style: italic;"
      class="span-10 last" id="post_date"><a href="/index.html">Posted</a> on April 22, 2012</div>
  </div>

  <hr/>

  <div id="post_body">
    <p>In the <a href="/posts/2012-04-17-haskell-application-ocaml.html">previous post</a> I introduced the <code>$</code> operator to OCaml using two different approaches:</p>
<ol style="list-style-type: decimal">
<li>Renaming the operator to <code>**$</code> or <code>@$</code> in order to achieve the necessary associativity.</li>
<li>Leveraging Camlp4 to provide a syntax extension.</li>
</ol>
<p>In the comments section<sup><a href="#fn1" class="footnoteRef" id="fnref1">1</a></sup>, variants of these operators were provided that mirror Haskell’s relative precedence of application and composition.</p>
<p>As a postscript, I thought it might be interesting to look at the implementation of <code>$</code> in Standard ML. Here it is in the <a href="http://www.smlnj.org">SML/NJ</a> toplevel,</p>
<pre><code>Standard ML of New Jersey v110.74 [built: Wed Apr 11 13:33:07 2012]
- infixr 0 $;
infixr $
- fun (f $ x) = f x;
val $ = fn : (&#39;a -&gt; &#39;b) * &#39;a -&gt; &#39;b</code></pre>
<p>Of note:</p>
<ul>
<li>Standard ML lets us specify the associativity of newly defined operators explicitly (using the <code>infix*</code> fixity directives) whereas OCaml follows an operator naming convention.</li>
<li>As such, we have no need to fall back on syntax extensions here; <code>$</code> is a valid name for a right associative operator.</li>
</ul>
<p>To replicate the target example of the previous post we’ll need to define a few utilities,</p>
<pre><code>- fun succ x = x + 1;
val succ = fn : int -&gt; int
- val sum = foldl op+ 0;
val sum = fn : int list -&gt; int
- fun printLn str = print $ str ^ &quot;\n&quot;;
val printLn = fn : string -&gt; unit</code></pre>
<p>Note that <code>printLn</code> is defined using <code>$</code>; the standard approach would be <code>fun printLn str = print (str ^ &quot;\n&quot;)</code>.</p>
<p>With these definitions in place, we can employ <code>$</code> to print the desired result:</p>
<pre><code>- printLn $ Int.toString $ succ $ sum [1, 2, 3];
7
val it = () : unit</code></pre>
<p>Finally, since <code>$</code> was defined with a precedence level of <code>0</code>, it interacts correctly with SML’s composition operator, <code>o</code>, which has a precedence of <code>3</code> (as per the Standard):</p>
<pre><code>- printLn o Int.toString $ succ $ sum [1, 2, 3];
7
val it = () : unit</code></pre>
<div class="footnotes">
<hr />
<ol>
<li id="fn1"><p>See the closing <em>Update</em>.<a href="#fnref1">↩</a></p></li>
</ol>
</div>
  </div>
</div>

<hr/>

<div id="disqus_thread"></div>
<script type="text/javascript">
  var disqus_shortname = '0branch'; // required: replace example with your forum shortname
  /* * * DON'T EDIT BELOW THIS LINE * * */
  (function() {
  var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
  dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></summary>
</entry>
<entry>
    <title>Implementing Haskell's $ in OCaml</title>
    <link href="http://newblog.0branch.com/posts/2012-04-17-haskell-application-ocaml.html" />
    <id>http://newblog.0branch.com/posts/2012-04-17-haskell-application-ocaml.html</id>
    <published>2012-04-17T00:00:00Z</published>
    <updated>2012-04-17T00:00:00Z</updated>
    <summary type="html"><![CDATA[<div id="blog_post">
  <div class="span-20">
    <div id="post_title" class="span-10"><h1>Implementing Haskell's $ in OCaml</h1></div>
    <div style="text-align: right; font-style: italic;"
      class="span-10 last" id="post_date"><a href="/index.html">Posted</a> on April 17, 2012</div>
  </div>

  <hr/>

  <div id="post_body">
    <p>Haskell provides a convenient function application operator, <code>$</code>, that can improve code readability and concision. This post discusses the problems that arise in trying to implement an identical operator in OCaml.</p>
<h3 id="why-do-we-need-an-application-operator">Why do we need an application operator?</h3>
<p>In most situations, we don’t. Having said that, <code>$</code> can be useful – its right-associativity sometimes obviates the need for parentheses which can result in cleaner code. As the <a href="http://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Prelude.html#v:-36-">Haskell documentation</a> states:</p>
<blockquote>
<p>This operator is redundant, since ordinary application (f x) means the same as (f $ x). However, $ has low, right-associative binding precedence, so it sometimes allows parentheses to be omitted.</p>
</blockquote>
<p>It turns out that implementing <code>$</code> in OCaml is an interesting undertaking in itself.</p>
<h3 id="the-shape-of-things-to-come">The shape of things to come</h3>
<p>When we’re done, the following code should compile and print “7” upon execution:</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="kw">let</span> sum = List<span class="kw">.</span>fold_left (+) 0
<span class="kw">let</span> res xs = string_of_int $ succ $ sum xs
<span class="kw">let</span> () = print_endline $ res [1; 2; 3]</code></pre>
<p>By definition of <code>$</code>, <code>res [1; 2; 3]</code> is equivalent to the expression <code>string_of_int (succ (sum [1; 2; 3]))</code>.</p>
<h2 id="naive-approach">Naive approach</h2>
<p>As a first pass we could try implementing <code>$</code> as follows:</p>
<pre><code># let ($) f x = f x;;
val ( $ ) : (&#39;a -&gt; &#39;b) -&gt; &#39;a -&gt; &#39;b = &lt;fun&gt;</code></pre>
<p>Testing on a simple case suggests that this implementation is a suitable analogue of Haskell’s operator:</p>
<pre><code># let sum xs = List.fold_left (+) 0 xs;;
val sum : int list -&gt; int = &lt;fun&gt;
# succ $ sum [1; 2; 3];;
- : int = 7</code></pre>
<p>However, problems become apparent when we attempt to chain together more than two functions. As an example, let’s try out the body of <code>res</code>:</p>
<pre><code># string_of_int $ succ $ sum [1; 2; 3];;
Error: This expression has type int -&gt; int
       but an expression was expected of type int</code></pre>
<p>The reported error indicates that <code>string_of_int</code> was applied to a function from <code>int -&gt; int</code> rather than an <code>int</code> as intended: our operator is <em>left-associative</em>. <code>string_of_int</code> is being passed the <code>succ</code> function rather than the result of applying <code>succ</code> to the <code>sum</code> of <code>[1; 2; 3]</code>.</p>
<p>Perhaps this is unsurprising given that we didn’t specify associativity anywhere. Here’s the interesting bit: the associativity of our operator is a direct consequence of its <em>name</em>. To clarify, we’ll need to consult the operator associativity table in the <a href="http://caml.inria.fr/pub/docs/manual-ocaml/expr.html">language manual</a>. The relevant row is repeated below:</p>
<ul>
<li>Construction or operator
<ul>
<li><code>=... &lt;... &gt;... |... &amp;... $...</code></li>
</ul></li>
<li>Associativity: left</li>
</ul>
<p>In other words: any operator identifier beginning with <code>$</code> is left associative. To get the right-associativity, a right-associative prefix must be used:</p>
<ul>
<li>Construction or operator
<ul>
<li><code>@... ^...</code></li>
<li><code>**... lsl lsr asr</code></li>
</ul></li>
<li>Associativity: <em>right</em></li>
</ul>
<p>So to fix our earlier implementation we simply need to rename <code>$</code>; here are two possible implementations that behave correctly:</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="kw">let</span> ( **$ ) f x = f x;;
<span class="kw">let</span> (  @$ ) f x = f x;;</code></pre>
<p>Whichever we choose, the toplevel expression now evaluates as anticipated:</p>
<pre><code># string_of_int **$ succ **$ sum [1; 2; 3];;
- : string = &quot;7&quot;
# string_of_int @$ succ @$ sum [1; 2; 3];;
- : string = &quot;7&quot;</code></pre>
<p>As a final note: there’s nothing special about non-prefix <code>$</code> when used as part of an operator name – it has been retained for the sake of consistency. If you’ve used <em>OCaml Batteries Included</em>, you might find the first operator familiar – in <a href="http://ocaml-batteries-team.github.com/batteries-included/hdoc/BatPervasives.html">BatPervasives</a> it’s named <code>**&gt;</code>; the leading <code>**</code> is necessary for the reasons outlined above.</p>
<h2 id="enter-camlp4">Enter Camlp4</h2>
<p>In the previous section we implemented <code>$</code> but were forced – due to grammatical constraints – to settle for slightly cryptic operator names. Can we do any better? After all, the goal of this post is to compile the snippet provided in the opening section. So far it can only be approximated as follows:</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="kw">let</span> sum = List<span class="kw">.</span>fold_left (+) 0
<span class="kw">let</span> res xs = string_of_int @$ succ @$ sum xs
<span class="kw">let</span> () = print_endline @$ res [1; 2; 3]</code></pre>
<p>We need a way of telling OCaml that <code>$</code> is a right-associative operator <em>in spite of its name</em>. If that sentence makes you wonder whether this is even possible given the grammatical constraints outlined above, you’re on the right track – it’s not. Instead, we’ll need to write a <em>syntax extension</em> with the help of <em>Camlp4</em> that allows us to (in a sense) “violate” the operator naming rules. More accurately: to extend the grammar such that <code>$</code> is defined as a right-associative operator.</p>
<h4 id="camlp4whatnow">Camlp4whatNow?</h4>
<p>To quote the <a href="http://caml.inria.fr/pub/docs/manual-camlp4/manual001.html">documentation</a>,</p>
<blockquote>
<p>Camlp4 is a Pre-Processor-Pretty-Printer for OCaml. It offers syntactic tools (parsers, extensible grammars), the ability to extend the concrete syntax of OCaml (quotations, syntax extensions), and to redefine it from scratch.</p>
</blockquote>
<p>Of relevance here is the ability to extend OCaml’s (concrete) syntax.</p>
<h3 id="writing-the-extension">Writing the extension</h3>
<p>While Camlp4 can be somewhat esoteric (the latest version isn’t officially documented), our requirements are simple enough that a simple 4 line module will do the trick.</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="ot">open</span> Camlp4<span class="kw">.</span>PreCast<span class="kw">.</span>Syntax
<span class="dt">EXTEND</span> <span class="dt">Gram</span>
    expr: <span class="dt">BEFORE</span> <span class="st">&quot;apply&quot;</span>
        [ <span class="st">&quot;applOp&quot;</span> <span class="dt">RIGHTA</span> [ f = expr; <span class="st">&quot;$&quot;</span>; x = expr -&gt; <span class="st">&lt;:expr&lt;$f$ $x$&gt;&gt;</span> ]];
<span class="dt">END</span></code></pre>
<p>The above code extends the default OCaml grammar <code>Gram</code> by adding a new rule under the <code>expr</code> entry. This rule</p>
<ul>
<li>extends the <code>expr</code> (expression) entry by inserting the rule at the supplied precedence level – before application;</li>
<li>is named <em>applOp</em> (an arbitrarily chosen name);</li>
<li>is right-associative (<code>RIGHTA</code>);</li>
<li>rewrites <code>f $ x</code> as <code>f x</code> for all expressions (<code>expr</code>s) <code>f</code>, <code>x</code>.</li>
</ul>
<p>To make use of this extension it must first be compiled – here with the help of <code>ocamlfind</code>,</p>
<pre><code>$ ocamlfind ocamlc -linkpkg -syntax camlp4o \
    -package camlp4.extend -package camlp4.quotations -c appop.ml</code></pre>
<p>and passed to the <code>-pp</code> switch of <code>ocamlc</code> during batch compilation of <code>dollar.ml</code>,</p>
<pre><code>$ ocamlc -pp &quot;camlp4o ./appop.cmo&quot; dollar.ml -o dollar
$ ./dollar
7</code></pre>
<p>To see what’s going on under the hood, we can pre-process <code>dollar.ml</code> and output the (source) result to <code>stdout</code>,</p>
<pre><code>$ camlp4o ./appop.cmo dollar.ml
let sum = List.fold_left ( + ) 0

let res xs = string_of_int (succ (sum xs))

let () = print_endline (res [ 1; 2; 3 ])</code></pre>
<p>Finally, the extension can also be used interactively from the toplevel,</p>
<pre><code># #use &quot;topfind&quot;;;
- : unit = ()
Findlib has been successfully loaded. Additional directives:
  #require &quot;package&quot;;;      to load a package
  #list;;                   to list the available packages
  #camlp4o;;                to load camlp4 (standard syntax)
  #camlp4r;;                to load camlp4 (revised syntax)
  #predicates &quot;p,q,...&quot;;;   to set these predicates
  Topfind.reset();;         to force that packages will be reloaded
  #thread;;                 to enable threads

- : unit = ()

# #camlp4o;;
/opt/godi/lib/ocaml/std-lib/dynlink.cma: loaded
/opt/godi/lib/ocaml/std-lib/camlp4: added to search path
/opt/godi/lib/ocaml/std-lib/camlp4/camlp4o.cma: loaded
    Camlp4 Parsing version 3.12.1

# #load &quot;appop.cmo&quot;;;

# let sum = List.fold_left (+) 0;;
val sum : int list -&gt; int = &lt;fun&gt;

# string_of_int $ succ $ sum [1; 2; 3];;
- : string = &quot;7&quot;</code></pre>
<h2 id="a-health-warning">A health warning</h2>
<p>This post is simply meant to introduce the relationship between associativity and operator naming–I don’t meant to suggest that the above syntax extension is the “best” solution. On the contrary, accomodating OCaml’s naming conventions is appealing for two reasons:</p>
<ol style="list-style-type: decimal">
<li>Simpler implementation;</li>
<li>Satisfies the grammatical expectations of its clients.</li>
</ol>
<p>Having said that, it’s both useful and reassuring to know that should we need to “violate” these conventions, Camlp4 is there to help.</p>
<h2 id="update">Update</h2>
<p>A commenter on the original post (<code>@ManInTheMiddle</code>) pointed out that</p>
<blockquote>
<p>Considering the non-Camlp4 approach, it seems to me your solution misses the point that the <code>$</code> operator should also mix well with function composition (Haskell <code>.</code> operator). This operator is left-associative and should be of higher-precedence than function application. So if you choose the <code>**...</code> prefix for <code>$</code> then you will not find any such operator for composition.Hence I suggest using only <code>@...</code> or <code>^...</code> for application. Then you can choose <code>*...</code>, <code>/...</code> or <code>%...</code> for composition.</p>
</blockquote>
<p>I agree – the above glosses over the relationship between precedence and operator naming. Interestingly, the Batteries project uses <code>(-|)</code> for composition and <code>(**&gt;)</code> for application (see <a href="http://ocaml-batteries-team.github.com/batteries-included/hdoc/BatPervasives.html">here</a>); the precedence you’d expect coming from Haskell is inverted. We’d need to define a new application operator to address this problem as the commenter suggested:</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="kw">let</span> (^^) f x = f x</code></pre>
<p>yielding,</p>
<pre><code># print_endline -| string_of_int **&gt; succ **&gt; sum [1; 2; 3];;
Error: This expression has type string but an expression was expected of type &#39;a -&gt; string
# print_endline -| string_of_int ^^ succ ^^ sum [1; 2; 3];;
7</code></pre>
<p>Along similar lines, an improvement to the syntax extension can also be made,</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="ot">open</span> Camlp4<span class="kw">.</span>PreCast<span class="kw">.</span>Syntax
<span class="dt">EXTEND</span> <span class="dt">Gram</span>
    expr: <span class="dt">BEFORE</span> <span class="st">&quot;^&quot;</span>
        [ <span class="st">&quot;applOp&quot;</span> <span class="dt">RIGHTA</span> [ f = expr; <span class="st">&quot;$&quot;</span>; x = expr -&gt; <span class="st">&lt;:expr&gt;&lt;$f$&gt;&gt;</span> ]];
<span class="dt">END</span></code></pre>
<p>thereby licensing:</p>
<pre><code># print_endline -| string_of_int $ succ $ sum [1; 2; 3];;
7
- : unit = ()</code></pre>
  </div>
</div>

<hr/>

<div id="disqus_thread"></div>
<script type="text/javascript">
  var disqus_shortname = '0branch'; // required: replace example with your forum shortname
  /* * * DON'T EDIT BELOW THIS LINE * * */
  (function() {
  var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
  dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></summary>
</entry>
<entry>
    <title>From Functor to Applicative</title>
    <link href="http://newblog.0branch.com/posts/2012-03-26-02-from-functor.html" />
    <id>http://newblog.0branch.com/posts/2012-03-26-02-from-functor.html</id>
    <published>2012-03-26T00:00:00Z</published>
    <updated>2012-03-26T00:00:00Z</updated>
    <summary type="html"><![CDATA[<div id="blog_post">
  <div class="span-20">
    <div id="post_title" class="span-10"><h1>From Functor to Applicative</h1></div>
    <div style="text-align: right; font-style: italic;"
      class="span-10 last" id="post_date"><a href="/index.html">Posted</a> on March 26, 2012</div>
  </div>

  <hr/>

  <div id="post_body">
    <p>In the <a href="/posts/2012-03-26-01-implementing-functor-ocaml.html">previous post</a> we looked at the <em>Functor</em> structure; now we turn to <em>Applicative Functor</em>. One important enrichment is that applicatives allow for the application of functions that are in some context <code>t</code> to values in the same context.</p>
<p>Once more, a couple of definitions to get things rolling,</p>
<ul>
<li>Given a function in context, <code>val f: ('a -&gt; 'b) t</code>, <em>Applicative Functor</em> provides an operation, <code>&lt;*&gt;</code>, such that <code>f</code> can be applied to values in like context: <code>val (&lt;*&gt;) : ('a -&gt; 'b) t -&gt; 'a t -&gt; 'b t</code>.</li>
<li>Given a value <code>'a</code>, the <em>Applicative Functor</em> function <code>pure</code> will lift <code>'a</code> into the appropriate (“default” or pure) context: <code>val pure: 'a -&gt; 'a t</code>.</li>
</ul>
<p>Something that I glossed over last time around are the laws associated with the structures under discussion. A module can implement a signature validly while violating these laws. For <em>Functor</em>, the laws are as follows:</p>
<ol style="list-style-type: decimal">
<li>Mapping the identity function over a value in context <code>t</code> should have no effect – i.e., <code>fmap id = id</code>.
<ul>
<li>For example, <code>OptionFunctor.fmap (fun x -&gt; x) (Some 5) = Some 5</code> and <code>EitherStringFunctor.fmap (fun x -&gt; x) (Left &quot;Oops&quot;) = Left &quot;Oops&quot;</code>.</li>
</ul></li>
<li>Mapping the composition of functions <code>g</code> and <code>h</code> over a value in context <code>t</code> should be equivalent to mapping function <code>g</code> over this value, then mapping function <code>h</code> over the output (i.e., the composition of mapping <code>g</code> and mapping <code>h</code>).</li>
</ol>
<p>The salient law for <em>Applicative Functor</em> is addressed below.</p>
<h2 id="applicative-structure">Applicative Structure</h2>
<p>Let’s begin by providing the signature for an <em>Applicative Functor</em>, defined in terms of <code>Functor</code>:</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="ot">module</span> <span class="kw">type</span> <span class="dt">Functor</span> = <span class="kw">sig</span>
    <span class="kw">type</span> &#39;a t
    <span class="kw">val</span> fmap: (&#39;a -&gt; &#39;b) -&gt; &#39;a t -&gt; &#39;b t
<span class="kw">end</span>

<span class="ot">module</span> <span class="kw">type</span> <span class="dt">ApplicativeFunctor</span> = <span class="kw">sig</span>
    <span class="ot">include</span> Functor

    <span class="kw">val</span> pure: &#39;a -&gt; &#39;a t
    <span class="kw">val</span> (&lt;*&gt;): (&#39;a -&gt; &#39;b) t -&gt; &#39;a t -&gt; &#39;b t
    <span class="kw">val</span> (&lt;$&gt;): (&#39;a -&gt; &#39;b)   -&gt; &#39;a t -&gt; &#39;b t
<span class="kw">end</span></code></pre>
<p>In addition to the signatures mentioned in the introduction, we also define a function (operator) named <code>&lt;$&gt;</code> with a signature identical to <code>fmap</code>. Strictly speaking, this should be declared in the <code>Functor</code> signature… since we’ll only be using it with applicatives, though, the above factoring will suffice.</p>
<h2 id="first-out-of-the-blocks-option">First out of the blocks, option</h2>
<p>As before, we’ll start out by implementing something simple – an <em>Applicative Functor</em> module for <code>option</code>s. To save some space, only incremental additions will be supplied inline. Here’re our two implementations alongside a simple demo,</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="ot">module</span> OptionFunctor : <span class="dt">Functor</span>
<span class="kw">with</span> <span class="kw">type</span> &#39;a t = &#39;a <span class="dt">option</span> = <span class="kw">struct</span>
    <span class="kw">type</span> &#39;a t = &#39;a <span class="dt">option</span>
    <span class="kw">let</span> fmap f = <span class="kw">function</span>
        <span class="dt">Some</span> a  -&gt; <span class="dt">Some</span> (f a)
        | _     -&gt; <span class="dt">None</span>
<span class="kw">end</span>

<span class="ot">module</span> OptionApplicativeF : <span class="dt">ApplicativeFunctor</span>
<span class="kw">with</span> <span class="kw">type</span> &#39;a t = &#39;a <span class="dt">option</span> = <span class="kw">struct</span>
    <span class="ot">include</span> OptionFunctor

    <span class="kw">let</span> pure x = <span class="dt">Some</span> x

    <span class="kw">let</span> (&lt;*&gt;) f a = <span class="kw">match</span> (f, a) <span class="kw">with</span>
        (<span class="dt">Some</span> f, <span class="dt">Some</span> a)    -&gt; <span class="dt">Some</span> (f a)
        | _                 -&gt; <span class="dt">None</span>

    <span class="kw">let</span> (&lt;$&gt;) = fmap
<span class="kw">end</span>

<span class="ot">module</span> Demo1(<span class="dt">A:</span> <span class="dt">ApplicativeFunctor</span>) = <span class="kw">struct</span>
    <span class="ot">include</span> A
    <span class="kw">let</span> double x = x * 2
    <span class="kw">let</span> eg1 x    = double &lt;$&gt; x
    <span class="kw">let</span> eg2 x y  = pure (+) &lt;*&gt; x &lt;*&gt; y
    <span class="kw">let</span> eg3 x y  = (+) &lt;$&gt; x &lt;*&gt; y
<span class="kw">end</span>

<span class="ot">module</span> OptionDemo1 = <span class="dt">Demo1</span>(<span class="dt">OptionApplicativeF</span>)</code></pre>
<p>The inheritance relationship indicated in the module signatures is mirrored in in the implementations – <code>OptionApplicativeF</code> is defined in terms of <code>OptionFunctor</code>: <code>fmap</code> is borrowed; <code>pure</code>, <code>&lt;*&gt;</code> and <code>&lt;$&gt;</code> defined explicitly.</p>
<p>Without further ado, the examples in action:</p>
<pre><code># OptionDemo1.eg1 (Some 5);;
- : int OptionDemo1.t = Some 10

# OptionDemo1.eg1 None;;
- : int OptionDemo1.t = None

# OptionDemo1.eg2 (Some 5) (Some 10);;
- : int OptionDemo1.t = Some 15

# OptionDemo1.eg2 (Some 5) None;;
- : int OptionDemo1.t = None

# OptionDemo1.eg2 None (Some 10);;
- : int OptionDemo1.t = None

# OptionDemo1.eg2 None None;;
- : int OptionDemo1.t = None

# OptionDemo1.eg3 (Some 5) (Some 10);;
- : int OptionDemo1.t = Some 15

# OptionDemo1.eg3 (Some 5) None;;
- : int OptionDemo1.t = None

# OptionDemo1.eg3 None (Some 10);;
- : int OptionDemo1.t = None

# OptionDemo1.eg3 None None;;
- : int OptionDemo1.t = None</code></pre>
<p>Notice how <code>Demo.eg2</code> and <code>Demo.eg3</code> are functionally identical in spite of the variation in their definitions. Where <code>eg2</code> raises <code>+</code> into context <code>t</code> with <code>pure</code> before applying <code>&lt;*&gt;</code>, <code>eg3</code> simply <code>fmap</code>s via the convenience operator <code>&lt;$&gt;</code>. This is essentially syntactic sugar, allowing for a pipelined style. Here we’ve hit on the salient law for <em>Applicative Functor</em>:</p>
<pre><code>fmap g x = pure g &lt;*&gt; x</code></pre>
<p>or using <code>&lt;$&gt;</code>:</p>
<pre><code>g &lt;$&gt; x = pure g &lt;*&gt; x</code></pre>
<p>Before we leave <code>OptionApplicativeF</code>, consider the following case in which the function itself is absent (i.e., <code>None</code>) in a <code>&lt;*&gt;</code> chain:</p>
<pre><code># None &lt;*&gt; (Some 1) &lt;*&gt; (Some 2);;
- : &#39;_a t = None</code></pre>
<p>This follows from definition of <code>&lt;*&gt;</code>: unless both the function and its argument are present (<code>Some _</code>), the result of <code>&lt;*&gt;</code> is <code>None</code>.</p>
<h2 id="adding-either-into-the-mix">Adding either into the mix</h2>
<p>To make things more interesting, we’ll re-introduce <code>either</code> and provide an <code>ApplicativeFunctor</code> implementation:</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="kw">type</span> (&#39;a, &#39;b) either = <span class="dt">Left</span> <span class="kw">of</span> &#39;a | <span class="dt">Right</span> <span class="kw">of</span> &#39;b

<span class="ot">module</span> <span class="kw">type</span> <span class="dt">Typed</span> = <span class="kw">sig</span> <span class="kw">type</span> t <span class="kw">end</span>

<span class="ot">module</span> EitherFunctor(<span class="dt">T:</span> <span class="dt">Typed</span>) : <span class="dt">Functor</span>
<span class="kw">with</span> <span class="kw">type</span> &#39;a t = (T<span class="kw">.</span>t, &#39;a) either = <span class="kw">struct</span>
    <span class="kw">type</span> &#39;a t = (T<span class="kw">.</span>t, &#39;a) either
    <span class="kw">let</span> fmap f = <span class="kw">function</span>
        <span class="dt">Right</span> r     -&gt; <span class="dt">Right</span> (f r)
        | <span class="dt">Left</span> l    -&gt; <span class="dt">Left</span> l
<span class="kw">end</span>

<span class="ot">module</span> EitherApplicativeF(<span class="dt">T:</span> <span class="dt">Typed</span>) : <span class="dt">ApplicativeFunctor</span>
<span class="kw">with</span> <span class="kw">type</span> &#39;a t = (T<span class="kw">.</span>t, &#39;a) either = <span class="kw">struct</span>
    <span class="ot">include</span> EitherFunctor(<span class="dt">T</span>)

    <span class="kw">let</span> pure x = <span class="dt">Right</span> x

    <span class="kw">let</span> (&lt;*&gt;) f a = <span class="kw">match</span> (f, a) <span class="kw">with</span>
        (<span class="dt">Right</span> f, <span class="dt">Right</span> a)  -&gt; <span class="dt">Right</span> (f a)
        | (<span class="dt">Left</span> err, _)     -&gt; <span class="dt">Left</span> err
        | (_, <span class="dt">Left</span> err)     -&gt; <span class="dt">Left</span> err

    <span class="kw">let</span> (&lt;$&gt;) = fmap
<span class="kw">end</span>

<span class="co">(* Demo1 as before *)</span>
<span class="ot">module</span> EitherDemo1 = <span class="dt">Demo1</span>(<span class="dt">EitherApplicativeF</span>(<span class="dt">String</span>))</code></pre>
<p>As with <code>OptionFunctorF</code>, the only success condition accounted for by <code>&lt;*&gt;</code> is where both the function and its value are valid – in the <code>option</code> case, <code>Some _</code> and in the <code>either</code> case, <code>Right _</code>. If the first clause of <code>match</code> can be unified, <code>f</code> is applied to <code>a</code> and wrapped with the <code>Right</code> constructor as an indication of success. In the other two cases, the error (typed <code>T.t</code>) is propagated.</p>
<p>The examples in <code>EitherDemo1</code> should yield few surprises,</p>
<pre><code># EitherDemo1.eg1 (Right 5);;
- : int EitherDemo1.t = Right 10

# EitherDemo1.eg3 (Right 5) (Right 10);;
- : int EitherDemo1.t = Right 15

# EitherDemo1.eg3 (Right 5) (Left &quot;Oops&quot;);;
- : int EitherDemo1.t = Left &quot;Oops&quot;

# EitherDemo1.eg3 (Left &quot;Um&quot;) (Left &quot;Oops&quot;);;
- : int EitherDemo1.t = Left &quot;Um&quot;

# EitherDemo1.eg3 (Left &quot;Um&quot;) (Right 15);;
- : int EitherDemo1.t = Left &quot;Um&quot;</code></pre>
<h2 id="recognising-abstractions">Recognising abstractions</h2>
<p>The demonstration functions defined in <code>Demo1</code> share a common semantic purpose: generalising a given function <code>f</code> so that it can apply in a context <code>t</code>. This is known as <em>lifting</em>.</p>
<p>By way of example: Consider the <code>double</code> function: this has the signature <code>val double : int -&gt; int</code>; in <code>Demo1.eg1</code> we wish to <em>lift</em> <code>double</code> into context <code>t</code> and double whatever value is embedded in this context – in the case of <code>OptionDemo1</code> the context is <code>option</code>, in <code>EitherDemo1</code>, <code>either</code>.</p>
<p>Since we’ve recognised and named this process, all that remains is to implement it. To start with, here’s a module signature that declares three lift operations:</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="ot">module</span> <span class="kw">type</span> <span class="dt">Lift</span> = <span class="kw">sig</span>
    <span class="kw">type</span> &#39;a t
    <span class="kw">val</span> lift:  (&#39;a -&gt; &#39;b) -&gt; &#39;a t -&gt; &#39;b t
    <span class="kw">val</span> lift2: (&#39;a -&gt; &#39;b -&gt; &#39;c) -&gt; &#39;a t -&gt; &#39;b t -&gt; &#39;c t
    <span class="kw">val</span> lift3: (&#39;a -&gt; &#39;b -&gt; &#39;c -&gt; &#39;d) -&gt; &#39;a t -&gt; &#39;b t -&gt; &#39;c t -&gt; &#39;d t
<span class="kw">end</span></code></pre>
<p>where <code>lift</code> takes a function from <code>'a -&gt; 'b</code> and a single input in context <code>t</code>, mapping to <code>'b t</code>. <code>lift2</code> takes a “two-argument” function (glossing over currying) and performs the same generalisation over a context; similarly for <code>lift3</code>.</p>
<p>We can use an OCaml <a href="http://caml.inria.fr/pub/docs/manual-ocaml/manual004.html#toc15">functor</a> to provide a generic implementation of lifting by leveraging <code>&lt;$&gt;</code> and <code>&lt;*&gt;</code>:</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="ot">module</span> ApplicativeLift(<span class="dt">A:</span> <span class="dt">ApplicativeFunctor</span>) : <span class="dt">Lift</span>
<span class="kw">with</span> <span class="kw">type</span> &#39;a t := &#39;a A<span class="kw">.</span>t = <span class="kw">struct</span>
    <span class="ot">include</span> A
    <span class="kw">let</span> lift  f a       = f &lt;$&gt; a
    <span class="kw">let</span> lift2 f a b     = f &lt;$&gt; a &lt;*&gt; b
    <span class="kw">let</span> lift3 f a b c   = f &lt;$&gt; a &lt;*&gt; b &lt;*&gt; c
<span class="kw">end</span></code></pre>
<p>Finally, let’s compose the signatures of <code>Lift</code> and <code>ApplicativeFunctor</code> to produce a new signature, <code>Applicative</code>,</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="co">(* Composition of ApplicativeFunctor and Lift *)</span>
<span class="ot">module</span> <span class="kw">type</span> <span class="dt">Applicative</span> = <span class="kw">sig</span>
    <span class="ot">include</span> Lift 
    <span class="ot">include</span> ApplicativeFunctor <span class="kw">with</span> <span class="kw">type</span> &#39;a t := &#39;a t
<span class="kw">end</span></code></pre>
<p>Note that this use of destructive substitution (<code>with type 'a t := 'a t</code>) requires OCaml 3.12 – see the <a href="http://caml.inria.fr/pub/docs/manual-ocaml/manual021.html#toc83">documentation</a> for more information.</p>
<p>With the above in place, we can now define a couple of enriched applicative modules (with less unseemly names),</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="ot">module</span> OptionApplicative : <span class="dt">Applicative</span>
<span class="kw">with</span> <span class="kw">type</span> &#39;a t = &#39;a <span class="dt">option</span> = <span class="kw">struct</span>
    <span class="ot">include</span> OptionApplicativeF
    <span class="ot">include</span> ApplicativeLift(<span class="dt">OptionApplicativeF</span>)
<span class="kw">end</span>

<span class="ot">module</span> EitherApplicative(<span class="dt">T:</span> <span class="dt">Typed</span>) : <span class="dt">Applicative</span>
<span class="kw">with</span> <span class="kw">type</span> &#39;a t = (T<span class="kw">.</span>t, &#39;a) either = <span class="kw">struct</span>
    <span class="ot">module</span> EA = <span class="dt">EitherApplicativeF</span>(<span class="dt">T</span>)
    <span class="ot">include</span> EA
    <span class="ot">include</span> ApplicativeLift(<span class="dt">EA</span>)
<span class="kw">end</span></code></pre>
<p>And expand on our earlier set of demos,</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="ot">module</span> Demo2(<span class="dt">A:</span> <span class="dt">Applicative</span>) = <span class="kw">struct</span>
    <span class="ot">include</span> A
    <span class="kw">let</span> double x  = x * 2
    <span class="kw">let</span> eg1 x     = lift double x
    <span class="kw">let</span> eg2 x y   = lift2 (+) x y
    <span class="kw">let</span> eg3 x     = double &lt;$&gt; x
    <span class="kw">let</span> eg4 x y   = (+) &lt;$&gt; x &lt;*&gt; y
    <span class="kw">let</span> eg5 f x y = lift2 f x y
<span class="kw">end</span>

<span class="ot">module</span> OptionDemo2       = <span class="dt">Demo2</span>(<span class="dt">OptionApplicative</span>)
<span class="ot">module</span> EitherStringDemo2 = <span class="dt">Demo2</span>(<span class="dt">EitherApplicative</span>(<span class="dt">String</span>))
<span class="ot">module</span> EitherIntDemo2    = <span class="dt">Demo2</span>(<span class="dt">EitherApplicative</span>(<span class="kw">struct</span> <span class="kw">type</span> t = <span class="dt">int</span> <span class="kw">end</span>))</code></pre>
<p>This composed structure proves to be very useful in practice – as a sketch, here’s an example of lifting a function into <code>either</code> context:</p>
<pre><code># let foo x y = (x * y) - (x + y);;
val foo : int -&gt; int -&gt; int = &lt;fun&gt;

# EitherStringDemo2.eg5 foo (Left &quot;Err&quot;) (Right 10);;
- : int EitherStringDemo2.t = Left &quot;Err&quot;

# EitherStringDemo2.eg5 foo (Right 15) (Right 10);;
- : int EitherStringDemo2.t = Right 125</code></pre>
<h2 id="further-reading">Further reading</h2>
<p>As before, I recommend reading the <a href="http://www.haskell.org/haskellwiki/Typeclassopedia#Applicative">Haskell Typeclassopedia</a> entry on <em>Applicative Functors</em>. This in turn links to some excellent resources on the subject and addresses the relationship between Applicatives and Monads.</p>
<p>A full code example can be found <a href="https://gist.github.com/bf9d03b23f4370192bdc">here</a>.</p>
  </div>
</div>

<hr/>

<div id="disqus_thread"></div>
<script type="text/javascript">
  var disqus_shortname = '0branch'; // required: replace example with your forum shortname
  /* * * DON'T EDIT BELOW THIS LINE * * */
  (function() {
  var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
  dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></summary>
</entry>
<entry>
    <title>Implementing Functor in OCaml</title>
    <link href="http://newblog.0branch.com/posts/2012-03-26-01-implementing-functor-ocaml.html" />
    <id>http://newblog.0branch.com/posts/2012-03-26-01-implementing-functor-ocaml.html</id>
    <published>2012-03-26T00:00:00Z</published>
    <updated>2012-03-26T00:00:00Z</updated>
    <summary type="html"><![CDATA[<div id="blog_post">
  <div class="span-20">
    <div id="post_title" class="span-10"><h1>Implementing Functor in OCaml</h1></div>
    <div style="text-align: right; font-style: italic;"
      class="span-10 last" id="post_date"><a href="/index.html">Posted</a> on March 26, 2012</div>
  </div>

  <hr/>

  <div id="post_body">
    <p>This post explores the implementation of <em>Functor</em> structures (not to be confused with <a href="http://caml.inria.fr/pub/docs/manual-ocaml/manual004.html#toc15">functor</a>) in <a href="http://www.ocaml-lang.org">OCaml</a>.</p>
<p>Let’s begin with some definitions,</p>
<ul>
<li>A <em>Functor</em> is (somewhat informally): a structure that provides a <em>mapping operation</em> that applies to a <em>value</em> in a given <em>context</em>, preserving that context.</li>
<li>Put differently: given a function <code>val f: 'a -&gt; 'b</code>, a <em>Functor</em> allows us to apply <code>f</code> to values in some context <code>t</code> such that the mapping operation is of type <code>'a t -&gt; 'b t</code>.</li>
<li>This mapping operation is typically named <code>fmap</code> and has the type signature <code>val fmap: ('a -&gt; 'b) -&gt; 'a t -&gt; 'b   t</code>.</li>
<li><p>The signature of <code>fmap</code> may be familiar: when <code>type 'a t = 'a   list</code>, the <code>fmap</code> function is simply <code>List.map</code>,</p>
<pre><code># List.map;;
- : (&#39;a -&gt; &#39;b) -&gt; &#39;a list -&gt; &#39;b list = &lt;fun&gt;</code></pre></li>
</ul>
<h2 id="begin-the-begin">Begin the Begin</h2>
<p>The <code>option</code> type seems as good a place to start as any. Let’s state the problem clearly: given some function <code>f</code> that maps from values of type <code>'a -&gt; 'b</code>, how can we apply this <code>f</code> to optional values of type <code>'a option</code> and mantain the optional context? Here’s a preliminary definition of <code>OptionFunctor</code> and some demo code where <code>f</code> is <code>abs</code>:</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="ot">module</span> OptionFunctor = <span class="kw">struct</span>
    <span class="kw">let</span> fmap f = <span class="kw">function</span>
        <span class="dt">Some</span> a  -&gt; <span class="dt">Some</span> (f a)
        | _     -&gt; <span class="dt">None</span>
<span class="kw">end</span>

<span class="ot">module</span> Demo1 = <span class="kw">struct</span>
    <span class="ot">open</span> OptionFunctor
    <span class="kw">let</span> eg1 = fmap abs (<span class="dt">Some</span>   5 )
    <span class="kw">let</span> eg2 = fmap abs (<span class="dt">Some</span> (<span class="dv">-5</span>))
    <span class="kw">let</span> eg3 = fmap abs <span class="dt">None</span>
<span class="kw">end</span></code></pre>
<p>In keeping with the definition of <em>Functor</em>, <code>OptionFunctor</code> provides an <code>fmap</code> function that takes two arguments (we’ll gloss over currying): some function <code>f</code> which maps from <code>'a -&gt; 'b</code> and an argument of type <code>'a option</code>. When the second argument is present (in the form <code>Some a</code>), <code>f</code> is applied to <code>a</code> and wrapped with the <code>Some</code> constructor; otherwise (where the second argument is <code>None</code>), the result is the same.</p>
<p>Less verbosely, <code>fmap</code> has type: <code>('a -&gt; 'b) -&gt; 'a option -&gt; 'b option</code>. Here it is in practice,</p>
<pre><code># Demo1.eg1;;
- : int option = Some 5

# Demo1.eg2;;
- : int option = Some 5

# Demo1.eg3;;
- : int option = None</code></pre>
<p>To make this example a little more interesting, let’s introduce another type, <code>validation</code>, and implement a <em>Functor</em> instance for it:</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="kw">type</span> &#39;a validation = <span class="dt">Success</span> <span class="kw">of</span> &#39;a | <span class="dt">Failure</span> <span class="kw">of</span> <span class="dt">string</span>

<span class="ot">module</span> OptionFunctor = <span class="kw">struct</span>
    <span class="kw">let</span> fmap f = <span class="kw">function</span>
        <span class="dt">Some</span> a  -&gt; <span class="dt">Some</span> (f a)
        | _     -&gt; <span class="dt">None</span>
<span class="kw">end</span>

<span class="ot">module</span> ValidationFunctor = <span class="kw">struct</span>
    <span class="kw">let</span> fmap f = <span class="kw">function</span>
        <span class="dt">Success</span>   a -&gt; <span class="dt">Success</span> (f a)
        | <span class="dt">Failure</span> e -&gt; <span class="dt">Failure</span> e
<span class="kw">end</span>

<span class="ot">module</span> Demo1 = <span class="kw">struct</span>
    <span class="ot">open</span> OptionFunctor
    <span class="kw">let</span> eg1 = fmap abs (<span class="dt">Some</span>   5 )
    <span class="kw">let</span> eg2 = fmap abs (<span class="dt">Some</span> (<span class="dv">-5</span>))
    <span class="kw">let</span> eg3 = fmap abs <span class="dt">None</span>
<span class="kw">end</span>

<span class="ot">module</span> Demo2 = <span class="kw">struct</span>
    <span class="ot">open</span> ValidationFunctor
    <span class="kw">let</span> eg4 = fmap abs (<span class="dt">Success</span>   5 )
    <span class="kw">let</span> eg5 = fmap abs (<span class="dt">Success</span> (<span class="dv">-5</span>))
    <span class="kw">let</span> eg6 = fmap abs (<span class="dt">Failure</span> <span class="st">&quot;Something went wrong&quot;</span>)
<span class="kw">end</span></code></pre>
<p>Our validation type has two constructors: <code>Success</code> and <code>Failure</code> – a slight enrichment of the builtin <code>option</code> type through the provision of reason for failure (= absence of value). <code>ValidationFunctor</code> strongly resembles <code>OptionFunctor</code>, the only difference being that a failure string is propagated in the second prong of the <code>function</code> match. The new <code>Demo2</code> module demonstrates usage; calls to <code>fmap</code> look identical, though <code>validation</code> constructors are employed in <code>Demo2</code>:</p>
<pre><code># Demo2.eg4;;
- : int validation = Success 5

# Demo2.eg5;;
- : int validation = Success 5

# Demo2.eg6;;
- : int validation = Failure &quot;Something went wrong&quot;</code></pre>
<p>So far, so good – but can we introduce a generic function and use it with both <code>option</code> and <code>validation</code> types? Unsurprisingly, the answer is yes: To do so, let’s introduce a <code>Functor</code> signature and augment our two <em>Functor</em> modules with type specifications.</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="kw">type</span> &#39;a validation = <span class="dt">Success</span> <span class="kw">of</span> &#39;a | <span class="dt">Failure</span> <span class="kw">of</span> <span class="dt">string</span>

<span class="ot">module</span> <span class="kw">type</span> <span class="dt">Functor</span> = <span class="kw">sig</span>
    <span class="kw">type</span> &#39;a t
    <span class="kw">val</span> fmap : (&#39;a -&gt; &#39;b) -&gt; &#39;a t -&gt; &#39;b t
<span class="kw">end</span>

<span class="ot">module</span> OptionFunctor = <span class="kw">struct</span>
    <span class="kw">type</span> &#39;a t = &#39;a <span class="dt">option</span>
    <span class="kw">let</span> fmap f = <span class="kw">function</span>
        <span class="dt">Some</span> a  -&gt; <span class="dt">Some</span> (f a)
        | _     -&gt; <span class="dt">None</span>
<span class="kw">end</span>

<span class="ot">module</span> ValidationFunctor = <span class="kw">struct</span>
    <span class="kw">type</span> &#39;a t = &#39;a validation
    <span class="kw">let</span> fmap f = <span class="kw">function</span>
        <span class="dt">Success</span>   a -&gt; <span class="dt">Success</span> (f a)
        | <span class="dt">Failure</span> e -&gt; <span class="dt">Failure</span> e
<span class="kw">end</span>

<span class="ot">module</span> Demo3(<span class="dt">F:</span> <span class="dt">Functor</span>) = <span class="kw">struct</span>
    <span class="kw">let</span> eg7 x   = F<span class="kw">.</span>fmap abs x
    <span class="kw">let</span> eg8 f x = F<span class="kw">.</span>fmap   f x
<span class="kw">end</span>

<span class="ot">module</span> OptionDemo3      = <span class="dt">Demo3</span>(<span class="dt">OptionFunctor</span>)
<span class="ot">module</span> ValidationDemo3  = <span class="dt">Demo3</span>(<span class="dt">ValidationFunctor</span>)</code></pre>
<p><code>Demo3</code> is a <code>functor</code> in the OCaml sense – a module that is parameterised by another module. Since the parameter <code>F</code> is required to implement the <code>Functor</code> signature (here we’re using structural typing to unify with <code>OptionDemo</code> and <code>ValidationDemo</code>), <code>fmap</code> can be used in the module body. The <code>abs</code> examples simply take a value in context,</p>
<pre><code># OptionDemo3.eg7 (Some 10);;
- : int OptionFunctor.t = Some 10

# OptionDemo3.eg7 None;;
- : int OptionFunctor.t = None

# ValidationDemo3.eg7 (Success 12);;
- : int ValidationFunctor.t = Success 12

# ValidationDemo3.eg7 (Failure &quot;Something went wrong&quot;);;
- : int ValidationFunctor.t = Failure &quot;Something went wrong&quot;</code></pre>
<p>while <code>eg8</code> allows us to provide a function,</p>
<pre><code># let square x = x * x;;
val square : int -&gt; int = &lt;fun&gt;

# OptionDemo3.eg8 square (Some 4);;
- : int OptionFunctor.t = Some 16

# OptionDemo3.eg8 square None;;
- : int OptionFunctor.t = None

# ValidationDemo3.eg8 square (Success 5);;
- : int ValidationFunctor.t = Success 25

# ValidationDemo3.eg8 square (Failure &quot;Oops&quot;);;
- : int ValidationFunctor.t = Failure &quot;Oops&quot;</code></pre>
<p>Finally, here’s how we can define a <code>ListFunctor</code> at the toplevel to make use of <code>Demo3</code>:</p>
<pre><code># module ListFunctor = struct type &#39;a t = &#39;a list let fmap = List.map end;;
module ListFunctor :
  sig type &#39;a t = &#39;a list val fmap : (&#39;a -&gt; &#39;b) -&gt; &#39;a list -&gt; &#39;b list end

# module ListDemo3 = Demo3(ListFunctor);;
module ListDemo3 : sig val eg7 : int ListFunctor.t -&gt; int ListFunctor.t end

# ListDemo3.eg7 [1; 2; 3];;
- : int ListFunctor.t = [1; 2; 3]

# ListDemo3.eg7 [1; 2; -3];;
- : int ListFunctor.t = [1; 2; 3]

# ListDemo3.eg7 [];;
- : int ListFunctor.t = []

# ListDemo3.eg8 square [1; 2; 3];;
- : int ListFunctor.t = [1; 4; 9]

# ListDemo3.eg8 square [];;
- : int ListFunctor.t = []</code></pre>
<h2 id="providing-signatures-for-functors">Providing signatures for <em>Functors</em></h2>
<p>Above, we used structural typing to match <code>OptionFunctor</code> and <code>ValidationFunctor</code> against the <code>Functor</code> signature. How about defining these modules with the <code>Functor</code> signature at the outset?</p>
<p>First of all, consider what happens when we attempt to create a new module, <code>OF</code>, by signing <code>OptionFunctor</code> with <code>Functor</code>:</p>
<pre><code> # module OF = (OptionFunctor : Functor);;
 module OF : Functor

 # OF.fmap abs (Some 5);;
 Error: This expression has type &#39;a option
        but an expression was expected of type int OF.t</code></pre>
<p>In specifying a signature, the type representation in <code>OptionFunctor</code> has been <em>abstracted over</em>. The same problem is evidenced in the following code,</p>
<pre><code> # module type A = sig type t val id: t -&gt; t end;;
 module type A = sig type t val id : t -&gt; t end

 # module B = struct type t = int let id x = x end;;
 module B : sig type t = int val id : &#39;a -&gt; &#39;a end

 # B.id;;
 - : &#39;a -&gt; &#39;a = &lt;fun&gt;

 # B.id 10;;
 - : int = 10

 # module C = (B: A);;
 module C : A

 # C.id;;
 - : C.t -&gt; C.t = &lt;fun&gt;

 # C.id 10;;
 Error: This expression has type int but an expression was expected of type
          C.t</code></pre>
<p>By defining module <code>C</code> as having signature <code>A</code>, type <code>C.t</code> has been rendered abstract. What we meant to express is that <code>C</code> is a kind of <code>A</code>… and that <code>type t = int</code>. To achieve this, a sharing constraint is necessary:</p>
<pre><code> # module D = (B: A with type t = int);;
 module D : sig type t = int val id : t -&gt; t end

 # D.id 10;;
 - : D.t = 10</code></pre>
<p>Returning to <code>OptionFunctor</code> and its cousin <code>OF</code>, let’s look more closely at each module’s signature (by using the assign-to-peek trick in the toplevel):</p>
<pre><code> # module T = OptionFunctor;;
 module T :
   sig
     type &#39;a t = &#39;a option
     val fmap : (&#39;a -&gt; &#39;b) -&gt; &#39;a option -&gt; &#39;b option
   end

 # module T = OF;;
 module T :
   sig
     type &#39;a t = &#39;a OF.t
     val fmap : (&#39;a -&gt; &#39;b) -&gt; &#39;a t -&gt; &#39;b t
   end</code></pre>
<p>As anticipated above, <code>OF.t</code> is abstract; to “fix” <code>fmap</code> we’ll need to add a sharing constraint as follows,</p>
<pre><code> # module OF2 = (OptionFunctor : Functor with type &#39;a t = &#39;a option);;
 module OF2 :
   sig
     type &#39;a t = &#39;a option
     val fmap : (&#39;a -&gt; &#39;b) -&gt; &#39;a t -&gt; &#39;b t
   end</code></pre>
<p>Notice that <code>t</code> has a concrete type in <code>OF2</code> while it remains abstract in <code>OF</code> (as seen through <code>T</code>). The provision of a sharing constraint solves the type incompatibility problem,</p>
<pre><code> # OF.fmap abs (Some 5);;
 Error: This expression has type &#39;a option
        but an expression was expected of type int OF.t

 # OF2.fmap abs (Some 5);;
 - : int OF2.t = Some 5</code></pre>
<p>So: is it worth explicitly specifying the signature or not in light of the potential confusion caused by sharing constraints? On the one hand, explicit specification buys encapsulation and compile-time safety – the compiler will alert us to missing definitions. On the other, constraint provision is required to prevent nasty surprises from arising – perhaps we could make do with structural typing.</p>
<p>Since ensuring that implementations satisfy the conditions of formal categories (in this case, <em>Functor</em>) is desirable, explicit signatures will be employed for the remainder of the post. This is by no means required for what follows though.</p>
<h2 id="back-to-the-code">Back to the code</h2>
<p>As a final step, let’s introduce an <code>either</code> type – more general than <code>validation</code> in that its failure type is parametric – and <code>Functor</code> signatures for all of our implementations:</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="kw">type</span>  &#39;a      validation    = <span class="dt">Success</span> <span class="kw">of</span> &#39;a | <span class="dt">Failure</span> <span class="kw">of</span> <span class="dt">string</span>
<span class="kw">type</span> (&#39;a, &#39;b) either        = <span class="dt">Left</span>    <span class="kw">of</span> &#39;a | <span class="dt">Right</span>   <span class="kw">of</span> &#39;b

<span class="ot">module</span> <span class="kw">type</span> <span class="dt">Functor</span> = <span class="kw">sig</span>
    <span class="kw">type</span> &#39;a t
    <span class="kw">val</span> fmap : (&#39;a -&gt; &#39;b) -&gt; &#39;a t -&gt; &#39;b t
<span class="kw">end</span>

<span class="ot">module</span> OptionFunctor : <span class="dt">Functor</span>
<span class="kw">with</span> <span class="kw">type</span> &#39;a t = &#39;a <span class="dt">option</span> = <span class="kw">struct</span>
    <span class="kw">type</span> &#39;a t = &#39;a <span class="dt">option</span>
    <span class="kw">let</span> fmap f = <span class="kw">function</span>
        <span class="dt">Some</span> a  -&gt; <span class="dt">Some</span> (f a)
        | _     -&gt; <span class="dt">None</span>
<span class="kw">end</span>

<span class="ot">module</span> ValidationFunctor : <span class="dt">Functor</span>
<span class="kw">with</span> <span class="kw">type</span> &#39;a t = &#39;a validation = <span class="kw">struct</span>
    <span class="kw">type</span> &#39;a t = &#39;a validation
    <span class="kw">let</span> fmap f = <span class="kw">function</span>
        <span class="dt">Success</span>   a -&gt; <span class="dt">Success</span> (f a)
        | <span class="dt">Failure</span> e -&gt; <span class="dt">Failure</span> e
<span class="kw">end</span>

<span class="ot">module</span> ListFunctor : <span class="dt">Functor</span>
<span class="kw">with</span> <span class="kw">type</span> &#39;a t = &#39;a <span class="dt">list</span> = <span class="kw">struct</span>
    <span class="kw">type</span> &#39;a t = &#39;a <span class="dt">list</span>
    <span class="kw">let</span> fmap = List<span class="kw">.</span>map
<span class="kw">end</span>

<span class="co">(* Signature of any module providing its type in `t&#39; *)</span>
<span class="ot">module</span> <span class="kw">type</span> <span class="dt">Typed</span> = <span class="kw">sig</span> <span class="kw">type</span> t <span class="kw">end</span>

<span class="ot">module</span> EitherFunctor(<span class="dt">T:</span> <span class="dt">Typed</span>) : <span class="dt">Functor</span>
<span class="kw">with</span> <span class="kw">type</span> &#39;a t = (T<span class="kw">.</span>t, &#39;a) either = <span class="kw">struct</span>
    <span class="kw">type</span> &#39;a t = (T<span class="kw">.</span>t, &#39;a) either
    <span class="kw">let</span> fmap f = <span class="kw">function</span>
        <span class="dt">Right</span> r     -&gt; <span class="dt">Right</span> (f r)
        | <span class="dt">Left</span> l    -&gt; <span class="dt">Left</span> l
<span class="kw">end</span>

<span class="ot">module</span> Demo3(<span class="dt">F:</span> <span class="dt">Functor</span>) = <span class="kw">struct</span>
    <span class="kw">let</span> eg7 x   = F<span class="kw">.</span>fmap abs x
    <span class="kw">let</span> eg8 f x = F<span class="kw">.</span>fmap   f x
<span class="kw">end</span>

<span class="ot">module</span> OptionDemo3      = <span class="dt">Demo3</span>(<span class="dt">OptionFunctor</span>)
<span class="ot">module</span> ValidationDemo3  = <span class="dt">Demo3</span>(<span class="dt">ValidationFunctor</span>)
<span class="ot">module</span> ListDemo3        = <span class="dt">Demo3</span>(<span class="dt">ListFunctor</span>)
<span class="ot">module</span> EitherDemo3      = <span class="dt">Demo3</span>(<span class="dt">EitherFunctor</span>(<span class="dt">String</span>))</code></pre>
<p>Here <code>EitherFunctor</code> is an (OCaml) <code>functor</code> (i.e., a module parameterised with another module). The module argument must satisfy the signature <code>Typed</code> by providing a type member <code>t</code>; this is used for the <code>Left</code> (failure) case.</p>
<pre><code># EitherDemo3.eg7 (Right 12);;
- : int EitherFunctor(String).t = Right 12

# EitherDemo3.eg7 (Left &quot;Something went wrong&quot;);;
- : int EitherFunctor(String).t = Left &quot;Something went wrong&quot;

# EitherDemo3.eg8 square (Right 12);;
- : int EitherFunctor(String).t = Right 144

# EitherDemo3.eg8 square (Left &quot;Oh dear&quot;);;
- : int EitherFunctor(String).t = Left &quot;Oh dear&quot;</code></pre>
<p>Why do we need to use a <code>functor</code> here? The answer lies with the arity of <code>Functor.t</code>. Recall that <code>either</code> is defined as,</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="kw">type</span> (&#39;a, &#39;b) either = <span class="dt">Left</span> <span class="kw">of</span> &#39;a | <span class="dt">Right</span> <span class="kw">of</span> &#39;b</code></pre>
<p>while <code>Functor</code> expects a type matching</p>
<pre class="sourceCode OCaml"><code class="sourceCode ocaml"><span class="kw">type</span> &#39;a t</code></pre>
<p>Without the <code>functor</code>, we’d receive a signature mismatch error.</p>
<h2 id="applicative-functors">Applicative Functors</h2>
<p><a href="/posts/2012-03-26-02-from-functor.html">Next time</a> we’ll look at <em>Applicative Functor</em>, built on top of the <code>Functor</code> modules described in this post.</p>
<p>For more information on <em>Functor</em>, the <a href="http://www.haskell.org/haskellwiki/Typeclassopedia#Functor">Haskell Typeclassopedia</a> is an excellent reference.</p>
  </div>
</div>

<hr/>

<div id="disqus_thread"></div>
<script type="text/javascript">
  var disqus_shortname = '0branch'; // required: replace example with your forum shortname
  /* * * DON'T EDIT BELOW THIS LINE * * */
  (function() {
  var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
  dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></summary>
</entry>
<entry>
    <title>Mercurial: Prettypaths extension</title>
    <link href="http://newblog.0branch.com/posts/2011-09-04-prettypaths-extension.html" />
    <id>http://newblog.0branch.com/posts/2011-09-04-prettypaths-extension.html</id>
    <published>2011-09-04T00:00:00Z</published>
    <updated>2011-09-04T00:00:00Z</updated>
    <summary type="html"><![CDATA[<div id="blog_post">
  <div class="span-20">
    <div id="post_title" class="span-10"><h1>Mercurial: Prettypaths extension</h1></div>
    <div style="text-align: right; font-style: italic;"
      class="span-10 last" id="post_date"><a href="/index.html">Posted</a> on September  4, 2011</div>
  </div>

  <hr/>

  <div id="post_body">
    <p>This is a simple extension to improve the output of the ‘hg paths’ command. I originally submitted a patch to the <em>mercurial-devel</em> list to achieve the same thing—given that my change alters the format of a builtin command, I don’t expect it to be accepted.</p>
<p>And so, the <em>prettypaths</em> extension: <a href="http://hg.0branch.com/hg-prettypaths">hg-prettypaths</a>.</p>
<p>Configuration is minimal:</p>
<p>First, add the following to your <code>~/.hgrc</code>:</p>
<pre class="sourceCode Ini"><code class="sourceCode ini"><span class="kw">[extensions]</span>
<span class="co"># ...</span>
<span class="dt">prettypaths </span><span class="ot">=</span><span class="st"> /path/to/prettypaths.py</span></code></pre>
<p>Then (optionally) set <code>--pretty</code> as default:</p>
<pre class="sourceCode Ini"><code class="sourceCode ini"><span class="kw">[defaults]</span>
<span class="co"># ...</span>
<span class="dt">paths </span><span class="ot">=</span><span class="st"> --pretty</span></code></pre>
  </div>
</div>

<hr/>

<div id="disqus_thread"></div>
<script type="text/javascript">
  var disqus_shortname = '0branch'; // required: replace example with your forum shortname
  /* * * DON'T EDIT BELOW THIS LINE * * */
  (function() {
  var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
  dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></summary>
</entry>
<entry>
    <title>Mercurial: Persona extension</title>
    <link href="http://newblog.0branch.com/posts/2011-08-27-persona-extension.html" />
    <id>http://newblog.0branch.com/posts/2011-08-27-persona-extension.html</id>
    <published>2011-08-27T00:00:00Z</published>
    <updated>2011-08-27T00:00:00Z</updated>
    <summary type="html"><![CDATA[<div id="blog_post">
  <div class="span-20">
    <div id="post_title" class="span-10"><h1>Mercurial: Persona extension</h1></div>
    <div style="text-align: right; font-style: italic;"
      class="span-10 last" id="post_date"><a href="/index.html">Posted</a> on August 27, 2011</div>
  </div>

  <hr/>

  <div id="post_body">
    <p>I’ve recently switched (back) from Git to Mercurial. This post isn’t intended to be a recapitulation of pros and cons; both are great tools. Instead I’d like to mention a problem that came up in my workflow and a small extension written to address it.</p>
<p>Here’s the problem: I use Mercurial for both personal and work projects—and different usernames for each. This situation may well be familiar to some:</p>
<ol style="list-style-type: decimal">
<li>You save your work (personal) username in the <code>ui</code> section of <code>$HOME/.hgrc</code></li>
<li>For personal (work) repositories you edit the <code>.hg/hgrc</code> and specify a local username</li>
<li>You often forget to do (2) and end up committing changesets with the <em>wrong username</em></li>
</ol>
<p>The <code>persona</code> extension is designed to make this process slightly less of a headache: It provides an easy way to set <code>ui.username</code> on a repository by repository basis.</p>
<p>At its simplest, the extension lets you specify a username for a repository as follows:</p>
<pre><code># hg persona -n &quot;Firstname Lastname &lt;firstname@some.domain&gt;&quot;</code></pre>
<p>To save repetition, you can configure individual personas in your <code>~/.hgrc</code>:</p>
<pre class="sourceCode Ini"><code class="sourceCode ini"><span class="kw">[persona]</span>
<span class="dt">home        </span><span class="ot">=</span><span class="st"> Firstname Lastname &lt;firstname@home.domain&gt;</span>
<span class="dt">work        </span><span class="ot">=</span><span class="st"> Firstname Lastname &lt;lastname@work.domain&gt;</span></code></pre>
<p>which allows for quick switching in a given repository:</p>
<pre><code># hg persona -n home
~ Setting username to &#39;Firstname Lastname &lt;firstname@home.domain&gt;&#39;

# hg persona -n work
~ Setting username to &#39;Firstname Lastname &lt;lastname@work.domain&gt;&#39;</code></pre>
<p>Finally, you can specify the <code>--persona</code> option to clone:</p>
<pre><code># hg clone --persona work src dest</code></pre>
<p>To use this extension, clone <a href="http://hg.0branch.com/hg-persona">http://hg.0branch.com/hg-persona</a> and add the following to your <code>~/.hgrc</code>:</p>
<pre class="sourceCode Ini"><code class="sourceCode ini"><span class="kw">[extensions]</span>
<span class="dt">persona </span><span class="ot">=</span><span class="st"> /path/to/persona.py</span></code></pre>
<p>Feedback appreciated!</p>
<p><em>Update</em>: The following hook is also handy for displaying <code>ui.username</code> info on each commit (allowing you to catch repository-username mismatches early):</p>
<pre class="sourceCode Ini"><code class="sourceCode ini"><span class="kw">[hooks]</span>
<span class="co"># ...</span>
<span class="dt">pre-commit </span><span class="ot">=</span><span class="st"> echo &quot;(as &#39;`hg showconfig ui.username`&#39;)&quot;</span></code></pre>
  </div>
</div>

<hr/>

<div id="disqus_thread"></div>
<script type="text/javascript">
  var disqus_shortname = '0branch'; // required: replace example with your forum shortname
  /* * * DON'T EDIT BELOW THIS LINE * * */
  (function() {
  var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
  dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></summary>
</entry>
<entry>
    <title>Moose and Type Constraints</title>
    <link href="http://newblog.0branch.com/posts/2011-04-02-moose-type-constraints.html" />
    <id>http://newblog.0branch.com/posts/2011-04-02-moose-type-constraints.html</id>
    <published>2011-04-02T00:00:00Z</published>
    <updated>2011-04-02T00:00:00Z</updated>
    <summary type="html"><![CDATA[<div id="blog_post">
  <div class="span-20">
    <div id="post_title" class="span-10"><h1>Moose and Type Constraints</h1></div>
    <div style="text-align: right; font-style: italic;"
      class="span-10 last" id="post_date"><a href="/index.html">Posted</a> on April  2, 2011</div>
  </div>

  <hr/>

  <div id="post_body">
    <h1 id="overview">Overview</h1>
<p>This is the first in a series of posts documenting my experiments in <a href="http://search.cpan.org/~drolsky/Moose-1.24/lib/Moose.pm">Moose</a>, an OOP framework for Perl 5. Throughout this series, I will be exploring its feature set by building a simple system for writing <a href="http://ifwiki.org/index.php/FAQ#What_is_.22interactive_fiction.22.3F">Interactive Fiction</a>.</p>
<p>The particular focus of this article is <code>Moose</code>’s type system and pitfalls that might crop up on first use.</p>
<h1 id="prerequisites">Prerequisites</h1>
<p>If you want to run the examples that follow, you’ll need to install <code>Moose</code>. The easiest way to do so is through <code>CPAN</code>:</p>
<pre><code># cpan Moose</code></pre>
<p>or</p>
<pre><code># cpanm Moose</code></pre>
<h1 id="getting-started">Getting Started</h1>
<p>To get started, we’ll need to define classes representing both generic objects in the game world and the player character. For now we won’t worry about the world as a whole (nor its I/O) and will ignore special object behaviour (for example, containment and supporter relationships). Also note that the design decisions taken here are subject to change in later posts — we might, for example, wish to make the <code>Player</code> a special kind of <code>Person</code>.</p>
<p>At its most fundamental, a game object should provide a description to return upon examination and a name to be used by the parser for referring to the object in question. Here’s a first pass,</p>
<pre class="sourceCode Perl"><code class="sourceCode perl"><span class="kw">package</span> Object;
<span class="kw">use</span> Moose;

has <span class="kw">&#39;</span><span class="st">name</span><span class="kw">&#39;</span> =&gt; (
  isa       =&gt; <span class="kw">&#39;</span><span class="st">Str</span><span class="kw">&#39;</span>,
  is        =&gt; <span class="kw">&#39;</span><span class="st">rw</span><span class="kw">&#39;</span>,
  required  =&gt; <span class="dv">1</span>,
);

has <span class="kw">&#39;</span><span class="st">description</span><span class="kw">&#39;</span> =&gt; (
  isa       =&gt; <span class="kw">&#39;</span><span class="st">Str</span><span class="kw">&#39;</span>,
  is        =&gt; <span class="kw">&#39;</span><span class="st">rw</span><span class="kw">&#39;</span>,
  default   =&gt; <span class="kw">sub </span>{
    <span class="kw">&quot;</span><span class="st">You see nothing special about the </span><span class="kw">&quot;</span> . <span class="fu">shift</span>-&gt;name;
  },
);

<span class="kw">no</span> Moose;
<span class="dv">1</span>;</code></pre>
<p>This class introduces us to <code>Moose</code> in general. As with standard Perl OOP, a class is simply a package. Using <code>Moose</code>’s syntactic sugar, we define two <em>attributes</em> for holding an object’s internal <code>name</code> (which will eventually manifest itself as a noun referring to the instance) and a <code>description</code>. Attributes defined with <code>has</code> are similar to C++ member variables or Java fields — though closer to C# properties as <code>Moose</code> provides us with implicit accessor methods.</p>
<p>Both <code>name</code> and <code>description</code> are marked as <code>rw</code> meaning that public accessors are available for setting and getting these attributes. Both are also defined as <code>isa =&gt; 'Str'</code> — this is the first sign of the type checking provided out of the box by <code>Moose</code>. Any attempt to set <code>name</code> or <code>description</code> to values that aren’t understood to be strings will result in an exception. Consider the following script,</p>
<pre class="sourceCode Perl"><code class="sourceCode perl"><span class="kw">#!/usr/bin/env perl</span>

<span class="kw">use</span> <span class="kw">strict</span>;
<span class="kw">use</span> <span class="kw">warnings</span>;

<span class="kw">use</span> Object;

<span class="kw">my</span> <span class="dt">$foo</span> = Object-&gt;new(name =&gt; <span class="kw">&quot;</span><span class="st">lamp</span><span class="kw">&quot;</span>);
<span class="kw">my</span> <span class="dt">$bar</span> = Object-&gt;new(name =&gt; {});</code></pre>
<p>Notice that we attempt to construct <code>$bar</code> by supplying an anonymous hash to <code>name</code>. This is what happens at runtime,</p>
<pre><code>Attribute (name) does not pass the type constraint because: Validation failed for &#39;Str&#39; with value HASH(0x803eb0) at /Users/marc/perl5/perlbrew/perls/perl-5.12.3/lib/site_perl/5.12.3/darwin-2level/Moose/Meta/Attribute.pm line 883
  Moose::Meta::Attribute::_coerce_and_verify(&#39;Moose::Meta::Attribute=HASH(0x9b06d0)&#39;, &#39;HASH(0x803eb0)&#39;, &#39;Object=HASH(0x9199d0)&#39;) called at /Users/marc/perl5/perlbrew/perls/perl-5.12.3/lib/site_perl/5.12.3/darwin-2level/Moose/Meta/Attribute.pm line 483
  Moose::Meta::Attribute::initialize_instance_slot(&#39;Moose::Meta::Attribute=HASH(0x9b06d0)&#39;, &#39;Moose::Meta::Instance=HASH(0x9b2120)&#39;, &#39;Object=HASH(0x9199d0)&#39;, &#39;HASH(0x803e80)&#39;) called at /Users/marc/perl5/perlbrew/perls/perl-5.12.3/lib/site_perl/5.12.3/darwin-2level/Class/MOP/Class.pm line 603
  Class::MOP::Class::_construct_instance(&#39;Moose::Meta::Class=HASH(0x97c430)&#39;, &#39;HASH(0x803e80)&#39;) called at /Users/marc/perl5/perlbrew/perls/perl-5.12.3/lib/site_perl/5.12.3/darwin-2level/Class/MOP/Class.pm line 576
  Class::MOP::Class::new_object(&#39;Moose::Meta::Class=HASH(0x97c430)&#39;, &#39;HASH(0x803e80)&#39;) called at /Users/marc/perl5/perlbrew/perls/perl-5.12.3/lib/site_perl/5.12.3/darwin-2level/Moose/Meta/Class.pm line 256
  Moose::Meta::Class::new_object(&#39;Moose::Meta::Class=HASH(0x97c430)&#39;, &#39;HASH(0x803e80)&#39;) called at /Users/marc/perl5/perlbrew/perls/perl-5.12.3/lib/site_perl/5.12.3/darwin-2level/Moose/Object.pm line 26
  Moose::Object::new(&#39;Object&#39;, &#39;name&#39;, &#39;HASH(0x803eb0)&#39;) called at ./mismatch.pl line 9</code></pre>
<p>Now for the player character. A trivial implementation that meets the requirements outlined above would be:</p>
<pre class="sourceCode Perl"><code class="sourceCode perl"><span class="kw">package</span> Player;
<span class="kw">use</span> Moose;

has <span class="kw">&#39;</span><span class="st">inventory</span><span class="kw">&#39;</span> =&gt; (
  default =&gt; <span class="kw">sub </span>{ [] },
  is      =&gt; <span class="kw">&#39;</span><span class="st">ro</span><span class="kw">&#39;</span>,
);

<span class="kw">sub </span><span class="fu">list_inventory</span> {
  <span class="kw">my</span> <span class="dt">$self</span>    = <span class="fu">shift</span>;
  <span class="kw">my</span> <span class="dt">@inv</span>     = <span class="dt">@</span>{ <span class="dt">$self</span>-&gt;<span class="dt">inventory</span> };

  <span class="kw">return</span> <span class="kw">&quot;</span><span class="st">You are carrying nothing.</span><span class="kw">&quot;</span> <span class="kw">unless</span> <span class="dt">@inv</span>;

  <span class="kw">my</span> <span class="dt">$output</span>  = <span class="kw">&quot;</span><span class="st">You are currently carrying:</span><span class="ch">\n</span><span class="kw">&quot;</span>;
  <span class="dt">$output</span>    .= (<span class="kw">&quot;</span><span class="st">* </span><span class="kw">&quot;</span> . <span class="dt">$_</span>-&gt;<span class="dt">name</span> . <span class="kw">&quot;</span><span class="ch">\n</span><span class="kw">&quot;</span>) <span class="kw">foreach</span>(<span class="dt">@inv</span>);
  <span class="kw">return</span> <span class="dt">$output</span>;
}

<span class="kw">sub </span><span class="fu">obtain</span> {
  <span class="kw">my</span> (<span class="dt">$self</span>, <span class="dt">$object</span>) = <span class="dt">@_</span>;
  <span class="fu">push</span> <span class="dt">@</span>{ <span class="dt">$self</span>-&gt;<span class="dt">inventory</span> }, <span class="dt">$object</span>;
}

<span class="kw">no</span> Moose;
<span class="dv">1</span>;</code></pre>
<p><code>Player</code> has a single attribute, <code>inventory</code>, an array reference used to store a list of objects being carried. The <code>list_inventory</code> method returns a string listing the current inventory in a structured way. An <code>obtain</code> method is used to add objects to the player’s inventory by <code>push</code>ing them onto the end of the array referenced by <code>inventory</code>.</p>
<p>Let’s try this out;</p>
<pre class="sourceCode Perl"><code class="sourceCode perl"><span class="kw">#!/usr/bin/env perl</span>

<span class="kw">use</span> <span class="kw">strict</span>;
<span class="kw">use</span> <span class="kw">warnings</span>;
<span class="kw">use</span> feature <span class="kw">&#39;</span><span class="st">say</span><span class="kw">&#39;</span>;

<span class="kw">use</span> Object;
<span class="kw">use</span> Player;

<span class="kw">my</span> <span class="dt">$lamp</span>    = Object-&gt;new(name =&gt; <span class="kw">&quot;</span><span class="st">lamp</span><span class="kw">&quot;</span>);
<span class="kw">my</span> <span class="dt">$player</span>  = Player-&gt;new;

say <span class="dt">$player</span>-&gt;<span class="dt">list_inventory</span>;
<span class="dt">$player</span>-&gt;<span class="dt">obtain</span>(<span class="dt">$lamp</span>);
say <span class="dt">$player</span>-&gt;<span class="dt">list_inventory</span>;

<span class="co"># but:</span>
<span class="dt">$player</span>-&gt;<span class="dt">obtain</span>(<span class="dt">$player</span>);</code></pre>
<p>Running <code>entry.pl</code> yields the following:</p>
<pre><code># ./entry.pl
You are carrying nothing.
You are currently carrying:
* lamp</code></pre>
<p>The code works as expected, but notice that the final line of our script — <code>$player-&gt;obtain($player)</code> — is perfectly legal, meaning that a player can pick up instances of <code>Player</code> as well as <code>Object</code>. Is this desirable?</p>
<p>Programmers versed in dynamically typed object systems might well answer <em>yes</em>; we don’t need typing on the inventory to prevent such errors. Rather, we need good API documentation and well written client code. Type mismatches will manifest when, for example, we try to list the inventory after a <code>Player</code> instance has been added:</p>
<pre><code>Can&#39;t locate object method &quot;name&quot; via package &quot;Player&quot; at Player.pm line 16.</code></pre>
<p>This run time exception indicates that we’re attempting to call a method on an object of inappropriate type. Good testing practices could iron out such errors, case closed.</p>
<p>If you find this reasoning satisfactory, the rest of this post probably won’t be of interest. My intention is not to debate the relative merits or shortcomings of approaches to typing in general. Rather, I take the position that catching as many errors as early as possible is <em>A Good Thing</em>. While compile-time type checking with <code>Moose</code> isn’t possible, we <em>can</em> prevent instances of the wrong type from being added to our inventory; as such, we don’t have to wait for a method call on an object to tell us that we’ve screwed up somewhere along the way.</p>
<h1 id="moose-and-types">Moose and Types</h1>
<p>Conceptually, we want to encode the following in <code>Player</code>:</p>
<ul>
<li>A <code>Player</code> has an <code>inventory</code>;</li>
<li>This inventory only contains instances of type <code>Object</code> (or its subtypes)</li>
</ul>
<p>Notice that I talk of types and subtypes here rather than classes and subclasses. We have opened the door to a headache or two as will become clear below.</p>
<h2 id="type-parameters">Type Parameters</h2>
<p><code>Moose</code> offers a <a href="http://search.cpan.org/~drolsky/Moose-1.24/lib/Moose/Manual/Types.pod">type system for attributes</a>. As noted in the documentation, though,</p>
<blockquote>
<p>this is not a “real” type system. Moose does not magically make Perl start associating types with variables. This is just an advanced parameter checking system which allows you to associate a name with a constraint.</p>
</blockquote>
<p>With that in mind, let’s add type checking to our <code>inventory</code>:</p>
<pre class="sourceCode Perl"><code class="sourceCode perl"><span class="kw">package</span> Player;
<span class="kw">use</span> Moose;

has <span class="kw">&#39;</span><span class="st">inventory</span><span class="kw">&#39;</span> =&gt; (
  handles =&gt; { obtain =&gt; <span class="kw">&#39;</span><span class="st">push</span><span class="kw">&#39;</span> },
  isa     =&gt; <span class="kw">&#39;</span><span class="st">ArrayRef[Object]</span><span class="kw">&#39;</span>,
  default =&gt; <span class="kw">sub </span>{ [] },
  traits  =&gt; [<span class="kw">&#39;</span><span class="st">Array</span><span class="kw">&#39;</span>],
  is      =&gt; <span class="kw">&#39;</span><span class="st">ro</span><span class="kw">&#39;</span>,
);

<span class="kw">sub </span><span class="fu">list_inventory</span> {
  <span class="kw">my</span> <span class="dt">$self</span>    = <span class="fu">shift</span>;
  <span class="kw">my</span> <span class="dt">@inv</span>     = <span class="dt">@</span>{ <span class="dt">$self</span>-&gt;<span class="dt">inventory</span> };

  <span class="kw">return</span> <span class="kw">&quot;</span><span class="st">You are carrying nothing.</span><span class="kw">&quot;</span> <span class="kw">unless</span> <span class="dt">@inv</span>;

  <span class="kw">my</span> <span class="dt">$output</span>  = <span class="kw">&quot;</span><span class="st">You are currently carrying:</span><span class="ch">\n</span><span class="kw">&quot;</span>;
  <span class="dt">$output</span>    .= (<span class="kw">&quot;</span><span class="st">* </span><span class="kw">&quot;</span> . <span class="dt">$_</span>-&gt;<span class="dt">name</span> . <span class="kw">&quot;</span><span class="ch">\n</span><span class="kw">&quot;</span>) <span class="kw">foreach</span>(<span class="dt">@inv</span>);
  <span class="kw">return</span> <span class="dt">$output</span>;
}

<span class="kw">no</span> Moose;
<span class="dv">1</span>;</code></pre>
<p>Notice that we have:</p>
<ol style="list-style-type: decimal">
<li>Removed the <code>obtain</code> method in favour of a <code>handle</code> key that delegates to <code>push</code>.</li>
<li>Changed the type of inventory to <code>ArrayRef[Object]</code></li>
<li>Specified the <code>Array</code> trait.</li>
</ol>
<p>The array trait,</p>
<blockquote>
<p>provides native delegation methods for array references</p>
</blockquote>
<p>In particular, it allows us to define <code>obtain</code> with respect to a parameterised type. For more information on the trait, see <a href="http://search.cpan.org/~drolsky/Moose-1.24/lib/Moose/Meta/Attribute/Native/Trait/Array.pm">here</a>.</p>
<h3 id="first-problem">First Problem</h3>
<p>Running our entry script again, we see the same output:</p>
<pre><code># ./entry.pl
You are carrying nothing.
You are currently carrying:
* lamp</code></pre>
<p>Why hasn’t the type parameter taken effect? The answer is that our class <code>Object</code> clashes with a built in type.</p>
<h4 id="which-object">Which Object?</h4>
<p>Browsing the <code>Moose</code> documentation, one might be inclined to think that we’re defining a class that already exists — namely, <code>Moose::Object</code>. Consider the following,</p>
<blockquote>
<p>[Moose::Object] is the default base class for all Moose-using classes. When you use Moose in this class, your class will inherit from this class.</p>
</blockquote>
<p>This description will be familiar to Java and Smalltalk programmers as it suggests a Unified Type System (where the class/type hierarchy has a single root node, <code>Object</code>).</p>
<p>This is not the case here though. Let’s review the <code>isa</code> key used with <code>has</code>:</p>
<blockquote>
<p>The <em>isa</em> option uses Moose’s type constraint facilities to set up runtime type checking for this attribute. Moose will perform the checks during class construction, and within any accessors. The <code>$type_name</code> argument must be a string. The string may be either a class name or a type defined using Moose’s type definition features.</p>
</blockquote>
<p>We haven’t used any of <code>Moose</code>’s custom type definition features, so let’s take a look at the builtin type hierarchy:</p>
<pre><code>Any
Item
    Bool
    Maybe[`a]
    Undef
    Defined
        Value
            Str
                Num
                    Int
                ClassName
                RoleName
        Ref
            ScalarRef[`a]
            ArrayRef[`a]
            HashRef[`a]
            CodeRef
            RegexpRef
            GlobRef
                FileHandle
            Object</code></pre>
<p>Notice that <code>Object</code> is a built-in <em>type constraint</em> — this constraint shares a name with our <code>Object</code> class and has taken precedence. We are warned of such behaviour in the <a href="http://search.cpan.org/~drolsky/Moose-1.24/lib/Moose/Manual/Types.pod#TYPE_NAMES">documentation</a>.</p>
<p>To understand why our <code>Object</code> and <code>Player</code> instances meet the constraint, consider its definition:</p>
<pre class="sourceCode Perl"><code class="sourceCode perl">subtype <span class="kw">&#39;</span><span class="st">Object</span><span class="kw">&#39;</span> =&gt; as <span class="kw">&#39;</span><span class="st">Ref</span><span class="kw">&#39;</span> =&gt;
  where { blessed(<span class="dt">$_</span>) &amp;&amp; blessed(<span class="dt">$_</span>) <span class="kw">ne</span> <span class="kw">&#39;</span><span class="st">Regexp</span><span class="kw">&#39;</span> } =&gt;
  optimize_as \&amp;<span class="fu">Moose::Util</span>::<span class="fu">TypeConstraints</span>::<span class="fu">OptimizedConstraints</span>::<span class="fu">Object</span>;</code></pre>
<p>in <code>Moose::Util::TypeConstraints</code> (see <a href="https://github.com/moose/moose/blob/1.24/lib/Moose/Util/TypeConstraints.pm#L749">here</a>). Any blessed reference will meet this constraint and can therefore be stored in <code>inventory</code>.</p>
<h3 id="solution">Solution</h3>
<p>We need to disambiguate our <code>Object</code> — either by placing it in a different package or renaming the class so that it doesn’t clash with a built in type constraint name. For simplicitly, let’s pick a different name, <code>Entity</code>:</p>
<pre class="sourceCode Perl"><code class="sourceCode perl"><span class="kw">package</span> Entity;
<span class="kw">use</span> Moose;

has <span class="kw">&#39;</span><span class="st">name</span><span class="kw">&#39;</span> =&gt; (
  isa       =&gt; <span class="kw">&#39;</span><span class="st">Str</span><span class="kw">&#39;</span>,
  is        =&gt; <span class="kw">&#39;</span><span class="st">rw</span><span class="kw">&#39;</span>,
  required  =&gt; <span class="dv">1</span>,
);

has <span class="kw">&#39;</span><span class="st">description</span><span class="kw">&#39;</span> =&gt; (
  isa       =&gt; <span class="kw">&#39;</span><span class="st">Str</span><span class="kw">&#39;</span>,
  is        =&gt; <span class="kw">&#39;</span><span class="st">rw</span><span class="kw">&#39;</span>,
  default   =&gt; <span class="kw">sub </span>{
    <span class="kw">&quot;</span><span class="st">You see nothing special about the </span><span class="kw">&quot;</span> . <span class="fu">shift</span>-&gt;name;
  },
);

<span class="kw">no</span> Moose;
<span class="dv">1</span>;</code></pre>
<p>meaning that <code>Player</code> needs to be changed to read,</p>
<pre class="sourceCode Perl"><code class="sourceCode perl"><span class="kw">package</span> Player;
<span class="kw">use</span> Moose;

has <span class="kw">&#39;</span><span class="st">inventory</span><span class="kw">&#39;</span> =&gt; (
  handles =&gt; { obtain =&gt; <span class="kw">&#39;</span><span class="st">push</span><span class="kw">&#39;</span> },
  isa     =&gt; <span class="kw">&#39;</span><span class="st">ArrayRef[Entity]</span><span class="kw">&#39;</span>,
  default =&gt; <span class="kw">sub </span>{ [] },
  traits  =&gt; [<span class="kw">&#39;</span><span class="st">Array</span><span class="kw">&#39;</span>],
  is      =&gt; <span class="kw">&#39;</span><span class="st">ro</span><span class="kw">&#39;</span>,
);

<span class="kw">sub </span><span class="fu">list_inventory</span> {
  <span class="kw">my</span> <span class="dt">$self</span>    = <span class="fu">shift</span>;
  <span class="kw">my</span> <span class="dt">@inv</span>     = <span class="dt">@</span>{ <span class="dt">$self</span>-&gt;<span class="dt">inventory</span> };

  <span class="kw">return</span> <span class="kw">&quot;</span><span class="st">You are carrying nothing.</span><span class="kw">&quot;</span> <span class="kw">unless</span> <span class="dt">@inv</span>;

  <span class="kw">my</span> <span class="dt">$output</span>  = <span class="kw">&quot;</span><span class="st">You are currently carrying:</span><span class="ch">\n</span><span class="kw">&quot;</span>;
  <span class="dt">$output</span>    .= (<span class="kw">&quot;</span><span class="st">* </span><span class="kw">&quot;</span> . <span class="dt">$_</span>-&gt;<span class="dt">name</span> . <span class="kw">&quot;</span><span class="ch">\n</span><span class="kw">&quot;</span>) <span class="kw">foreach</span>(<span class="dt">@inv</span>);
  <span class="kw">return</span> <span class="dt">$output</span>;
}

<span class="kw">no</span> Moose;
<span class="dv">1</span>;</code></pre>
<p>We also need to update <code>entity.pl</code>:</p>
<pre class="sourceCode Perl"><code class="sourceCode perl"><span class="kw">#!/usr/bin/env perl</span>

<span class="kw">use</span> <span class="kw">strict</span>;
<span class="kw">use</span> <span class="kw">warnings</span>;
<span class="kw">use</span> feature <span class="kw">&#39;</span><span class="st">say</span><span class="kw">&#39;</span>;

<span class="kw">use</span> Entity;
<span class="kw">use</span> Player;

<span class="kw">my</span> <span class="dt">$lamp</span>    = Entity-&gt;new(name =&gt; <span class="kw">&quot;</span><span class="st">lamp</span><span class="kw">&quot;</span>);
<span class="kw">my</span> <span class="dt">$player</span>  = Player-&gt;new;

say <span class="dt">$player</span>-&gt;<span class="dt">list_inventory</span>;
<span class="dt">$player</span>-&gt;<span class="dt">obtain</span>(<span class="dt">$lamp</span>);
say <span class="dt">$player</span>-&gt;<span class="dt">list_inventory</span>;

<span class="co"># but:</span>
<span class="dt">$player</span>-&gt;<span class="dt">obtain</span>(<span class="dt">$player</span>);</code></pre>
<p>Running it now gives the following output:</p>
<pre><code># ./entity.pl
You are carrying nothing.
You are currently carrying:
* lamp
A new member value for &#39;inventory&#39; does not pass its type constraint because:
Validation failed for &#39;Entity&#39; with value Player=HASH(0x803f20) (not isa Entity)
  at ./entity.pl line 20</code></pre>
<p>Attempting to add <code>$player</code> to the inventory has thrown an exception as desired.</p>
<p>Finally, we can modify <code>entry.pl</code> to fix the error:</p>
<pre class="sourceCode Perl"><code class="sourceCode perl"><span class="kw">#!/usr/bin/env perl</span>

<span class="kw">use</span> <span class="kw">strict</span>;
<span class="kw">use</span> <span class="kw">warnings</span>;
<span class="kw">use</span> feature <span class="kw">&#39;</span><span class="st">say</span><span class="kw">&#39;</span>;

<span class="kw">use</span> Entity;
<span class="kw">use</span> Player;

<span class="kw">my</span> <span class="dt">$lamp</span>    = Entity-&gt;new(name =&gt; <span class="kw">&quot;</span><span class="st">lamp</span><span class="kw">&quot;</span>);
<span class="kw">my</span> <span class="dt">$player</span>  = Player-&gt;new;

say <span class="dt">$player</span>-&gt;<span class="dt">list_inventory</span>;
<span class="dt">$player</span>-&gt;<span class="dt">obtain</span>(<span class="dt">$lamp</span>);
say <span class="dt">$player</span>-&gt;<span class="dt">list_inventory</span>;

<span class="co"># this will throw an exception:</span>
<span class="co"># $player-&gt;obtain($player);</span></code></pre>
<h2 id="summary-of-pitfalls">Summary of pitfalls</h2>
<p>Consider what we have learnt so far,</p>
<ul>
<li>The existence of a type in <code>Moose</code> is <em>either</em> the result of an explicit constraint <em>or</em> an existing class.</li>
<li>In the latter case, an implicit type will be inferred on first use.</li>
<li>While a derived class can be considered a subtype of its base class, we don’t have a homogeneous type system;</li>
<li>Arbitrary constraints can be written that disregard inheritance relationships entirely.</li>
<li>As such, there is no clear relationship between subtype and subclass.</li>
</ul>
<h1 id="conclusion">Conclusion</h1>
<p>First of all, it would be unfair not to repeat the <a href="http://search.cpan.org/~drolsky/Moose-1.24/lib/Moose/Util/TypeConstraints.pm#Important_Caveat">important caveat</a> presented in the <code>Moose</code> documentation:</p>
<blockquote>
<p>This is <strong>NOT</strong> a type system for Perl 5. These are type constraints, and they are not used by Moose unless you tell it to. No type inference is performed, expressions are not typed, etc. etc. etc.</p>
<p>A type constraint is at heart a small “check if a value is valid” function. A constraint can be associated with an attribute. This simplifies parameter validation, and makes your code clearer to read, because you can refer to constraints by name.</p>
</blockquote>
<p>Reasoning about types in a <code>Moose</code> system seems quite problematic given the overlap between two rather distinct means of predicating on instances.</p>
<p>Rather than thinking in terms of type systems, it may be more helpful to think of <code>Moose</code>’s “type constraints” as simple predicates that have nothing to do with OOP typing. Rather, <code>Moose</code> provides:</p>
<ol style="list-style-type: decimal">
<li>A hierarchy of built in predicates that can be used to place constraints on data and,</li>
<li>A means for automatically generating transitive predicates on the basis of the class hierarchy.</li>
</ol>
  </div>
</div>

<hr/>

<div id="disqus_thread"></div>
<script type="text/javascript">
  var disqus_shortname = '0branch'; // required: replace example with your forum shortname
  /* * * DON'T EDIT BELOW THIS LINE * * */
  (function() {
  var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
  dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></summary>
</entry>

</feed>
