<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Passing Curiosity: Posts tagged functional programming</title>
    <link href="https://passingcuriosity.com/tags/functional-programming/functional-programming.xml" rel="self" />
    <link href="https://passingcuriosity.com" />
    <id>https://passingcuriosity.com/tags/functional-programming/functional-programming.xml</id>
    <author>
        <name>Thomas Sutton</name>
        
        <email>me@thomas-sutton.id.au</email>
        
    </author>
    <updated>2017-01-02T00:00:00Z</updated>
    <entry>
    <title>Yow! Conference, Sydney 2013</title>
    <link href="https://passingcuriosity.com/2017/yow-developer-conference/" />
    <id>https://passingcuriosity.com/2017/yow-developer-conference/</id>
    <published>2017-01-02T00:00:00Z</published>
    <updated>2017-01-02T00:00:00Z</updated>
    <summary type="html"><![CDATA[<blockquote>
<p>It’s the beginning of a new year so I’m cleaning out some files in
my drafts directory. This post was started on December 13, 2013.</p>
</blockquote>
<ul>
<li>~40 speakers</li>
<li>~440 attendees</li>
<li>three cities</li>
</ul>
<p>YOW! LambdaJam in May was excellent and this was pretty great too. The YOW!
people seem to put on great conferences.</p>
<h2 id="day-one">Day one</h2>
<h3 id="jeff-hawkins-on-machine-intelligence">Jeff Hawkins on machine intelligence</h3>
<p>The day kicked off with Jeff Hawkins (of Palm and Handspring fame) giving a
keynote in which he described the neurologically-inspired approach to machine
intelligence being developed by his current company (<a href="https://groksolutions.com/">Grok Solutions</a>) and
others. The basis of this approach is in building learning systems with many of
the properties of biological intelligence (universality, robustness, etc.) by
modelling them on the operation of neural structures in the <a href="http://en.wikipedia.org/wiki/Neocortex">neocortex</a>.</p>
<p>One of the key points was the use of representations which enable data storage
and processing in ways which are efficient and accurate <em>enough</em> for machine
intelligence. In particular, the use of <em>sparse distributed representations</em>
(SDR) is key to the model of intelligence described. Dense representations
(such as ASCII) use a very small number of bits to represent particular states
but each bit is devoid of semantic information: the state of “bit 3” in an
ASCII character conveys no useful information. An SDR uses many more bits, each
representing a particular feature in the learning domain (e.g. a property of
objects or a word in a corpus); as such, most bits in a particular SDR instance
will be 0 (hence the “sparse” in the name).</p>
<p>SDRs have several properties which make them useful for learning tasks: similar
objects have similar representations; they allow sub-sampling without losing
all meaning; they behave well with union/membership and other set operations
(an SDR is, in some sense, similar to a <a href="http://en.wikipedia.org/wiki/Bloom_filter">Bloom filter</a>). According to Jeff:</p>
<blockquote>
<p>“All intelligent machines will be based on sparse distributed representations.”</p>
</blockquote>
<p>The <em>cortical learning algorithm</em> developed by Grok Systems and implemented in
the <a href="http://numenta.org/">Numenta Platform for Intelligent Computing</a> open source project
(GPLv3) builds on these ideas and implements a learning system modelled on a
cortical region to learn about “normal” inputs and then predict and detect
anomalies from streaming input. Jeff described two applications in which this
software has been deployed: monitoring and detecting anomalies in monitoring
server metrics, and natural language processing.</p>
<p>The first example (built by Grok Systems and included in the NuPIC open source
project) is used to monitor metrics from resources in Amazon Web Services and
to detect anomalies in their behaviour. This approach can identify conditions
which traditional (and, it must be said, much, much simpler) threshold-based
approaches cannot.</p>
<p>The second example – developed by <a href="http://www.cept.at/">CEPT Systems</a> – derives SDRs of words
from Wikipedia pages and then deploys these SDRs in particular learning
problems. This can be used to demonstrate the set-like properties of SDRs:
sdr(apple) - sdr(fruit) = sdr(computer). A CLA trained on inputs like “ANIMAL
VERB OBJECT” was able to make sensible predictions for new inputs it hadn’t
seen before, including “fox” and “eat” yielding “rodent”.</p>
<p>This was a pretty great talk and got the conference off to a great start!</p>
<h3 id="charles-nutter-on-language-engineering-for-the-jvm">Charles Nutter on language engineering for the JVM</h3>
<p>In the second session I saw <a href="http://blog.headius.com/">Charles Nutter</a>’s talk “Beyond JVM” in which
he discussed the engineering issues which face JVM-targeting languages like
<a href="http://jruby.org/">JRuby</a>. Charles discussed some of the pros and cons for targeting the JVM
(many of the pros <em>are</em> also cons) and then jumped into four of the key
challenges faced by the JRuby project: startup time, native interoperability,
language performance, and the lack of flexibility in the JVM (the big ball of
C++).</p>
<p>Charles discussed a number of ways to improve JVM and application <strong>startup
time</strong>: tweaking JVM flags helps, but can be fragile in the face of different
JVMs, JVM version changes, and typically impact later performance; keeping
persistent JVM instances (using tools like <a href="http://www.martiansoftware.com/nailgun/">Nailgun</a>) can be cause
problems cleaning up resources (memory leaks, background threads, etc);
pre-loading JVMs with tools like <a href="https://github.com/flatland/drip">Drip</a> can improve performance while
avoiding the cleanup problems with persistent JVMs.</p>
<p>The problem of <strong>native interoperability</strong> is a complex one with a range of
solutions. The traditional approach used JNI which is horrible: you write code
for both your intention (“I want to call getpid()”) <em>and</em> how to implement it.
The JNR project provide a real foreign function interface on the JVM structured
into a number of layers: jffi provides platform-specific FFI functionality,
jnr-ffi defines structures, etc. to interface with jffi, jnr-posix exposes a
range of POSIX APIs (the ones JRuby have needed so far) and jnr-constants
defines a range of constants as defined on the host platform, and jnr-enxio
implements Java NIO for arbitrary file descriptors (allowing a range of I/O
functionality which can’t otherwise be expressed on JVM). JNR generates code
which is as direct as possible for each particular case, resulting in very low
overheads for each call.</p>
<p>One of the key motivations for JRuby is <strong>language performance</strong>. While the JVM
specification made mention of non-Java languages, it didn’t go out of it’s way
to actually support them. The relatively new <code>invokedynamic</code> bytecode allows
language implementers to customise invocation mechanisms to suit the specifics
of their language. The JVM will cache and optimise the results of dynamic
invocations as normal. This can result in plain ruby code run on JRuby being
faster than using a native extension under CRuby (redblack tree benchmark).</p>
<p>Finally, Charles discussed approaches that language implementors can use to
deal with the <strong>inflexibility of the JVM internals</strong>. The <a href="http://openjdk.java.net/projects/graal/">Graal project</a>
allows language implementors to customise the way that their implementations
are optimised and emit the ASM/HotSpot intermediate representation appropriate
for the particular language’s constructs. Truffle, a framework built on top of
Graal, allows you to implement an interpreter for your language (structured and
annotated in a particular way) and to automatically derive a JIT for it. (This
sounds a little like the second Futamura projection to me.)</p>
<p>This talk was very well presented and very informative. If I’d known it was
“about” JRuby I probably wouldn’t have gone but I’m glad I did!</p>
<h3 id="julien-verlaguet-on-facebooks-static-typing-for-php">Julien Verlaguet on Facebook’s static typing for PHP</h3>
<p><a href="https://www.facebook.com/julien.verlaguet">Julien Verlaguet</a> is an engineer at Facebook and spoke about the work
they’ve done to improve on the PHP language with <a href="http://www.hiphop-php.com/">HHVM</a> and “Hack” - a
statically typed version of PHP which was the primary subject of the talk.</p>
<p>Contrary to Facebook’s earlier attempts at improving the deployment and runtime
story for PHP (the HipHop compiler translated PHP code into C++ which compiled
into a native binary), HHVM is a fairly traditional virtual machine with a JIT.
The <a href="http://www.hiphop-php.com/blog/">HHVM blog</a> has a bunch of interesting posts about the development of
the VM and the JIT both, go read it!</p>
<p>HHVM supports two source languages: normal PHP and Hack. Hack (the code name
might change) is a statically typed variant of PHP which is compatible with
PHP, uses the same run-time representations within the VM and was designed for
incremental adoption (a necessity when dealing with massive codebases like
Facebook.com).</p>
<p>The static typing for Hack requires that the programmer add type annotations to
class members, function parameters and return values and infers all other
types. The types supported include the basic types built-in to PHP, collections
and generics. It also distinguishes the types of nullable and non-nullable
values. PHP was not designed for type checking, so the type checker must make
several allowances. The most interesting is, perhaps, the delay of type
unification to call sites rather than function definitions.</p>
<p>The Hack type checker is implemented as a daemon which listens for file system
events on the code base and communicates with a client to “run” a check and
present errors. The errors are designed to give specific, useful feedback to
the programmer including references to each annotation which resulted in the
error (“it tells a story”). The checker is also able to output coloured
“coverage” style reports of code showing which code is checked/unchecked.</p>
<p>Conversion of existing PHP to Hack has happened in two ways: organic adoption
by developers as they and their teams take up Hack; and automatic conversion
using tools to analyse, refactor and monitor changes in the code base. This
includes support for “soft” conversions, which are monitored but not enforced
until they are known to be accurate.</p>
<p>Hack and HHVM sound like great improvements over PHP. I never got around to
trying HPHP before it went away but perhaps I’ll give HHVM a go.</p>
<h3 id="kevlin-henney-deconstructed-the-solid-principles">Kevlin Henney deconstructed the SOLID principles</h3>
<p><a href="http://kevlin.tel/">Kevlin Henney</a></p>
<p>I’m not really one for talks about methodologies and such, but Kevlin’s talk
“the <a href="http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)">SOLID</a> Design Principles Deconstructed” was entertaining and not a
little informative.</p>
<h3 id="gilad-bracha-on-dart-and-newspeak">Gilad Bracha on Dart and Newspeak</h3>
<p><a href="http://bracha.org/">Gilad Bracha</a> is an engineer at Google where he works on Dart. He spoke
about Dart and Newspeak.</p>
<h3 id="joe-albahari-on-concurrency-in-.net">Joe Albahari on concurrency in .NET</h3>
<p><a href="http://www.albahari.com/">Joe Albahari</a> spoke about concurrency in C# 5.</p>
<h3 id="scott-hanselman-on-the-web-platform">Scott Hanselman on the web platform</h3>
<p><a href="http://www.hanselman.com/">Scott Hanselman</a> works on Azure and ASP.NET for Microsoft.</p>
<h2 id="day-two">Day Two</h2>
<h3 id="philip-wadler-reprised-the-first-monad-tutorial">Philip Wadler reprised the first monad tutorial</h3>
<p><a href="http://homepages.inf.ed.ac.uk/wadler/">Philip Wadler</a></p>
<h3 id="aaron-bedra-on-behaviour-and-reputation-based-security-controls">Aaron Bedra on behaviour and reputation based security controls</h3>
<p><a href="http://aaronbedra.com/">Aaron Bedra</a></p>
<h3 id="sam-newman-on-microservice-architecture">Sam Newman on microservice architecture</h3>
<p><a href="http://blog.magpiebrain.com/">Sam Newman</a></p>
<h3 id="functional-programming-in-industry">Functional programming in industry</h3>
<p><a href="http://korny.info/">Kornelis Sietsma</a>, <a href="http://www.michaelneale.net/">Michael Neale</a> and <a href="http://twitter.com/jedws">Jed Wesley-Smith</a>
gave a set of three talks about the adoption and use of functional programming
languages at three different companies.</p>
<h3 id="jay-fields-on-adopting-clojure">Jay Fields on adopting Clojure</h3>
<p><a href="http://jayfields.com/">Jay Fields</a></p>
<h3 id="daniel-spiewak-on-modules-and-the-expression-problem">Daniel Spiewak on modules and the expression problem</h3>
<p><a href="http://www.codecommit.com/blog/">Daniel Spiewak</a></p>
<h3 id="stewart-gleadow-on-mobile-app-and-their-apis">Stewart Gleadow on mobile app and their APIs</h3>
<p><a href="http://www.stewgleadow.com/">Stewart Gleadow</a></p>
<h2 id="sponsors-and-exhibitors">Sponsors and Exhibitors</h2>
<p>Sponsors include Suncorp, DiUS, ThoughtWorks, Mashery,</p>]]></summary>
</entry>
<entry>
    <title>Some ScalaCheck generators fail too much</title>
    <link href="https://passingcuriosity.com/2015/scalacheck-discarded-tests/" />
    <id>https://passingcuriosity.com/2015/scalacheck-discarded-tests/</id>
    <published>2015-12-29T00:00:00Z</published>
    <updated>2015-12-29T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>Most of the code I’ll be working with in my new job (BTW blog: I have
a new job) is written in <a href="http://www.scala-lang.org/">Scala</a> and uses property based testing
with <a href="https://scalacheck.org/">ScalaCheck</a>. Yesterday I ran into a problem with an existing
test suite that suddenly began failing with too many discarded tests:</p>
<pre><code>[info] FormattersSpec
[info]   Formatters are invertible for:
[info]     + Mapping
[info]     + Identifier
[info]
[error]     x Metadata
[error]  Gave up after only 39 passed tests. 197 tests were discarded. (FormattersSpec.scala:11)</code></pre>
<p>This test generates random <code>Metadata</code> values and makes sure that they
can be serialised and deserialised correctly (i.e. values can be
round-tripped). The property being test here is identical, only the
<code>Arbitrary</code>, <code>Serialise</code>, and <code>Deserialise</code> instances vary in each
case. The truly odd thing is that the pertinent code looks like this:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode scala"><code class="sourceCode scala"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="cf">case</span> <span class="kw">class</span> <span class="fu">Identifier</span><span class="op">(</span>name<span class="op">:</span> <span class="ex">String</span><span class="op">)</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="cf">case</span> <span class="kw">class</span> <span class="fu">Metadata</span> <span class="op">(</span>id<span class="op">:</span> Identifier<span class="op">,</span> maps<span class="op">:</span> <span class="ex">Set</span><span class="op">[</span>Identifier<span class="op">])</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="kw">implicit</span> <span class="kw">val</span> ArbIdentifier <span class="op">=</span> <span class="fu">Arbitrary</span><span class="op">(</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a>  <span class="cf">for</span> <span class="op">{</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>    name  <span class="op">&lt;-</span> arbitrary<span class="op">[</span><span class="ex">String</span><span class="op">]</span></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>  <span class="op">}</span> <span class="cf">yield</span> <span class="fu">Identifier</span><span class="op">(</span>name<span class="op">)</span></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a><span class="op">)</span></span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a><span class="kw">implicit</span> <span class="kw">val</span> ArbMetadata <span class="op">=</span> <span class="fu">Arbitrary</span><span class="op">(</span></span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a>  <span class="cf">for</span> <span class="op">{</span></span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a>    identifier <span class="op">&lt;-</span> arbitrary<span class="op">[</span>Identifier<span class="op">]</span></span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a>    mappings   <span class="op">&lt;-</span> arbitrary<span class="op">[</span><span class="ex">Set</span><span class="op">[</span>Identifier<span class="op">]]</span></span>
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a>  <span class="op">}</span> <span class="cf">yield</span> <span class="fu">Metadata</span><span class="op">(</span>identifier<span class="op">,</span> mappings<span class="op">)</span></span>
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a><span class="op">)</span></span></code></pre></div>
<p>My first step was redefining a few related <code>Arbitrary</code> instances to
avoid using <code>suchThat</code> (which discards invalid values) but this didn’t
fix the problem. Eventually I tried redefining <code>ArbMetadata</code> like
this:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode scala"><code class="sourceCode scala"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="kw">implicit</span> <span class="kw">val</span> ArbMetadata <span class="op">=</span> <span class="fu">Arbitrary</span><span class="op">(</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>  <span class="cf">for</span> <span class="op">{</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>    identifier <span class="op">&lt;-</span> arbitrary<span class="op">[</span>Identifier<span class="op">]</span></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>    mappings   <span class="op">&lt;-</span> Gen<span class="op">.</span><span class="fu">const</span><span class="op">(</span><span class="ex">Set</span><span class="op">.</span>empty<span class="op">[</span><span class="ex">Set</span><span class="op">[</span>Identifier<span class="op">]])</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a>  <span class="op">}</span> <span class="cf">yield</span> <span class="fu">Metadata</span><span class="op">(</span>identifier<span class="op">,</span> mappings<span class="op">)</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a><span class="op">)</span></span></code></pre></div>
<p>and the problem went away. Trying to use <code class="sourceCode scala">arbitrary<span class="op">[</span><span class="ex">Set</span><span class="op">[</span>Identifier<span class="op">]]</span></code>
in various ways in the Scala REPL confirmed that it is the problem; we
can easily generate as large a <code>List[Identifier]</code> as we like, but a
<code>Set[Identifier]</code> fails fairly frequently:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode scala"><code class="sourceCode scala"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co">// This always generates a Some[List[Identifier]] value.</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>Gen<span class="op">.</span><span class="fu">listOfN</span><span class="op">(</span><span class="dv">100</span><span class="op">,</span> arbitrary<span class="op">[</span>Identifier<span class="op">]).</span><span class="fu">map</span><span class="op">(</span>_<span class="op">.</span>length<span class="op">).</span>sample</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="co">// Sometimes we get a Some[List[Set[Identifier]]] and others None.</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a>Gen<span class="op">.</span><span class="fu">listOfN</span><span class="op">(</span><span class="dv">100</span><span class="op">,</span> arbitrary<span class="op">[</span><span class="ex">Set</span><span class="op">[</span>Identifier<span class="op">]]).</span><span class="fu">map</span><span class="op">(</span>_<span class="op">.</span>length<span class="op">).</span>sample</span></code></pre></div>
<p>It appears as though whatever mechanism is used by <code>arbitrary[Set[_]]</code>
to construct the sets, it doesn’t fails when the generator for the
value type returns duplicate elements. You can confirm this easily by
trying <code>arbitrary[Set[Unit]]</code>; any <code>Gen[Unit]</code> has no choice but to
return a the single value of type <code>Unit</code> (or to fail) and, as
expected, this almost never succeeds. Replacing the problematic
<code>arbitrary[Set[Identifier]]</code> in the original code with
<code class="sourceCode scala">arbitrary<span class="op">[</span><span class="bu">Seq</span><span class="op">[</span>Identifier<span class="op">]].</span><span class="fu">map</span><span class="op">(</span>_<span class="op">.</span>toSet<span class="op">)</span></code> resolves the issue:
constructing a set from a list of possibly duplicate <code>Identifier</code>s
always works.</p>
<p>After a bit of reading in the ScalaCheck source code it <em>seems</em> as
though the root cause of this problem is some instance of
<code class="sourceCode scala">CanBuildFrom<span class="op">[</span><span class="ex">Set</span><span class="op">[</span>_<span class="op">],</span> A<span class="op">,</span> <span class="ex">Set</span><span class="op">[</span>A<span class="op">]]</span></code> but I’ve no idea how to go
about figure out which one or why it’s broken. In any case, I now know
a bit more about working with Scala.</p>
<p>For more information, see the <a href="https://github.com/rickynils/scalacheck/issues/89">ScalaCheck issue #89</a>.</p>]]></summary>
</entry>
<entry>
    <title>Testing multiple GHC versions on Travis CI</title>
    <link href="https://passingcuriosity.com/2015/testing-multiple-ghc-versions-on-travis-ci/" />
    <id>https://passingcuriosity.com/2015/testing-multiple-ghc-versions-on-travis-ci/</id>
    <published>2015-04-04T00:00:00Z</published>
    <updated>2015-04-04T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>GHC 7.10.1 was recently released and, if nothing else, that means there’s a new
version of the base library so a lot of developers will need to bump the upper
bound specified in their cabal files. Having multiple versions of the compiler
installed isn’t all that difficult, but actually building an testing a cabal
package with multiple compiler versions <em>is</em> pretty tedious. It’s easy enough
to do on Travis CI though and with just a little bit of cargo-culting, you too
can be extending your set of supported GHC versions.</p>
<h2 id="multiple-versions-of-ghc-on-travis-ci">Multiple versions of GHC on Travis CI</h2>
<p><a href="https://travis-ci.org/">Travis CI</a>, for those who aren’t familiar with it, is a continuous
integration service with pretty tight integration with <a href="https://github.com/">GitHub</a>. Using it is
pretty straightforward: you add a YAML file to your repository describing how
to test your project and then you turn it on. Using <a href="http://docs.travis-ci.com/user/environment-variables/#Matrix-Variables">matrix variables</a> in
your YAML file you can specify multiple values for various aspects of your
build process and Travis CI will run your job multiple times - once for each
combination of values. The standard approach to testing with <a href="https://github.com/hvr/multi-ghc-travis">multiple GHC
versions</a> on Travis CI uses this to specify which versions of GHC and Cabal
to install and use in the build; specify four versions and get run four times.
Magic!</p>
<p>The <code>.travis.yml</code> file you use to do this is a little more complex than the
usual one saying “make my Haskell go!”, but you can generally just copy it
around from project to project. I edited <a href="https://github.com/thsutton/edit-distance-vector/blob/master/.travis.yml">my <code>.travis.yml</code> file</a> slightly to
tweak the way Travis CI sends me email and to select the versions of GHC that I
care about and now <a href="https://travis-ci.org/thsutton/edit-distance-vector">every build for that project</a> automatically covers all
the cases I care about.</p>
<h2 id="so-what">So what?</h2>
<p>My <code>edit-distance-vector</code> package is very simple: it’s one module with 166
lines (including comments and white space) and 100 lines of tests (again,
including comments and white space). Here are the issues picked up by testing
with four versions of GHC:</p>
<ol type="1">
<li><p>Obviously, the version bounds on the base library need to be broadened. I’ve
used <code>base &gt;=4.5 &amp;&amp; &lt;4.9</code> now.</p></li>
<li><p>Next I learn that the <a href="http://hackage.haskell.org/package/base-4.8.0.0/docs/Data-Monoid.html#t:Sum">Sum</a> type didn’t have a <a href="http://hackage.haskell.org/package/base-4.8.0.0/docs/Prelude.html#t:Num">Num</a> instance in
earlier versions. This means that constants like <code>1</code> can’t have types like
<code>Sum Int</code> so I’ve just applied the <code>Sum</code> constructor manually: <code>Sum 1</code>.</p></li>
<li><p>Then I learned that importing a module <code>hiding</code> something that it doesn’t
export used to be an error (it is now a warning).</p></li>
</ol>
<p>The <a href="https://github.com/thsutton/edit-distance-vector/commit/d287d8b97deee5cb3b3e2fe74e155226c40b96a4">commit</a> fixing these issues is pretty trivial but made the library
usable in a wider range of environments. Yay!</p>
<p>I think I’ll be using this by default in new Haskell repositories; when my code
doesn’t work with some version of GHC I’d like it to be because I <em>decided</em> to
do it, not just that I didn’t know.</p>]]></summary>
</entry>
<entry>
    <title>Released some Haskell packages</title>
    <link href="https://passingcuriosity.com/2015/released-haskell-packages/" />
    <id>https://passingcuriosity.com/2015/released-haskell-packages/</id>
    <published>2015-04-03T00:00:00Z</published>
    <updated>2015-04-03T00:00:00Z</updated>
    <summary type="html"><![CDATA[<h2 id="edit-distance-vector">edit-distance-vector</h2>
<p>The <a href="https://hackage.haskell.org/package/edit-distance-vector">edit-distance-vector</a> package is a small library for calculating the
optimal edit script and cost to transform one sequence of values into another.
The implementation uses the <a href="https://en.wikipedia.org/wiki/Wagner–Fischer_algorithm">Wagner-Fischer</a> algorithm and the rather fun
<a href="https://hackage.haskell.org/package/vector/docs/Data-Vector.html#v:constructN"><code>constructN</code></a> function.</p>
<p>I have a draft blog post on the way about the details of this package but until
that’s done you’ll have to make do with the <a href="https://hackage.haskell.org/package/edit-distance-vector-1.0/docs/Data-Vector-Distance.html">documentation</a>.</p>
<h2 id="aeson-diff">aeson-diff</h2>
<p>The <a href="https://hackage.haskell.org/package/aeson-diff">aeson-diff</a> package includes a library and two command-line programs
for extracting the differences between two JSON documents and for applying
these changes. The commands are:</p>
<ul>
<li><p><code>aeson-diff</code> which compares two JSON documents and generates a patch
describing the differences between them; and</p></li>
<li><p><code>aeson-patch</code> which takes a JSON document and updates it according to patch.</p></li>
</ul>
<p>I find the <code>aeson-diff</code> command quite useful for comparing different versions
of the JSON documents spewed out by several systems I have to deal with at
work.</p>]]></summary>
</entry>
<entry>
    <title>Reading from processes safely in Haskell</title>
    <link href="https://passingcuriosity.com/2015/haskell-reading-process-safe-deadlock/" />
    <id>https://passingcuriosity.com/2015/haskell-reading-process-safe-deadlock/</id>
    <published>2015-03-04T00:00:00Z</published>
    <updated>2015-03-04T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>Quite a bit of my work lately has been implementing HTTP interfaces to existing
systems. In a few cases this required invoking existing command-line tools and
parsing their output. The naive approach to invoking a <a href="https://hackage.haskell.org/package/process">process</a> in Haskell
and reading its output goes something like this:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">System.Exit</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">System.Process</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="ot">main ::</span> <span class="dt">IO</span> ()</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>main <span class="ot">=</span> <span class="kw">do</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a>    <span class="kw">let</span> p <span class="ot">=</span> (shell <span class="st">&quot;cat /usr/share/dict/words&quot;</span>)</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a>            { std_in  <span class="ot">=</span> <span class="dt">Inherit</span></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a>            , std_out <span class="ot">=</span> <span class="dt">CreatePipe</span></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a>            , std_err <span class="ot">=</span> <span class="dt">Inherit</span></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a>            }</span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a>    (<span class="dt">Nothing</span>, <span class="dt">Just</span> out, <span class="dt">Nothing</span>, ph) <span class="ot">&lt;-</span> createProcess p</span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a>    ec <span class="ot">&lt;-</span> waitForProcess ph</span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a>    <span class="kw">case</span> ph <span class="kw">of</span></span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a>        <span class="dt">ExitSuccess</span>   <span class="ot">-&gt;</span> hGetContents out <span class="op">&gt;&gt;=</span> <span class="fu">print</span></span>
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a>        <span class="dt">ExitFailure</span> _ <span class="ot">-&gt;</span> <span class="fu">error</span> <span class="st">&quot;Bad things happened. :-(&quot;</span></span></code></pre></div>
<p>There is a potential problem in this code: we wait until the process has
terminated before reading the <code>Handle</code> allowing its output to accumulate in the
pipe buffer managed by the operating system in the mean time. This buffer has
a fixed size on most systems (this is a good thing!); when it fills up, the
writing process will go to sleep until the reader has consumed some data and
freed some buffer space to hold the next write. Alas, the reader (the Haskell
code above) is sleeping, waiting for the writer to terminate. The reader is
sleeping, waiting for the writer to terminate; and the writer is sleeping,
waiting for the reader to read. This is a <a href="https://en.wikipedia.org/wiki/Deadlock">deadlock</a>!</p>
<p>The solution is to do the Right Thing (tm) and take care of any buffering
behaviour we want ourselves. Thankfully this is pretty straightforward and it’s
the sort of code you generally only need to write once. The very simplest case
– reading from a process with a single output <code>Handle</code> – looks like this:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ot">gatherOutput ::</span> <span class="dt">ProcessHandle</span> <span class="ot">-&gt;</span> <span class="dt">Handle</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">ExitCode</span>, <span class="dt">ByteString</span>)</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>gatherOutput ph h <span class="ot">=</span> work <span class="fu">mempty</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>  <span class="kw">where</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>    work acc <span class="ot">=</span> <span class="kw">do</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a>        <span class="co">-- Read any outstanding input.</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>        bs <span class="ot">&lt;-</span> BS.hGetNonBlocking h (<span class="dv">64</span> <span class="op">*</span> <span class="dv">1024</span>)</span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>        <span class="kw">let</span> acc' <span class="ot">=</span> acc <span class="op">&lt;&gt;</span> bs</span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a>        <span class="co">-- Check on the process.</span></span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a>        s <span class="ot">&lt;-</span> getProcessExitCode ph</span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a>        <span class="co">-- Exit or loop.</span></span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a>        <span class="kw">case</span> s <span class="kw">of</span></span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a>            <span class="dt">Nothing</span> <span class="ot">-&gt;</span> work acc'</span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a>            <span class="dt">Just</span> ec <span class="ot">-&gt;</span> <span class="kw">do</span></span>
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a>                <span class="co">-- Get any last bit written between the read and the status</span></span>
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a>                <span class="co">-- check.</span></span>
<span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a>                <span class="fu">last</span> <span class="ot">&lt;-</span> BS.hGetContents h</span>
<span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a>                <span class="fu">return</span> (ec, acc' <span class="op">&lt;&gt;</span> <span class="fu">last</span>)</span></code></pre></div>
<p>This is essentially a loop which reads some input from the <code>Handle</code> (possibly
an empty string), checks to see if the process has terminated, and either
returns the accumulated input or loops again. Extending this to gather the
output of two handles (like <code>stderr</code> and <code>stdout</code>) is relatively
straightforward.</p>]]></summary>
</entry>
<entry>
    <title>Multiple JSON encodings in Haskell</title>
    <link href="https://passingcuriosity.com/2015/multiple-json-representations-haskell/" />
    <id>https://passingcuriosity.com/2015/multiple-json-representations-haskell/</id>
    <published>2015-02-12T00:00:00Z</published>
    <updated>2015-02-12T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>I’m currently working on a small RESTful API to control a system with
a command-line interface. The command produces JSON output but it’s not really
ideal to expose in an API. This post describes the approach I took to
supporting two different JSON encodings for the same set of data types – one
for communicating with API clients and another for communicating with the
upstream system.</p>
<p>I’ll start with some data types to represent the data my API manages. In this
post I’ll use the example of a painting robot. The robot can carry several
colours of paint but can only paint with one “active” colour at a time. Here
are some data types to represent these details:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">newtype</span> <span class="dt">ColourName</span> <span class="ot">=</span> <span class="dt">ColourName</span> {<span class="ot"> unColourName ::</span> <span class="dt">Text</span> }</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>  <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>)</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Colour</span> <span class="ot">=</span> <span class="dt">Colour</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>    {<span class="ot"> colorName ::</span> <span class="dt">ColourName</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a>    ,<span class="ot"> colourRGB ::</span> (<span class="dt">Word8</span>, <span class="dt">Word8</span>, <span class="dt">Word8</span>)</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a>    }</span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a>  <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>)</span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="kw">newtype</span> <span class="dt">RobotName</span> <span class="ot">=</span> <span class="dt">RobotName</span> {<span class="ot"> unRobotName ::</span> <span class="dt">Text</span> }</span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a>  <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>)</span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Robot</span> <span class="ot">=</span> <span class="dt">Robot</span></span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a>    {<span class="ot"> robotName ::</span> <span class="dt">RobotName</span></span>
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a>    ,<span class="ot"> robotActiveColour ::</span> <span class="dt">ColourName</span></span>
<span id="cb1-16"><a href="#cb1-16" aria-hidden="true" tabindex="-1"></a>    ,<span class="ot"> robotAvailableColours ::</span> [<span class="dt">Colour</span>]</span>
<span id="cb1-17"><a href="#cb1-17" aria-hidden="true" tabindex="-1"></a>    }</span></code></pre></div>
<h2 id="json-for-the-api-clients">JSON for the API clients</h2>
<p>The JSON encoding of <code>Robot</code> that I’d like to provide to API clients is pretty
straightforward:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode json"><code class="sourceCode json"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span> <span class="dt">&quot;name&quot;</span> <span class="fu">:</span> <span class="st">&quot;Rosie the robot&quot;</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="fu">,</span> <span class="dt">&quot;activeColour&quot;</span> <span class="fu">:</span> <span class="st">&quot;red&quot;</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="fu">,</span> <span class="dt">&quot;availableColours&quot;</span> <span class="fu">:</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>    <span class="fu">{</span> <span class="dt">&quot;red&quot;</span>   <span class="fu">:</span> <span class="fu">{</span> <span class="dt">&quot;R&quot;</span><span class="fu">:</span> <span class="dv">255</span><span class="fu">,</span> <span class="dt">&quot;G&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;B&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">}</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a>    <span class="fu">,</span> <span class="dt">&quot;green&quot;</span> <span class="fu">:</span> <span class="fu">{</span> <span class="dt">&quot;R&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;G&quot;</span><span class="fu">:</span> <span class="dv">255</span><span class="fu">,</span> <span class="dt">&quot;B&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">}</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>    <span class="fu">,</span> <span class="dt">&quot;blue&quot;</span>  <span class="fu">:</span> <span class="fu">{</span> <span class="dt">&quot;R&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;G&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;B&quot;</span><span class="fu">:</span> <span class="dv">255</span><span class="fu">}</span></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>    <span class="fu">}</span></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a><span class="fu">}</span></span></code></pre></div>
<p>The Haskell code to parse this JSON using <a href="https://hackage.haskell.org/package/aeson">aeson</a> is straightforward too
(though please note that the instances derived for the <code>newtype</code> are only safe
to use <em>within</em> a larger JSON structure as they result in bare JSON strings,
not objects or arrays):</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="kw">deriving</span> <span class="kw">instance</span> <span class="dt">FromJSON</span> <span class="dt">ColourName</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="kw">deriving</span> <span class="kw">instance</span> <span class="dt">ToJSON</span> <span class="dt">ColourName</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">FromJSON</span> [<span class="dt">Colour</span>] <span class="kw">where</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a>    parseJSON (<span class="dt">Object</span> v) <span class="ot">=</span> <span class="fu">mapM</span> (<span class="fu">uncurry</span> colour) <span class="op">$</span> HashMap.toList v</span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a>      <span class="kw">where</span></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a>        colour name (<span class="dt">Object</span> o) <span class="ot">=</span> <span class="dt">Colour</span></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a>                <span class="op">&lt;$&gt;</span> parseJSON (<span class="dt">String</span> name)</span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a>                <span class="op">&lt;*&gt;</span> ((,,) <span class="op">&lt;$&gt;</span> o <span class="op">.:</span> <span class="st">&quot;R&quot;</span> <span class="op">&lt;*&gt;</span> o <span class="op">.:</span> <span class="st">&quot;G&quot;</span> <span class="op">&lt;*&gt;</span> o <span class="op">.:</span> <span class="st">&quot;B&quot;</span>)</span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a>        colour _ _ <span class="ot">=</span> <span class="fu">fail</span> <span class="st">&quot;Colour must be a JSON object&quot;</span></span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a>    parseJSON _ <span class="ot">=</span> <span class="fu">fail</span> <span class="st">&quot;Colours must be a JSON object&quot;</span></span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a><span class="kw">deriving</span> <span class="kw">instance</span> <span class="dt">FromJSON</span> <span class="dt">RobotName</span></span>
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a><span class="kw">deriving</span> <span class="kw">instance</span> <span class="dt">ToJSON</span> <span class="dt">RobotName</span></span>
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-16"><a href="#cb3-16" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">FromJSON</span> <span class="dt">Robot</span> <span class="kw">where</span></span>
<span id="cb3-17"><a href="#cb3-17" aria-hidden="true" tabindex="-1"></a>    parseJSON (<span class="dt">Object</span> v) <span class="ot">=</span> <span class="dt">Robot</span></span>
<span id="cb3-18"><a href="#cb3-18" aria-hidden="true" tabindex="-1"></a>        <span class="op">&lt;$&gt;</span> v <span class="op">.:</span> <span class="st">&quot;name&quot;</span></span>
<span id="cb3-19"><a href="#cb3-19" aria-hidden="true" tabindex="-1"></a>        <span class="op">&lt;*&gt;</span> v <span class="op">.:</span> <span class="st">&quot;activeColour&quot;</span></span>
<span id="cb3-20"><a href="#cb3-20" aria-hidden="true" tabindex="-1"></a>        <span class="op">&lt;*&gt;</span> v <span class="op">.:</span> <span class="st">&quot;availableColours&quot;</span></span>
<span id="cb3-21"><a href="#cb3-21" aria-hidden="true" tabindex="-1"></a>    parseJSON _ <span class="ot">=</span> <span class="fu">fail</span> <span class="st">&quot;Robot must be a JSON object&quot;</span></span></code></pre></div>
<p>To talk to the upstream system I’ll use the <a href="https://hackage.haskell.org/package/process">process</a> library to execute
a command which produces JSON on its standard output. A simple function to
invoke a command, parse the JSON, and return the value (or an error) keeps the
boilerplate contained:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>shellOutJSON</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="ot">    ::</span> (<span class="dt">MonadError</span> <span class="dt">String</span> m, <span class="dt">MonadIO</span> m, <span class="dt">FromJSON</span> a)</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>    <span class="ot">=&gt;</span> <span class="dt">String</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a>    <span class="ot">-&gt;</span> [<span class="dt">String</span>]</span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a>    <span class="ot">-&gt;</span> m a</span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a>shellOutJSON cmd args <span class="ot">=</span> <span class="kw">do</span></span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a>    <span class="co">-- Execute the command.</span></span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a>    (exit_code, out, _err) <span class="ot">&lt;-</span> liftIO <span class="op">$</span> readProcessWithExitCode cmd args <span class="st">&quot;&quot;</span></span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a>    <span class="co">-- Check it succeeded.</span></span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a>    output <span class="ot">&lt;-</span> <span class="kw">case</span> exit_code <span class="kw">of</span></span>
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true" tabindex="-1"></a>        <span class="dt">ExitSuccess</span> <span class="ot">-&gt;</span> <span class="fu">return</span> <span class="op">$</span> BS.pack out</span>
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true" tabindex="-1"></a>        <span class="dt">ExitFailure</span> err <span class="ot">-&gt;</span> throwError <span class="op">$</span></span>
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true" tabindex="-1"></a>            <span class="st">&quot;Could not execute command: error &quot;</span> <span class="op">&lt;&gt;</span> <span class="fu">show</span> err</span>
<span id="cb4-15"><a href="#cb4-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-16"><a href="#cb4-16" aria-hidden="true" tabindex="-1"></a>    <span class="co">-- Decode the JSON.</span></span>
<span id="cb4-17"><a href="#cb4-17" aria-hidden="true" tabindex="-1"></a>    <span class="kw">case</span> eitherDecode output <span class="kw">of</span></span>
<span id="cb4-18"><a href="#cb4-18" aria-hidden="true" tabindex="-1"></a>        <span class="dt">Left</span> e <span class="ot">-&gt;</span> throwError <span class="op">$</span> <span class="st">&quot;Error decoding JSON: &quot;</span> <span class="op">&lt;&gt;</span> e</span>
<span id="cb4-19"><a href="#cb4-19" aria-hidden="true" tabindex="-1"></a>        <span class="dt">Right</span> v <span class="ot">-&gt;</span> <span class="fu">return</span> v</span></code></pre></div>
<p>It’s important to note that the <em>call site</em> is responsible for fixing the type
<code>a</code> of value to be parsed from the JSON. This means that <code>shellOutJSON</code> will
happily attempt to parse the JSON into <em>any</em> type you ask it to (so long as it
has a <code>FromJSON</code> instance), whether or not you should expect the command to
produce such JSON. The obvious potential problem – a caller asking for data in
the wrong format – occurred twice in a dozen lines of code in my current
project.</p>
<h2 id="json-for-the-upstream-system">JSON for the upstream system</h2>
<p>The second JSON encoding is the one used to communicate with the command-line
application. The main difference from the API encoding is that it represents
the active colour by adding a <code>status</code> property to each colours; exactly one of
them is <code>active</code> and the rest are <code>available</code>. Rosie the robot is looks like
this:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode json"><code class="sourceCode json"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span> <span class="dt">&quot;name&quot;</span> <span class="fu">:</span> <span class="st">&quot;Rosie the robot&quot;</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="fu">,</span> <span class="dt">&quot;colours&quot;</span> <span class="fu">:</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a>    <span class="fu">{</span> <span class="dt">&quot;red&quot;</span>   <span class="fu">:</span> <span class="fu">{</span> <span class="dt">&quot;R&quot;</span><span class="fu">:</span> <span class="dv">255</span><span class="fu">,</span> <span class="dt">&quot;G&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;B&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;status&quot;</span><span class="fu">:</span> <span class="st">&quot;active&quot;</span><span class="fu">}</span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>    <span class="fu">,</span> <span class="dt">&quot;green&quot;</span> <span class="fu">:</span> <span class="fu">{</span> <span class="dt">&quot;R&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;G&quot;</span><span class="fu">:</span> <span class="dv">255</span><span class="fu">,</span> <span class="dt">&quot;B&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;status&quot;</span><span class="fu">:</span> <span class="st">&quot;available&quot;</span><span class="fu">}</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a>    <span class="fu">,</span> <span class="dt">&quot;blue&quot;</span>  <span class="fu">:</span> <span class="fu">{</span> <span class="dt">&quot;R&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;G&quot;</span><span class="fu">:</span>   <span class="dv">0</span><span class="fu">,</span> <span class="dt">&quot;B&quot;</span><span class="fu">:</span> <span class="dv">255</span><span class="fu">,</span> <span class="dt">&quot;status&quot;</span><span class="fu">:</span> <span class="st">&quot;available&quot;</span><span class="fu">}</span></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a>    <span class="fu">}</span></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a><span class="fu">}</span></span></code></pre></div>
<p>This is structure is great if you are using the data to output a nice table for
a human to read but not so great in an API.</p>
<p>This additional format could be implemented with new data types to represent
robots and colours and a few conversion functions (probably using the excellent
<a href="https://hackage.haskell.org/package/lens">lens</a> package) to represent the weirdly formatted versions of our types. Or
I could keep the same data types but create a <code>newtype</code> wrapper around each of
them with new <code>FromJSON</code> instances implementing the new format.</p>
<p>Instead I’ll add a “wrapper” type with which to distinguish a normal <code>Robot</code>
from one which should be formatted for the upstream system.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Upstream</span> a <span class="ot">=</span> <span class="dt">Upstream</span> {<span class="ot"> unwrapUpstream ::</span> a }</span></code></pre></div>
<p>This new type doesn’t “do” anything, it just tags the value it wraps and lets
me distinguish a <code>Robot</code> from an <code>Upstream Robot</code> which should be formatted for
the API and the upstream system respectively. (This is not strictly true: it
does take up memory and does cost an additional pointer dereference to
traverse). With the new <code>Upstream</code> type I can write a second <code>FromJSON</code>
instance each of my types.</p>
<p>If there is no special upstream format for a type the new instance can just
call the existing instance and stuff the result in an <code>Upstream</code> wrapper:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">FromJSON</span> (<span class="dt">Upstream</span> [<span class="dt">Colour</span>]) <span class="kw">where</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>    parseJSON j <span class="ot">=</span> <span class="dt">Upstream</span> <span class="op">&lt;$&gt;</span> parseJSON j</span></code></pre></div>
<p>When the upstream encoding and the API encoding do differ, I write a <code>FromJSON</code>
instance in exactly the same way I normally would (making sure to use the
<code>Upstream</code> version of any other <code>FromJSON</code> instances I use):</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">FromJSON</span> (<span class="dt">Upstream</span> <span class="dt">Robot</span>) <span class="kw">where</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>    parseJSON (<span class="dt">Object</span> v) <span class="ot">=</span> <span class="dt">Upstream</span> <span class="op">&lt;$&gt;</span></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a>        (<span class="dt">Robot</span></span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a>            <span class="op">&lt;$&gt;</span>  v <span class="op">.:</span> <span class="st">&quot;name&quot;</span></span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a>            <span class="op">&lt;*&gt;</span> (v <span class="op">.:</span> <span class="st">&quot;colours&quot;</span> <span class="op">&gt;&gt;=</span> activeColours <span class="op">&gt;&gt;=</span> exactlyOne)</span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a>            <span class="op">&lt;*&gt;</span> (unwrapUpstream <span class="op">&lt;$&gt;</span> v <span class="op">.:</span> <span class="st">&quot;colours&quot;</span>))</span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a>      <span class="kw">where</span></span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a>        <span class="co">-- Parse a JSON object of colours into a list of 'ColourName's which</span></span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a>        <span class="co">-- have @status == &quot;active&quot;.</span></span>
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a><span class="ot">        activeColours ::</span> <span class="dt">Value</span> <span class="ot">-&gt;</span> <span class="dt">Parser</span> [<span class="dt">ColourName</span>]</span>
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a>        activeColours (<span class="dt">Object</span> o) <span class="ot">=</span> (<span class="fu">fmap</span> <span class="fu">fst</span> <span class="op">.</span> <span class="fu">filter</span> <span class="fu">snd</span>) <span class="op">&lt;$&gt;</span></span>
<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a>            <span class="fu">mapM</span> (<span class="fu">uncurry</span> colour) (HM.toList o)</span>
<span id="cb8-13"><a href="#cb8-13" aria-hidden="true" tabindex="-1"></a>        activeColours _ <span class="ot">=</span> <span class="fu">fail</span> <span class="st">&quot;Colours must be a JSON object.&quot;</span></span>
<span id="cb8-14"><a href="#cb8-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-15"><a href="#cb8-15" aria-hidden="true" tabindex="-1"></a>        <span class="co">-- Given a name and a JSON value, parse a pair containing the name and</span></span>
<span id="cb8-16"><a href="#cb8-16" aria-hidden="true" tabindex="-1"></a>        <span class="co">-- whether the colour has @status == &quot;active&quot;@.</span></span>
<span id="cb8-17"><a href="#cb8-17" aria-hidden="true" tabindex="-1"></a><span class="ot">        colour ::</span> <span class="dt">Text</span> <span class="ot">-&gt;</span> <span class="dt">Value</span> <span class="ot">-&gt;</span> <span class="dt">Parser</span> (<span class="dt">ColourName</span>, <span class="dt">Bool</span>)</span>
<span id="cb8-18"><a href="#cb8-18" aria-hidden="true" tabindex="-1"></a>        colour name (<span class="dt">Object</span> o) <span class="ot">=</span> (,)</span>
<span id="cb8-19"><a href="#cb8-19" aria-hidden="true" tabindex="-1"></a>            <span class="op">&lt;$&gt;</span> parseJSON (<span class="dt">String</span> name)</span>
<span id="cb8-20"><a href="#cb8-20" aria-hidden="true" tabindex="-1"></a>            <span class="op">&lt;*&gt;</span> ((<span class="dt">String</span> <span class="st">&quot;active&quot;</span> <span class="op">==</span>) <span class="op">&lt;$&gt;</span> (o <span class="op">.:</span> <span class="st">&quot;status&quot;</span>))</span>
<span id="cb8-21"><a href="#cb8-21" aria-hidden="true" tabindex="-1"></a>        colour _ _ <span class="ot">=</span> <span class="fu">fail</span> <span class="st">&quot;Colour must be a JSON object.&quot;</span></span>
<span id="cb8-22"><a href="#cb8-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-23"><a href="#cb8-23" aria-hidden="true" tabindex="-1"></a>    parseJSON _ <span class="ot">=</span> <span class="fu">fail</span> <span class="st">&quot;Robot must be a JSON object&quot;</span></span>
<span id="cb8-24"><a href="#cb8-24" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-25"><a href="#cb8-25" aria-hidden="true" tabindex="-1"></a><span class="co">-- | Parser to check that a list contains exactly one value.</span></span>
<span id="cb8-26"><a href="#cb8-26" aria-hidden="true" tabindex="-1"></a><span class="ot">exactlyOne ::</span> [a] <span class="ot">-&gt;</span> <span class="dt">Parser</span> a</span>
<span id="cb8-27"><a href="#cb8-27" aria-hidden="true" tabindex="-1"></a>exactlyOne [] <span class="ot">=</span> <span class="fu">fail</span> <span class="st">&quot;Missing value&quot;</span></span>
<span id="cb8-28"><a href="#cb8-28" aria-hidden="true" tabindex="-1"></a>exactlyOne [a] <span class="ot">=</span> <span class="fu">pure</span> a</span>
<span id="cb8-29"><a href="#cb8-29" aria-hidden="true" tabindex="-1"></a>exactlyOne _ <span class="ot">=</span> <span class="fu">fail</span> <span class="st">&quot;More than one value&quot;</span></span></code></pre></div>
<p>With all these instances written I can update <code>shellOutJSON</code> to use the
<code>Upstream</code> instances when it interacts with the command-line program. Two small
changes – adding <code>Upstream</code> to the <code>FromJSON</code> constraint and the “success”
pattern match – are enough to ensure that <em>all</em> communication with the
upstream system uses the <code>Upstream</code> JSON encoding:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>shellOutJSON</span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="ot">    ::</span> (<span class="dt">MonadError</span> <span class="dt">String</span> m, <span class="dt">MonadIO</span> m, <span class="dt">FromJSON</span> (<span class="dt">Upstream</span> a))</span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a>    <span class="ot">=&gt;</span> [<span class="dt">String</span>]</span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a>    <span class="ot">-&gt;</span> m a</span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a>shellOutJSON cmd <span class="ot">=</span> <span class="kw">do</span></span>
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a>    <span class="co">-- Execute the command.</span></span>
<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a>    (exit_code, out, _err) <span class="ot">&lt;-</span> liftIO <span class="op">$</span> readProcessWithExitCode cmd [] <span class="st">&quot;&quot;</span></span>
<span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a>    <span class="co">-- Check it succeeded.</span></span>
<span id="cb9-10"><a href="#cb9-10" aria-hidden="true" tabindex="-1"></a>    output <span class="ot">&lt;-</span> <span class="kw">case</span> exit_code <span class="kw">of</span></span>
<span id="cb9-11"><a href="#cb9-11" aria-hidden="true" tabindex="-1"></a>        <span class="dt">ExitSuccess</span> <span class="ot">-&gt;</span> <span class="fu">return</span> <span class="op">$</span> BS.pack out</span>
<span id="cb9-12"><a href="#cb9-12" aria-hidden="true" tabindex="-1"></a>        <span class="dt">ExitFailure</span> err <span class="ot">-&gt;</span> throwError <span class="op">$</span></span>
<span id="cb9-13"><a href="#cb9-13" aria-hidden="true" tabindex="-1"></a>            <span class="st">&quot;Could not execute command: errno = &quot;</span> <span class="op">&lt;&gt;</span> <span class="fu">show</span> err</span>
<span id="cb9-14"><a href="#cb9-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-15"><a href="#cb9-15" aria-hidden="true" tabindex="-1"></a>    <span class="co">-- Decode the JSON.</span></span>
<span id="cb9-16"><a href="#cb9-16" aria-hidden="true" tabindex="-1"></a>    <span class="kw">case</span> eitherDecode output <span class="kw">of</span></span>
<span id="cb9-17"><a href="#cb9-17" aria-hidden="true" tabindex="-1"></a>        <span class="dt">Left</span> e <span class="ot">-&gt;</span> throwError <span class="op">$</span> </span>
<span id="cb9-18"><a href="#cb9-18" aria-hidden="true" tabindex="-1"></a>            <span class="st">&quot;Error decoding JSON: &quot;</span> <span class="op">&lt;&gt;</span> e</span>
<span id="cb9-19"><a href="#cb9-19" aria-hidden="true" tabindex="-1"></a>        <span class="dt">Right</span> (<span class="dt">Upstream</span> v) <span class="ot">-&gt;</span> <span class="fu">return</span> v</span></code></pre></div>
<p>Now any call to <code>shellOutJSON</code> will automatically parse using the correct JSON
encoding and any existing code using <code>shellOutJSON</code> doesn’t have to change.
Even better, any call which needs a type without an <code>Upstream</code> instance of
<code>FromJSON</code> will result in a type error at run time:</p>
<pre><code>lib/Server.hs:115:5:
    Could not deduce (FromJSON (Upstream Colour))
      arising from a use of ‘shellOutJSON’
    from the context (MonadError String m, MonadIO m)
      bound by the type signature for
                 getColour :: (MonadError String m, MonadIO m) =&gt;
                                      ColourName -&gt; m Colour
      at lib/Server.hs:(109,8)-(112,18)
    In a stmt of a 'do' block: shellOutJSON cmd [&quot;colour&quot;, &quot;list&quot;, colour_name]
    In the expression:
      shellOutJSON cmd [&quot;colour&quot;, &quot;list&quot;, color_name]
    In an equation for ‘getColour’:
        getColour name
          = do { let colour_name = T.unpack $ unColourName name
                 shellOutJSON cmd [&quot;colour&quot;, ....] }</code></pre>
<p>The second line of the error tells you exactly what’s missing: the compiler
can’t find a <code>FromJSON</code> instance for <code>Upstream Colour</code>.</p>
<h2 id="conclusion">Conclusion</h2>
<p>By using a “wrapper” type like <code>Upstream a</code> I reduced the amount of code I need
to write and maintain (in particular, there’s no converting back and forth
between <code>Colour</code> and <code>WeirdlyFormattedColour</code> data types). The values of my
various types are clearly still related and <code>Upstream</code> is completely agnostic
to the type being wrapped – an <code>Upstream Robot</code> is just a <code>Robot</code> inside an
<code>Upstream</code> and neither the <code>Robot</code> not the <code>Upstream</code> cares about the other
part at all.</p>
<p>Making the wrapper parametric like this (as opposed to, for example, creating
a different <code>newtype</code> wrapper around each of the particular types) makes i
possible to write code which – like the modified <code>shellOutJSON</code> – doesn’t
care about the <em>what</em> is being wrapped, just that it <em>is</em> wrapped.</p>
<p>Adding and removing the <code>Upstream</code> wrapper at the system boundary minimises the
amount code which can incorrectly use the wrong representation and, in
particular, makes it impossible for these bugs to happen in the many places
I use <code>shellOutJSON</code>. This forces me to define wrapped <code>FromJSON</code> instances for
<em>all</em> the types, even the ones that use the same JSON representation, but this
is a price I’m willing to pay for an interface that makes a class of errors
impossible.</p>
<p>Using this approach in my current project made the code shorter, simpler (in
terms of number of data types and functions defined), fixed two “wrong format”
bugs, and made it impossible to reintroduce them.</p>]]></summary>
</entry>
<entry>
    <title>Haskell at Work</title>
    <link href="https://passingcuriosity.com/2015/haskell-at-work/" />
    <id>https://passingcuriosity.com/2015/haskell-at-work/</id>
    <published>2015-01-28T00:00:00Z</published>
    <updated>2015-01-28T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>I recently (nearly 7 months ago) started work at <a href="https://www.anchor.net.au/">Anchor Hosting</a> as
a software developer in the engineering department. Anchor is a hosting
company, and its engineering group works on a variety of different things like
time-series data storage and analysis, APIs and automation, business
applications, and development tools.</p>
<p>I’ll describe some of our development tooling in this post and leave discussion
of the “real” systems we work on for a later post.</p>
<h3 id="building-and-deploying">Building and deploying</h3>
<p>Given we’re a Haskell shop, we’ve needed to figure out how to build, deploy,
and manage systems written in Haskell. I’m still not sold on the whole
containerisation mania which seems to be sweeping certain parts of the IT world
(and certainly not for general purpose multi-tenancy). Nevertheless, we’re
finding <a href="https://www.docker.com/">Docker</a> quite useful.</p>
<p>We have a Docker image containing GHC and cabal configured to use the
<a href="http://www.stackage.org/">Stackage</a> package set, with a bunch of frequently used Haskell
packages already installed. This image is built automatically using a <a href="https://github.com/anchor/docker-haskell" title="Build a Docker image with GHC and Stackage">small
set of scripts</a>.</p>
<p>Most of the systems we build operate as services (HTTP servers, agents on
message queue, etc.) and we “package” them as Docker images too. We have
<a href="https://github.com/anchor/docker-build" title="Build a Haskell package and make a Docker image of it.">another set of scripts</a> which use the Haskell image to build
a cabal package, extract the artefacts, and stuff them into a new Docker image.
This approach results in an image which is significantly smaller than it
otherwise would be.</p>
<p>Both sets of scripts can be used manually but they are also used in
<a href="http://jenkins-ci.org/">Jenkins</a> jobs. Like everything mentioned so far Jenkins and its
builders all run in Docker too, so we also have some scripts to build <a href="https://github.com/anchor/docker-jenkins" title="Build Docker images for Jenkins servers and builders.">Jenkins
Docker images</a>.</p>
<p>All of these Docker images are run on <a href="https://coreos.com/">CoreOS</a> servers hosted on
<a href="http://www.anchor.com.au/opencloud/" title="Ultra-high performance, public OpenStack cloud.">Anchor OpenCloud</a>, Anchor’s new <a href="http://www.openstack.org/">OpenStack</a> deployment. Generally, we
run each service as a Docker container managed by a <code>systemd</code> unit with its
configuration and data files (such as they are) mounted in from the host file
system. Each <code>systemd</code> unit deletes any old container and pulls the latest
image before starting the service, so upgrading an instance is easy: just
restart the <code>systemd</code> unit.</p>
<h3 id="development-tools">Development tools</h3>
<p>Various members of the team all use different operating systems (various
flavours of BSD, Linux, and Mac OS X), editors (vim, emacs, Sublime Text) and
have different opinions and habits about coding style, etc. To help manage
improve the consistency and, hopefully, quality of our code, we developed
<a href="https://github.com/anchor/git-vogue/">git-vogue</a>. <code>git-vogue</code> runs as a pre-commit hook (currently for <code>git</code>,
but it can be extended) and runs a range of checks over the modified or,
optionally, all files in the repository. This isn’t perfect but has helped
improve our code quite considerably.</p>]]></summary>
</entry>
<entry>
    <title>FP-Syd, October 2013</title>
    <link href="https://passingcuriosity.com/2013/fp-syd-october/" />
    <id>https://passingcuriosity.com/2013/fp-syd-october/</id>
    <published>2013-10-16T00:00:00Z</published>
    <updated>2013-10-16T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>Little mention of <a href="http://linux.conf.au/">linux.conf.au 2014</a> and how we should all take a
look at the available programme and see if we want to go.</p>
<h2 id="eriks-icfp-roundup">Erik’s ICFP roundup</h2>
<p>The <a href="http://www.icfpconference.org">International Conference on Functional Programming</a> (ICFP for short) is
a three-day core conference and is collocated with a number of related events.
<a href="http://www.icfpconference.org/icfp2013/">ICFP 2013</a> was in Boston and a number of FP-Syd regulars presented and/or
attended.</p>
<p>Erik was a long time LCA attendee but ICFP has supplanted it as his “must go”
conference. I hope to make the same switch in <a href="http://www.icfpconference.org/icfp2014/">2014</a>!</p>
<p>The <strong>Haskell Implementers Workshop</strong> covers the internals of Haskell
implementations which, these days, means GHC to a very large extent. Covers a
lot of interesting techniques, with a particular focus on compilers. Erik
mentioned work on the non-safety of generalised <code>newtype</code> deriving; using
Hermit (a dynamic/guided optimisation framework) to optimise
scrap-your-boilerplate code; and Habit (a strict Haskell dialect for OS
programming).</p>
<p>The <strong>Commercial Users of Functional Programming</strong> was, reportedly, a bit
boring, but I’ve liked the few <a href="http://www.youtube.com/channel/UCfSUv7I_aHgzcnXMcd8obsw">CUFP 2013 YouTube videos</a> I’ve watched so
far. YMMV.</p>
<p>The <strong>Haskell Symposium</strong> was a main draw (for Erik). Highlights which Erik
found worth mentioning and I found worth noting down include:</p>
<ul>
<li><p>Oleg asked difficult questions of a lot of speakers. I wonder what would
happen if he asked an easy one?</p></li>
<li><p>Effects seemed something of a hot topic.</p></li>
<li><p>Demonstrations of <a href="http://hackage.haskell.org/package/liquidhaskell">Liquid Haskell</a> (which sounds pretty great), and a
Javascript backend for GHC.</p></li>
</ul>
<ul>
<li><p>Intel are developing a research compiler which uses GHC’s front-end to
compile to Core and then uses their own backend. Does loop vectorisation,
currently only better performance on a few benchmarks.</p></li>
<li><p>The third iteration of the I/O manager for GHC. Multithreaded, influenced by
Kazu Yamamoto’s work on Warp and mighttpd. Benchmarks against Nginx seem very
good; Warp with multiple cores sees extremely good speedups (contra Nginx).</p></li>
</ul>
<p>The main event – <strong>ICFP</strong> – is an academic conference and a lot of the
content will fly straight over the head of many a “working programmer”. Some of
the highlights included:</p>
<ul>
<li><p>A few talks on vectorisation (w/ SIMD from Intel, stream fusion, etc.) and
optimisation (for GPUs, etc.)</p></li>
<li><p>A few talks on dependent types.</p></li>
<li><p>Tactics in Coq are untyped; one talk discussed an approach to typed tactic
programming in Coq. Sounds especially interesting now that there is a “Coq
fight” in the FP-Syd calendar for next year!</p></li>
<li><p>People who didn’t attend are encourage to watch the video of the “fun with
semi-rings” talk. I haven’t been able to find it, though.</p></li>
<li><p>One talk described a useful-sounding approach to parsing context free
grammars with a divide-and-conquer approach, allowing partial and parallel
parsing.</p></li>
<li><p>Simon Peyton-Jones discussed the new curriculum for secondary computer
science education in the United Kingdom.</p></li>
<li><p>An extension or two to System F: System Fc (explicitly kind equality) and
System Fi (type indices). Everyone who can understand System F shouldn’t have
a problem reading the System Fi paper.)</p></li>
<li><p>Constrained monad problem (which, apparently, Oleg said was crap?). Paper on
solving a problem which occurs when using <code>Monad</code> but they should have used
<code>Applicative</code>. Seems as though they mostly wanted the <code>do</code> syntactic sugar;
see also idiom brackets and the attempt to generalise the Monad sugar.</p></li>
<li><p>“Querying ordered graphs.” Three words which sound interesting, but I’ve no
idea why I wrote them down.</p></li>
<li><p>Also: experience reports! Someone took a Scheme compiler from 4-5 to 25
passes (“nanoparsing”?) and, at the same time, also added a good colouring
register allocator. Apparently one of these changes made it better.</p></li>
</ul>
<p>Other events:</p>
<ul>
<li><p>Talking about a benchmark/framework to compare approaches to generic
programming at the Workshop on Generic Programming.</p></li>
<li><p>Brent Yorgey doing animations with <code>diagrams</code> at the Workshop on Functional
Art, Music, Modeling and Design.</p></li>
<li><p>Chordify is a system (written in Haskell) to analyse recordings and generate
chord transcripts. It’s not perfect but gives pretty good approximations.</p></li>
</ul>
<h2 id="ben-talking-about-data-flow-fusion">Ben talking about Data Flow Fusion</h2>
<p><a href="http://www.cse.unsw.edu.au/~benl/">Ben Lippmeier</a> – an FP-Syd regular – presented a paper at ICFP and
reprised that presentation back in Sydney for those of use who weren’t in
Boston. He described an approach using data flow to guide the compilation of
programs using stream fusion.</p>
<p>Wants to process a list of points, adding 1 to each, filtering those about 0
and also finding the maximum.</p>
<p>Doing stream fusion</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">map</span> f <span class="ot">=</span> unstream <span class="op">.</span> mapsS f <span class="op">.</span> stream</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">filter</span> f <span class="ot">=</span> unstream <span class="op">.</span> filterS f <span class="op">.</span> stream</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="co">-- RULE to remove (stream . unstream)</span></span></code></pre></div>
<p>Example computes <code>(vec3, n)</code> can’t float <code>vec3</code> because it’s being used in the
result <em>and</em> in the computation of <code>n</code>. So we get two loops.</p>
<pre><code>**1** -&gt; 2 -&gt; **3** -&gt; 4
	              |      |
              (    ,    )</code></pre>
<p><code>zipWithX</code> tends to use X+1 loop counters for stream fusion. There’re only 8
registeres to use on some platforms.</p>
<h3 id="data-flow-fusion">Data Flow Fusion</h3>
<h4 id="slight-manual-refactor">Slight manual refactor</h4>
<p>Split <code>filter</code> into two combinators <code>flag</code> – which contains <code>True</code> or <code>False</code>
for each member – and <code>pack</code> – which does the filtering.</p>
<h4 id="extract-the-data-flow-graph">Extract the data flow graph</h4>
<p>This code generates the data flow graph.</p>
<pre><code>fun vec1 (\s1 -&gt; 
  let s2    = map (+ 1) s1
      flags = map (&gt; 0) s2
  in mkSel flags (\sel -&gt;
  let s3   = pack sel s2
      vec3 = create s3
      n    = fold max 0 s3
  in (vec3, n)))

vec1 :: Vector Int
s1 :: Series k1 Int
s2 :: Series k1 Int
flags :: Sel k1 k2
s3 :: Series k2 Int</code></pre>
<p>Series has a phantom type variable which helps keep track of the code which can
be fused into a single loop.</p>
<p>We learn that <code>k1 &gt;= k2</code></p>
<p>With the flow graph (annotated with operations, etc.), throw away the source.</p>
<h4 id="schedule-the-grapch-into-an-abstract-loop-nest">Schedule the grapch into an abstract loop nest</h4>
<p>Abstract loop nest:</p>
<pre><code>loop k1 {

  start: ....
  
  body: ....
  
  inner: ...
  
  end: ...

} yields ...</code></pre>
<p>Start at the front of the data flow graph and add elements of the graph to the
nested abstract loop.</p>
<p>Operations go into different places in the nested abstract loop. A <code>fold</code>, for
example, allocates and accumulator in <code>start</code>, increments somewhere within
<code>body</code> and reads it in <code>end</code>.</p>
<h4 id="extract-implementation-from-abstract-loop-nest.">Extract implementation from abstract loop nest.</h4>
<p>Translate the various bits and pieces of the abstract loop nest data structure
into different Haskell combinators.</p>
<h3 id="implementation">Implementation</h3>
<p>GHC plugin which grabs Core, does data flow compilation and generates Core to
give back to GHC.</p>
<p>Some issues in current implementation where LLVM doesn’t realise that writing
to the output doesn’t <em>need</em> to reload the start and length numbers.</p>
<blockquote>
<p><strong>If</strong> your program is first order (argument functions take scalars,
not series), non-recursive, synchronous, finite data flow program
using out combinators.</p>
<p><strong>Then</strong> by construction your program will be compiled correctly by
this system.</p>
</blockquote>
<h2 id="liam-on-cdsl">Liam on CDSL</h2>
<p>Liam O’Connor works for NICTA. Instead of talking about something he recently
learned, he’s talking about work: CDSL - a restricted functional language for
file system verification.</p>
<p>Trying to establish a formal proof of the correctness of a file system driver
in an operating system.</p>
<p>Already have an architecture for this sort of problem (from seL4):</p>
<ol type="1">
<li><p>Abstract spec - high-level, nondeterministic (followed by an “interesting”
proof of relation to ~ 15%)</p></li>
<li><p>Low level spec - purely functional (followed by a “largely boring” proof of
relation to ~ 30%)</p></li>
<li><p>C implementation - efficient.</p></li>
</ol>
<p>~ 55% is showing that the other proofs don’t do something stupid; proving
invariants all hold.</p>
<p>Ignoring the kernel proper, architecture support, and drivers (another NICTA
project), the largest part of the Linux kernel is the <code>fs/</code> directory; 31
different file systems were supported by the kernel running on some random
NICTA server.</p>
<p>There are lots of file systems with, one assumes, quite a lot of common
functionality and infrastructure. The goal of the project is not to make a
cathedral of a single verified file system, more a factory for churning out
numerous file systems. The approach is to use a DSL to generate the low-level
spec, proof and implementation. High-level spec and proof are done by hand, so
generated outputs need to be readable.</p>
<p>Should</p>
<ul>
<li><p>establish key verification properties</p></li>
<li><p>compete with efficient C code (imperative, destructive updates, etc.)</p></li>
<li><p>be expressive enough to write a file system</p></li>
</ul>
<p>But:</p>
<ul>
<li>doesn’t need to express <em>everything</em> in a file system. Hand-written components
could be plugged in to the DSL (and, hopefully, re-used).</li>
</ul>
<h3 id="simply-typed-lambda-calculus">Simply-typed lambda calculus</h3>
<p>Simple-typed lambda calculus is strongly normalising (you can’t write general
recursion, e.g. the Y combinator).</p>
<p>First-order language: lambdas go away, use <code>let</code> binding and restrict to
defining top-level functions. Added structural rules for mixing, weakening, ?</p>
<p>Need to do memory management which is safe, expressive (no pass by value, we
need the heap), no GC (you’d have to verify it, introduce latency, etc.)</p>
<p>Automatic member management (GC) is too big a burden. Many static automatic
memory management is inefficient or unsafe.</p>
<p>What about manual memory management?</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> x <span class="ot">=</span> allocateData ()</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>    x' <span class="ot">=</span> updateData x</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a>    _ <span class="ot">=</span> free x</span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a><span class="kw">in</span> x'</span></code></pre></div>
<p>But this is terrible! Unsafe, inefficient, etc.</p>
<p>So have a linear type system, throwing away weakening, etc. Forces use of
things exactly matching (can’t alloc and not use, doesn’t discharge the new
fact). The typing rules require that introduction and elemination be paired.</p>
<p>Linear types means that the elimination operations (e.g. <code>updateDate</code>) are the
<em>last</em> to access terms, so they can do destructive updates.</p>
<p>Two interpresations of these semantics:</p>
<ul>
<li><p>value semantics: pass by value, no heap, immutability, reasoning.</p></li>
<li><p>update semantics: heap, updates, deallocates, implementation.</p></li>
</ul>
<p>Linear types allow for both.</p>
<p>But sometimes you want non-linear, pass-by-value (arithmetic operations, etc.):</p>
<ul>
<li>Unboxed types, ints, small structs</li>
<li>Functions themselves</li>
</ul>
<p>Allow structural rules (dereliction and contraction) for certain types only. So
now we have <code>T_{.}</code> and <code>T_{#}</code> (unboxed and value types).</p>
<h3 id="buffer-interface">Buffer interface</h3>
<pre><code>make : () -&gt; .Buf
free : .Buf -&gt; ()
length : .Buf -&gt; (#U32, .Buf)

serialise : (.Obj, .Buf) -&gt; (.Obj, .Buf)
deserialise : .Buf -&gt; (.Obj, .Buf)</code></pre>
<p>Non-linear “look but don’t touch” references with <code>*</code>:</p>
<pre><code>make : () -&gt; .Buf
free : .Buf -&gt; ()

length : *Buf -&gt; #U32
serialise : (*Obj, .Buf) -&gt; .Buf
deserialise : *Buf -&gt; .Obj</code></pre>
<p>Use <code>let!</code> construct which is like <code>let</code> but we mark specific variables as
read-only within the <code>let</code> clauses and back to linear in the <code>in</code>.</p>
<p>But this is unsafe (read-only can escape the let). Could use regions, but
choose not to unless it’s required.</p>
<p>Linear typing breaks some control flow:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> x <span class="ot">=</span> alloc ()</span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a><span class="kw">in</span> <span class="kw">if</span> cond</span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a>   <span class="kw">then</span> update(x)</span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a>   <span class="kw">else</span> x</span></code></pre></div>
<h3 id="loops">Loops</h3>
<p>Hardest, most annoying part of the formalisation of the language.</p>
<p>Built-in loop combinators, map, fold, with, for.</p>
<pre><code>let sum = for (x,y) in fold(arr) with 0
              do (x + y)

let arr', sum = for (x,y) in map(arr) with 0
                    do (x * 2, x + y)</code></pre>
<p>Alas, this is unsafe. Double free, etc. But you can restrict linear types in
the loop expression. Then have to make any required linear types into
accumulator parms.</p>
<h3 id="error-handling">Error handling</h3>
<p>The return-code convention using in languages like C is pretty bad. Instead,
separate statements and expressions.</p>
<p>Statements have three types:</p>
<ul>
<li>s : <span class="math inline">\({\bar T_{s}}\)</span></li>
<li>s : <span class="math inline">\(fails {\bar T_{f}}\)</span></li>
<li>s : <span class="math inline">\({\bar T_{?}} fails {\bar T_{?}}\)</span></li>
</ul>
<p>Type of <code>if then else</code> is <code>T_{t} \leastupperbound T_{e}</code>. Lattice join,
subtype, etc.</p>
<p>Make <code>let</code> and <code>let!</code> only handle success cases. Force sub-expressions to
handle potential errors. Type system <em>forces</em> you to handle your errors and the
<em>linear</em> type system forces you to free your resources.</p>
<h3 id="types">Types</h3>
<p>Product and sum types (implemented as structs and tagged unions).</p>
<p>Accessing members of linear records is problematic as you use the record
multiple times:</p>
<pre><code>let sum = operation(x.field1, x.field2)</code></pre>
<p>Instead use an open/close structure.</p>]]></summary>
</entry>
<entry>
    <title>FP-Syd, August 2013</title>
    <link href="https://passingcuriosity.com/2013/fp-syd-august/" />
    <id>https://passingcuriosity.com/2013/fp-syd-august/</id>
    <published>2013-08-28T00:00:00Z</published>
    <updated>2013-08-28T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>Here are some nodes from the August 2013 meeting of the <a href="http://fp-syd.ouroborus.net/">FP-Syd</a>
functional programming group.</p>
<h2 id="julian-gamble-on-simulation-testing-in-datomic">Julian Gamble on Simulation Testing in Datomic</h2>
<p><a href="http://juliangamble.com">Julian Gamble</a> ([<span class="citation" data-cites="juliansgamble">@juliansgamble</span>][] on Twitter) gave
his first FP-Syd talk with an introduction to simulation testing using [Datomic][].</p>
<p><span class="citation" data-cites="juliansgamble">[@juliansgamble]</span>: http://twitter.com/juliansgamble
[Datomic]: http://www.datomic.com</p>
<blockquote>
<p>Plug: He’s writing a book called <a href="http://clojurerecipes.net/">Clojure Recipes</a> which is due out in January 2014.</p>
</blockquote>
<p><a href="https://github.com/Datomic/simulant">Simulant</a> – the subject of the talk – is a framework for Datomic database.
It’s for <em>simulation testing</em>.</p>
<p>Many types of testing (in something resembling order of popularity):</p>
<ul>
<li>Unit testing</li>
<li>User acceptance testing</li>
<li>Performance testing</li>
<li>Simulation testing</li>
</ul>
<p>Simulation testing uses modeling and simulation to “test” systems which are too
complex for linear models like unit testing. Generations of simulations:</p>
<ul>
<li><p>High school solving maths problems</p></li>
<li><p>Stock analysts modelling and analysing companies</p></li>
<li><p>Analytics driven audits simulating systems for comparison.</p></li>
<li><p>Business scenarios predicting responses to, e.g., market crashes.</p></li>
</ul>
<p>Most of these can be done on a piece of paper or on a single machine, but
systems which aren’t amenable to such approaches are becoming more common.</p>
<p><a href="http://www.amazon.com/Purely-Functional-Structures-Chris-Okasaki/dp/0521663504">Chris Okasaki’s book Purely Functional Data Structures</a> popularised the
use of purely functional approaches to data structures through sharing.</p>
<p>Datomic is “a database as a value”. Or, put another way, a database as a
persistent data structure. This makes state management easier for, e.g.,
reproducing problems for bug fixing.</p>
<p>Built on pluggable storage system. Uses a Java-native store locally, can use
Amazon Dynamo DB. Writing is done through a single transactor process with
querying done directly from the data store.</p>
<p>Simulant is a framework which uses Datomic to help to distribute and scale
simulation testing. Assumes that you’ll be modelling <em>agents</em> and <em>actions</em> –
which are stored in the Simulant schema – and additonal model details stored
in your own schema. Uses git too, to keep track of version of the simulation
changing over time.</p>
<h3 id="process">Process</h3>
<ol type="1">
<li><p>Develop a Datomic schema for your model. This will be used to record the
generic details of the simulation – the actions performed by the agents – and
the domain specific details.</p></li>
<li><p>Set the model parameters (stocks/prices, etc. or ants/food/world size)</p></li>
<li><p>Make statistical assertions about the system. These will be verified against
the data recorded during the simulation.</p></li>
</ol>
<p>There are more details to this, but they flew past and I couldn’t get them down.</p>
<h3 id="why-datomic">Why Datomic?</h3>
<p>Being persistent (in the “persistent data structures” sense), Datomic makes it
far easier to review old data from older simulations, add additional
statistical assertions, etc. without having to jump through the many and varied
hoops you’d need for, e.g., a relational database.</p>
<p>I’m not sure how true a comparison this is, given that Datomic forces all
writes to the database through the single transactor. A similar architecture
with a relational database could quite easily use a single transactor to
enforce timestamp consistency on data being recorded. I must be missing
something.</p>
<h3 id="applicability">Applicability</h3>
<ul>
<li><p>Non-trivial system with multiple agents.</p></li>
<li><p>Datomic’s database as value, thing.</p></li>
<li><p>Where you have statistical assertions to be evaluated.</p></li>
</ul>
<h2 id="shane-stephens-on-web-animations">Shane Stephens on Web Animations</h2>
<p>Works on the <a href="http://w3.org/TR/web-animations/">web animations specification</a> for the W3C. Unifies SVG and
CSS animations on the web.</p>
<p>The web animations specification defines a Javascript API which looks something
like this:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">new</span> <span class="fu">Animation</span>(</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>	<span class="bu">document</span><span class="op">.</span><span class="fu">getElementById</span>(<span class="st">'hello'</span>)<span class="op">,</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>	[ {<span class="st">&quot;left&quot;</span> <span class="op">:</span> <span class="st">&quot;200px&quot;</span>}<span class="op">,</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>	  {<span class="st">&quot;left&quot;</span> <span class="op">:</span> <span class="st">&quot;400px&quot;</span><span class="op">,</span> <span class="st">&quot;height&quot;</span> <span class="op">:</span> <span class="st">&quot;100px&quot;</span>}]<span class="op">,</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>	<span class="dv">1</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a>)<span class="op">;</span></span></code></pre></div>
<p>This talk isn’t about “generating a functional API for web animations” but he
thought it was two weeks ago. He tried to generate bindings, but failed.
Instead, it’s a discussion about the attempt and the result.</p>
<p>I think there might be animations of yak shaving involved.</p>
<h3 id="haskell-to-js-compilers">Haskell to JS compilers</h3>
<p>There are quite a few functional languages which target Javascript and they
all, in Shane’s opinion, hate the web.</p>
<h4 id="utrecht-haskell-compiler-javascript-backend">Utrecht Haskell Compiler JavaScript backend</h4>
<p>The <a href="https://github.com/UU-ComputerScience/uhc">UHC</a> Javascript backend has little documentation, claims to “compile
most of Hackage” and provides an FFI to interact with “native” Javascript code.</p>
<p>The barrier between Haskell and Javascript is the problem. Everything on the
web “platform” is exposed with APIs in Javascript. Having a UHC-JS generate a
blob of HTML and CSS and Javascript stuff is pretty hard to compose with other
web-ish things.</p>
<p>There’s a big impedance mismatch between Haskell and Javascript.</p>
<h4 id="elm">Elm</h4>
<p><a href="http://elm-lang.org/">Elm</a> is a functional reactive programming language which compiles to
Javascript. Lots of documentation, an online editor, and it already has
animations.</p>
<p>But Elm is another “replace the world” abstraction.</p>
<h4 id="roy">Roy</h4>
<p><a href="http://roy.brianmckenna.org/">Roy</a> has a much saner approach, largely just syntactice sugar around
Javascript:</p>
<ul>
<li>Javascript functions are available</li>
<li>Roy types are almost Javascript “types”</li>
</ul>
<p>But no ADTs, etc. Because JS is pretty shitty with no recursion, etc.</p>
<h4 id="krazy">krazy</h4>
<p>So with no “good” existing languages he started his own language called krazy.</p>
<ul>
<li><p>The current implementation is a PEG parser and interpreter in Javascript.</p></li>
<li><p>Functional types are Javascript types (lists, for example, really are Javascript arrays).</p></li>
<li><p>Supports ADTs, HOFs, pattern matching, etc.</p></li>
<li><p>JS interop “constrained” by type assertions.</p></li>
<li><p>Will probably add record with optional, structural typing.</p></li>
</ul>
<h3 id="animations">Animations</h3>
<p>Back to the web animations API.</p>
<p>The web animations specification has side-effect free constructors for
animations, effects, timing groups, etc.</p>
<p>This could be exposed to library authors and used as an interface or to
generate an interface automatically? I’m not sure.</p>
<h2 id="thomas-sewellts-on-learnings-about-sat">[Thomas Sewell][ts] on learnings about SAT</h2>
<p><a href="http://ssrg.nicta.com.au/people/?cn=Thomas+Sewell">Thomas Sewell</a></p>
<blockquote>
<p>Survey: who can name an NP-complete problem?</p>
</blockquote>
<p>NP-complete problems can be solved by a non-deterministic machine but the
solutions can be checked by a deterministic machine. In essence, they are very
hard to solve but easy to check.</p>
<p>Circuit satisfiability can be encoded in SAT.</p>
<p>The SAT problem attempts to assign values to logical variables in a formula in
conjunctive normal form and produces either a set of assignments (if the
formula is satisfiable) or “no” (if there is no assignment).</p>
<p>The DPLL algorithm is pretty naive and does lots of backtracking.</p>
<p>The CDCL algorithm – discovered in the 90s – increased the size of viable
problems to millions of variables. Instead of having to “re-learn” the same
pieces of information repeatedly when backtracking, the Conflict Driven Clause
Learning algorithm tracks the “cause” of a clause you learn and, when a
contradiction is derived, it learns the inverse of it’s parent.</p>
<p>E.g.</p>
<blockquote>
<p>If we reach contratiction, and the parents are <span class="math inline">\(x_{1}\)</span>, <span class="math inline">\(\neg x_{2}\)</span>, <span class="math inline">\(x_{12}\)</span>.
Then we need to learn <span class="math inline">\(\neg x_{1} \vee x_{2} \vee \neg x_{12}\)</span> as at least one
of the assumptions are false, so the negation of their disjunction must hold.</p>
</blockquote>
<h3 id="learnings">Learnings</h3>
<ul>
<li><p>Competitions - progress</p></li>
<li><p>Fast propagation - a modern SAT solver needs a very efficient implementation
of the propagation algorithm.</p></li>
<li><p>Locality - solvers make decisions “near” previous decisions. Need a heuristic
to find “nearby” variables for choice.</p></li>
<li><p>Phases - alternate between phases focussed on SAT and un-SAT phases.</p></li>
<li><p>Pruning - prune the database of clauses periodically to speed propagation.</p></li>
<li><p>Glue - Not sure what this means?</p></li>
<li><p>Rewriting - preprocessing the problem into an equisatisfiable problem. Make
the problem “better”, works well as a first step. Useful on problems like
CPUs problems.</p></li>
</ul>
<p>Lots of problems have nice and/or useful SAT encodings.</p>
<p>NP-complete problems were, in the not too distant past, primarily useful as a
polite “no” for managers. (You can’t have your cake and eat it too.)</p>
<h3 id="sat-with-proofs">SAT with Proofs</h3>
<p>Some solvers produce a resolution proof.</p>
<p>Reverse Unit Propagation of a proof is a services of clauses that can be
learned by unit propagation only. The conflict clauses of a CDCL solver in the
order they are learned form a RUP proof.</p>
<p>DRUP adds clause deletion, to speed up unit propagation.</p>
<p>Having useful proofs with rewriting is complex. Checking that a SAT proof for a
rewritten problem is tricky; dealing with the rewriting (incorporating it into
the proof and validating the rewriting is often as complex as the SAT problem
itself, etc.)</p>
<h3 id="motivations">Motivations</h3>
<p>Have some SMT proofs and would love to check them in HOL4 or Isabelle/HOL.
Satisfiability Modulo Theories (SMT) incorporates SAT as part of it. HOL4 and
Isabelle/HOL are highly trusted but very slow. Using SMT/SAT to solve a problem
quickly and Isabelle/HOL to replay and verify the result should result in a
fast, trusted proof.</p>
<p>There are SAT replay tools that do this sort of thing, but they were all pretty
or extremely slow. Turns out millions of variables are hard in more traditional
tools.</p>]]></summary>
</entry>
<entry>
    <title>FP-Syd, July 2013</title>
    <link href="https://passingcuriosity.com/2013/fpsyd-july/" />
    <id>https://passingcuriosity.com/2013/fpsyd-july/</id>
    <published>2013-07-24T00:00:00Z</published>
    <updated>2013-07-24T00:00:00Z</updated>
    <summary type="html"><![CDATA[<h2 id="accelerate-with-foreign-functions">Accelerate with foreign functions</h2>
<p>More on accelerate. Looking at using it with other GPGPU frameworks. Two
distinct problems:</p>
<ol type="1">
<li><p>Calling CUDA C programs from Accelerate.</p></li>
<li><p>Calling Accelerate from CUDA C programs.</p></li>
</ol>
<h3 id="calling-cuda-c-from-accelerate">Calling CUDA C from Accelerate</h3>
<blockquote>
<p>Smooth Life is Conway’s Game of Life generalised to confinuous domains.</p>
<p>Lots of variants (magic numbers). This was smooth life “L”.</p>
<p>Gliders can move in any direction.</p>
</blockquote>
<p>Relies on FFT as part of in implementation. Write own FFT in Accelerate <em>or</em>
use cuFFT library.</p>
<p>The cuFFT library wants pointers to GPU memory as parameters to its function,
but Accelerate is high-level (no pointers into GPU memory). So added a new
operation:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="ot">foreignAcc ::</span> (<span class="dt">Arrays</span> arr, <span class="dt">Arrays</span> res, <span class="dt">Foreign</span> ff)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>           <span class="ot">=&gt;</span> ff arr res <span class="co">-- ^ The foriegn code</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>           <span class="ot">-&gt;</span> (<span class="dt">Acc</span> arr <span class="ot">-&gt;</span> <span class="dt">Acc</span> res) <span class="co">-- ^ The pure equivalent</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>           <span class="ot">-&gt;</span> <span class="dt">Acc</span> res</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>           <span class="ot">-&gt;</span> <span class="dt">Acc</span> res</span></code></pre></div>
<p>Each backend needs to provide its own instance of Foreign; subclass of Typable2
to avoid the expression problem (so that backends can use their own types for
the foreign stuff).</p>
<p>Use an “abstract” monad CIO, which is like IO but has a few new operations:
<code>allocateArray</code>, <code>devicePtrsOfArray</code>, <code>peekArray</code>, <code>pokeArray</code>. Here “abstract”
is just “private parts are private”.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ot">doFFT ::</span> <span class="dt">Acc</span> (<span class="dt">Array</span> <span class="dt">DIM2</span> <span class="dt">Complex</span>)</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>      <span class="ot">-&gt;</span> <span class="dt">Acc</span> (<span class="dt">Array</span> <span class="dt">DIM2</span> <span class="dt">Complex</span>)</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>doFFT arr <span class="ot">=</span> foreignAcc (<span class="dt">CuForeign</span> foreignFFT)</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>                       pureFFT</span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a>                       arr</span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>    <span class="kw">where</span></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>      pureFFT arr <span class="ot">=</span> <span class="op">...</span> a slow, <span class="fu">pure</span> <span class="dt">Accelerate</span> <span class="dt">FFT</span> <span class="op">...</span></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a>      foriegnFFT arr <span class="ot">=</span> <span class="op">...</span></span></code></pre></div>
<p>You can nest calls to <code>foreignAcc</code> in the pure branch to offer implementations
for multiple backends.</p>
<h3 id="calling-accelerate-programs-from-c">Calling Accelerate programs from C</h3>
<p><code>foreignAccModule</code> is a piece of Template Haskell magic which, when a module is
compiled, generates a C header file for the module.</p>
<p>Additional Template Haskell functions <code>exportAfun1</code>, <code>exportAfun2</code>, etc. are
used to export specific functions (the <code>dotp</code> function in this example).</p>
<p>Generates two C functions from <code>dotp</code>:</p>
<ul>
<li><p><code>dotp_compile(...)</code> compiles an Accelerate program (the <code>dotp</code> program).</p></li>
<li><p><code>dotp_run(...)</code> executes a compiled Accelerate program (the <code>dotp</code> program).</p></li>
</ul>
<h2 id="how-mark-writes-haskell">How Mark writes Haskell</h2>
<p>Demo of how “I” write Haskell.</p>
<blockquote>
<p>If you use vim you are bad.</p>
</blockquote>
<p>Programming has two cultures:</p>
<ol type="1">
<li><p>Tools-oriented cultures like Java, with lots of IDEs, etc.</p></li>
<li><p>Language-oriented cultures like Haskell.</p></li>
</ol>
<p>ghcmod? is a tool for Haskell; supports emacs <em>and</em> vim. Does recompilation and
test on save, integrates with Hoogle (insert module import statements).</p>
<p>Ruby tool called guard. DSL to watch file system changes and do various things
pass code through compiler for fast feedback about compiler errors. Run it in a
window beside emacs and see things happen as you save!</p>
<p>The doctest library allows you to write quickcheck properties in haddock
comments.</p>
<p>Similar: hdevtools for vim. Has a persistent server.</p>
<h2 id="ben-talking-about-ddc">Ben talking about DDC</h2>
<blockquote>
<p>Note to self: the way you’ve captured the typing rules in this section is
truly horrible. Please remember LaTeX and figure out how you want to render
it with Pandoc &amp; Hakyll.</p>
</blockquote>
<p>Pushing to make DDC be something like LLVM for functional programming
languages; a generally applicable core language.</p>
<h3 id="typing-application">Typing application</h3>
<p><span class="math display">\[\large\frac{
	\Gamma \vdash M :: t_{1} \rightarrow t_{2}
	\qquad
	\Gamma \vdash N :: t_{1}
}{
	\Gamma \vdash M N :: t_{2}
}\]</span></p>
<p>Evaluation</p>
<ol type="1">
<li><p>M reduces to a value (an abstraction).</p></li>
<li><p>N reduces to a value</p></li>
<li><p>Substitution N into M.</p></li>
<li><p>M[N/x] is new value to reduce.</p></li>
</ol>
<p>Is there something in the typing rule which represents all four of these
stages? Not really. (From me: should it be linear logic)</p>
<p>Perhaps adding effects to types.</p>
<p><span class="math display">\[\large\frac{
  \Gamma \vdash M :: t_{1} \rightarrow t_{2} ; e1
  \qquad
  \Gamma \vdash N :: t_{1} ; e2
}{
  \Gamma \vdash M N :: t_{3} ; e_{1} \vee e_{2} \vee e_{3}
}\]</span></p>
<blockquote>
<p>Where <span class="math inline">\(\vee\)</span> is a lattice join:</p>
<p><span class="math inline">\(e_{1} \vee e_{2} = e_{2} \vee e_{1}\)</span></p>
<p><span class="math inline">\(e_{1} \vee e_{1} = e_{1}\)</span></p>
</blockquote>
<p>Now there’s something in the typing rule which represents each phase:</p>
<ol type="1">
<li>Is e1</li>
<li>Is e2</li>
<li>Is e3?</li>
<li>Is the join of them?</li>
</ol>
<h3 id="monads">Monads</h3>
<blockquote>
<p>People who think Haskell is cool might have heard of this thing called a
monad.</p>
</blockquote>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">return</span><span class="ot"> ::</span> a <span class="ot">-&gt;</span> m a</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="ot">bind ::</span> m a <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> m b) <span class="ot">-&gt;</span> m b</span></code></pre></div>
<p>There’s no good way to abstract over monads: each <code>m</code> must be a single monad.
You can use monad transformers, but they suck.</p>
<p>An effect system allows you to write something like</p>
<p><span class="math display">\[\large foo :: Int \xrightarrow{State \vee IO} Int\]</span></p>
<p>instead of choosing between options like:</p>
<p><span class="math display">\[\large foo :: Int \rightarrow \text{State Int}\]</span></p>
<p><span class="math display">\[\large foo :: Int \rightarrow \text{IO Int}\]</span></p>
<p><span class="math display">\[\large foo :: Int \rightarrow \text{StateT s IO Int}\]</span></p>
<h3 id="impact-on-kinds">Impact on kinds</h3>
<p>Adding effects forces us to change the kind of the <code>-&gt;</code>. In Haskell:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="ot">(-&gt;) ::</span> <span class="op">*</span> <span class="ot">-&gt;</span> <span class="op">*</span> <span class="ot">-&gt;</span> <span class="op">*</span></span></code></pre></div>
<p>In an effectful language, every arrow has an effect component:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="ot">(-&gt;) ::</span> <span class="op">*</span> <span class="ot">-&gt;</span> <span class="op">*</span> <span class="ot">-&gt;</span> <span class="dt">Effect</span> <span class="ot">-&gt;</span> <span class="op">*</span></span></code></pre></div>
<p>Could maybe (and Ben’s PhD thesis does) add a <code>Pure</code> effect to non-effectful
arrows.</p>
<p>It also changes the nature of application (just substitution) and mixing the
“other stuff” in.</p>
<h3 id="value-only-languages">Value-only languages</h3>
<p>What if we remove substitution (can only apply values to values):</p>
<p><span class="math display">\[\large\frac{
  \Gamma \vdash v_{1} :: t_{1} \xrightarrow{e_{1}} t_{2} ; \bot
  \qquad
  \Gamma \vdash v_{2} :: t_{1} ; \bot
}{
  \Gamma \vdash v_{1} v_{2} :: t_{2} ; e_{2}
}\]</span></p>
<p>(Note, if you haven’t already, that an effect of <span class="math inline">\(\bot\)</span> is pure.)</p>
<p>So:</p>
<p><span class="math display">\[\large foo :: Int \rightarrow \text{S (State}\vee\text{IO) Int}\]</span></p>
<p>Here <code>S</code> is a suspended computation which, when invoked, will perform some
actions and return an integer.</p>
<p>Introducing a suspended computation:</p>
<p><span class="math display">\[\large\frac{
  \Gamma \vdash M :: t_{1} ; e_{1}
}{
  \Gamma \vdash suspend M :: S e_{1} t_{1} ; \bot
}\]</span></p>
<p>Running a suspended computation:</p>
<p><span class="math display">\[\large\frac{
\Gamma \vdash M :: S e_{1} t; e_{2}
}{
\Gamma \vdash run M :: t ; e_{2} \vee e_{1}
}\]</span></p>
<p>Also need:</p>
<p><span class="math display">\[\large\frac{
\Gamma, x : t_1 \vdash M :: t_2 ; \bot
}{
\Gamma \vdash (\lambda (x:t_1) . M) :: t_1 \rightarrow t_2 ; \bot
}\]</span></p>
<p><span class="math display">\[\large\frac{
\Gamma \in x:t_1
}{
\Gamma \vdash x :: t_1 ; \bot
}\]</span></p>
<p>And application
<span class="math display">\[\large\frac{
  \Gamma \vdash M :: t_1 \rightarrow t2; e_1
  \qquad
  \Gamma \vdash N :: t_1 ; e_2
}{
  \Gamma \vdash M N :: t_2 ; e_1 \vee e_2
}\]</span></p>
<p>Claim that this system is better than the original effect system which forces
us to add an effect to the arrow kind (our arrow has the original pure type)
and also the Haskell approach with monads.</p>
<p>Use <code>suspend</code> within a lambda abstraction.</p>
<pre><code>\lambda(x:T1). suspend ....</code></pre>
<p>It’s obvious where to insert <code>suspend</code> as abstration bodies must be pure.</p>
<p><code>suspend</code> and <code>run</code> are syntactic; generated by compiler for source language.</p>
<h3 id="qa">Q&amp;A</h3>
<p>Constructive logics have judgements like this (M terminates and proves A is
true):</p>
<p><span class="math display">\[\large\Gamma \vdash M :: \text{A true}\]</span></p>
<p>Also have a judgement like this (<em>if</em> M terminates, it proves A):</p>
<p><span class="math display">\[\large\Gamma \vdash M :: \text{A lax}\]</span></p>
<p>And:</p>
<p><span class="math display">\[\large\frac{
  \Gamma \vdash M :: \text{A lax}
}{
  \Gamma \vdash box M :: \Box \text{A true}
}\]</span></p>
<p>(See PL summer school video?)</p>
<p>See also the Haskell Symposium paper (papers!) doing extensible effects in
Haskell.</p>
<h2 id="yow-conference">Yow! Conference</h2>
<p>There’s a potential for members to get a discounted ticket (group rate). Talk
to Jed about it.</p>]]></summary>
</entry>

</feed>
