<?xml version="1.0"?>
<rss version="2.0">

<channel>
	<title>Planet Haskell</title>
	<link>http://planet.haskell.org/</link>
	<language>en</language>
	<description>Planet Haskell - http://planet.haskell.org/</description>

<item>
	<title>Functional Jobs: Senior software developer/Functional programmer at Vector Fabrics (Full-time)</title>
	<guid isPermaLink="false">urn:uuid:a19db162-93d3-4382-0641-2f8b210932f5</guid>
	<link>http://functionaljobs.com/jobs/154-senior-software-developer-functional-programmer-at-vector-fabrics</link>
	<description>&lt;p&gt;Vector Fabrics is hiring: we are looking for a top-notch programmer to extend our program-analysis and parallelization products. You design and implement algorithms to assist the programmer to create a parallel design from a sequential C or C++ program. You work with our international team of world-class computer scientists and experts in the Haskell / OCaml functional programming languages.&lt;/p&gt;

&lt;p&gt;Your work is at the forefront of technology, giving you the opportunity to publish your work in major conferences and directly cooperate with processor design companies and domain-specific application vendors.&lt;/p&gt;

&lt;p&gt;As we are a startup company, you will quickly have a major impact on our products and get to know all aspects of product creation. You will be part of a strongly committed development team and contribute to our agile development process and automated test suites. Interested? Send your CV, GitHub account or other proof of what you can do to &lt;span class=&quot;spam-protect&quot;&gt;&lt;span class=&quot;user&quot;&gt;jobs&lt;/span&gt; [at] &lt;span class=&quot;host&quot;&gt;vectorfabrics [dot] com&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;

&lt;h3&gt;Responsibilities&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Design and implement software
optimization (e.g. parallelization)
algorithms for CPUs and GPUs;&lt;/li&gt;
&lt;li&gt;Thoroughly test your code, create
automated test suites;&lt;/li&gt;
&lt;li&gt;Contribute to our agile development
planning and process;&lt;/li&gt;
&lt;li&gt;Analyze complex customer applications
for optimization opportunities and
translate this to new analysis
algorithms.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Profile&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Your friends and colleagues describe
you as a superb programmer; your
programming ability is way above
average;&lt;/li&gt;
&lt;li&gt;Demonstrable experience in design and
implementation of complex software
applications; prior experience in
functional programming languages is
preferred;&lt;/li&gt;
&lt;li&gt;You continuously surprise us with
your creative yet pragmatic solutions
for complex software problems;&lt;/li&gt;
&lt;li&gt;You are strongly committed to deliver
working software as early as
possible;&lt;/li&gt;
&lt;li&gt;You work against very high quality
standards. Refactoring is your bread
and butter, pair-programming is how
you prefer to review your code;&lt;/li&gt;
&lt;li&gt;Whatever technologies, languages, or
development environments you've been
using, we expect you have mastered
them in depth, and we expect that you
will be able to master any
technology, language, or development
environment that we need in the
future;&lt;/li&gt;
&lt;li&gt;Excellent command of written and
spoken English.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Education&lt;/h3&gt;

&lt;p&gt;MSc, MEng or PhD in Computer Science or significant relevant experience.&lt;/p&gt;

&lt;h3&gt;About Vector Fabrics&lt;/h3&gt;

&lt;p&gt;Vector Fabrics is a high-tech software company, developing tools for embedded multicore programming. Its technology and expertise is getting widespread recognition in the industry as being innovative and unique in their ability to address heterogeneous multicore application-specific silicon platforms. Due to the advanced nature of its tools, Vector Fabrics operates at the forefront of the next generation of embedded platforms for diverse markets ranging from supercomputers to automotive to cell phones.&lt;/p&gt;

&lt;p&gt;Vector Fabrics puts absolute priority on hiring top class individuals in key positions. Vector Fabrics’ team profile is exceptional and its ambition is to hire only individuals that match or surpass that profile. The company pays top salary and offers a challenging, engaging and stimulating work environment with a high degree of responsibility.&lt;/p&gt;
&lt;p&gt;Get information on &lt;a href=&quot;http://functionaljobs.com/jobs/154-senior-software-developer-functional-programmer-at-vector-fabrics&quot;&gt;how to apply&lt;/a&gt; for this position.&lt;/p&gt;</description>
	<pubDate>Tue, 18 Jun 2013 12:44:48 +0000</pubDate>
</item>
<item>
	<title>FP Complete: FP Haskell Center Beta Sign-Up</title>
	<guid isPermaLink="false">https://www.fpcomplete.com/blog/2013/06/beta-sign-up</guid>
	<link>http://feedproxy.google.com/~r/FpComplete/~3/o-o2AKSmDZc/beta-sign-up</link>
	<description>&lt;h4&gt;Beta sign-up Blog&lt;/h4&gt;&lt;p&gt;It’s almost here!  After months of hard work by our engineers, I am pleased to announce that we’ve opened up sign-up for &lt;a href=&quot;http://feeds.feedburner.com/business/designer-ide&quot;&gt;beta of FP Haskell Center&lt;/a&gt;, the world's first commercial Haskell IDE and deployment platform.  The beta will be released by the end of the month, and we are eager to have your active testing and feedback so we can deliver a great product that the market needs in early September.  As an appreciation and reward for being in the beta program, we will offer special incentives to the finished product when available.  Details of the offer will be announced in late July/early August.&lt;/p&gt;&lt;p&gt;The IDE includes a Haskell compiler and a continually updated set of vetted, tested and supported libraries and code templates.  There is no need to run Cabal or other installers.  The FP Haskell Application Server is used to deploy and run Haskell applications directly in the cloud with no additional effort.  A free shared instance is included with every account. Larger and dedicated instances are available for active project deployments at a reasonable monthly charge.&lt;/p&gt;&lt;h4&gt;FP Haskell Center’s key features and benefits are:&lt;/h4&gt;&lt;ul&gt;&lt;li&gt;Simplifies the writing and deploying of Haskell applications from a single online dashboard.&lt;/li&gt;&lt;li&gt;Cloud-based development frees you to move among multiple devices without needing your own functioning Haskell environment.&lt;/li&gt;&lt;li&gt;Integrated deployment frees you from needing to run a specific OS to match build and production environments.&lt;/li&gt;&lt;li&gt;A hierarchical module tree for:&lt;/li&gt;&lt;li&gt;convenient management (renaming, moving, deleting modules or whole trees)&lt;/li&gt;&lt;li&gt;navigation (much easier to read, expand/collapse)&lt;/li&gt;&lt;li&gt;Regular, automatic parsing and type checking as feedback inside the editor and unobtrusive error output below.&lt;/li&gt;&lt;li&gt;Type and documentation inspection of names.&lt;/li&gt;&lt;li&gt;Integrated access to sample code, School of Haskell tutorials, Haddock documentation service, and Hoogle resource database.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Sign-up for &lt;a href=&quot;http://feeds.feedburner.com/business/haskell-center&quot;&gt;beta of FP Haskell Center&lt;/a&gt;.&lt;/p&gt;&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/FpComplete?a=o-o2AKSmDZc:zpOQ0FhJErU:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/FpComplete?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.feedburner.com/~ff/FpComplete?a=o-o2AKSmDZc:zpOQ0FhJErU:V_sGLiPBpWU&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/FpComplete?i=o-o2AKSmDZc:zpOQ0FhJErU:V_sGLiPBpWU&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.feedburner.com/~ff/FpComplete?a=o-o2AKSmDZc:zpOQ0FhJErU:qj6IDK7rITs&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/FpComplete?d=qj6IDK7rITs&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.feedburner.com/~ff/FpComplete?a=o-o2AKSmDZc:zpOQ0FhJErU:gIN9vFwOqvQ&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/FpComplete?i=o-o2AKSmDZc:zpOQ0FhJErU:gIN9vFwOqvQ&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/FpComplete/~4/o-o2AKSmDZc&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Mon, 17 Jun 2013 14:42:00 +0000</pubDate>
</item>
<item>
	<title>FP Complete: Interim Web Site</title>
	<guid isPermaLink="false">https://www.fpcomplete.com/blog/2013/06/interim-web-site</guid>
	<link>http://feedproxy.google.com/~r/FpComplete/~3/YGZb2pYnjPw/interim-web-site</link>
	<description>&lt;h4&gt;Interim Site blog&lt;/h4&gt;&lt;p&gt;Welcome to our updated website! FP Complete is evolving into a full-fledged commercial developer of Haskell tools and services, as called for in our original plans.  The previous site showcased the School of Haskell. We have been delighted to see it used by thousands of Haskell learners and teachers, including some authors who’ve taken advantage of its unique Active Code features to help people learn.  Now that we are about to release the FP Haskell Center beta, the School remains an integral part of our offering as a place to teach and learn, but we are even more excited by the new full-powered commercial features.&lt;/p&gt;&lt;p&gt;This site is an interim redesign, and will be completed when we release FP Haskell Center, the world’s first commercial Haskell IDE and deployment platform, in early September.  The site, and our strategy, are based on 3 pillars:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;We must produce products and services needed by developers who are already using Haskell tools in their work. This is our base.&lt;/li&gt;&lt;li&gt;At the same time, we need to promote Haskell to the vastly larger non-Haskeller market which on the whole is unaware of Haskell’s existence.  This is a long-term effort that’s absolutely necessary if Haskell is going to have meaningful adoption in the mainstream market.  That’s why you see us putting a lot of effort into high-level discussions and information about what Haskell is, its feature advantages and strategic benefits.  The target audience is business management, engineering management and developers who need to be converted into Haskell supporters and users.  &lt;/li&gt;&lt;li&gt;In all our efforts, we are always working with the Haskell community.  FP Complete was started in 2012 with the help of some of the leaders of the community and is committed to continue working with the entire community to advance the technology and expand Haskell adoption in the commercial market.  This follows a well-proven model of success for companies commercializing open-source technologies starting with Red Hat.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;In the coming months, we will be continually adding more content to fulfill our goal to be a major resource for all things Haskell.  Things like more whitepapers, case studies, video testimonials, tutorials and sample codes.  If you have any suggestions or things to contribute, be sure to let us know.  As always, we welcome constructive comments from the community.&lt;/p&gt;&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/FpComplete?a=YGZb2pYnjPw:Cjpscs1vI9g:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/FpComplete?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.feedburner.com/~ff/FpComplete?a=YGZb2pYnjPw:Cjpscs1vI9g:V_sGLiPBpWU&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/FpComplete?i=YGZb2pYnjPw:Cjpscs1vI9g:V_sGLiPBpWU&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.feedburner.com/~ff/FpComplete?a=YGZb2pYnjPw:Cjpscs1vI9g:qj6IDK7rITs&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/FpComplete?d=qj6IDK7rITs&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.feedburner.com/~ff/FpComplete?a=YGZb2pYnjPw:Cjpscs1vI9g:gIN9vFwOqvQ&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/FpComplete?i=YGZb2pYnjPw:Cjpscs1vI9g:gIN9vFwOqvQ&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/FpComplete/~4/YGZb2pYnjPw&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Mon, 17 Jun 2013 12:42:00 +0000</pubDate>
</item>
<item>
	<title>Douglas M. Auclair (geophf): Thoughts on Kleisli Arrows in Java (WIP)</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-4650294074444534066.post-5758139597428992929</guid>
	<link>http://logicaltypes.blogspot.com/2013/06/thoughts-on-kleisli-arrows-in-java-wip.html</link>
	<description>Here are some ramblings as I puzzle my way through on how to represent Kleisli arrows in Java (and why I would want to do that, anyway). This is very much a work in progress, so no solid conclusions from this posting.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Categories are abstractions. They have objects and morphisms. The thing of Category theory is that the objects can be anything, as they are atomic, and the morphisms aren't necessarily functions. So, the objects can be numbers, not that we care, or they could be arrows (another term for morphisms), or they could be categories themselves, so the morphisms become morphisms between categories. Or if the objects are arrows then the morphisms are higher-order functions.&lt;br /&gt;&lt;br /&gt;Neat-o!&lt;br /&gt;&lt;br /&gt;So, John Baez did some wonderful pieces on higher-order categories in the direction of physical math: topologies and groups and such, and this was replicated in Edward Kmett's work with ... what are they called? Ah, yes: semigroupoids (how could I forget), where the monoids have no zero bases and so identity functions aren't necessary, I suppose.&lt;br /&gt;&lt;br /&gt;Interesting! Half-monoids!&lt;br /&gt;&lt;br /&gt;But if we look at Categories as simply that: objects and morphisms, and we look at morphisms as simply arrows from a to b, then does that simplify my implementation of my categories library?&lt;br /&gt;&lt;br /&gt;Monads no longer are generically typeful but now use inheritance to describe the type families:&lt;br /&gt;&lt;br /&gt;Monad&amp;lt;a&amp;gt; is the interface and Maybe&amp;lt;a&amp;gt; extends Monad&amp;lt;a&amp;gt; instead of&lt;br /&gt;&lt;br /&gt;Monad&amp;lt;M extends Monad, a&amp;gt; being the interface as Maybe&amp;lt;a&amp;gt; extends Monad&amp;lt;Maybe, a&amp;gt;.&lt;br /&gt;&lt;br /&gt;The problem is in the generic functions, how does one grab the 'm' in the monadic type? Or is it as simple as using inheritance and forgetting the thorny problem of genericity, or passing that problem off to the inheritance structure?&lt;br /&gt;&lt;br /&gt;Same thing for Arrow ...&lt;br /&gt;&lt;br /&gt;instead of Arrow&amp;lt;a, b, c&amp;gt;&lt;br /&gt;&lt;br /&gt;we have Arrow&amp;lt;b, c&amp;gt;&lt;br /&gt;&lt;br /&gt;and then the Kleisli arrow becomes more simple, perhaps? Because&lt;br /&gt;&lt;br /&gt;KleisliArrow&amp;lt;m, b, c&amp;gt; extends Arrow&amp;lt;b, m c&amp;gt;&lt;br /&gt;&lt;br /&gt;... but how does that work? Can it work? I don't see how that works.&lt;br /&gt;&lt;br /&gt;for first :: a b c -&amp;gt; a (b, d) (c, d)&lt;br /&gt;&lt;br /&gt;how does that work for the KleisliArrow, and for chaining the monadic computation?&lt;br /&gt;&lt;br /&gt;It appears further research is necessary for me to get a solid grasp of this to be able to implement this properly in Java (as opposed to writing a haskell parser on top of Java, which is also a viable way of going about it, except for the fact that there is political resistance to learning a domain-specific language from devs brought on to code 'only' in language-of-choice X).&lt;br /&gt;&lt;br /&gt;So that's my problem, because in my 'Monads in Java' article I concluded with a:&lt;br /&gt;&lt;br /&gt;So you see we can now do the following:&lt;br /&gt;&lt;br /&gt;f.apply(m).bind(g).bind(h)&lt;br /&gt;&lt;br /&gt;and I now know that f.apply(m) is a weakness in coding. This should all be strung together with Kleisli arrows and the resulting morphism be run through the Kleisli computer.&lt;br /&gt;&lt;br /&gt;&quot;The (explicit) use of apply considered harmful&quot;?&lt;br /&gt;&lt;br /&gt;I mean, Gah! How bold! How daring! How so against the grain!&lt;br /&gt;&lt;br /&gt;And it isn't even really 'harmful' either. More like obtuse or obnoxious. And not even that, more like: inelegant. That's the word: inelegant. And we're not even 'using' apply in the above computation. I mean, we are, but we are always using apply. It's so inherent that now just juxtaposition is now apply, so it's not the '(explicit) use' of apply, it's the '(explicit) call' or '(explicit) invocation' of apply that's inelegant.&lt;br /&gt;&lt;br /&gt;I mean, when I see the above formulation, I now shudder, whereas before I might've said, with a pause so slight it didn't even register, 'What do we do here? Oh, use apply!' knowing, at the back of my mind, that this didn't sit perfectly right, but what else was there to do?&lt;br /&gt;&lt;br /&gt;Well, with Kleisli composition, there is nothing to do at all, but just do it&lt;br /&gt;&lt;br /&gt;runKleisli (f &amp;lt;+&amp;gt; g &amp;lt;+&amp;gt; h) m&lt;br /&gt;&lt;br /&gt;and we're done.&lt;br /&gt;&lt;br /&gt;Now, how to represent that in Java, ... well, I do have a KleisliArrow type that does return the underlying arrow, but what about composition ... it probably has that, too:&lt;br /&gt;&lt;br /&gt;f &amp;gt;&amp;gt;&amp;gt; g &amp;gt;&amp;gt;&amp;gt; h&lt;br /&gt;&lt;br /&gt;But the gnawing problem there is that KleisliArrow is not related to Arrow, because, properly, it isn't: KleisliArrow on a specific monadic type m IS related to Arrow.&lt;br /&gt;&lt;br /&gt;And I don't know how to represent that in Java.&lt;br /&gt;&lt;br /&gt;Yet.&lt;br /&gt;</description>
	<pubDate>Mon, 17 Jun 2013 12:39:32 +0000</pubDate>
	<author>noreply@blogger.com (geophf)</author>
</item>
<item>
	<title>hyperq: Trading: a hacker approach?</title>
	<guid isPermaLink="true">http://hyperq.github.io/blog/trading-a-hacker-approach.html</guid>
	<link>http://hyperq.github.io/blog/trading-a-hacker-approach.html</link>
	<description>&lt;blockquote&gt;
&lt;p&gt;
You are startled by the sound of an alarm. It is followed by an urgent voice
which warns that the Arcada has been boarded by unknown intruders. It ends
abruptly. &lt;br /&gt;
&amp;gt; &lt;br /&gt;
&lt;a href=&quot;http://sarien.net/spacequest#anotherhallway&quot;&gt;start of Space Quest I&lt;/a&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;
Most hackers involved in the world of trading enter from the technology side
of the business. And there's two main gateways are via trader enhancement or
trader replacement. Making traders smarter and faster using technology is one
well worn road. There's lots of room to streamline the human trading process:
automation of regular tasks, expansion of back-testing capabilities and easy
gains to be had in better trader dashboards to get information when and where
needed.
&lt;/p&gt;

&lt;p&gt;
Trader replacement is a little harder but also hackable. There's plenty of
tricks out there to shave a few msecs of computation and execution time, and
bringing bigdata testing and conversion of a sometimes fuzzy human rule-set
into a more rigorous computational exercise.
&lt;/p&gt;

&lt;p&gt;
Either way, a trading runtime ends up paving the cow-paths of institutional
finance which look somewhat like this:
&lt;/p&gt;

&lt;img src=&quot;http://hyperq.github.io/assets/ats_features_diagram.png&quot; alt=&quot;design&quot; width=&quot;100%&quot; /&gt;

&lt;p&gt;
Over at &lt;a href=&quot;http://hyperq.github.io&quot;&gt;hyperq&lt;/a&gt;, we've been thinking about the above diagram and how to get
together a decent trading runtime. Now when a wildly ambitious objective meets
a meager resource base you have two options:
&lt;/p&gt;

&lt;ul class=&quot;org-ul&quot;&gt;
&lt;li&gt;go on a &lt;a href=&quot;http://hyperq.github.io/../blog/trading-a-hacker-approach/ENOxd.jpg&quot;&gt;kamikaze&lt;/a&gt; death march
&lt;/li&gt;
&lt;li&gt;take the team on a &lt;a href=&quot;http://www.qt.com.au/news/abolish-fringe-benefits-tax-bring-back-long-lunch-/1895360/&quot;&gt;long lunch&lt;/a&gt; and redefine
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
Since this is all open source, our long lunch redefinitional musings led us to
computer sciencing the bejesus out of the trading problem domain.
&lt;/p&gt;

&lt;p&gt;
Here's the alternative design specification document we wrote on one of the
drink coasters:
&lt;/p&gt;

&lt;div class=&quot;org-src-container&quot;&gt;

&lt;pre class=&quot;src src-haskell&quot;&gt;&lt;span class=&quot;org-function-name&quot;&gt;trade&lt;/span&gt; &lt;span class=&quot;org-variable-name&quot;&gt;::&lt;/span&gt; [&lt;span class=&quot;org-type&quot;&gt;MarketData&lt;/span&gt; a] &lt;span class=&quot;org-variable-name&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;org-type&quot;&gt;Book&lt;/span&gt; b &lt;span class=&quot;org-variable-name&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;org-type&quot;&gt;IO&lt;/span&gt; [&lt;span class=&quot;org-type&quot;&gt;Order&lt;/span&gt; b]
&lt;span class=&quot;org-function-name&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;org-variable-name&quot;&gt;=&lt;/span&gt; forever &lt;span class=&quot;org-variable-name&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;org-keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;org-variable-name&quot;&gt;.&lt;/span&gt; trade
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Having just cut 3 months out of our critical path we even had time for some
Zork:
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;
You are in an open field west of a big white house with a boarded
front door.
There is a small mailbox here.&lt;br /&gt;
&amp;gt; &lt;br /&gt;
&lt;a href=&quot;http://thcnet.net/error/index.php&quot;&gt;Zork&lt;/a&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;
Long lunch over and the new specs still seem sweet. Some immediate ideas:
&lt;/p&gt;

&lt;ul class=&quot;org-ul&quot;&gt;
&lt;li&gt;the concept of market data becomes naturally abstractable.  Data can
include multiple sources, news flow or whatever universe observation you
can think of. Do you need the Market prefix? 
&lt;/li&gt;
&lt;li&gt;there is an immediate reminder of real world interaction with the IO monad.
&lt;/li&gt;
&lt;li&gt;subsequent functions scan more easily and can be categorized - often as a
matter of taste.  For example, a complex event process (CEP), a fashionable
big deal in trading system circles, seems logically to have this type:

&lt;div class=&quot;org-src-container&quot;&gt;

&lt;pre class=&quot;src src-haskell&quot;&gt;&lt;span class=&quot;org-function-name&quot;&gt;cep&lt;/span&gt; &lt;span class=&quot;org-variable-name&quot;&gt;::&lt;/span&gt; [&lt;span class=&quot;org-type&quot;&gt;MarketData&lt;/span&gt; a] &lt;span class=&quot;org-variable-name&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;org-type&quot;&gt;MarketData&lt;/span&gt; a
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Whether to put this prior to or inside the trade function then becomes a
matter of taste.
&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;Should the input be [Maybe MarketData a]? This puts the real world
likelihood of the data feed being down front and center, rather than
designing a system assuming an idealized world and then panicking when
something breaks.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
More generally, a more hacker approach leads you away from bigdata phd
solutions that dominate hft and algorithmic trading and towards the important,
small and obvious stuff (that may not lend themselves to a phd dissertation).
The market is closed (unexpectedly) - I better not try and trade, or I had
better try and trade elsewhere given the sorry state of Book a. Gee, there's a
lot of volatility out and about today - is it a big news day? The last news
piece of note was a facebook announcement. Wow, facebook really tanked but but
zygna didn't - what gives there? Someone must have forgot to turn the market
feed on. etc etc
&lt;/p&gt;

&lt;p&gt;
And I suspect this approach leads further to a big, big gap in the market.
Imagine on a busy day in the market you could slow down time. The e-mini
suddenly drops by 1% in the space of a few heartbeats. What just happened?
Rewind the video tape and look more carefully at the last few minutes. Look
back at the news-flow over the last 10 minutes and look for keywords. Check
other markets - are they all tanking or is it just a local event? Or did some
human just enter an extra zero or three again?
&lt;/p&gt;

&lt;p&gt;
If you can do all of that in a few seconds your process is way ahead of the
competition. The HFT guys have already panicked and run away to hide behind
their statistical order flow models. Algorithmic trades are pinging their stop
loss instructions blindly creating what may be a forecastable trend.
Meanwhile, discretionary day traders have just noticed a small section of one
of their screen is flashing red…
&lt;/p&gt;

&lt;p&gt;
In this zone, a hacker trader with a hacker-like trading process can find all
sorts of edges and market tells.
&lt;/p&gt;

&lt;p&gt;
So do we want our trading process to look and feel like a big finance
organizational structure? Or should for hyperq to have a Roger Wilco attitude:
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;
Anyway, I aborted the launch and jetted out of there in an escape pod. I
crawled into the sleep chamber, and the next thing I knew, I woke up in a
trash freighter! Yeah, things didn't look too good, but I blasted out of the
freighter in an old jalopy I resurrected from the rubble.
~ &lt;a href=&quot;http://spacequest.wikia.com/wiki/Roger's_Dialogue&quot;&gt;Roger Wilco&lt;/a&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;
Much more fun than a death march to pave the cow-paths.
&lt;/p&gt;</description>
	<pubDate>Mon, 17 Jun 2013 08:19:00 +0000</pubDate>
</item>
<item>
	<title>Manuel M T Chakravarty: Data Flow Fusion with Series Expressions in Haskell</title>
	<guid isPermaLink="true">http://justtesting.org/post/53175916852</guid>
	<link>http://justtesting.org/post/53175916852</link>
	<description>&lt;p&gt;We are currently exploring &lt;em&gt;flow fusion&lt;/em&gt;, a new fusion method for purely functional array code that overcomes the main limitation of stream fusion, namely stream fusion’s inability to fuse branching streams. Our current flow-fusion prototype in the Glasgow Haskell Compiler manages to achieve a twofold speedup over stream fusion for computing convex hulls of 2D points using the &lt;a href=&quot;http://en.wikipedia.org/wiki/QuickHull&quot;&gt;QuickHull&lt;/a&gt; algorithm. In fact, the code generated by flow fusion is only a few percent points away from hand-written C code. We have summarised all the details in a draft paper &lt;a href=&quot;http://www.cse.unsw.edu.au/~chak/papers/BCKR13.html&quot;&gt;Data Flow Fusion with Series Expressions in Haskell&lt;/a&gt;.&lt;/p&gt;</description>
	<pubDate>Mon, 17 Jun 2013 05:39:34 +0000</pubDate>
</item>
<item>
	<title>Paul Potts: Objective-Dylan, or Perhaps Subjective-C?</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-21500237.post-8240094091528688502</guid>
	<link>http://praisecurseandrecurse.blogspot.com/2013/06/objective-dylan-or-perhaps-subjective-c.html</link>
	<description>&lt;p&gt;Yesterday my wife took the kids with her on an overnight trip to Ann Arbor so I've had a bit of extra quiet time. How am I making use of this bounty? Getting on with some minor home repairs? Cleaning my office from top to bottom? Er, no... porting the game logic I've written so far in Objective-C back to Dylan, so that I can do some more thinking about it.&lt;/p&gt; &lt;p&gt;So after a phone job interview yesterday (which went well, I thought -- I'm optimistic about this possibility!) I started working on this task, and then about twelve hours later, around 2 a.m., I had the basic setup and population of the game board working. It's embarrassing to admit how long it took. I started on my Mac, and when I began encountering constant runtime errors switched over to my Ubuntu box, thinking that the Mac version of Open Dylan might just be broken (it isn't; I got the identical behavior on the Linux build). I finally figured out workarounds -- it's funny how taking a break clears my head far better than pressing on ever does -- then read a little Gene Wolfe (I'm working my way through &lt;i&gt;In Green's Jungles&lt;/i&gt;, one of his books I've repeatedly tried and failed to finish), and fell asleep with no children in the bed to kick or otherwise interfere with a good night's sleep. I'm back up this morning, had a bath, and I'm drinking a large coffee with soy creamer and stevia and trying to hold off on a lunch break until I have some more done. It's about 10 a.m. and I'm expecting my family back in about six hours, so the race is on!&lt;/p&gt; &lt;p&gt;This has taken far longer than I hoped; I lost quite a bit of time stumbling across things in Open Dylan that still seem just plain broken. I had to start working on a smaller and smaller program to figure out exactly what was broken. These things I've flagged in comments, as places where, basically, I wish Dylan worked a certain way, and it doesn't. I may just be asking for something that doesn't quite match the original spec or isn't quite possible, but I'll share those with the Dylan Hackers team and see if it seems like I can help with them. The biggest thing that was broken, though, was me -- my brain, that is -- since it's been a long time since I've worked with Dylan's &lt;b&gt;type-union&lt;/b&gt; and &lt;b&gt;singleton&lt;/b&gt; pseudo-classes and I had forgotten the details. The compiler was not a big help with this, since it is such a &lt;i&gt;dynamic&lt;/i&gt; language and leaves an awful lot of things to the runtime to figure out, which it does by throwing an error message that may or may not help much. The documentation is a bit scanty, but it does contain everything you need to know, if you re-read and squint at the scanty examples that are out there hard enough.&lt;/p&gt; &lt;p&gt;The good news is that the port is working and I'd like to share it. Dylan is still up there with Scheme (and now Haskell) as one of my favorite languages for &lt;i&gt;designing&lt;/i&gt; programs -- yes, even though Dylan is quite old as languages go. I like to see what it can do especially with generic functions and its sophisticated model for object-oriented &lt;i&gt;dispatch&lt;/i&gt;. I've been a little stymied as to how to express the design best in Objective-C. If it was a complicated game design, I wouldn't feel bad about having a program that looked complex. But it's really an elegantly simple game, and so I feel like the implementation should reflect that. My Objective-C implementation has been feeling more and more bloated and pointlessly complex, although it works, so my thought was to get it down to a simple implementation that takes full advantage of Dylan's object-oriented programming features, largely borrowed from CLOS, and then port that back to Objective-C, adding whatever minimalist support is needed to fake up some of the features that Dylan gives me that Objective-C doesn't have. This might be by way of also writing a Haskell or Scala implementation later, for yet more learning and language comparison, although really what I should focus on is getting the iOS GUI up and working so that I have something to show people.&lt;/p&gt; &lt;p&gt;Anyway, I've got a Dylan program that plays the Polar game, using singletons to represent tile types, and methods dispatched on singletons to handle specific kinds of collisions. The classes -- which are empty, pretty much used &lt;i&gt;only&lt;/i&gt; for their usefulness as types, for driving dispatch -- are like so:&lt;/p&gt; &lt;div style=&quot;clear: both; text-align: center;&quot; class=&quot;separator&quot;&gt;&lt;a style=&quot;margin-left: 1em; margin-right: 1em;&quot; href=&quot;http://2.bp.blogspot.com/-PiK8rgZXxVo/Ubx_P2ZZKtI/AAAAAAAADGA/N3msDgs2kPQ/s1600/tile-classes-75-percent.png&quot;&gt;&lt;img src=&quot;http://2.bp.blogspot.com/-PiK8rgZXxVo/Ubx_P2ZZKtI/AAAAAAAADGA/N3msDgs2kPQ/s1600/tile-classes-75-percent.png&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/div&gt; &lt;p&gt;In Dylan you can create some instances, and create something called a &lt;b&gt;type-union&lt;/b&gt;, which is something that is a type, I think, but not a class. You can use it to define a slot type or a parameter type. But you can't make one:&lt;/p&gt; &lt;pre&gt;define constant $the-bomb = make(&amp;lt;bomb&amp;gt;);&lt;br /&gt;define constant $the-empty = make(&amp;lt;empty&amp;gt;);&lt;br /&gt;define constant $the-heart = make(&amp;lt;heart&amp;gt;);&lt;br /&gt;define constant $the-house = make(&amp;lt;house&amp;gt;);&lt;br /&gt;define constant $the-ice-block = make(&amp;lt;ice-block&amp;gt;);&lt;br /&gt;define constant $the-mountain = make(&amp;lt;mountain&amp;gt;);&lt;br /&gt;define constant $the-tree = make(&amp;lt;tree&amp;gt;);&lt;br /&gt;&lt;br /&gt;define constant &amp;lt;tile-or-false&amp;gt; = type-union(&lt;br /&gt;    singleton( $the-bomb ), singleton( $the-empty ),&lt;br /&gt;    singleton( $the-heart ), singleton( $the-house ),&lt;br /&gt;    singleton( $the-ice-block ), singleton( $the-mountain ),&lt;br /&gt;    singleton( $the-tree ), singleton( #f ) );&lt;/pre&gt; &lt;p&gt;And eventually dispatch on singletons -- meaning that a given method will be called with it is called with references to the exact objects that you specify:&lt;/p&gt; &lt;pre&gt;define method collide( model :: &amp;lt;model&amp;gt;, dir :: &amp;lt;dir&amp;gt;,&lt;br /&gt;    heart-pos :: &amp;lt;pos&amp;gt;, heart-tile == $the-heart,&lt;br /&gt;    house-pos :: &amp;lt;pos&amp;gt;, house-tile == $the-house )&lt;br /&gt;    format-out( &quot;collide: $the-heart / $the-house\n&quot; );&lt;br /&gt;    setTileAtPos( model, heart-pos, $the-empty );&lt;br /&gt;    model.decrementHeartCount();&lt;br /&gt;end;&lt;/pre&gt; &lt;p&gt;That gives you an idea of how some of the code in the Dylan program is organized. I have it mostly working, however, I'm not going to present the full code quite yet because I have a crashing bug, and I haven't yet been able to figure out if it is a dumb mistake on my part or a compiler or runtime bug in Open Dylan. I've also asked the Dylan hackers to take a look at my design and see what they think -- if they can find, as I put it, &quot;a simpler design struggling to get out.&quot; Which is always the challenge, when trying to write not just functional, but &lt;i&gt;model&lt;/i&gt; code, isn't it?&lt;/p&gt;</description>
	<pubDate>Mon, 17 Jun 2013 01:09:00 +0000</pubDate>
	<author>noreply@blogger.com (Paul Potts)</author>
</item>
<item>
	<title>The Gentoo Haskell Team: Call for help: wiki.gentoo.org documentation</title>
	<guid isPermaLink="false">http://gentoohaskell.wordpress.com/?p=86</guid>
	<link>http://gentoohaskell.wordpress.com/2013/06/16/call-for-help-wiki-gentoo-org-documentation/</link>
	<description>&lt;p&gt;I’d like to ask gentoo-haskell community for help. We have a nice &lt;a href=&quot;http://wiki.gentoo.org&quot;&gt;wiki&lt;/a&gt; and our project page have moved &lt;a href=&quot;http://wiki.gentoo.org/wiki/Project:Haskell&quot;&gt;there&lt;/a&gt;. But it seems that we don’t have enough documentation quality for end-user application. As a developers we support proper builds and tests for that packages but we are not expert users for many of them. So I’d like to ask community to add some docs and tips for applications you use. This basically means installation, advanced config (examples), interesting use cases, links to external resources (blog posts/documentation) and so on. It can help a lot for new Gentoo users.&lt;/p&gt;
&lt;p&gt;The most interesting projects are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://wiki.gentoo.org/wiki/Pandoc&quot;&gt;pandoc&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://wiki.gentoo.org/wiki/Git-annex&quot;&gt;git-annex&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://wiki.gentoo.org/wiki/Gitit&quot;&gt;gitit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://wiki.gentoo.org/wiki/yi&quot;&gt;yi&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://wiki.gentoo.org/wiki/Xmonad&quot;&gt;xmonad&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
&lt;br /&gt;  &lt;a href=&quot;http://feeds.wordpress.com/1.0/gocomments/gentoohaskell.wordpress.com/86/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/comments/gentoohaskell.wordpress.com/86/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;img src=&quot;http://stats.wordpress.com/b.gif?host=gentoohaskell.wordpress.com&amp;amp;blog=7667502&amp;amp;post=86&amp;amp;subd=gentoohaskell&amp;amp;ref=&amp;amp;feed=1&quot; alt=&quot;&quot; height=&quot;1&quot; border=&quot;0&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Sun, 16 Jun 2013 19:30:25 +0000</pubDate>
</item>
<item>
	<title>Jan Stolarek: Getting friendly with STG</title>
	<guid isPermaLink="false">http://lambda.jstolarek.com/?p=1210</guid>
	<link>http://lambda.jstolarek.com/2013/06/getting-friendly-with-stg/</link>
	<description>&lt;p style=&quot;text-align: justify;&quot;&gt;I’ve been spending last months on developing GHC. No rocket science so far, just a bit of hacking here and there. The biggest thing I am working on is &lt;a href=&quot;http://hackage.haskell.org/trac/ghc/ticket/6135&quot;&gt;ticket #6135&lt;/a&gt;, which is about changing some of the existing &lt;a href=&quot;http://hackage.haskell.org/trac/ghc/wiki/Commentary/PrimOps&quot;&gt;PrimOps&lt;/a&gt; to return unboxed &lt;code&gt;Int#&lt;/code&gt; instead of &lt;code&gt;Bool&lt;/code&gt;. This means that the result of comparing two unboxed values will be either an unboxed &lt;code&gt;0#&lt;/code&gt; or unboxed &lt;code&gt;1#&lt;/code&gt;, instead of a tagged pointer to statically allocated object representing &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt;. This modification will allow to write branchless algorithms in Haskell. I promise to write about this one day, but today I want to blog about a different topic.&lt;/p&gt;
&lt;p style=&quot;text-align: justify;&quot;&gt;It so happens that things I’ve been doing in GHC require me to make changes in the code generator. This is a bit challenging for me, because the code generator is something that didn’t interest me much when I started to learn about compilers. Probably the main reason for this is that code generation means dealing with assembly. I’ve been programming for about 16 years and only two languages caused me problems when I tried to learn them. Assembly is one of them&lt;sup&gt;&lt;a title=&quot; In case you’re interested, the other one is Io&quot; href=&quot;http://lambda.jstolarek.com/2013/06/getting-friendly-with-stg/#footnote_0_1210&quot; id=&quot;identifier_0_1210&quot; class=&quot;footnote-link footnote-identifier-link&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;. I have been learning it for one year during my studies and, although I had no problems with understanding the idea behind assembly and writing short snippets of code, writing a larger piece of code always ended up in a headache.&lt;/p&gt;
&lt;p style=&quot;text-align: justify;&quot;&gt;It looks that the time has come to overcome my fear. During last months I’ve been reading a lot of assembly generated by GHC and I even made some attempts at writing assembly code by myself (well, using intrinsics, but I guess that counts). But between Haskell source code and the generated executable there are many intermediate steps. From my observations it seems that many Haskellers have basic knowledge of Core – GHC’s intermediate language. Most have also heard about other two intermediate representations used by GHC – STG and Cmm – but it seems that few people know them, unless they hack the compiler. And since I’m hacking the compiler I should probably have more knowledge about these two representations, right?&lt;/p&gt;
&lt;p style=&quot;text-align: justify;&quot;&gt;There’s a classic paper by Simon Peyton-Jones “Implementing lazy functional languages on stock hardware: the Spineless Tagless G-machine”. It is quite long – 87 pages total – and, being published in 1992, it is mostly out of date. These two things kept me from reading it, although I think that being out of date was only a pretext for me to avoid reading almost 90 pages of text. But, since I need to learn about STG, I finally decided to give it a shot. Reading the paper took my four days. Paper is very well written and in general is an easy read. I was afraid that I might not understand formal description of operational semantics of STG, but it turned out to be well explained so I had no problem with that. The major problem turned out to be the amount of knowledge I had to learn while reading. This resulted in problems with fully understanding last sections of the paper. Not because they are more difficult than the initial ones, but because I didn’t fully remember all the details that were discussed earlier. An important question is which information is not up to date. I’m not yet familiar with the existing implementation, but it seems that many things have changed: the Spineless Tagless G-machine is not tagless any more since the introduction of pointer tagging; curried function are now evaluated using eval/apply convention, while the paper describes push/enter; the paper discusses only compilation to C, while currently C back-end is becoming deprecated in favour of native code generator and LLVM; and finally the layout of closures is now slightly different than the one presented in the paper. I am almost certain that garbage collection is also performed differently. These are the differences that I noticed, which means that really a lot has changed since the publication over 20 years ago. Surprisingly, this doesn’t seem like a big problem, because the most important thing is that the paper presents an idea of how STG works, while the mentioned changes are only not so important details.&lt;/p&gt;
&lt;p style=&quot;text-align: justify;&quot;&gt;So, now that I have a basic idea of how STG works, what comes next? There are a few follow up papers:&lt;/p&gt;
&lt;ul&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;“The STG runtime system (revised)” – an updated description of STG written in 1999 by Simon Peyton Jones and Simon Marlow. I guess it’s also outdated, but still probably worth reading. It has only 65 pages :)&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;“Making a Fast Curry. Push-Enter vs. Eval-Apply for Higher-order Languages” – this described the mentioned eval/apply and push/enter strategies. Already read this one.&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;“Faster Laziness Using Dynamic Pointer Tagging” – this will tell you why STG is not tagless. Read this one also.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And once I’ll deal with STG I’ll have to learn about Cmm.&lt;/p&gt;
&lt;ol class=&quot;footnotes&quot;&gt;&lt;li id=&quot;footnote_0_1210&quot; class=&quot;footnote&quot;&gt; In case you’re interested, the other one is &lt;a href=&quot;http://iolanguage.org/&quot;&gt;Io&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;</description>
	<pubDate>Thu, 13 Jun 2013 20:45:46 +0000</pubDate>
</item>
<item>
	<title>Daniil Frumin: Building GHCJS</title>
	<guid isPermaLink="false">http://parenz.wordpress.com/?p=49</guid>
	<link>http://parenz.wordpress.com/2013/06/12/ghcjs-build/</link>
	<description>&lt;div id=&quot;outline-container-sec-1&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-1&quot;&gt;&lt;span class=&quot;section-number-2&quot;&gt;1&lt;/span&gt; Intro&lt;/h2&gt;
&lt;div id=&quot;text-1&quot; class=&quot;outline-text-2&quot;&gt;
&lt;p&gt;
In this post I would like to talk about my experience with&lt;br /&gt;
bootstrapping &lt;a href=&quot;http://weblog.luite.com/wordpress/?p=14&quot;&gt;GHCJS&lt;/a&gt; using the provided facilities &lt;a href=&quot;https://github.com/ghcjs/ghcjs-build&quot;&gt;ghcjs-build&lt;/a&gt;. I&lt;br /&gt;
never used tools like Vagrant or Puppet before so all of this was&lt;br /&gt;
kinda new to me.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&quot;outline-container-sec-2&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-2&quot;&gt;&lt;span class=&quot;section-number-2&quot;&gt;2&lt;/span&gt; Initial installation&lt;/h2&gt;
&lt;div id=&quot;text-2&quot; class=&quot;outline-text-2&quot;&gt;
&lt;p&gt;
GHCJS can’t actually work with vanilla GHC 7.* as it requires to&lt;br /&gt;
apply some patches (in order to get JS ffi to work, it adds&lt;br /&gt;
&lt;code&gt;JavaScriptFFI&lt;/code&gt; language extension among other modifications).
&lt;/p&gt;
&lt;p&gt;
&lt;a href=&quot;https://github.com/ghcjs/ghcjs-build&quot;&gt;ghcjs-build&lt;/a&gt; uses &lt;a href=&quot;http://vagrantup.com&quot;&gt;Vagrant&lt;/a&gt; (a tool for automatically building and&lt;br /&gt;
running work environments) to mange the work environment, so prior to&lt;br /&gt;
running GHCJS you need to install vagrant and &lt;a href=&quot;http://virtualbox.org&quot;&gt;VirtualBox&lt;/a&gt;. It’s actually&lt;br /&gt;
a sensible way to tackle a project like that: everyone has similar&lt;br /&gt;
work environments, you don’t have to mess with your local GHC&lt;br /&gt;
installation. It also make use of &lt;a href=&quot;http://puppetlabs.com&quot;&gt;Puppet&lt;/a&gt; deployment system in&lt;br /&gt;
&lt;code&gt;puppetlabs-vcsrepo&lt;/code&gt; module for cloning Git repositories.
&lt;/p&gt;
&lt;p&gt;
Currently, there are two ways to start up GHCJS using &lt;code&gt;ghcjs-build&lt;/code&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&quot;outline-container-sec-2-1&quot; class=&quot;outline-3&quot;&gt;
&lt;h3 id=&quot;sec-2-1&quot;&gt;&lt;span class=&quot;section-number-3&quot;&gt;2.1&lt;/span&gt; Using the prebuilt version&lt;/h3&gt;
&lt;div id=&quot;text-2-1&quot; class=&quot;outline-text-3&quot;&gt;
&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-sh&quot;&gt;git clone https://github.com/ghcjs/ghcjs-build.git
&lt;span style=&quot;color: #D0D0FF;&quot;&gt;cd&lt;/span&gt; ghcjs-build
git checkout prebuilt
vagrant up
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Using this configuration the following procedures are performed:
&lt;/p&gt;
&lt;ol class=&quot;org-ol&quot;&gt;
&lt;li&gt;Vagrant sets up a 32-bit Ubuntu Precise system (/Note: if this is&lt;br /&gt;
your first time running Vagrant it downloads the 280Mb&lt;br /&gt;
precise32.box file from the Vagrant site/)
&lt;/li&gt;
&lt;li&gt;Vagrants does some provisioning using Puppet (downloads and&lt;br /&gt;
installs necessary packages)
&lt;/li&gt;
&lt;li&gt;A 1.4GB archive with ghcjs and other prebuilt tools are downloaded&lt;br /&gt;
and extracted.
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&quot;outline-container-sec-2-2&quot; class=&quot;outline-3&quot;&gt;
&lt;h3 id=&quot;sec-2-2&quot;&gt;&lt;span class=&quot;section-number-3&quot;&gt;2.2&lt;/span&gt; Compiling from source&lt;/h3&gt;
&lt;div id=&quot;text-2-2&quot; class=&quot;outline-text-3&quot;&gt;
&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-sh&quot;&gt;git clone https://github.com/ghcjs/ghcjs-build.git
&lt;span style=&quot;color: #D0D0FF;&quot;&gt;cd&lt;/span&gt; ghcjs-build
vagrant up
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Apart from setting up the box this will
&lt;/p&gt;
&lt;ol class=&quot;org-ol&quot;&gt;
&lt;li&gt;Get the GHC sources from Git HEAD and applies the GHCJS &lt;a href=&quot;http://ghcjs.github.io/patches/ghc-ghcjs.patch&quot;&gt;patch&lt;/a&gt;.
&lt;/li&gt;
&lt;li&gt;Get all the necessary packages for ghcjs
&lt;/li&gt;
&lt;li&gt;Get the latest Cabal from Git HEAD, applies the GHCJS &lt;a href=&quot;http://ghcjs.github.io/patches/cabal-ghcjs.patch&quot;&gt;patch&lt;/a&gt; and&lt;br /&gt;
build it.
&lt;/li&gt;
&lt;li&gt;Compile the necessary libraries using ghcjs
&lt;/li&gt;
&lt;li&gt;Compile &lt;code&gt;ghcjs-examples&lt;/code&gt; and its dependencies (it appears that it&lt;br /&gt;
can take a lot of time to compile gtk2hs and gtk2hs’s tools)
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Please note, that depending on your computer, you might want to go for&lt;br /&gt;
a long walk, enjoy a small book or get a night sleep (assuming you are&lt;br /&gt;
not scared by the sound of computer fans).
&lt;/p&gt;
&lt;p&gt;
Apart from being slow, the process of compiling everything from&lt;br /&gt;
source is error prone. To give you a taste, last night I was not able&lt;br /&gt;
to reproduce a working environment myself, because of some recent&lt;br /&gt;
changes in GHC HEAD. The prebuilt version on the other hand is&lt;br /&gt;
guaranteed to install correctly.
&lt;/p&gt;
&lt;p&gt;
Hopefully, the GHCJS patches will be merged upstream before the GHC&lt;br /&gt;
7.8 is out. That way you won’t need to partake in building GHC from&lt;br /&gt;
the source in order to use GHCJS.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&quot;outline-container-sec-2-3&quot; class=&quot;outline-3&quot;&gt;
&lt;h3 id=&quot;sec-2-3&quot;&gt;&lt;span class=&quot;section-number-3&quot;&gt;2.3&lt;/span&gt; Communicating with the VM&lt;/h3&gt;
&lt;div id=&quot;text-2-3&quot; class=&quot;outline-text-3&quot;&gt;
&lt;p&gt;
After you’ve finished with the initial setup you should be able just&lt;br /&gt;
to
&lt;/p&gt;
&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-sh&quot;&gt;vagrant ssh
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
in your new vm and start messing around.
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;ghcjs&lt;/code&gt; command is available to you and Vagrant kindly forwards the&lt;br /&gt;
3000 port on the VM to the local 3030 port, allowing you to run web&lt;br /&gt;
servers like &lt;code&gt;warp&lt;/code&gt; on the VM and accessing them locally.
&lt;/p&gt;
&lt;p&gt;
You can access your local project directory under &lt;code&gt;/vagrant&lt;/code&gt; in VM:
&lt;/p&gt;
&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-sh&quot;&gt;$ ls /vagrant
keys  manifests  modules  outputs  README.rst  Vagrantfile
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
However, copying file back-and-forth is not a perfect solution. I&lt;br /&gt;
recommend setting up a sshfs filesystem (&lt;i&gt;Note: if you are on OSX,&lt;br /&gt;
don’t forget to install fuse4x kernel extension&lt;/i&gt;):
&lt;/p&gt;
&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-sh&quot;&gt;$ vagrant ssh-config
  Host default
    HostName 127.0.0.1
    User vagrant
    Port 2222
    UserKnownHostsFile /dev/null
    StrictHostKeyChecking no
    PasswordAuthentication no
    IdentityFile &lt;span style=&quot;color: #b5bd68;&quot;&gt;&quot;/Users/dan/.vagrant.d/insecure_private_key&quot;&lt;/span&gt;
    IdentitiesOnly yes
    LogLevel FATAL
$ sshfs vagrant@localhost:/home/vagrant ../vm -p2222 -oreconnect,defer_permissions,negative_vncache,&lt;span style=&quot;color: #cc6666;&quot;&gt;volname&lt;/span&gt;=ghcjs,&lt;span style=&quot;color: #cc6666;&quot;&gt;IdentityFile&lt;/span&gt;=~/.vagrant.d/insecure_private_key 
$ ls ../vm
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
When you are done you can just &lt;code&gt;umount ../vm&lt;/code&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&quot;outline-container-sec-3&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-3&quot;&gt;&lt;span class=&quot;section-number-2&quot;&gt;3&lt;/span&gt; Compiling other packages&lt;/h2&gt;
&lt;div id=&quot;text-3&quot; class=&quot;outline-text-2&quot;&gt;
&lt;p&gt;
Since the &lt;code&gt;diagrams&lt;/code&gt; package on Hackage depends on the older version&lt;br /&gt;
of base we are going to use the latest version from Git:
&lt;/p&gt;
&lt;pre class=&quot;example&quot;&gt;mkdir dia; cd dia
git clone git://github.com/diagrams/diagrams-core.git
cd diagram-core &amp;amp;&amp;amp; cabal install &amp;amp;&amp;amp; cd ..

cabal unpack active
cd active-0.1*
cat &amp;gt;version.patch &amp;lt;&amp;lt;EOF
--- active.cabal        2013-06-12 12:58:40.082914214 +0000
+++ active.cabal.new    2013-06-12 12:58:31.029465815 +0000
@@ -19,7 +19,7 @@

 library
   exposed-modules:     Data.Active
-  build-depends:       base &amp;gt;= 4.0 &amp;amp;&amp;amp; &amp;lt; 4.7,
+  build-depends:       base &amp;gt;= 4.0 &amp;amp;&amp;amp; &amp;lt; 4.8,
                        array &amp;gt;= 0.3 &amp;amp;&amp;amp; &amp;lt; 0.5,
                        semigroups &amp;gt;= 0.1 &amp;amp;&amp;amp; &amp;lt; 0.10,
                        semigroupoids &amp;gt;= 1.2 &amp;amp;&amp;amp; &amp;lt; 3.1,
@@ -31,7 +31,7 @@
 test-suite active-tests
     type:              exitcode-stdio-1.0
     main-is:           active-tests.hs
-    build-depends:     base &amp;gt;= 4.0 &amp;amp;&amp;amp; &amp;lt; 4.7,
+    build-depends:     base &amp;gt;= 4.0 &amp;amp;&amp;amp; &amp;lt; 4.8,
                        array &amp;gt;= 0.3 &amp;amp;&amp;amp; &amp;lt; 0.5,
                        semigroups &amp;gt;= 0.1 &amp;amp;&amp;amp; &amp;lt; 0.10,
                        semigroupoids &amp;gt;= 1.2 &amp;amp;&amp;amp; &amp;lt; 3.1,
EOF
patch active.cabal &amp;lt; version.patch
cabal install
cd ..

git clone git://github.com/diagrams/diagrams-lib.git
cd diagrams-lib &amp;amp;&amp;amp; cabal install &amp;amp;&amp;amp; cd ..

git clone git://github.com/diagrams/diagrams-svg.git
cd diagram-svg &amp;amp;&amp;amp; cabal install &amp;amp;&amp;amp; cd ..
&lt;/pre&gt;
&lt;p&gt;
Other packages I had to install already had their Hackage versions&lt;br /&gt;
updated.
&lt;/p&gt;
&lt;p&gt;
Now you can try to build a test diagram to see that everything works
&lt;/p&gt;
&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-haskell&quot;&gt;&lt;span style=&quot;color: #b294bb;&quot;&gt;module&lt;/span&gt; &lt;span style=&quot;color: #f0c674;&quot;&gt;Main&lt;/span&gt; &lt;span style=&quot;color: #b294bb;&quot;&gt;where&lt;/span&gt;

&lt;span style=&quot;color: #b294bb;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f0c674;&quot;&gt;Diagrams.Prelude&lt;/span&gt;
&lt;span style=&quot;color: #b294bb;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f0c674;&quot;&gt;Diagrams.Backend.SVG.CmdLine&lt;/span&gt;

&lt;span style=&quot;color: #81a2be;&quot;&gt;d&lt;/span&gt; &lt;span style=&quot;color: #cc6666;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: #f0c674;&quot;&gt;Diagram&lt;/span&gt; &lt;span style=&quot;color: #f0c674;&quot;&gt;SVG&lt;/span&gt; &lt;span style=&quot;color: #f0c674;&quot;&gt;R2&lt;/span&gt;
&lt;span style=&quot;color: #81a2be;&quot;&gt;d&lt;/span&gt; &lt;span style=&quot;color: #cc6666;&quot;&gt;=&lt;/span&gt; square 20 &lt;span style=&quot;color: #cc6666;&quot;&gt;#&lt;/span&gt; lw 0&lt;span style=&quot;color: #cc6666;&quot;&gt;.&lt;/span&gt;5
              &lt;span style=&quot;color: #cc6666;&quot;&gt;#&lt;/span&gt; fc black
              &lt;span style=&quot;color: #cc6666;&quot;&gt;#&lt;/span&gt; lc green
              &lt;span style=&quot;color: #cc6666;&quot;&gt;#&lt;/span&gt; dashing [0&lt;span style=&quot;color: #cc6666;&quot;&gt;.&lt;/span&gt;2,0&lt;span style=&quot;color: #cc6666;&quot;&gt;.&lt;/span&gt;2] 0

&lt;span style=&quot;color: #81a2be;&quot;&gt;main&lt;/span&gt; &lt;span style=&quot;color: #cc6666;&quot;&gt;=&lt;/span&gt; defaultMain (pad 1&lt;span style=&quot;color: #cc6666;&quot;&gt;.&lt;/span&gt;1 d)
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
then you can compile and run it
&lt;/p&gt;
&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-sh&quot;&gt;ghc --make Test.hs 
./Test -w 400 -o /vagrant/test.svg
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a href=&quot;http://parenz.files.wordpress.com/2013/06/screen-shot-2013-06-12-at-5-19-03-pm.png&quot;&gt;&lt;img src=&quot;http://parenz.files.wordpress.com/2013/06/screen-shot-2013-06-12-at-5-19-03-pm.png?w=300&amp;amp;h=289&quot; alt=&quot;Screen Shot 2013-06-12 at 5.19.03 PM&quot; height=&quot;289&quot; class=&quot;alignnone size-medium wp-image-47&quot; width=&quot;300&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;
And that’s it!
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&quot;outline-container-sec-4&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-4&quot;&gt;&lt;span class=&quot;section-number-2&quot;&gt;4&lt;/span&gt; Outro&lt;/h2&gt;
&lt;div id=&quot;text-4&quot; class=&quot;outline-text-2&quot;&gt;
&lt;p&gt;
I would also like to note that we are currently polishing the GHCJS&lt;br /&gt;
build process. Luite, especially is working on making ghcjs work (and&lt;br /&gt;
run tests) with &lt;a href=&quot;https://travis-ci.org/&quot;&gt;Travis CI&lt;/a&gt; (it take quite a bit of time to build ghcjs&lt;br /&gt;
and sometimes travis is timeouting) and I am working on tidying up&lt;br /&gt;
the build config.
&lt;/p&gt;
&lt;p&gt;
Stay tuned for more updates.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt; Tagged: &lt;a href=&quot;http://parenz.wordpress.com/tag/diagrams/&quot;&gt;diagrams&lt;/a&gt;, &lt;a href=&quot;http://parenz.wordpress.com/tag/ghcjs/&quot;&gt;ghcjs&lt;/a&gt;, &lt;a href=&quot;http://parenz.wordpress.com/tag/haskell-2/&quot;&gt;haskell&lt;/a&gt;, &lt;a href=&quot;http://parenz.wordpress.com/tag/soc/&quot;&gt;soc&lt;/a&gt; &lt;a href=&quot;http://feeds.wordpress.com/1.0/gocomments/parenz.wordpress.com/49/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/comments/parenz.wordpress.com/49/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;img src=&quot;http://stats.wordpress.com/b.gif?host=parenz.wordpress.com&amp;amp;blog=26722965&amp;amp;post=49&amp;amp;subd=parenz&amp;amp;ref=&amp;amp;feed=1&quot; alt=&quot;&quot; height=&quot;1&quot; border=&quot;0&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Thu, 13 Jun 2013 06:05:11 +0000</pubDate>
</item>
<item>
	<title>Daniil Frumin: Summer of Code</title>
	<guid isPermaLink="false">http://parenz.wordpress.com/?p=42</guid>
	<link>http://parenz.wordpress.com/2013/06/10/soc-2013/</link>
	<description>&lt;p&gt;Hello, everyone!&lt;/p&gt;
&lt;p&gt;I’ve decided to reinstate this blog since I’ve got accepted to this year’s Google Summer of Code program. I’ll blog about my updates, stuff that I’ve been working on and bottlenecks and problems I’ve encountered.&lt;/p&gt;
&lt;p&gt;My project is a pastebin site using diagrams and &lt;a href=&quot;http://weblog.luite.com/wordpress/?p=14&quot;&gt;GHCJS&lt;/a&gt; to generate embeddable interactive widgets and static images/text in case when the pasted code does not require additional interaction. My mentor is Luite Stegeman, and Brent Yorgey and other nice people from the diagrams community has agreed to help.&lt;/p&gt;
&lt;p&gt;I am very excited about this and happy that I’ve got a whole bunch of smart people to help me with this.&lt;/p&gt;
&lt;p&gt;Unfortunately, as we haven’t sorted out a completely safe way to evaluate code coming from 3rd parties, there is no public version hosted anywhere yet. Meanwhile, there is a &lt;a href=&quot;https://github.com/co-dan/interactive-diagrams&quot;&gt;project on GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Hopefully, soon I’ll be able to publish a post about my experience with bootstrapping GHCJS.&lt;br /&gt;
Until then, stay tuned!&lt;/p&gt;
&lt;br /&gt; Tagged: &lt;a href=&quot;http://parenz.wordpress.com/tag/diagrams/&quot;&gt;diagrams&lt;/a&gt;, &lt;a href=&quot;http://parenz.wordpress.com/tag/ghcjs/&quot;&gt;ghcjs&lt;/a&gt;, &lt;a href=&quot;http://parenz.wordpress.com/tag/haskell-2/&quot;&gt;haskell&lt;/a&gt;, &lt;a href=&quot;http://parenz.wordpress.com/tag/interactive-diagrams/&quot;&gt;interactive-diagrams&lt;/a&gt;, &lt;a href=&quot;http://parenz.wordpress.com/tag/soc/&quot;&gt;soc&lt;/a&gt; &lt;a href=&quot;http://feeds.wordpress.com/1.0/gocomments/parenz.wordpress.com/42/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/comments/parenz.wordpress.com/42/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;img src=&quot;http://stats.wordpress.com/b.gif?host=parenz.wordpress.com&amp;amp;blog=26722965&amp;amp;post=42&amp;amp;subd=parenz&amp;amp;ref=&amp;amp;feed=1&quot; alt=&quot;&quot; height=&quot;1&quot; border=&quot;0&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Wed, 12 Jun 2013 14:17:57 +0000</pubDate>
</item>
<item>
	<title>Tom Moertel: Tricks of the trade: Recursion to Iteration, Part 4: The Trampoline</title>
	<guid isPermaLink="true">http://blog.moertel.com/posts/2013-06-12-recursion-to-iteration-4-trampolines.html</guid>
	<link>http://blog.moertel.com/posts/2013-06-12-recursion-to-iteration-4-trampolines.html</link>
	<description>&lt;div class=&quot;info&quot;&gt;Posted on June 12, 2013&lt;/div&gt;
&lt;div class=&quot;tags&quot;&gt;Tags: &lt;a href=&quot;http://blog.moertel.com/tags/programming.html&quot;&gt;programming&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/recursion.html&quot;&gt;recursion&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/iteration.html&quot;&gt;iteration&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/python.html&quot;&gt;python&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/recursion-to-iteration series.html&quot;&gt;recursion-to-iteration series&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/tail calls.html&quot;&gt;tail calls&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/data structures.html&quot;&gt;data structures&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/trampolines.html&quot;&gt;trampolines&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;This is the fourth article in &lt;a href=&quot;http://blog.moertel.com/tags/recursion-to-iteration%20series.html&quot;&gt;a series on converting recursive algorithms into iterative algorithms&lt;/a&gt;. If you haven’t read the earlier articles first, you may want to do so before continuing.&lt;/p&gt;
&lt;p&gt;In &lt;a href=&quot;http://blog.moertel.com/posts/2013-05-11-recursive-to-iterative.html&quot;&gt;the first article of our series&lt;/a&gt;, we showed that if you can convert an algorithm’s recursive calls into tail calls, you can eliminate those tail calls to create an iterative version of the algorithm using The Simple Method. In this article, we’ll look at another way to eliminate tail calls: the &lt;em&gt;trampoline&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The idea behind the trampoline is this: before making a tail call, manually remove the current execution frame from the stack, eliminating stack build-up.&lt;/p&gt;
&lt;h3 id=&quot;execution-frames-and-the-stack&quot;&gt;Execution frames and the stack&lt;/h3&gt;
&lt;p&gt;To understand why we might want to manually remove an execution frame, let’s back up and think about what happens when we call a function. The language runtime needs some place to store housekeeping information and any local variables the function may use, so it allocates a new execution frame on the stack. Then it turns control over to the function. When the function is done, it executes a &lt;code&gt;return&lt;/code&gt; statement. This statement tells the runtime to remove the execution frame from the stack and to give control (and any result) back to the caller.&lt;/p&gt;
&lt;p&gt;But what if the function doesn’t return right away? What if it makes another function call instead? In that case, the runtime must create a new execution frame for &lt;em&gt;that&lt;/em&gt; call and push it onto the stack, on top of the current frame. If the function ends up calling itself many times recursively, each call will add another frame to the stack, and pretty soon we will have eaten up a lot of stack space.&lt;/p&gt;
&lt;h3 id=&quot;eliminating-stack-build-up&quot;&gt;Eliminating stack build-up&lt;/h3&gt;
&lt;p&gt;To avoid this problem, some programming languages guarantee that they will recycle the current execution frame whenever a function makes a tail call. That is, if the function calls some other function (or itself recursively) and just returns that function’s result verbatim, that’s a tail call. In that case, the runtime will recycle the current function’s execution frame before transferring control to the other function, making it so that the other function will return its result directly to the original function’s caller. This process is called &lt;em&gt;tail-call elimination&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;But in languages like Python that don’t offer tail-call elimination, every call, even if it’s a tail call, pushes a new frame onto the stack. So if we want to prevent stack build-up, we must somehow eliminate the current frame from the stack ourselves, before making a tail call.&lt;/p&gt;
&lt;p&gt;But how? The only obvious way to eliminate the current frame is to &lt;code&gt;return&lt;/code&gt; to our caller. If we’re to make this work, then, the caller must be willing to help us out. That’s where the trampoline comes in. It’s our co-conspirator in the plot to eliminate stack build-up.&lt;/p&gt;
&lt;h3 id=&quot;the-trampoline&quot;&gt;The trampoline&lt;/h3&gt;
&lt;p&gt;Here’s what the trampoline does:&lt;/p&gt;
&lt;ol style=&quot;&quot;&gt;
&lt;li&gt;It calls our function &lt;code&gt;f&lt;/code&gt;, making itself the current caller.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;f&lt;/code&gt; wants to make a recursive tail call to itself, it returns the instruction &lt;code&gt;call(f)(*args, **kwds)&lt;/code&gt;. The language runtime dutifully removes the current execution frame from the stack and returns control to the trampoline, passing it the instruction.&lt;/li&gt;
&lt;li&gt;The trampoline interprets the instruction and calls &lt;code&gt;f&lt;/code&gt; back, giving it the supplied arguments, and again making itself the caller.&lt;/li&gt;
&lt;li&gt;This process repeats until &lt;code&gt;f&lt;/code&gt; wants to return a final result &lt;code&gt;z&lt;/code&gt;; then it returns the new instruction &lt;code&gt;result(z)&lt;/code&gt; instead. As before, the runtime removes the current execution frame from the stack and returns control to the trampoline.&lt;/li&gt;
&lt;li&gt;But now when the trampoline interprets the new instruction it will return &lt;code&gt;z&lt;/code&gt; to &lt;em&gt;its&lt;/em&gt; caller, ending the trampoline dance.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Now you can see how the trampoline got its name. When our function uses a &lt;code&gt;return&lt;/code&gt; statement to remove its own execution frame from the stack, the trampoline bounces control back to it with new arguments.&lt;/p&gt;
&lt;p&gt;Here’s a simple implementation. First, we will encode our instructions to the trampoline as triples. We’ll let &lt;code&gt;call(f)(*args, **kwds)&lt;/code&gt; be the triple &lt;code&gt;(f, args, kwds)&lt;/code&gt;, and &lt;code&gt;result(z)&lt;/code&gt; be the triple &lt;code&gt;(None, z, None)&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; call(f):
    &lt;span class=&quot;co&quot;&gt;&quot;&quot;&quot;Instruct trampoline to call f with the args that follow.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; g(*args, **kwds):
        &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; f, args, kwds
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; g

&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; result(value):
    &lt;span class=&quot;co&quot;&gt;&quot;&quot;&quot;Instruct trampoline to stop iterating and return a value.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;, value, &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we’ll create a decorator to wrap a function with a trampoline that will interpret the instructions that the function returns:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;ch&quot;&gt;import&lt;/span&gt; functools

&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; with_trampoline(f):
    &lt;span class=&quot;co&quot;&gt;&quot;&quot;&quot;Wrap a trampoline around a function that expects a trampoline.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;ot&quot;&gt;@functools.wraps&lt;/span&gt;(f)
    &lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; g(*args, **kwds):
        h = f
        &lt;span class=&quot;co&quot;&gt;# the trampoline&lt;/span&gt;
        &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; h is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
            h, args, kwds = h(*args, **kwds)
        &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; args
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; g&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that the trampoline boils down to three lines:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; h is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
    h, args, kwds = h(*args, **kwds)
&lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; args&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Basically, the trampoline keeps calling whatever function is in &lt;code&gt;h&lt;/code&gt; until that function returns a &lt;code&gt;result(z)&lt;/code&gt; instruction, at which time the loop exits and &lt;code&gt;z&lt;/code&gt; is returned. The original recursive tail calls have been boiled down to a &lt;code&gt;while&lt;/code&gt; loop. Recursion has become iteration.&lt;/p&gt;
&lt;h3 id=&quot;example-factorial&quot;&gt;Example: factorial&lt;/h3&gt;
&lt;p&gt;To see how we might use this implementation, let’s return to the factorial example from &lt;a href=&quot;http://blog.moertel.com/posts/2013-05-11-recursive-to-iterative.html&quot;&gt;the first article in our series&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; factorial(n):
    &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; n &amp;lt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;:
        &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; n * factorial(n - &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Step one, as before, is to tail-convert the lone recursive call:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt; &lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; factorial(n, acc=&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;):
     &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; n &amp;lt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;:
         &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; acc
     &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; factorial(n - &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, acc * n)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can create an equivalent function that uses trampoline idioms:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; trampoline_factorial(n, acc=&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;):
    &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; n &amp;lt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;:
        &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; result(acc)
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; call(trampoline_factorial)(n - &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, n * acc)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note how the &lt;code&gt;return&lt;/code&gt; statements have been transformed.&lt;/p&gt;
&lt;p&gt;Finally, we can wrap this function with a trampoline to get a callable version that we can use just like the original:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;factorial = with_trampoline(trampoline_factorial)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s take it for a spin:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&amp;gt;&amp;gt;&amp;gt; factorial(&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;)
&lt;span class=&quot;dv&quot;&gt;120&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To really see what’s going on, be sure to use the Online Python Tutor’s visualizer to step through the original, tail-recursive, and trampoline versions of the function. Just open this link: &lt;a href=&quot;http://www.pythontutor.com/visualize.html#code=%23+our+trampoline+library%0A%0Aimport+functools%0A%0Adef+call(f)%3A%0A++++%22%22%22Instruct+trampoline+to+call+f+with+the+args+that+follow.%22%22%22%0A++++def+g(*args,+**kwds)%3A%0A++++++++return+f,+args,+kwds%0A++++return+g%0A%0Adef+result(value)%3A%0A++++%22%22%22Instruct+trampoline+to+stop+iterating+and+return+a+value.%22%22%22%0A++++return+None,+value,+None%0A%0Adef+with_trampoline(f)%3A%0A++++%22%22%22Wrap+a+trampoline+around+a+function+that+expects+a+trampoline.%22%22%22%0A++++%40functools.wraps(f)%0A++++def+g(*args,+**kwds)%3A%0A++++++++h+%3D+f%0A++++++++%23+the+trampoline%0A++++++++while+h+is+not+None%3A%0A++++++++++++h,+args,+kwds+%3D+h(*args,+**kwds)%0A++++++++return+args%0A++++return+g%0A%0A%0A%23+original+recursive+version+of+factorial+function%0A%0Adef+factorial(n)%3A%0A++++if+n+%3C+2%3A%0A++++++++return+1%0A++++return+n+*+factorial(n+-+1)%0A%0Aprint+factorial(5)%0A%0A%0A%23+tail-call+recursive+version%0A%0Adef+factorial(n,+acc%3D1)%3A%0A+++++if+n+%3C+2%3A%0A+++++++++return+acc%0A+++++return+factorial(n+-+1,+acc+*+n)%0A%0Aprint+factorial(5)%0A%0A%0A%23+trampoline-based+tail-call+version+(%3D+iterative)%0A%0Adef+trampoline_factorial(n,+acc%3D1)%3A%0A++++if+n+%3C+2%3A%0A++++++++return+result(acc)%0A++++return+call(trampoline_factorial)(n+-+1,+n+*+acc)%0A%0Afactorial+%3D+with_trampoline(trampoline_factorial)%0A%0Aprint+factorial(5)%0A&amp;amp;mode=display&amp;amp;cumulative=false&amp;amp;heapPrimitives=false&amp;amp;drawParentPointers=false&amp;amp;textReferences=false&amp;amp;showOnlyOutputs=false&amp;amp;py=2&amp;amp;curInstr=0&quot;&gt;Visualize the execution&lt;/a&gt;. (ProTip: use a new tab.)&lt;/p&gt;
&lt;h3 id=&quot;why-use-the-trampoline&quot;&gt;Why use the trampoline?&lt;/h3&gt;
&lt;p&gt;As I mentioned at the beginning of this article, if you can convert a function’s recursive calls into tail calls – which you must do to use a trampoline – you can also use the Simple Method on the function. For example, here’s what the Simple Method does to our original &lt;code&gt;factorial&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; factorial(n, acc=&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;):
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; n &amp;gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;:
        (n, acc) = (n - &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, acc * n)
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; acc&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This version is simpler and more efficient than the trampoline version. So why not use the Simple Method always?&lt;/p&gt;
&lt;p&gt;The answer is that the Simple Method is tricky to apply to functions that make tail calls from within loops. Recall that it introduces a loop around a function’s body and replaces recursive tail calls with &lt;code&gt;continue&lt;/code&gt; statements. But if the function already has its own loops, replacing a tail call within one of them with a &lt;code&gt;continue&lt;/code&gt; statement will restart that inner loop instead of the whole-body loop, as desired. In that case, you must add condition flags to make sure the right loop gets restarted, and that gets old fast. Then, using a trampoline may be a win.&lt;/p&gt;
&lt;p&gt;That said, I almost never use trampolines. Getting a function into tail-call form is nine tenths of the battle. If I’ve gone that far already, I’ll usually go the rest of the way to get a tight, iterative version.&lt;/p&gt;
&lt;p&gt;Why, then, did we make this effort to understand the trampoline? Two reasons. First, it’s semi-common in programming lore, so it’s best to know about it. Second, it’s a stepping stone to a more-general, more-powerful technique: &lt;em&gt;continuation-passing-style expressions&lt;/em&gt;. That’s our subject for next time.&lt;/p&gt;
&lt;p&gt;In the meantime, if you want another take on trampolines in Python, Kyle Miller wrote a nice article on the subject: &lt;a href=&quot;http://web.mit.edu/kmill/www/programming/tailcall.html&quot;&gt;Tail call recursion in Python&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Thanks for reading! As always, if you have questions or comments, please leave a comment on the blog or hit me at &lt;a href=&quot;https://twitter.com/tmoertel&quot;&gt;@tmoertel&lt;/a&gt;.&lt;/p&gt;</description>
	<pubDate>Wed, 12 Jun 2013 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Mike Izbicki: HLearn’s code is shorter and clearer than Weka’s</title>
	<guid isPermaLink="false">http://izbicki.me/blog/?p=2520</guid>
	<link>http://izbicki.me/blog/hlearns-code-is-shorter-and-clearer-than-wekas?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=hlearns-code-is-shorter-and-clearer-than-wekas</link>
	<description>&lt;p&gt;&lt;img src=&quot;http://izbicki.me/blog/wp-content/uploads/2013/05/weka-lambda-haskell-300x150.png&quot; alt=&quot;weka-lambda-haskell&quot; height=&quot;120&quot; class=&quot;alignright  wp-image-2478&quot; width=&quot;240&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Haskell code is expressive.  The &lt;a href=&quot;https://github.com/mikeizbicki/HLearn&quot;&gt;HLearn library&lt;/a&gt; uses 6 lines of Haskell to define a function for training a Bayesian classifier; the equivalent code in the &lt;a href=&quot;http://www.cs.waikato.ac.nz/ml/weka/&quot;&gt;Weka library&lt;/a&gt; uses over 100 lines of Java.  That’s a big difference!  In this post, we’ll look at the actual code and see why the Haskell is so much more concise.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;But first, a disclaimer:&lt;/strong&gt;  It is really hard to fairly compare two code bases this way.  In both libraries, there is a lot of supporting code that goes into defining each classifier, and it’s not obvious what code to include and not include.  For example, both libraries implement interfaces to a number of probability distributions, and this code is not contained in the source count.  The Haskell code takes more advantage of this abstraction, so this is one language-agnostic reason why the Haskell code is shorter.  If you think I’m not doing a fair comparison, here’s some links to the full repositories so you can do it yourself:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;line-height: 12px;&quot; class=&quot;Apple-style-span&quot;&gt;&lt;a href=&quot;https://github.com/mikeizbicki/HLearn/blob/master/HLearn-classification/src/HLearn/Models/Classifiers/Bayes.hs&quot;&gt;HLearn’s bayesian classifier source code&lt;/a&gt; (74 lines of code)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://svn.cms.waikato.ac.nz/svn/weka/trunk/weka/src/main/java/weka/classifiers/bayes/NaiveBayes.java&quot;&gt;Weka’s naive bayes source code&lt;/a&gt; (946 lines of code)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span id=&quot;more-2520&quot;&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;The HLearn code&lt;/h3&gt;
&lt;p&gt;HLearn implements training for a &lt;a href=&quot;https://en.wikipedia.org/wiki/Naive_Bayes_classifier&quot;&gt;bayesian classifier&lt;/a&gt; with these six lines of Haskell:&lt;/p&gt;
&lt;pre&gt;newtype Bayes labelIndex dist = Bayes dist
    deriving (Read,Show,Eq,Ord,Monoid,Abelian,Group)

instance (Monoid dist, HomTrainer dist) =&amp;gt; HomTrainer (Bayes labelIndex dist) where
    type Datapoint (Bayes labelIndex dist) = Datapoint dist
    train1dp dp = Bayes $ train1dp dp&lt;/pre&gt;
&lt;p&gt;This code elegantly captures how to train a Bayesian classifier—just train a probability distribution.  Here’s an explanation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The first two lines define the Bayes data type as a wrapper around a distribution.&lt;/li&gt;
&lt;li&gt;The fourth line says that we’re implementing the Bayesian classifier using the HomTrainer type class.  We do this because &lt;strong&gt;the Haskell compiler automatically generates a parallel batch training function, an online training function, and a fast cross-validation function for all HomTrainer instances.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;The fifth line says that our data points have the same type as the underlying distribution.&lt;/li&gt;
&lt;li&gt;The sixth line says that in order to train, just train the corresponding distribution.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We only get the benefits of the HomTrainer type class because the bayesian classifier is a monoid.  But we didn’t even have to specify what the monoid instance for bayesian classifiers looks like!  In this case, it’s automatically derived from the monoid instances for the base distributions using a language extension called &lt;a href=&quot;http://www.haskell.org/ghc/docs/7.6.1/html/users_guide/deriving.html&quot;&gt;GeneralizedNewtypeDeriving&lt;/a&gt;.  For examples of these monoid structures, check out the algebraic structure of the &lt;a href=&quot;http://izbicki.me/blog/gausian-distributions-are-monoids&quot;&gt;normal&lt;/a&gt; and &lt;a href=&quot;http://izbicki.me/blog/the-categorical-distributions-algebraic-structure&quot;&gt;categorical&lt;/a&gt; distributions, or more complex distributions using &lt;a href=&quot;http://izbicki.me/blog/markov-networks-monoids-and-futurama&quot;&gt;Markov networks&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;The Weka code&lt;/h3&gt;
&lt;p&gt;Look for these differences between the HLearn and Weka source:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In Weka we must separately define the online and batch trainers, whereas Haskell derived these for us automatically.&lt;/li&gt;
&lt;li&gt;Weka must perform a variety of error handling that Haskell’s type system takes care of in HLearn.&lt;/li&gt;
&lt;li&gt;The Weka code is tightly coupled to the underlying probability distribution, whereas the Haskell code was generic enough to handle any distribution. This means that while Weka must make the “naive bayes assumption” that all attributes are independent of each other, HLearn can support any dependence structure.&lt;/li&gt;
&lt;li&gt;Weka’s code is made more verbose by for loops and if statements that aren’t necessary for HLearn.&lt;/li&gt;
&lt;li&gt;The Java code requires extensive comments to maintain readability, but the Haskell code is simple enough to be self-documenting (at least once you know how to read Haskell).&lt;/li&gt;
&lt;li&gt;Weka does not have parallel training, fast cross-validation, data point subtraction, or weighted data points, but HLearn does.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;/**
   * Generates the classifier.
   *
   * @param instances set of instances serving as training data 
   * @exception Exception if the classifier has not been generated 
   * successfully
   */
  public void buildClassifier(Instances instances) throws Exception {

    // can classifier handle the data?
    getCapabilities().testWithFail(instances);

    // remove instances with missing class
    instances = new Instances(instances);
    instances.deleteWithMissingClass();

    m_NumClasses = instances.numClasses();

    // Copy the instances
    m_Instances = new Instances(instances);

    // Discretize instances if required
    if (m_UseDiscretization) {
      m_Disc = new weka.filters.supervised.attribute.Discretize();
      m_Disc.setInputFormat(m_Instances);
      m_Instances = weka.filters.Filter.useFilter(m_Instances, m_Disc);
    } else {
      m_Disc = null;
    }

    // Reserve space for the distributions
    m_Distributions = new Estimator[m_Instances.numAttributes() - 1]
      [m_Instances.numClasses()];
    m_ClassDistribution = new DiscreteEstimator(m_Instances.numClasses(), 
                                                true);
    int attIndex = 0;
    Enumeration enu = m_Instances.enumerateAttributes();
    while (enu.hasMoreElements()) {
      Attribute attribute = (Attribute) enu.nextElement();

      // If the attribute is numeric, determine the estimator 
      // numeric precision from differences between adjacent values
      double numPrecision = DEFAULT_NUM_PRECISION;
      if (attribute.type() == Attribute.NUMERIC) {
	m_Instances.sort(attribute);
	if ( (m_Instances.numInstances() &amp;gt; 0)
	    &amp;amp;&amp;amp; !m_Instances.instance(0).isMissing(attribute)) {
	  double lastVal = m_Instances.instance(0).value(attribute);
	  double currentVal, deltaSum = 0;
	  int distinct = 0;
	  for (int i = 1; i &amp;lt; m_Instances.numInstances(); i++) { 	    
            Instance currentInst = m_Instances.instance(i); 	    
              if (currentInst.isMissing(attribute)) {
                break; 	    
              }
 	    currentVal = currentInst.value(attribute);
 	    if (currentVal != lastVal) {
 	      deltaSum += currentVal - lastVal;
 	      lastVal = currentVal;
 	      distinct++;
 	    }
 	  }
 	  if (distinct &amp;gt; 0) {
	    numPrecision = deltaSum / distinct;
	  }
	}
      }

      for (int j = 0; j &amp;lt; m_Instances.numClasses(); j++) {
	switch (attribute.type()) {
	case Attribute.NUMERIC: 
	  if (m_UseKernelEstimator) {
	    m_Distributions[attIndex][j] = 
	      new KernelEstimator(numPrecision);
	  } else {
	    m_Distributions[attIndex][j] = 
	      new NormalEstimator(numPrecision);
	  }
	  break;
	case Attribute.NOMINAL:
	  m_Distributions[attIndex][j] = 
	    new DiscreteEstimator(attribute.numValues(), true);
	  break;
	default:
	  throw new Exception(&quot;Attribute type unknown to NaiveBayes&quot;);
	}
      }
      attIndex++;
    }

    // Compute counts
    Enumeration enumInsts = m_Instances.enumerateInstances();
    while (enumInsts.hasMoreElements()) {
      Instance instance = 
	(Instance) enumInsts.nextElement();
      updateClassifier(instance);
    }

    // Save space
    m_Instances = new Instances(m_Instances, 0);
  }&lt;/pre&gt;
&lt;p&gt;And the code for online learning is:&lt;/p&gt;
&lt;pre&gt;/**
   * Updates the classifier with the given instance.
   *
   * @param instance the new training instance to include in the model 
   * @exception Exception if the instance could not be incorporated in
   * the model.
   */
  public void updateClassifier(Instance instance) throws Exception {

    if (!instance.classIsMissing()) {
      Enumeration enumAtts = m_Instances.enumerateAttributes();
      int attIndex = 0;
      while (enumAtts.hasMoreElements()) {
	Attribute attribute = (Attribute) enumAtts.nextElement();
	if (!instance.isMissing(attribute)) {
	  m_Distributions[attIndex][(int)instance.classValue()].
            addValue(instance.value(attribute), instance.weight());
	}
	attIndex++;
      }
      m_ClassDistribution.addValue(instance.classValue(),
                                   instance.weight());
    }
  }&lt;/pre&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;Every algorithm implemented in HLearn uses similarly concise code.  I invite you to &lt;a href=&quot;https://github.com/mikeizbicki/HLearn/&quot;&gt;browse the repository&lt;/a&gt; and see for yourself.  The most complicated algorithm is for Markov chains which use only &lt;a href=&quot;https://github.com/mikeizbicki/HLearn/blob/master/HLearn-markov/src/HLearn/Models/Markov/MarkovChain.hs&quot;&gt;6 lines for training, and about 20 for defining the Monoid&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can expect lots of tutorials on how to incorporate the HLearn library into Haskell programs over the next few months.&lt;/p&gt;
&lt;p&gt;Subscribe to the &lt;a href=&quot;http://izbicki.me/blog/feed&quot;&gt;RSS feed&lt;/a&gt; to stay tuned!&lt;/p&gt;
 &lt;img src=&quot;http://izbicki.me/blog/?feed-stats-post-id=2520&quot; style=&quot;display: none;&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Tue, 11 Jun 2013 17:50:09 +0000</pubDate>
</item>
<item>
	<title>Philip Wadler: Iain Banks</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-9757377.post-7244538253070285153</guid>
	<link>http://wadler.blogspot.com/2013/06/iain-banks.html</link>
	<description>&lt;div style=&quot;clear: both; text-align: center;&quot; class=&quot;separator&quot;&gt;&lt;a style=&quot;margin-left: 1em; margin-right: 1em;&quot; href=&quot;http://1.bp.blogspot.com/-0cLAVwyWSKU/Ubbaw7r7N9I/AAAAAAAACKI/DItsN5fXHQE/s1600/scotsbanks-003.jpg&quot;&gt;&lt;img src=&quot;http://1.bp.blogspot.com/-0cLAVwyWSKU/Ubbaw7r7N9I/AAAAAAAACKI/DItsN5fXHQE/s400/scotsbanks-003.jpg&quot; title=&quot;Writer Iain Banks seen in front of the Scottish Parliament Building at Holyrood in Edinburgh. Photograph: Murdo MacLeod (Observer)&quot; height=&quot;240&quot; width=&quot;400&quot; alt=&quot;Writer Iain Banks seen in front of the Scottish Parliament Building at Holyrood in Edinburgh. Photograph: Murdo MacLeod (Observer)&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/div&gt;In April, Iain Banks discovered he had cancer of the gall bladder, and proposed to his girl friend by requesting she `do me the honour of becoming my widow'.  Yesterday his death was announced.  In his honour, here is something he &lt;a href=&quot;http://www.guardian.co.uk/culture/2011/aug/28/scottish-independence-snp-iain-banks?INTCMP=SRCH&quot;&gt;wrote for the Observer&lt;/a&gt;.  See also this &lt;a href=&quot;http://www.guardian.co.uk/books/2013/jun/10/iain-banks-ken-macleod-science-fiction&quot;&gt;tribute from Ken McLeod&lt;/a&gt;.&lt;br /&gt;&lt;blockquote class=&quot;tr_bq&quot;&gt;&lt;div style=&quot;clear: both; text-align: center;&quot; class=&quot;separator&quot;&gt;&lt;a style=&quot;margin-left: 1em; margin-right: 1em;&quot; href=&quot;http://1.bp.blogspot.com/-0cLAVwyWSKU/Ubbaw7r7N9I/AAAAAAAACKI/DItsN5fXHQE/s1600/scotsbanks-003.jpg&quot;&gt;&lt;/a&gt;&lt;/div&gt;These days, I support the idea of an independent Scotland. It's with a  heavy heart in some ways; I think I'd still sacrifice an independent  Scotland for a socialist UK, but… I can't really see that happening.  What I can imagine is England continuing to turn to the right and  eventually leaving the EU altogether.&lt;br /&gt;&lt;br /&gt;Scotland, though, could have  a viable future either as a completely independent country or – more  likely – within Europe. The European ideal is taking a battering right  now, certainly, and the gloss has come off comparing our prospects to  Ireland's or Iceland's, but it remains both possible and plausible that  Scotland could become a transparent, low-inequality society on the  Scandinavian model, with fair, non-regressive taxes, strong unions, a  nuclear-free policy, a non-punitive tertiary education system,  enlightened social policies in general and long-term support for green  energy programmes.&lt;br /&gt;&lt;br /&gt;We'd need to make sure our banks were small  enough to fail, and there are problems of poverty, ill health and  religious tribalism that will take decades to overcome. But with the  advantages and attractions that Scotland already has, and, more  importantly, taking into account the morale boost, the sheer  energisation of a whole people that would come about because we would  finally have our destiny at least largely back in our own hands again – I  think we could do it.&lt;br /&gt;&lt;br /&gt;And that we should.&lt;/blockquote&gt;</description>
	<pubDate>Tue, 11 Jun 2013 08:24:56 +0000</pubDate>
	<author>noreply@blogger.com (Philip Wadler)</author>
</item>
<item>
	<title>Theory Lunch (Institute of Cybernetics, Tallinn): When does an endofunctor derive from an adjunction?</title>
	<guid isPermaLink="false">http://theorylunch.wordpress.com/?p=768</guid>
	<link>http://theorylunch.wordpress.com/2013/05/30/when-does-an-endofunctor-derive-from-an-adjunction/</link>
	<description>&lt;p&gt;This is the first of two talks based on Andrea Schalk’s very good introduction to monads, which can be retrieved &lt;a href=&quot;http://www.cs.man.ac.uk/~schalk/notes/monads.pdf&quot; target=&quot;_blank&quot; title=&quot;http://www.cs.man.ac.uk/~schalk/notes/monads.pdf‎&quot;&gt;HERE&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In the following, if &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt; is a category, we indicate by &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%7C%5Cmathcal%7BC%7D%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;|\mathcal{C}|&quot; class=&quot;latex&quot; title=&quot;|\mathcal{C}|&quot; /&gt; the collection of objects of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt;, and by &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D%28A%2CB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}(A,B)&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}(A,B)&quot; /&gt; the collection of morphisms in &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt; from &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A&quot; class=&quot;latex&quot; title=&quot;A&quot; /&gt; to &lt;img src=&quot;http://s0.wp.com/latex.php?latex=B&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;B&quot; class=&quot;latex&quot; title=&quot;B&quot; /&gt;.&lt;/p&gt;
&lt;p&gt;As we know, there are two basic ways of defining an adjunction: &lt;span id=&quot;more-768&quot;&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Definition 1.&lt;/strong&gt; Let &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BD%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{D}&quot; class=&quot;latex&quot; title=&quot;\mathcal{D}&quot; /&gt; be categories; let &lt;img src=&quot;http://s0.wp.com/latex.php?latex=F+%3A+%5Cmathcal%7BC%7D+%5Cto+%5Cmathcal%7BD%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F : \mathcal{C} \to \mathcal{D}&quot; class=&quot;latex&quot; title=&quot;F : \mathcal{C} \to \mathcal{D}&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=F+%3A+%5Cmathcal%7BD%7D+%5Cto+%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F : \mathcal{D} \to \mathcal{C}&quot; class=&quot;latex&quot; title=&quot;F : \mathcal{D} \to \mathcal{C}&quot; /&gt; be functors. An &lt;em&gt;adjunction&lt;/em&gt; from &lt;img src=&quot;http://s0.wp.com/latex.php?latex=F&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F&quot; class=&quot;latex&quot; title=&quot;F&quot; /&gt; to &lt;img src=&quot;http://s0.wp.com/latex.php?latex=G&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G&quot; class=&quot;latex&quot; title=&quot;G&quot; /&gt;, written &lt;img src=&quot;http://s0.wp.com/latex.php?latex=F+%5Cdashv+G&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F \dashv G&quot; class=&quot;latex&quot; title=&quot;F \dashv G&quot; /&gt;, is a quadruple &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2CG%2C%5Ceta%2C%5Cvarepsilon%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F,G,\eta,\varepsilon)&quot; class=&quot;latex&quot; title=&quot;(F,G,\eta,\varepsilon)&quot; /&gt; where &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Ceta%3A+%5Cmathrm%7BId%7D_%5Cmathcal%7BC%7D+%5Cto+GF&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\eta: \mathrm{Id}_\mathcal{C} \to GF&quot; class=&quot;latex&quot; title=&quot;\eta: \mathrm{Id}_\mathcal{C} \to GF&quot; /&gt; (the &lt;em&gt;unit&lt;/em&gt; of the adjunction) and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cvarepsilon%3A+FG+%5Cto+%5Cmathrm%7BId%7D_%5Cmathcal%7BD%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\varepsilon: FG \to \mathrm{Id}_\mathcal{D}&quot; class=&quot;latex&quot; title=&quot;\varepsilon: FG \to \mathrm{Id}_\mathcal{D}&quot; /&gt; (the &lt;em&gt;counit&lt;/em&gt;) are natural transformations such that , for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A+%5Cin+%7C%5Cmathcal%7BC%7D%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A \in |\mathcal{C}|&quot; class=&quot;latex&quot; title=&quot;A \in |\mathcal{C}|&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=S+%5Cin+%7C%5Cmathcal%7BD%7D%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;S \in |\mathcal{D}|&quot; class=&quot;latex&quot; title=&quot;S \in |\mathcal{D}|&quot; /&gt;, &lt;img src=&quot;http://s0.wp.com/latex.php?latex=G%5Cvarepsilon_S+%5Ccirc+%5Ceta_%7BGS%7D+%3D+%5Cmathrm%7Bid%7D_%7BGS%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G\varepsilon_S \circ \eta_{GS} = \mathrm{id}_{GS}&quot; class=&quot;latex&quot; title=&quot;G\varepsilon_S \circ \eta_{GS} = \mathrm{id}_{GS}&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cvarepsilon_%7BFA%7D+%5Ccirc+F%5Ceta_%7BA%7D+%3D+%5Cmathrm%7Bid%7D_%7BFA%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\varepsilon_{FA} \circ F\eta_{A} = \mathrm{id}_{FA}&quot; class=&quot;latex&quot; title=&quot;\varepsilon_{FA} \circ F\eta_{A} = \mathrm{id}_{FA}&quot; /&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Definition 2.&lt;/strong&gt; Let &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BD%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{D}&quot; class=&quot;latex&quot; title=&quot;\mathcal{D}&quot; /&gt; be categories. We call &lt;em&gt;adjunction quadruple&lt;/em&gt; a quadruple &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2C+G%2C+%5Ceta%2C+%28%5Ccdot%29%5E%5Csharp%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F, G, \eta, (\cdot)^\sharp)&quot; class=&quot;latex&quot; title=&quot;(F, G, \eta, (\cdot)^\sharp)&quot; /&gt; such that:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=G+%3A+%5Cmathcal%7BD%7D+%5Cto+%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G : \mathcal{D} \to \mathcal{C}&quot; class=&quot;latex&quot; title=&quot;G : \mathcal{D} \to \mathcal{C}&quot; /&gt; is a functor,&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=F+%3A+%7C%5Cmathcal%7BC%7D%7C+%5Cto+%7C%5Cmathcal%7BD%7D%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F : |\mathcal{C}| \to |\mathcal{D}|&quot; class=&quot;latex&quot; title=&quot;F : |\mathcal{C}| \to |\mathcal{D}|&quot; /&gt; is a mapping, and&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Ceta&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\eta&quot; class=&quot;latex&quot; title=&quot;\eta&quot; /&gt; associates to every object &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A&quot; class=&quot;latex&quot; title=&quot;A&quot; /&gt; a morphism &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Ceta_A+%3A+A+%5Cto+GFA&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\eta_A : A \to GFA&quot; class=&quot;latex&quot; title=&quot;\eta_A : A \to GFA&quot; /&gt; so that&lt;/li&gt;
&lt;li&gt;for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%3A+A+%5Cto+GS&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f : A \to GS&quot; class=&quot;latex&quot; title=&quot;f : A \to GS&quot; /&gt; there exists a unique &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%5E%5Csharp+%3A+FA+%5Cto+S&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f^\sharp : FA \to S&quot; class=&quot;latex&quot; title=&quot;f^\sharp : FA \to S&quot; /&gt; such that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=Gf%5E%5Csharp+%5Ccirc+%5Ceta_A+%3D+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;Gf^\sharp \circ \eta_A = f&quot; class=&quot;latex&quot; title=&quot;Gf^\sharp \circ \eta_A = f&quot; /&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The two definitions above are equivalent in the following sense. If &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2C+G%2C+%5Ceta%2C+%5Cvarepsilon%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F, G, \eta, \varepsilon)&quot; class=&quot;latex&quot; title=&quot;(F, G, \eta, \varepsilon)&quot; /&gt; is an adjunction according to Definition 1, and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%5E%5Csharp+%3D+%5Cvarepsilon_S+%5Ccirc+Ff&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f^\sharp = \varepsilon_S \circ Ff&quot; class=&quot;latex&quot; title=&quot;f^\sharp = \varepsilon_S \circ Ff&quot; /&gt;, then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2C+G%2C+%5Ceta%2C+%28%5Ccdot%29%5E%5Csharp%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F, G, \eta, (\cdot)^\sharp)&quot; class=&quot;latex&quot; title=&quot;(F, G, \eta, (\cdot)^\sharp)&quot; /&gt; is an adjunction quadruple according to Definition 2. On the other hand, if &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2C+G%2C+%5Ceta%2C+%28%5Ccdot%29%5E%5Cast%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F, G, \eta, (\cdot)^\ast)&quot; class=&quot;latex&quot; title=&quot;(F, G, \eta, (\cdot)^\ast)&quot; /&gt; is an adjunction quadruple according to Definition 2, and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28%5Ccdot%29_%5Cflat&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(\cdot)_\flat&quot; class=&quot;latex&quot; title=&quot;(\cdot)_\flat&quot; /&gt; is the inverse operation of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28%5Ccdot%29%5E%5Csharp&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(\cdot)^\sharp&quot; class=&quot;latex&quot; title=&quot;(\cdot)^\sharp&quot; /&gt;—that is, &lt;img src=&quot;http://s0.wp.com/latex.php?latex=g_%5Cflat+%3D+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;g_\flat = f&quot; class=&quot;latex&quot; title=&quot;g_\flat = f&quot; /&gt; if and only if &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%5E%5Csharp+%3D+g&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f^\sharp = g&quot; class=&quot;latex&quot; title=&quot;f^\sharp = g&quot; /&gt;—then necessarily &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Ceta_A+%3D+%28%5Cmathrm%7Bid%7D_%7BFA%7D%29_%5Cflat&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\eta_A = (\mathrm{id}_{FA})_\flat&quot; class=&quot;latex&quot; title=&quot;\eta_A = (\mathrm{id}_{FA})_\flat&quot; /&gt;, and by putting &lt;img src=&quot;http://s0.wp.com/latex.php?latex=Ff+%3D+%28%5Ceta_B+%5Ccirc+f%29%5E%5Csharp&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;Ff = (\eta_B \circ f)^\sharp&quot; class=&quot;latex&quot; title=&quot;Ff = (\eta_B \circ f)^\sharp&quot; /&gt; for &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%5Cin+%5Cmathcal%7BC%7D%28A%2CB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f \in \mathcal{C}(A,B)&quot; class=&quot;latex&quot; title=&quot;f \in \mathcal{C}(A,B)&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cvarepsilon_S+%3D+%28%5Cmathrm%7Bid%7D_%7BGS%7D%29%5E%5Csharp&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\varepsilon_S = (\mathrm{id}_{GS})^\sharp&quot; class=&quot;latex&quot; title=&quot;\varepsilon_S = (\mathrm{id}_{GS})^\sharp&quot; /&gt; for &lt;img src=&quot;http://s0.wp.com/latex.php?latex=S+%5Cin+%7C%5Cmathcal%7BD%7D%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;S \in |\mathcal{D}|&quot; class=&quot;latex&quot; title=&quot;S \in |\mathcal{D}|&quot; /&gt; we define an adjunction according to Definition 1.&lt;/p&gt;
&lt;p&gt;If &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2CG%2C+%5Ceta%2C+%5Cvarepsilon%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F,G, \eta, \varepsilon)&quot; class=&quot;latex&quot; title=&quot;(F,G, \eta, \varepsilon)&quot; /&gt; is an adjunction, then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+GF+%3A+%5Cmathcal%7BC%7D+%5Cto+%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = GF : \mathcal{C} \to \mathcal{C}&quot; class=&quot;latex&quot; title=&quot;T = GF : \mathcal{C} \to \mathcal{C}&quot; /&gt; is an endofunctor.The first question that comes to our mind is:&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;&lt;em&gt;when does an endofunctor derive from an adjunction?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Let us check some basic properties such an endofunctor must satisfy. First of all, &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu+%3A+T%5E2+%5Cto+T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu : T^2 \to T&quot; class=&quot;latex&quot; title=&quot;\mu : T^2 \to T&quot; /&gt; defined by &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu_A+%3D+G%5Cvarepsilon_%7BFA%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu_A = G\varepsilon_{FA}&quot; class=&quot;latex&quot; title=&quot;\mu_A = G\varepsilon_{FA}&quot; /&gt; is a natural transformation and satisfies&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu_A+%5Ccirc+T%5Ceta_A+%3D+%5Cmu_A+%5Ccirc+%5Ceta_%7BTA%7D+%3D+%5Cmathrm%7Bid%7D_%7BTA%7D+%5C%3B%5C%3B+%5Cforall+A+%5Cin+%7C%5Cmathcal%7BC%7D%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu_A \circ T\eta_A = \mu_A \circ \eta_{TA} = \mathrm{id}_{TA} \;\; \forall A \in |\mathcal{C}|&quot; class=&quot;latex&quot; title=&quot;\mu_A \circ T\eta_A = \mu_A \circ \eta_{TA} = \mathrm{id}_{TA} \;\; \forall A \in |\mathcal{C}|&quot; /&gt;&lt;/p&gt;
&lt;p style=&quot;text-align: left;&quot;&gt;Moreover, as &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cvarepsilon+%3A+FG+%5Cto+%5Cmathrm%7BId%7D_%7B%5Cmathcal%7BD%7D%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\varepsilon : FG \to \mathrm{Id}_{\mathcal{D}}&quot; class=&quot;latex&quot; title=&quot;\varepsilon : FG \to \mathrm{Id}_{\mathcal{D}}&quot; /&gt; is a natural transformation, by choosing &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%3D+%5Cvarepsilon_%7BFA%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f = \varepsilon_{FA}&quot; class=&quot;latex&quot; title=&quot;f = \varepsilon_{FA}&quot; /&gt; we get &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cvarepsilon_%7BFA%7D+%5Ccirc+%5Cvarepsilon_%7BFGFA%7D+%3D+%5Cvarepsilon_%7BFA%7D+%5Ccirc+FG%5Cvarepsilon_%7BFA%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\varepsilon_{FA} \circ \varepsilon_{FGFA} = \varepsilon_{FA} \circ FG\varepsilon_{FA}&quot; class=&quot;latex&quot; title=&quot;\varepsilon_{FA} \circ \varepsilon_{FGFA} = \varepsilon_{FA} \circ FG\varepsilon_{FA}&quot; /&gt;, which after an application of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=G&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G&quot; class=&quot;latex&quot; title=&quot;G&quot; /&gt; yields&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu_A+%5Ccirc+%5Cmu_%7BTA%7D+%3D+%5Cmu_A+%5Ccirc+T%5Cmu_A+%5C%3B%5C%3B+%5Cforall+A+%5Cin+%7C%5Cmathcal%7BC%7D%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu_A \circ \mu_{TA} = \mu_A \circ T\mu_A \;\; \forall A \in |\mathcal{C}|&quot; class=&quot;latex&quot; title=&quot;\mu_A \circ \mu_{TA} = \mu_A \circ T\mu_A \;\; \forall A \in |\mathcal{C}|&quot; /&gt;&lt;/p&gt;
&lt;p style=&quot;text-align: left;&quot;&gt;It will turn out that these two properties are precisely what we need.&lt;/p&gt;
&lt;p style=&quot;text-align: left;&quot;&gt;&lt;strong&gt;Definition 3.&lt;/strong&gt; A &lt;em&gt;monad&lt;/em&gt; on a category &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt; is a triple &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+%28T%2C+%5Ceta%2C+%5Cmu%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = (T, \eta, \mu)&quot; class=&quot;latex&quot; title=&quot;T = (T, \eta, \mu)&quot; /&gt; where:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3A+%5Cmathcal%7BC%7D+%5Cto+%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T : \mathcal{C} \to \mathcal{C}&quot; class=&quot;latex&quot; title=&quot;T : \mathcal{C} \to \mathcal{C}&quot; /&gt; is an endofunctor,&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Ceta+%3A+%5Cmathrm%7BId%7D_%5Cmathcal%7BC%7D+%5Cto+T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\eta : \mathrm{Id}_\mathcal{C} \to T&quot; class=&quot;latex&quot; title=&quot;\eta : \mathrm{Id}_\mathcal{C} \to T&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu%3A+T%5E2+%5Cto+T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu: T^2 \to T&quot; class=&quot;latex&quot; title=&quot;\mu: T^2 \to T&quot; /&gt; are natural transformations, and&lt;/li&gt;
&lt;li&gt;for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A+%5Cin+%7C%5Cmathcal%7BC%7D%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A \in |\mathcal{C}|&quot; class=&quot;latex&quot; title=&quot;A \in |\mathcal{C}|&quot; /&gt; we have &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu_A+%5Ccirc+%5Ceta_%7BTA%7D+%3D+%5Cmu_A+%5Ccirc+T%5Ceta_A+%3D+%5Cmathrm%7Bid%7D_%7BTA%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu_A \circ \eta_{TA} = \mu_A \circ T\eta_A = \mathrm{id}_{TA}&quot; class=&quot;latex&quot; title=&quot;\mu_A \circ \eta_{TA} = \mu_A \circ T\eta_A = \mathrm{id}_{TA}&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu_A+%5Ccirc+%5Cmu_%7BTA%7D+%3D+%5Cmu_A+%5Ccirc+T%5Cmu_A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu_A \circ \mu_{TA} = \mu_A \circ T\mu_A&quot; class=&quot;latex&quot; title=&quot;\mu_A \circ \mu_{TA} = \mu_A \circ T\mu_A&quot; /&gt;,&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;As a very basic example, the &lt;em&gt;free monoid&lt;/em&gt; construction is a monad on &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathbf%7BSet%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathbf{Set}&quot; class=&quot;latex&quot; title=&quot;\mathbf{Set}&quot; /&gt;, where &lt;img src=&quot;http://s0.wp.com/latex.php?latex=TA+%3D+A%5E%5Cast&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;TA = A^\ast&quot; class=&quot;latex&quot; title=&quot;TA = A^\ast&quot; /&gt;, &lt;img src=&quot;http://s0.wp.com/latex.php?latex=Tf%28s%29+%3D+%5Bf%28a%29+%5C%3B+%5Cmathtt%7Bfor%7D+%5C%3B+a+%5C%3B+%5Cmathtt%7Bin%7D+%5C%3B+s%5D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;Tf(s) = [f(a) \; \mathtt{for} \; a \; \mathtt{in} \; s]&quot; class=&quot;latex&quot; title=&quot;Tf(s) = [f(a) \; \mathtt{for} \; a \; \mathtt{in} \; s]&quot; /&gt;, &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Ceta_A%28a%29+%3D+%5Ba%5D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\eta_A(a) = [a]&quot; class=&quot;latex&quot; title=&quot;\eta_A(a) = [a]&quot; /&gt;, and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu_A%28%5B%5Ba%5E1_1%2C+%5Cldots%2C+a%5E1_%7Bn_1%7D%5D%2C+%5Cldots%2C+%5Ba%5Em_1%2C+%5Cldots%2C+a%5Em_%7Bn_m%7D%5D%5D+%3D+%5Ba%5E1_1%2C+%5Cldots%2C+a%5E1_%7Bn_1%7D%2C+%5Cldots%2C+a%5Em_1%2C+%5Cldots%2C+a%5Em_%7Bn_m%7D%5D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu_A([[a^1_1, \ldots, a^1_{n_1}], \ldots, [a^m_1, \ldots, a^m_{n_m}]] = [a^1_1, \ldots, a^1_{n_1}, \ldots, a^m_1, \ldots, a^m_{n_m}]&quot; class=&quot;latex&quot; title=&quot;\mu_A([[a^1_1, \ldots, a^1_{n_1}], \ldots, [a^m_1, \ldots, a^m_{n_m}]] = [a^1_1, \ldots, a^1_{n_1}, \ldots, a^m_1, \ldots, a^m_{n_m}]&quot; /&gt;.&lt;/p&gt;
&lt;p&gt;As a less basic example, suppose &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D+%3D+%28S%2C+%5Cleq%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C} = (S, \leq)&quot; class=&quot;latex&quot; title=&quot;\mathcal{C} = (S, \leq)&quot; /&gt; is a poset: what is a monad on &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt;? First of all, an endofunctor on a poset is a monotone function; next, if there is &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Ceta_A+%3A+A+%5Cto+TA&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\eta_A : A \to TA&quot; class=&quot;latex&quot; title=&quot;\eta_A : A \to TA&quot; /&gt;, then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A+%5Cleq+TA&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A \leq TA&quot; class=&quot;latex&quot; title=&quot;A \leq TA&quot; /&gt;; finally, if there is &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu_A+%3A+T%5E2A+%5Cto+TA&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu_A : T^2A \to TA&quot; class=&quot;latex&quot; title=&quot;\mu_A : T^2A \to TA&quot; /&gt;, then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T%5E2A+%5Cleq+TA&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T^2A \leq TA&quot; class=&quot;latex&quot; title=&quot;T^2A \leq TA&quot; /&gt;, which together with the previous inequality yields &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T%5E2A+%3D+TA&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T^2A = TA&quot; class=&quot;latex&quot; title=&quot;T^2A = TA&quot; /&gt;. On the other hand, any nondecreasing idempotent is the endofunctor component of a monad: the monad equations are actually ensured by &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt; being a poset, so that any two maps with same domain and same codomain are equal.&lt;/p&gt;
&lt;p&gt;We then restate our original problem as follows:&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;&lt;em&gt;given a monad &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+%28T%2C+%5Ceta%2C+%5Cmu%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = (T, \eta, \mu)&quot; class=&quot;latex&quot; title=&quot;T = (T, \eta, \mu)&quot; /&gt;, find an adjunction &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2C+G%2C+%5Ceta%2C+%5Cvarepsilon%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F, G, \eta, \varepsilon)&quot; class=&quot;latex&quot; title=&quot;(F, G, \eta, \varepsilon)&quot; /&gt; such that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+GF&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = GF&quot; class=&quot;latex&quot; title=&quot;T = GF&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu+%3D+G+%5Cvarepsilon_F&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu = G \varepsilon_F&quot; class=&quot;latex&quot; title=&quot;\mu = G \varepsilon_F&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;If the adjunction &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2C+G%2C+%5Ceta%2C+%5Cvarepsilon%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F, G, \eta, \varepsilon)&quot; class=&quot;latex&quot; title=&quot;(F, G, \eta, \varepsilon)&quot; /&gt; solves the problem above, we say that it &lt;em&gt;generates&lt;/em&gt; the monad &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;.&lt;/p&gt;
&lt;p&gt;The first solution to this problem was given by the Swiss mathematician Heinrich Kleisli, and is based on an alternative way of defining monads, as it is the case with adjunctions. Let us suppose &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+GF&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = GF&quot; class=&quot;latex&quot; title=&quot;T = GF&quot; /&gt; with &lt;img src=&quot;http://s0.wp.com/latex.php?latex=F+%5Cdashv+G&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F \dashv G&quot; class=&quot;latex&quot; title=&quot;F \dashv G&quot; /&gt;. If &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%3A+A+%5Cto+TB+%3D+G%28FB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f : A \to TB = G(FB)&quot; class=&quot;latex&quot; title=&quot;f : A \to TB = G(FB)&quot; /&gt;, then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%5E%5Csharp+%3A+FA+%5Cto+FB&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f^\sharp : FA \to FB&quot; class=&quot;latex&quot; title=&quot;f^\sharp : FA \to FB&quot; /&gt;, so that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=Gf%5E%5Csharp+%3A+TA+%5Cto+TB&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;Gf^\sharp : TA \to TB&quot; class=&quot;latex&quot; title=&quot;Gf^\sharp : TA \to TB&quot; /&gt;: and we know from the definition of monad that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=Gf%5E%5Csharp+%5Ccirc+%5Ceta_A+%3D+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;Gf^\sharp \circ \eta_A = f&quot; class=&quot;latex&quot; title=&quot;Gf^\sharp \circ \eta_A = f&quot; /&gt;. We can thus define an operator &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28%5Ccdot%29%5E%5Cast&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(\cdot)^\ast&quot; class=&quot;latex&quot; title=&quot;(\cdot)^\ast&quot; /&gt; that takes &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%5Cin+%5Cmathcal%7BC%7D%28A%2CTB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f \in \mathcal{C}(A,TB)&quot; class=&quot;latex&quot; title=&quot;f \in \mathcal{C}(A,TB)&quot; /&gt; into &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%5E%5Cast+%5Cin+%5Cmathcal%7BC%7D%28TA%2CTB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f^\ast \in \mathcal{C}(TA,TB)&quot; class=&quot;latex&quot; title=&quot;f^\ast \in \mathcal{C}(TA,TB)&quot; /&gt; in a way such that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%5E%5Cast+%5Ccirc+%5Ceta_A+%3D+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f^\ast \circ \eta_A = f&quot; class=&quot;latex&quot; title=&quot;f^\ast \circ \eta_A = f&quot; /&gt; whatever &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f&quot; class=&quot;latex&quot; title=&quot;f&quot; /&gt; is. The simplest example is &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%3D+%5Ceta_A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f = \eta_A&quot; class=&quot;latex&quot; title=&quot;f = \eta_A&quot; /&gt; itself, so that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28%5Ceta_A%29%5E%5Cast+%5Ccirc+%5Ceta_A+%3D+%5Ceta_A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(\eta_A)^\ast \circ \eta_A = \eta_A&quot; class=&quot;latex&quot; title=&quot;(\eta_A)^\ast \circ \eta_A = \eta_A&quot; /&gt;, and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28%5Ceta_A%29%5E%5Cast+%3D+%5Cmathrm%7Bid%7D_%7BTA%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(\eta_A)^\ast = \mathrm{id}_{TA}&quot; class=&quot;latex&quot; title=&quot;(\eta_A)^\ast = \mathrm{id}_{TA}&quot; /&gt; by uniqueness in the definition of adjunction quadruple. Moreover, if &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%3A+A+%5Cto+TB&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f : A \to TB&quot; class=&quot;latex&quot; title=&quot;f : A \to TB&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=g+%3A+B+%5Cto+TC&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;g : B \to TC&quot; class=&quot;latex&quot; title=&quot;g : B \to TC&quot; /&gt;, then&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=g%5E%5Cast+%5Ccirc+f+%3D+Gg%5E%5Csharp+%5Ccirc+%28Gf%5E%5Csharp+%5Ccirc+%5Ceta_A%29+%3D+%28g%5E%5Cast+%5Ccirc+f%5E%5Cast%29+%5Ccirc+%5Ceta_A+%5C%3B%2C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;g^\ast \circ f = Gg^\sharp \circ (Gf^\sharp \circ \eta_A) = (g^\ast \circ f^\ast) \circ \eta_A \;,&quot; class=&quot;latex&quot; title=&quot;g^\ast \circ f = Gg^\sharp \circ (Gf^\sharp \circ \eta_A) = (g^\ast \circ f^\ast) \circ \eta_A \;,&quot; /&gt;&lt;/p&gt;
&lt;p style=&quot;text-align: left;&quot;&gt;which implies &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28g%5E%5Cast+%5Ccirc+f%29%5E%5Cast+%3D+g%5E%5Cast+%5Ccirc+f%5E%5Cast&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(g^\ast \circ f)^\ast = g^\ast \circ f^\ast&quot; class=&quot;latex&quot; title=&quot;(g^\ast \circ f)^\ast = g^\ast \circ f^\ast&quot; /&gt; by uniqueness.&lt;/p&gt;
&lt;p&gt;This is the base of Kleisli’s solution to our problem, which we will discuss in a future talk.&lt;/p&gt;
&lt;br /&gt;  &lt;a href=&quot;http://feeds.wordpress.com/1.0/gocomments/theorylunch.wordpress.com/768/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/comments/theorylunch.wordpress.com/768/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;img src=&quot;http://stats.wordpress.com/b.gif?host=theorylunch.wordpress.com&amp;amp;blog=43735749&amp;amp;post=768&amp;amp;subd=theorylunch&amp;amp;ref=&amp;amp;feed=1&quot; alt=&quot;&quot; height=&quot;1&quot; border=&quot;0&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Mon, 10 Jun 2013 11:27:39 +0000</pubDate>
</item>
<item>
	<title>Theory Lunch (Institute of Cybernetics, Tallinn): An initial solution to the monad problem, and then some more</title>
	<guid isPermaLink="false">http://theorylunch.wordpress.com/?p=885</guid>
	<link>http://theorylunch.wordpress.com/2013/06/06/an-initial-solution-to-the-monad-problem-and-then-some-more/</link>
	<description>&lt;p&gt;This is the second of two talks about monads, based on &lt;a href=&quot;http://www.cs.man.ac.uk/~schalk/notes/monads.pdf&quot; target=&quot;_blank&quot; title=&quot;http://www.cs.man.ac.uk/~schalk/notes/monads.pdf&quot;&gt;the very good notes by Andrea Schalk&lt;/a&gt; and continuing &lt;a href=&quot;http://theorylunch.wordpress.com/2013/05/30/when-does-an-endofunctor-derive-from-an-adjunction/&quot; target=&quot;_blank&quot; title=&quot;http://theorylunch.wordpress.com/2013/05/30/when-does-an-endofunctor-derive-from-an-adjunction/&quot;&gt;the one I gave on the 30th of May&lt;/a&gt;. Recall that we are trying to solve the following problem:&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;&lt;em&gt;given a monad &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+%28T%2C+%5Ceta%2C+%5Cmu%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = (T, \eta, \mu)&quot; class=&quot;latex&quot; title=&quot;T = (T, \eta, \mu)&quot; /&gt;, find an adjunction &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2C+G%2C+%5Ceta%2C+%5Cvarepsilon%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F, G, \eta, \varepsilon)&quot; class=&quot;latex&quot; title=&quot;(F, G, \eta, \varepsilon)&quot; /&gt; such that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+GF&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = GF&quot; class=&quot;latex&quot; title=&quot;T = GF&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu+%3D+G+%5Cvarepsilon_F&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu = G \varepsilon_F&quot; class=&quot;latex&quot; title=&quot;\mu = G \varepsilon_F&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;If the adjunction &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2C+G%2C+%5Ceta%2C+%5Cvarepsilon%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F, G, \eta, \varepsilon)&quot; class=&quot;latex&quot; title=&quot;(F, G, \eta, \varepsilon)&quot; /&gt; solves the problem above, we say that it &lt;em&gt;generates&lt;/em&gt; the monad &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;.&lt;/p&gt;
&lt;p&gt;The first solution to this problem was given by the Swiss mathematician Heinrich Kleisli, and is based on an alternative way of defining monads, as it is the case with adjunctions. &lt;span id=&quot;more-885&quot;&gt;&lt;/span&gt; Let us suppose &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+GF&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = GF&quot; class=&quot;latex&quot; title=&quot;T = GF&quot; /&gt; with &lt;img src=&quot;http://s0.wp.com/latex.php?latex=F+%5Cdashv+G&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F \dashv G&quot; class=&quot;latex&quot; title=&quot;F \dashv G&quot; /&gt;. If &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%3A+A+%5Cto+TB+%3D+G%28FB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f : A \to TB = G(FB)&quot; class=&quot;latex&quot; title=&quot;f : A \to TB = G(FB)&quot; /&gt;, then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%5E%5Csharp+%3A+FA+%5Cto+FB&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f^\sharp : FA \to FB&quot; class=&quot;latex&quot; title=&quot;f^\sharp : FA \to FB&quot; /&gt;, so that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=Gf%5E%5Csharp+%3A+TA+%5Cto+TB&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;Gf^\sharp : TA \to TB&quot; class=&quot;latex&quot; title=&quot;Gf^\sharp : TA \to TB&quot; /&gt;: and we know from the definition of monad that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=Gf%5E%5Csharp+%5Ccirc+%5Ceta_A+%3D+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;Gf^\sharp \circ \eta_A = f&quot; class=&quot;latex&quot; title=&quot;Gf^\sharp \circ \eta_A = f&quot; /&gt;. We can thus define an operator &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28%5Ccdot%29%5E%5Cast&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(\cdot)^\ast&quot; class=&quot;latex&quot; title=&quot;(\cdot)^\ast&quot; /&gt; that takes &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%5Cin+%5Cmathcal%7BC%7D%28A%2CTB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f \in \mathcal{C}(A,TB)&quot; class=&quot;latex&quot; title=&quot;f \in \mathcal{C}(A,TB)&quot; /&gt; into &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%5E%5Cast+%3D+Gf%5E%5Csharp+%5Cin+%5Cmathcal%7BC%7D%28TA%2CTB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f^\ast = Gf^\sharp \in \mathcal{C}(TA,TB)&quot; class=&quot;latex&quot; title=&quot;f^\ast = Gf^\sharp \in \mathcal{C}(TA,TB)&quot; /&gt; so that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%5E%5Cast+%5Ccirc+%5Ceta_A+%3D+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f^\ast \circ \eta_A = f&quot; class=&quot;latex&quot; title=&quot;f^\ast \circ \eta_A = f&quot; /&gt; whatever &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f&quot; class=&quot;latex&quot; title=&quot;f&quot; /&gt; is. The simplest example is &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%3D+%5Ceta_A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f = \eta_A&quot; class=&quot;latex&quot; title=&quot;f = \eta_A&quot; /&gt; itself, which yields &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28%5Ceta_A%29%5E%5Cast+%5Ccirc+%5Ceta_A+%3D+%5Ceta_A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(\eta_A)^\ast \circ \eta_A = \eta_A&quot; class=&quot;latex&quot; title=&quot;(\eta_A)^\ast \circ \eta_A = \eta_A&quot; /&gt;, so that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28%5Ceta_A%29%5E%5Csharp+%3D+%5Cmathrm%7Bid%7D_%7BFA%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(\eta_A)^\sharp = \mathrm{id}_{FA}&quot; class=&quot;latex&quot; title=&quot;(\eta_A)^\sharp = \mathrm{id}_{FA}&quot; /&gt; by uniqueness in the definition of adjunction quadruple, and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28%5Ceta_A%29%5E%5Cast+%3D+%5Cmathrm%7Bid%7D_%7BTA%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(\eta_A)^\ast = \mathrm{id}_{TA}&quot; class=&quot;latex&quot; title=&quot;(\eta_A)^\ast = \mathrm{id}_{TA}&quot; /&gt;. Moreover, if &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%3A+A+%5Cto+TB&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f : A \to TB&quot; class=&quot;latex&quot; title=&quot;f : A \to TB&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=g+%3A+B+%5Cto+TC&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;g : B \to TC&quot; class=&quot;latex&quot; title=&quot;g : B \to TC&quot; /&gt;, then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=g%5E%5Cast+%5Ccirc+f+%3D+g%5E%5Cast+%5Ccirc+f%5E%5Cast+%5Ccirc+%5Ceta_A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;g^\ast \circ f = g^\ast \circ f^\ast \circ \eta_A&quot; class=&quot;latex&quot; title=&quot;g^\ast \circ f = g^\ast \circ f^\ast \circ \eta_A&quot; /&gt;, which implies &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28g%5E%5Cast+%5Ccirc+f%29%5E%5Cast+%3D+g%5E%5Cast+%5Ccirc+f%5E%5Cast&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(g^\ast \circ f)^\ast = g^\ast \circ f^\ast&quot; class=&quot;latex&quot; title=&quot;(g^\ast \circ f)^\ast = g^\ast \circ f^\ast&quot; /&gt; by uniqueness.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Definition 4.&lt;/strong&gt; A &lt;em&gt;Kleisli triple&lt;/em&gt; on a category &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt; is a triple &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28T%2C+%5Ceta%2C+%28%5Ccdot%29%5E%5Cast%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(T, \eta, (\cdot)^\ast)&quot; class=&quot;latex&quot; title=&quot;(T, \eta, (\cdot)^\ast)&quot; /&gt; where:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3A+%7C%5Cmathcal%7BC%7D%7C+%5Cto+%7C%5Cmathcal%7BC%7D%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T : |\mathcal{C}| \to |\mathcal{C}|&quot; class=&quot;latex&quot; title=&quot;T : |\mathcal{C}| \to |\mathcal{C}|&quot; /&gt; is a function,&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Ceta_A+%5Cin+%5Cmathcal%7BC%7D%28A%2C+TA%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\eta_A \in \mathcal{C}(A, TA)&quot; class=&quot;latex&quot; title=&quot;\eta_A \in \mathcal{C}(A, TA)&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A+%5Cin+%7C%5Cmathcal%7BC%7D%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A \in |\mathcal{C}|&quot; class=&quot;latex&quot; title=&quot;A \in |\mathcal{C}|&quot; /&gt;, and&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%5E%5Cast+%5Cin+%5Cmathcal%7BC%7D%28TA%2CTB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f^\ast \in \mathcal{C}(TA,TB)&quot; class=&quot;latex&quot; title=&quot;f^\ast \in \mathcal{C}(TA,TB)&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%5Cin+%5Cmathcal%7BC%7D%28A%2CTB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f \in \mathcal{C}(A,TB)&quot; class=&quot;latex&quot; title=&quot;f \in \mathcal{C}(A,TB)&quot; /&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;such that the following equations are satisfied:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%5E%5Cast+%5Ccirc+%5Ceta_A+%3D+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f^\ast \circ \eta_A = f&quot; class=&quot;latex&quot; title=&quot;f^\ast \circ \eta_A = f&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%3A+A+%5Cto+TB&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f : A \to TB&quot; class=&quot;latex&quot; title=&quot;f : A \to TB&quot; /&gt;;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28%5Ceta_A%29%5E%5Cast+%3D+%5Cmathrm%7Bid%7D_%7BTA%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(\eta_A)^\ast = \mathrm{id}_{TA}&quot; class=&quot;latex&quot; title=&quot;(\eta_A)^\ast = \mathrm{id}_{TA}&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A&quot; class=&quot;latex&quot; title=&quot;A&quot; /&gt;;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28g%5E%5Cast+%5Ccirc+f%29%5E%5Cast+%3D+g%5E%5Cast+%5Ccirc+f%5E%5Cast&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(g^\ast \circ f)^\ast = g^\ast \circ f^\ast&quot; class=&quot;latex&quot; title=&quot;(g^\ast \circ f)^\ast = g^\ast \circ f^\ast&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%3A+A+%5Cto+TB&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f : A \to TB&quot; class=&quot;latex&quot; title=&quot;f : A \to TB&quot; /&gt;, &lt;img src=&quot;http://s0.wp.com/latex.php?latex=g+%3A+B+%5Cto+TC&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;g : B \to TC&quot; class=&quot;latex&quot; title=&quot;g : B \to TC&quot; /&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2CG%2C+%5Ceta%2C+%28%5Ccdot%29%5E%5Csharp%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F,G, \eta, (\cdot)^\sharp)&quot; class=&quot;latex&quot; title=&quot;(F,G, \eta, (\cdot)^\sharp)&quot; /&gt; is an adjunction quadruple then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28GF%2C+%5Ceta%2C+G%28%5Ccdot%29%5E%5Csharp%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(GF, \eta, G(\cdot)^\sharp)&quot; class=&quot;latex&quot; title=&quot;(GF, \eta, G(\cdot)^\sharp)&quot; /&gt; is a Kleisli triple.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Theorem 1.&lt;/strong&gt; Let &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt; be a category.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;If &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28T%2C+%5Ceta%2C+%5Cmu%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(T, \eta, \mu)&quot; class=&quot;latex&quot; title=&quot;(T, \eta, \mu)&quot; /&gt; is a monad on &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt;, and if &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%5E%5Cast+%3D+%5Cmu_B+%5Ccirc+Tf&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f^\ast = \mu_B \circ Tf&quot; class=&quot;latex&quot; title=&quot;f^\ast = \mu_B \circ Tf&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%3A+A+%5Cto+TB&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f : A \to TB&quot; class=&quot;latex&quot; title=&quot;f : A \to TB&quot; /&gt;, then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28T%2C+%5Ceta%2C+%28%5Ccdot%29%5E%5Cast%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(T, \eta, (\cdot)^\ast)&quot; class=&quot;latex&quot; title=&quot;(T, \eta, (\cdot)^\ast)&quot; /&gt; is a Kleisli triple on &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt;.&lt;/li&gt;
&lt;li&gt;If &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28T%2C+%5Ceta%2C+%28%5Ccdot%29%5E%5Cast%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(T, \eta, (\cdot)^\ast)&quot; class=&quot;latex&quot; title=&quot;(T, \eta, (\cdot)^\ast)&quot; /&gt; is a Kleisli triple on &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt;, and if &lt;img src=&quot;http://s0.wp.com/latex.php?latex=Tf+%3D+%28%5Ceta_B+%5Ccirc+f%29%5E%5Cast&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;Tf = (\eta_B \circ f)^\ast&quot; class=&quot;latex&quot; title=&quot;Tf = (\eta_B \circ f)^\ast&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%3A+A+%5Cto+B&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f : A \to B&quot; class=&quot;latex&quot; title=&quot;f : A \to B&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu_A+%3D+%28%5Cmathrm%7Bid%7D_%7BTA%7D%29%5E%5Cast&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu_A = (\mathrm{id}_{TA})^\ast&quot; class=&quot;latex&quot; title=&quot;\mu_A = (\mathrm{id}_{TA})^\ast&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A&quot; class=&quot;latex&quot; title=&quot;A&quot; /&gt;, then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28T%2C+%5Ceta%2C+%5Cmu%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(T, \eta, \mu)&quot; class=&quot;latex&quot; title=&quot;(T, \eta, \mu)&quot; /&gt; is a monad on &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt;.&lt;/li&gt;
&lt;li&gt;The two operations from the previous points are each other’s converse.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;em&gt;Proof:&lt;/em&gt; Point 1 follows from naturality of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Ceta&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\eta&quot; class=&quot;latex&quot; title=&quot;\eta&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu&quot; class=&quot;latex&quot; title=&quot;\mu&quot; /&gt; and the three monad laws:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%5E%5Cast+%5Ccirc+%5Ceta_A+%3D+%5Cmu_B+%5Ccirc+Tf+%5Ccirc+%5Ceta_A+%3D+%5Cmu_B+%5Ccirc+%5Ceta_%7BTB%7D+%5Ccirc+f+%3D+%5Cmathrm%7Bid%7D_%7BTB%7D+%5Ccirc+f+%3D+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f^\ast \circ \eta_A = \mu_B \circ Tf \circ \eta_A = \mu_B \circ \eta_{TB} \circ f = \mathrm{id}_{TB} \circ f = f&quot; class=&quot;latex&quot; title=&quot;f^\ast \circ \eta_A = \mu_B \circ Tf \circ \eta_A = \mu_B \circ \eta_{TB} \circ f = \mathrm{id}_{TB} \circ f = f&quot; /&gt;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28%5Ceta_A%29%5E%5Cast+%3D+%5Cmu_%7BTA%7D+%5Ccirc+%5Ceta_A+%3D+%5Cmathrm%7Bid%7D_%7BTA%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(\eta_A)^\ast = \mu_{TA} \circ \eta_A = \mathrm{id}_{TA}&quot; class=&quot;latex&quot; title=&quot;(\eta_A)^\ast = \mu_{TA} \circ \eta_A = \mathrm{id}_{TA}&quot; /&gt;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28g%5E%5Cast+%5Ccirc+f%29%5E%5Cast+%3D+%5Cmu_C+%5Ccirc+T%5Cmu_C+%5Ccirc+T%5E2g+%5Ccirc+Tf+%3D+%5Cmu_C+%5Ccirc+%5Cmu_%7BTC%7D+%5Ccirc+T%5E2g+%5Ccirc+Tf+%3D+%5Cmu_C+%5Ccirc+Tg+%5Ccirc+%5Cmu_B+%5Ccirc+Tf+%3D+g%5E%5Cast+%5Ccirc+f%5E%5Cast&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(g^\ast \circ f)^\ast = \mu_C \circ T\mu_C \circ T^2g \circ Tf = \mu_C \circ \mu_{TC} \circ T^2g \circ Tf = \mu_C \circ Tg \circ \mu_B \circ Tf = g^\ast \circ f^\ast&quot; class=&quot;latex&quot; title=&quot;(g^\ast \circ f)^\ast = \mu_C \circ T\mu_C \circ T^2g \circ Tf = \mu_C \circ \mu_{TC} \circ T^2g \circ Tf = \mu_C \circ Tg \circ \mu_B \circ Tf = g^\ast \circ f^\ast&quot; /&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For point 2, functoriality of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;, naturality of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Ceta&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\eta&quot; class=&quot;latex&quot; title=&quot;\eta&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu&quot; class=&quot;latex&quot; title=&quot;\mu&quot; /&gt;, and monad laws follow from Kleisli laws:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=T%28g+%5Ccirc+f%29+%3D+%28%5Ceta_C+%5Ccirc+g+%5Ccirc+f%29%5E%5Cast+%3D+%28%28%5Ceta_C+%5Ccirc+g%29%5E%5Cast+%5Ccirc+%5Ceta_B+%5Ccirc+f%29%5E%5Cast+%3D+%28%5Ceta_C+%5Ccirc+g%29%5E%5Cast+%5Ccirc+%28%5Ceta_B+%5Ccirc+f%29%5E%5Cast+Tg+%5Ccirc+Tf&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T(g \circ f) = (\eta_C \circ g \circ f)^\ast = ((\eta_C \circ g)^\ast \circ \eta_B \circ f)^\ast = (\eta_C \circ g)^\ast \circ (\eta_B \circ f)^\ast Tg \circ Tf&quot; class=&quot;latex&quot; title=&quot;T(g \circ f) = (\eta_C \circ g \circ f)^\ast = ((\eta_C \circ g)^\ast \circ \eta_B \circ f)^\ast = (\eta_C \circ g)^\ast \circ (\eta_B \circ f)^\ast Tg \circ Tf&quot; /&gt;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=T%5Cmathrm%7Bid%7D_A+%3D+%28%5Ceta_A%29%5E%5Cast+%3D+%5Cmathrm%7Bid%7D_%7BTA%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T\mathrm{id}_A = (\eta_A)^\ast = \mathrm{id}_{TA}&quot; class=&quot;latex&quot; title=&quot;T\mathrm{id}_A = (\eta_A)^\ast = \mathrm{id}_{TA}&quot; /&gt;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=Tf+%5Ccirc+%5Ceta_A+%3D+%28%5Ceta_B+%5Ccirc+f%29%5E%5Cast+%5Ccirc+%5Ceta_A+%3D+%5Ceta_B+%5Ccirc+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;Tf \circ \eta_A = (\eta_B \circ f)^\ast \circ \eta_A = \eta_B \circ f&quot; class=&quot;latex&quot; title=&quot;Tf \circ \eta_A = (\eta_B \circ f)^\ast \circ \eta_A = \eta_B \circ f&quot; /&gt;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu_B+%5Ccirc+T%5E2f+%3D+%28%5Cmathrm%7Bid%7D_%7BTB%7D%5E%5Cast+%5Ccirc+%5Ceta_%7BTB%7D+%5Ccirc+%28%5Ceta_B+%5Ccirc+f%29%5E%5Cast%29%5E%5Cast+%3D+%28%5Ceta_B+%5Ccirc+Tf%29%5E%7B%5Cast%5Cast%7D+%3D+%28%28%5Ceta_B+%5Ccirc+f%29%5E%5Cast+%5Ccirc+%5Cmathrm%7Bid%7D_%7BTA%7D%29%5E%5Cast+%3D+Tf+%5Ccirc+%5Cmu_A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu_B \circ T^2f = (\mathrm{id}_{TB}^\ast \circ \eta_{TB} \circ (\eta_B \circ f)^\ast)^\ast = (\eta_B \circ Tf)^{\ast\ast} = ((\eta_B \circ f)^\ast \circ \mathrm{id}_{TA})^\ast = Tf \circ \mu_A&quot; class=&quot;latex&quot; title=&quot;\mu_B \circ T^2f = (\mathrm{id}_{TB}^\ast \circ \eta_{TB} \circ (\eta_B \circ f)^\ast)^\ast = (\eta_B \circ Tf)^{\ast\ast} = ((\eta_B \circ f)^\ast \circ \mathrm{id}_{TA})^\ast = Tf \circ \mu_A&quot; /&gt;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu_A+%5Ccirc+%5Ceta_%7BTA%7D+%3D+%28%5Cmathrm%7Bid%7D_%7BTA%7D%29%5E%5Cast+%5Ccirc+%5Ceta_%7BTA%7D+%3D+%5Cmathrm%7Bid%7D_%7BTA%7D+%3D+%28%5Cmathrm%7Bid%7D_%7BTA%7D+%5Ccirc+%5Ceta_A%29%5E%5Cast+%3D+%28%28%5Cmathrm%7Bid%7D_%7BTA%7D%29%5E%5Cast+%5Ccirc+%5Ceta_%7BTA%7D+%5Ccirc+%5Ceta_A%29%5E%5Cast+%3D+%5Cmu_A+%5Ccirc+T%5Ceta_A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu_A \circ \eta_{TA} = (\mathrm{id}_{TA})^\ast \circ \eta_{TA} = \mathrm{id}_{TA} = (\mathrm{id}_{TA} \circ \eta_A)^\ast = ((\mathrm{id}_{TA})^\ast \circ \eta_{TA} \circ \eta_A)^\ast = \mu_A \circ T\eta_A&quot; class=&quot;latex&quot; title=&quot;\mu_A \circ \eta_{TA} = (\mathrm{id}_{TA})^\ast \circ \eta_{TA} = \mathrm{id}_{TA} = (\mathrm{id}_{TA} \circ \eta_A)^\ast = ((\mathrm{id}_{TA})^\ast \circ \eta_{TA} \circ \eta_A)^\ast = \mu_A \circ T\eta_A&quot; /&gt;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu_A+%5Ccirc+%5Cmu_%7BTA%7D+%3D+%28%5Cmathrm%7Bid%7D_%7BTA%7D%5E%5Cast+%5Ccirc+%5Cmathrm%7Bid%7D_%7BT%5E2A%7D%29%5E%5Cast+%3D+%28%5Cmathrm%7Bid%7D_A+%5Ccirc+%5Cmathrm%7Bid%7D_%7BTA%7D%5E%5Cast%29%5E%5Cast+%3D+%28%5Cmathrm%7Bid%7D_%7BTA%7D%5E%5Cast+%5Ccirc+%5Ceta_%7BTA%7D+%5Ccirc+%5Cmathrm%7Bid%7D_%7BTA%7D%5E%5Cast%29%5E%5Cast+%3D+%5Cmu_A+%5Ccirc+T%5Cmu_A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu_A \circ \mu_{TA} = (\mathrm{id}_{TA}^\ast \circ \mathrm{id}_{T^2A})^\ast = (\mathrm{id}_A \circ \mathrm{id}_{TA}^\ast)^\ast = (\mathrm{id}_{TA}^\ast \circ \eta_{TA} \circ \mathrm{id}_{TA}^\ast)^\ast = \mu_A \circ T\mu_A&quot; class=&quot;latex&quot; title=&quot;\mu_A \circ \mu_{TA} = (\mathrm{id}_{TA}^\ast \circ \mathrm{id}_{T^2A})^\ast = (\mathrm{id}_A \circ \mathrm{id}_{TA}^\ast)^\ast = (\mathrm{id}_{TA}^\ast \circ \eta_{TA} \circ \mathrm{id}_{TA}^\ast)^\ast = \mu_A \circ T\mu_A&quot; /&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Point 3 is straightforward. &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5CBox&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\Box&quot; class=&quot;latex&quot; title=&quot;\Box&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Considering again the free monoid example, the corresponding Kleisli triple has&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%5E%5Cast%28s%29+%3D+%5B+x+%5C%3B+%5Cmathtt%7Bfor%7D+%5C%3B+x+%5C%3B+%5Cmathtt%7Bin%7D+%5C%3B+f%28a%29+%5C%3B+%5Cmathtt%7Bfor%7D+%5C%3B+a+%5C%3B+%5Cmathtt%7Bin%7D+%5C%3B+s+%5D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f^\ast(s) = [ x \; \mathtt{for} \; x \; \mathtt{in} \; f(a) \; \mathtt{for} \; a \; \mathtt{in} \; s ]&quot; class=&quot;latex&quot; title=&quot;f^\ast(s) = [ x \; \mathtt{for} \; x \; \mathtt{in} \; f(a) \; \mathtt{for} \; a \; \mathtt{in} \; s ]&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Theorem 1 says that we can restate our problem as follows:&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;&lt;em&gt;given a Kleisli triple &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28T%2C+%5Ceta%2C+%28%5Ccdot%29%5E%5Cast%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(T, \eta, (\cdot)^\ast)&quot; class=&quot;latex&quot; title=&quot;(T, \eta, (\cdot)^\ast)&quot; /&gt;,&lt;/em&gt;&lt;em&gt; find an adjunction quadruple &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2C+G%2C+%5Ceta%2C+%28%5Ccdot%29%5E%5Csharp%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F, G, \eta, (\cdot)^\sharp)&quot; class=&quot;latex&quot; title=&quot;(F, G, \eta, (\cdot)^\sharp)&quot; /&gt; such that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+GF&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = GF&quot; class=&quot;latex&quot; title=&quot;T = GF&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28%5Ccdot%29%5E%5Cast+%3D+G%28%28%5Ccdot%29%5E%5Csharp%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(\cdot)^\ast = G((\cdot)^\sharp)&quot; class=&quot;latex&quot; title=&quot;(\cdot)^\ast = G((\cdot)^\sharp)&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;If &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+GF&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = GF&quot; class=&quot;latex&quot; title=&quot;T = GF&quot; /&gt; with &lt;img src=&quot;http://s0.wp.com/latex.php?latex=F+%5Cdashv+G&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F \dashv G&quot; class=&quot;latex&quot; title=&quot;F \dashv G&quot; /&gt;, then for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A%2CB+%5Cin+%7C%5Cmathcal%7BC%7D%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A,B \in |\mathcal{C}|&quot; class=&quot;latex&quot; title=&quot;A,B \in |\mathcal{C}|&quot; /&gt; there is an isomorphism &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BD%7D%28FA%2CFB%29+%5Ccong+%5Cmathcal%7BC%7D%28A%2CTB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{D}(FA,FB) \cong \mathcal{C}(A,TB)&quot; class=&quot;latex&quot; title=&quot;\mathcal{D}(FA,FB) \cong \mathcal{C}(A,TB)&quot; /&gt;: this observation is at the base of Kleisli’s construction.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Definition 5.&lt;/strong&gt; Let &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+%28T%2C+%5Ceta%2C+%28%5Ccdot%29%5E%5Cast%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = (T, \eta, (\cdot)^\ast)&quot; class=&quot;latex&quot; title=&quot;T = (T, \eta, (\cdot)^\ast)&quot; /&gt; be a Kleisli triple on a category &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt;. The &lt;em&gt;Kleisli category &lt;/em&gt;of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt; is the category &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D_T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}_T&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}_T&quot; /&gt; defined as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%7C%5Cmathcal%7BC%7D_T%7C+%3D+%7C%5Cmathcal%7BC%7D%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;|\mathcal{C}_T| = |\mathcal{C}|&quot; class=&quot;latex&quot; title=&quot;|\mathcal{C}_T| = |\mathcal{C}|&quot; /&gt;;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D_T%28A%2CB%29+%3D+%5Cmathcal%7BC%7D%28A%2CTB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}_T(A,B) = \mathcal{C}(A,TB)&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}_T(A,B) = \mathcal{C}(A,TB)&quot; /&gt;;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathrm%7Bid%7D_%7BA%7D%5E%7B%5Cmathcal%7BC%7D_T%7D+%3D+%5Ceta_A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathrm{id}_{A}^{\mathcal{C}_T} = \eta_A&quot; class=&quot;latex&quot; title=&quot;\mathrm{id}_{A}^{\mathcal{C}_T} = \eta_A&quot; /&gt;, that is, the identity of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A&quot; class=&quot;latex&quot; title=&quot;A&quot; /&gt; in &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D_T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}_T&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}_T&quot; /&gt; is &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Ceta_A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\eta_A&quot; class=&quot;latex&quot; title=&quot;\eta_A&quot; /&gt;;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=g+%5Cbullet+f+%3D+g%5E%5Cast+%5Ccirc+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;g \bullet f = g^\ast \circ f&quot; class=&quot;latex&quot; title=&quot;g \bullet f = g^\ast \circ f&quot; /&gt;, that is, the composition of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f&quot; class=&quot;latex&quot; title=&quot;f&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;g&quot; class=&quot;latex&quot; title=&quot;g&quot; /&gt; in &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D_T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}_T&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}_T&quot; /&gt; is the composition of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f&quot; class=&quot;latex&quot; title=&quot;f&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=g%5E%5Cast&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;g^\ast&quot; class=&quot;latex&quot; title=&quot;g^\ast&quot; /&gt; in &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Theorem 2.&lt;/strong&gt; &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D_T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}_T&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}_T&quot; /&gt; is a category.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Proof:&lt;/em&gt; If &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%5Cin+%5Cmathcal%7BC%7D_T%28A%2CB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f \in \mathcal{C}_T(A,B)&quot; class=&quot;latex&quot; title=&quot;f \in \mathcal{C}_T(A,B)&quot; /&gt;, then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%5Cbullet+%5Cmathrm%7Bid%7D_%7BA%7D%5E%7B%5Cmathcal%7BC%7D_T%7D+%3D+f%5E%5Cast+%5Ccirc+%5Ceta_A+%3D+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f \bullet \mathrm{id}_{A}^{\mathcal{C}_T} = f^\ast \circ \eta_A = f&quot; class=&quot;latex&quot; title=&quot;f \bullet \mathrm{id}_{A}^{\mathcal{C}_T} = f^\ast \circ \eta_A = f&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathrm%7Bid%7D_%7BB%7D%5E%7B%5Cmathcal%7BC%7D_T%7D+%5Cbullet+f+%3D+%28%5Ceta_B%29%5E%5Cast+%5Ccirc+f+%3D+%5Cmathrm%7Bid%7D_%7BTB%7D+%5Ccirc+f+%3D+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathrm{id}_{B}^{\mathcal{C}_T} \bullet f = (\eta_B)^\ast \circ f = \mathrm{id}_{TB} \circ f = f&quot; class=&quot;latex&quot; title=&quot;\mathrm{id}_{B}^{\mathcal{C}_T} \bullet f = (\eta_B)^\ast \circ f = \mathrm{id}_{TB} \circ f = f&quot; /&gt; by the Kleisli laws. If &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%5Cin+%5Cmathcal%7BC%7D_T%28A%2CB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f \in \mathcal{C}_T(A,B)&quot; class=&quot;latex&quot; title=&quot;f \in \mathcal{C}_T(A,B)&quot; /&gt;, &lt;img src=&quot;http://s0.wp.com/latex.php?latex=g+%5Cin+%5Cmathcal%7BC%7D_T%28B%2CC%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;g \in \mathcal{C}_T(B,C)&quot; class=&quot;latex&quot; title=&quot;g \in \mathcal{C}_T(B,C)&quot; /&gt;, and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%5Cin+%5Cmathcal%7BC%7D_T%28C%2CD%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f \in \mathcal{C}_T(C,D)&quot; class=&quot;latex&quot; title=&quot;f \in \mathcal{C}_T(C,D)&quot; /&gt;, then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28h+%5Cbullet+g%29+%5Cbullet+f+%3D+%28h%5E%5Cast+%5Ccirc+g%29%5E%5Cast+%5Ccirc+f+%3D+h%5E%5Cast+%5Ccirc+g%5E%5Cast+%5Ccirc+f+%3D+h+%5Cbullet+%28g+%5Cbullet+f%29.&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(h \bullet g) \bullet f = (h^\ast \circ g)^\ast \circ f = h^\ast \circ g^\ast \circ f = h \bullet (g \bullet f).&quot; class=&quot;latex&quot; title=&quot;(h \bullet g) \bullet f = (h^\ast \circ g)^\ast \circ f = h^\ast \circ g^\ast \circ f = h \bullet (g \bullet f).&quot; /&gt; &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5CBox&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\Box&quot; class=&quot;latex&quot; title=&quot;\Box&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Our plan is to construct an adjunction quadruple &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F_T%2C+G_T%2C+%5Ceta%2C+%28%5Ccdot%29%5E%5Csharp%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F_T, G_T, \eta, (\cdot)^\sharp)&quot; class=&quot;latex&quot; title=&quot;(F_T, G_T, \eta, (\cdot)^\sharp)&quot; /&gt;, with &lt;img src=&quot;http://s0.wp.com/latex.php?latex=F_T+%3A+%5Cmathcal%7BC%7D+%5Cto+%5Cmathcal%7BC%7D_T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F_T : \mathcal{C} \to \mathcal{C}_T&quot; class=&quot;latex&quot; title=&quot;F_T : \mathcal{C} \to \mathcal{C}_T&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=G_T+%3A+%5Cmathcal%7BC%7D_T+%5Cto+%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G_T : \mathcal{C}_T \to \mathcal{C}&quot; class=&quot;latex&quot; title=&quot;G_T : \mathcal{C}_T \to \mathcal{C}&quot; /&gt;, such that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+G_T+F_T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = G_T F_T&quot; class=&quot;latex&quot; title=&quot;T = G_T F_T&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=G_T+f%5E%5Csharp+%3D+f%5E%5Cast&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G_T f^\sharp = f^\ast&quot; class=&quot;latex&quot; title=&quot;G_T f^\sharp = f^\ast&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%5Cin+%5Cmathcal%7BC%7D_T%28A%2CB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f \in \mathcal{C}_T(A,B)&quot; class=&quot;latex&quot; title=&quot;f \in \mathcal{C}_T(A,B)&quot; /&gt;. We do this as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=F_T+A+%3D+A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F_T A = A&quot; class=&quot;latex&quot; title=&quot;F_T A = A&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A+%5Cin+%7C%5Cmathcal%7BC%7D%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A \in |\mathcal{C}|&quot; class=&quot;latex&quot; title=&quot;A \in |\mathcal{C}|&quot; /&gt;;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=G_T+A+%3D+TA&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G_T A = TA&quot; class=&quot;latex&quot; title=&quot;G_T A = TA&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A+%5Cin+%7C%5Cmathcal%7BC%7D_T%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A \in |\mathcal{C}_T|&quot; class=&quot;latex&quot; title=&quot;A \in |\mathcal{C}_T|&quot; /&gt;;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=G_T+f+%3D+f%5E%5Cast&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G_T f = f^\ast&quot; class=&quot;latex&quot; title=&quot;G_T f = f^\ast&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%5Cin+%5Cmathcal%7BC%7D_T%28A%2CB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f \in \mathcal{C}_T(A,B)&quot; class=&quot;latex&quot; title=&quot;f \in \mathcal{C}_T(A,B)&quot; /&gt;;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%5E%5Csharp+%3D+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f^\sharp = f&quot; class=&quot;latex&quot; title=&quot;f^\sharp = f&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%5Cin+%5Cmathcal%7BC%7D%28A%2C+G_T+B%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f \in \mathcal{C}(A, G_T B)&quot; class=&quot;latex&quot; title=&quot;f \in \mathcal{C}(A, G_T B)&quot; /&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let us quickly check that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=G_T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G_T&quot; class=&quot;latex&quot; title=&quot;G_T&quot; /&gt; is indeed a functor.&lt;em&gt;&lt;/em&gt; If &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A+%5Cin+%7C%5Cmathcal%7BC%7D_T%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A \in |\mathcal{C}_T|&quot; class=&quot;latex&quot; title=&quot;A \in |\mathcal{C}_T|&quot; /&gt; then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=G_T+%5Cmathrm%7Bid%7D_%7BA%7D%5E%7B%5Cmathcal%7BC%7D_T%7D+%3D+%28%5Ceta_A%29%5E%5Cast+%3D+%5Cmathrm%7Bid%7D_%7BTA%7D+%3D+%5Cmathrm%7Bid%7D_%7BG_T+A%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G_T \mathrm{id}_{A}^{\mathcal{C}_T} = (\eta_A)^\ast = \mathrm{id}_{TA} = \mathrm{id}_{G_T A}&quot; class=&quot;latex&quot; title=&quot;G_T \mathrm{id}_{A}^{\mathcal{C}_T} = (\eta_A)^\ast = \mathrm{id}_{TA} = \mathrm{id}_{G_T A}&quot; /&gt;. If &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%5Cin+%5Cmathcal%7BC%7D_T%28A%2CB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f \in \mathcal{C}_T(A,B)&quot; class=&quot;latex&quot; title=&quot;f \in \mathcal{C}_T(A,B)&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=g+%5Cin+%5Cmathcal%7BC%7D_T%28B%2CC%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;g \in \mathcal{C}_T(B,C)&quot; class=&quot;latex&quot; title=&quot;g \in \mathcal{C}_T(B,C)&quot; /&gt;, then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=G_T%28g+%5Cbullet+f%29+%3D+%28g%5E%5Cast+%5Ccirc+f%29%5E%5Cast+%3D+g%5E%5Cast+%5Ccirc+f%5E%5Cast+%3D+G_Tg+%5Ccirc+G_Tf&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G_T(g \bullet f) = (g^\ast \circ f)^\ast = g^\ast \circ f^\ast = G_Tg \circ G_Tf&quot; class=&quot;latex&quot; title=&quot;G_T(g \bullet f) = (g^\ast \circ f)^\ast = g^\ast \circ f^\ast = G_Tg \circ G_Tf&quot; /&gt;. We are only left to determine, for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%5Cin+%5Cmathcal%7BC%7D%28A%2C+G_T+B%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f \in \mathcal{C}(A, G_T B)&quot; class=&quot;latex&quot; title=&quot;f \in \mathcal{C}(A, G_T B)&quot; /&gt;, a unique &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%5E%5Csharp+%5Cin+%5Cmathcal%7BC%7D_T%28F_T+A%2C+B%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f^\sharp \in \mathcal{C}_T(F_T A, B)&quot; class=&quot;latex&quot; title=&quot;f^\sharp \in \mathcal{C}_T(F_T A, B)&quot; /&gt; such that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=G_T+f%5E%5Csharp+%5Ccirc+%5Ceta_A+%3D+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G_T f^\sharp \circ \eta_A = f&quot; class=&quot;latex&quot; title=&quot;G_T f^\sharp \circ \eta_A = f&quot; /&gt;: but the entire construction leads to the choice &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%5E%5Csharp+%3D+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f^\sharp = f&quot; class=&quot;latex&quot; title=&quot;f^\sharp = f&quot; /&gt;! Indeed, &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D_T%28F_T+A%2C+B%29+%3D+%5Cmathcal%7BC%7D_T%28A%2CB%29+%3D+%5Cmathcal%7BC%7D%28A%2CTB%29+%3D+%5Cmathcal%7BC%7D%28A%2C+G_T+B%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}_T(F_T A, B) = \mathcal{C}_T(A,B) = \mathcal{C}(A,TB) = \mathcal{C}(A, G_T B)&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}_T(F_T A, B) = \mathcal{C}_T(A,B) = \mathcal{C}(A,TB) = \mathcal{C}(A, G_T B)&quot; /&gt;, and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=G_Tf+%5Ccirc+%5Ceta_A+%3D+f%5E%5Cast+%5Ccirc+%5Ceta_A+%3D+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G_Tf \circ \eta_A = f^\ast \circ \eta_A = f&quot; class=&quot;latex&quot; title=&quot;G_Tf \circ \eta_A = f^\ast \circ \eta_A = f&quot; /&gt; by the Kleisli laws. Observe that the functor &lt;img src=&quot;http://s0.wp.com/latex.php?latex=G_T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G_T&quot; class=&quot;latex&quot; title=&quot;G_T&quot; /&gt; is the one that does all the work, while the function &lt;img src=&quot;http://s0.wp.com/latex.php?latex=F_T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F_T&quot; class=&quot;latex&quot; title=&quot;F_T&quot; /&gt; is little more than a placeholder.&lt;/p&gt;
&lt;p&gt;By our identification of adjunctions with adjunction quadruples (see the previous talk) we also get &lt;img src=&quot;http://s0.wp.com/latex.php?latex=F_T+f+%3D+%28%5Ceta_B+%5Ccirc+f%29%5E%5Csharp+%3D+%5Ceta_B+%5Ccirc+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F_T f = (\eta_B \circ f)^\sharp = \eta_B \circ f&quot; class=&quot;latex&quot; title=&quot;F_T f = (\eta_B \circ f)^\sharp = \eta_B \circ f&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%5Cin+%5Cmathcal%7BC%7D%28A%2CB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f \in \mathcal{C}(A,B)&quot; class=&quot;latex&quot; title=&quot;f \in \mathcal{C}(A,B)&quot; /&gt;, and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28%5Cvarepsilon_T%29_S+%3D+%28%5Cmathrm%7Bid%7D_%7BG_T+S%7D%5E%7B%5Cmathcal%7BC%7D%7D%29%5E%5Csharp+%3D+%5Cmathrm%7Bid%7D_%7BTS%7D+%5Cin+%5Cmathcal%7BC%7D%28G_TF_TS%2C+TS%29+%3D+%5Cmathcal%7BC%7D_T%28F_TG_TS%2C+S%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(\varepsilon_T)_S = (\mathrm{id}_{G_T S}^{\mathcal{C}})^\sharp = \mathrm{id}_{TS} \in \mathcal{C}(G_TF_TS, TS) = \mathcal{C}_T(F_TG_TS, S)&quot; class=&quot;latex&quot; title=&quot;(\varepsilon_T)_S = (\mathrm{id}_{G_T S}^{\mathcal{C}})^\sharp = \mathrm{id}_{TS} \in \mathcal{C}(G_TF_TS, TS) = \mathcal{C}_T(F_TG_TS, S)&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=S+%5Cin+%7C%5Cmathcal%7BC%7D_T%7C+%3D+%7C%5Cmathcal%7BC%7D%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;S \in |\mathcal{C}_T| = |\mathcal{C}|&quot; class=&quot;latex&quot; title=&quot;S \in |\mathcal{C}_T| = |\mathcal{C}|&quot; /&gt;.&lt;/p&gt;
&lt;p&gt;Kleisli’s solution is not the only one, but just one among many: and, in a sense that will be clear later, the “simplest” one. Another solution was constructed by Eilenberg and Moore, and is based on a completely different approach: instead of keeping the objects and specializing the morphisms, one expands the objects and redefines the morphisms.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Definition 6.&lt;/strong&gt; Let &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt; be a category and let &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+%28T%2C+%5Ceta%2C+%5Cmu%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = (T, \eta, \mu)&quot; class=&quot;latex&quot; title=&quot;T = (T, \eta, \mu)&quot; /&gt; be a monad on &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt;.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A &lt;em&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;-algebra&lt;/em&gt; on &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt; is a pair &lt;img src=&quot;http://s0.wp.com/latex.php?latex=a+%3D+%28A%2Ca%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;a = (A,a)&quot; class=&quot;latex&quot; title=&quot;a = (A,a)&quot; /&gt; where &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A&quot; class=&quot;latex&quot; title=&quot;A&quot; /&gt; is an object in &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=a+%3A+TA+%5Cto+A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;a : TA \to A&quot; class=&quot;latex&quot; title=&quot;a : TA \to A&quot; /&gt; is such that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=a+%5Ccirc+%5Ceta_A+%3D+%5Cmathrm%7Bid%7D_A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;a \circ \eta_A = \mathrm{id}_A&quot; class=&quot;latex&quot; title=&quot;a \circ \eta_A = \mathrm{id}_A&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=a+%5Ccirc+%5Cmu_A+%3D+a+%5Ccirc+Ta&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;a \circ \mu_A = a \circ Ta&quot; class=&quot;latex&quot; title=&quot;a \circ \mu_A = a \circ Ta&quot; /&gt;.&lt;/li&gt;
&lt;li&gt;A morphism of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;-algebras from a &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;-algebra &lt;img src=&quot;http://s0.wp.com/latex.php?latex=a+%3D+%28A%2Ca%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;a = (A,a)&quot; class=&quot;latex&quot; title=&quot;a = (A,a)&quot; /&gt; to a &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;-algebra &lt;img src=&quot;http://s0.wp.com/latex.php?latex=b+%3D+%28B%2Cb%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;b = (B,b)&quot; class=&quot;latex&quot; title=&quot;b = (B,b)&quot; /&gt; is an arrow &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%5Cin+%5Cmathcal%7BC%7D%28A%2CB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f \in \mathcal{C}(A,B)&quot; class=&quot;latex&quot; title=&quot;f \in \mathcal{C}(A,B)&quot; /&gt; such that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=b+%5Ccirc+Tf+%3D+f+%5Ccirc+a&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;b \circ Tf = f \circ a&quot; class=&quot;latex&quot; title=&quot;b \circ Tf = f \circ a&quot; /&gt;.&lt;/li&gt;
&lt;li&gt;The category of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;-algebras on &lt;img src=&quot;http://s0.wp.com/latex.php?latex=C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;C&quot; class=&quot;latex&quot; title=&quot;C&quot; /&gt; is the category &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D%5ET&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}^T&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}^T&quot; /&gt; which has &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;-algebras as objects, morphisms of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;-algebras as morphisms, and where identities and composition are defined as in &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T%3DM&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T=M&quot; class=&quot;latex&quot; title=&quot;T=M&quot; /&gt; is the free monoid construction, then an &lt;img src=&quot;http://s0.wp.com/latex.php?latex=M&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;M&quot; class=&quot;latex&quot; title=&quot;M&quot; /&gt;-algebra is a function &lt;img src=&quot;http://s0.wp.com/latex.php?latex=a+%3A+A%5E%5Cast+%5Cto+A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;a : A^\ast \to A&quot; class=&quot;latex&quot; title=&quot;a : A^\ast \to A&quot; /&gt; such that&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=a%5Bx%5D+%3D+x&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;a[x] = x&quot; class=&quot;latex&quot; title=&quot;a[x] = x&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=x+%5Cin+A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;x \in A&quot; class=&quot;latex&quot; title=&quot;x \in A&quot; /&gt;, and&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=a%5Bu%5E1_1+%5Ccdots+u%5E1_%7Bn_1%7D+%5Ccdots+u%5Em_1+%5Ccdots+u%5Em_%7Bn_m%7D%5D+%3D+a%5Ba%5Bu%5E1_1+%5Ccdots+u%5E1_%7Bn_1%7D%5D+%5Ccdots+a%5Bu%5Em_1+%5Ccdots+u%5Em_%7Bn_m%7D%5D%5D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;a[u^1_1 \cdots u^1_{n_1} \cdots u^m_1 \cdots u^m_{n_m}] = a[a[u^1_1 \cdots u^1_{n_1}] \cdots a[u^m_1 \cdots u^m_{n_m}]]&quot; class=&quot;latex&quot; title=&quot;a[u^1_1 \cdots u^1_{n_1} \cdots u^m_1 \cdots u^m_{n_m}] = a[a[u^1_1 \cdots u^1_{n_1}] \cdots a[u^m_1 \cdots u^m_{n_m}]]&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=u%5E1_1%2C+%5Cldots%2C+u%5E1_%7Bn_1%7D%2C+%5Cldots%2C+u%5Em_1%2C+%5Cldots%2C+u%5Em_%7Bn_m%7D+%5Cin+A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;u^1_1, \ldots, u^1_{n_1}, \ldots, u^m_1, \ldots, u^m_{n_m} \in A&quot; class=&quot;latex&quot; title=&quot;u^1_1, \ldots, u^1_{n_1}, \ldots, u^m_1, \ldots, u^m_{n_m} \in A&quot; /&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt; is a monad, for every object &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A&quot; class=&quot;latex&quot; title=&quot;A&quot; /&gt; of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt; there is a &lt;em&gt;free &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;-algebra&lt;/em&gt; &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu_A+%3D+%28TA%2C+%5Cmu_A%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu_A = (TA, \mu_A)&quot; class=&quot;latex&quot; title=&quot;\mu_A = (TA, \mu_A)&quot; /&gt;, and every arrow &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%5Cin+%5Cmathcal%7BC%7D%28A%2CB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f \in \mathcal{C}(A,B)&quot; class=&quot;latex&quot; title=&quot;f \in \mathcal{C}(A,B)&quot; /&gt; induces a morphism of free &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;-algebras &lt;img src=&quot;http://s0.wp.com/latex.php?latex=Tf+%5Cin+%5Cmathcal%7BC%7D%5ET%28%5Cmu_A%2C+%5Cmu_B%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;Tf \in \mathcal{C}^T(\mu_A, \mu_B)&quot; class=&quot;latex&quot; title=&quot;Tf \in \mathcal{C}^T(\mu_A, \mu_B)&quot; /&gt;. Moreover, &lt;em&gt;any&lt;/em&gt; &lt;img src=&quot;http://s0.wp.com/latex.php?latex=a+%5Cin+%5Cmathcal%7BC%7D%28TA%2CA%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;a \in \mathcal{C}(TA,A)&quot; class=&quot;latex&quot; title=&quot;a \in \mathcal{C}(TA,A)&quot; /&gt;&lt;em&gt;&lt;/em&gt; is, by definition, also a morphism from &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28TA%2C+%5Cmu_A%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(TA, \mu_A)&quot; class=&quot;latex&quot; title=&quot;(TA, \mu_A)&quot; /&gt; to &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28TA%2C+a%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(TA, a)&quot; class=&quot;latex&quot; title=&quot;(TA, a)&quot; /&gt; in &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D%5ET&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}^T&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}^T&quot; /&gt;.&lt;/p&gt;
&lt;p&gt;This time, our plan is to construct an adjunction &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%5ET%2C+G%5ET%2C+%5Ceta%2C+%5Cmu%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F^T, G^T, \eta, \mu)&quot; class=&quot;latex&quot; title=&quot;(F^T, G^T, \eta, \mu)&quot; /&gt; such that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=F%5ET+%3A+%5Cmathcal%7BC%7D+%5Cto+%5Cmathcal%7BC%7D%5ET&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F^T : \mathcal{C} \to \mathcal{C}^T&quot; class=&quot;latex&quot; title=&quot;F^T : \mathcal{C} \to \mathcal{C}^T&quot; /&gt;, &lt;img src=&quot;http://s0.wp.com/latex.php?latex=G%5ET+%3A+%5Cmathcal%7BC%7D%5ET+%5Cto+%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G^T : \mathcal{C}^T \to \mathcal{C}&quot; class=&quot;latex&quot; title=&quot;G^T : \mathcal{C}^T \to \mathcal{C}&quot; /&gt;, &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+G%5ET+F%5ET&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = G^T F^T&quot; class=&quot;latex&quot; title=&quot;T = G^T F^T&quot; /&gt;, and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmu_A+%3D+G%5ET+%5Cvarepsilon_%7BF%5ET+A%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mu_A = G^T \varepsilon_{F^T A}&quot; class=&quot;latex&quot; title=&quot;\mu_A = G^T \varepsilon_{F^T A}&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A+%5Cin+%7C%5Cmathcal%7BC%7D%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A \in |\mathcal{C}|&quot; class=&quot;latex&quot; title=&quot;A \in |\mathcal{C}|&quot; /&gt;. We do this as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=F%5ET+A+%3D+%5Cmu_A+%3D+%28TA%2C+%5Cmu_A%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F^T A = \mu_A = (TA, \mu_A)&quot; class=&quot;latex&quot; title=&quot;F^T A = \mu_A = (TA, \mu_A)&quot; /&gt;;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=F%5ET+f+%3D+Tf&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F^T f = Tf&quot; class=&quot;latex&quot; title=&quot;F^T f = Tf&quot; /&gt;;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=G%5ET+a+%3D+A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G^T a = A&quot; class=&quot;latex&quot; title=&quot;G^T a = A&quot; /&gt; if &lt;img src=&quot;http://s0.wp.com/latex.php?latex=a+%3A+TA+%5Cto+A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;a : TA \to A&quot; class=&quot;latex&quot; title=&quot;a : TA \to A&quot; /&gt;;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=G%5ET+f+%3D+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G^T f = f&quot; class=&quot;latex&quot; title=&quot;G^T f = f&quot; /&gt;;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cvarepsilon%5ET_a+%3D+a&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\varepsilon^T_a = a&quot; class=&quot;latex&quot; title=&quot;\varepsilon^T_a = a&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=a+%3D+%28A%2Ca%29+%5Cin+%7C%5Cmathcal%7BC%7D%5ET%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;a = (A,a) \in |\mathcal{C}^T|&quot; class=&quot;latex&quot; title=&quot;a = (A,a) \in |\mathcal{C}^T|&quot; /&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Then clearly &lt;img src=&quot;http://s0.wp.com/latex.php?latex=G%5ET+F%5ET+%3D+T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G^T F^T = T&quot; class=&quot;latex&quot; title=&quot;G^T F^T = T&quot; /&gt;, while naturality of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cvarepsilon&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\varepsilon&quot; class=&quot;latex&quot; title=&quot;\varepsilon&quot; /&gt; follows from the properties of free &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;-algebras with respect to &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;-algebra morphisms. In addition, if &lt;img src=&quot;http://s0.wp.com/latex.php?latex=S+%3D+a+%3D+%28A%2Ca%29+%5Cin+%7C%5Cmathcal%7BC%7D%5ET%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;S = a = (A,a) \in |\mathcal{C}^T|&quot; class=&quot;latex&quot; title=&quot;S = a = (A,a) \in |\mathcal{C}^T|&quot; /&gt; then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=G%5ET+%5Cvarepsilon%5ET_S+%5Ccirc+%5Ceta_%7BG%5ET+S%7D+%3D+a+%5Ccirc+%5Ceta_A+%3D+%5Cmathrm%7Bid%7D_%7BG%5ET+S%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G^T \varepsilon^T_S \circ \eta_{G^T S} = a \circ \eta_A = \mathrm{id}_{G^T S}&quot; class=&quot;latex&quot; title=&quot;G^T \varepsilon^T_S \circ \eta_{G^T S} = a \circ \eta_A = \mathrm{id}_{G^T S}&quot; /&gt;, and if &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A+%5Cin+%5Cmathrm%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A \in \mathrm{C}&quot; class=&quot;latex&quot; title=&quot;A \in \mathrm{C}&quot; /&gt; then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cvarepsilon%5ET_%7BF%5ET+A%7D+%5Ccirc+F%5ET+%5Ceta_A+%3D+%5Cmu_A+%5Ccirc+T%5Ceta_A+%5Cmathrm%7Bid%7D_%7BTA%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\varepsilon^T_{F^T A} \circ F^T \eta_A = \mu_A \circ T\eta_A \mathrm{id}_{TA}&quot; class=&quot;latex&quot; title=&quot;\varepsilon^T_{F^T A} \circ F^T \eta_A = \mu_A \circ T\eta_A \mathrm{id}_{TA}&quot; /&gt;. We thus have a full-featured adjunction: this time, &lt;img src=&quot;http://s0.wp.com/latex.php?latex=F%5ET&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F^T&quot; class=&quot;latex&quot; title=&quot;F^T&quot; /&gt; is doing all the work, and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=G%5ET&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G^T&quot; class=&quot;latex&quot; title=&quot;G^T&quot; /&gt; is just a forgetful functor.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Theorem 3.&lt;/strong&gt; Let &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+%28T%2C+%5Ceta%2C+%5Cmu%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = (T, \eta, \mu)&quot; class=&quot;latex&quot; title=&quot;T = (T, \eta, \mu)&quot; /&gt; be a monad on a &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt;. Identify the monad &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt; with the corresponding Kleisli triple &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+%28T%2C+%5Ceta%2C+%28%5Ccdot%29%5E%5Cast%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = (T, \eta, (\cdot)^\ast)&quot; class=&quot;latex&quot; title=&quot;T = (T, \eta, (\cdot)^\ast)&quot; /&gt;. The Kleisli category &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D_T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}_T&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}_T&quot; /&gt; is equivalent to the full subcategory of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D%5ET&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}^T&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}^T&quot; /&gt; generated by the free &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;-algebras.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Proof:&lt;/em&gt; Define a functor &lt;img src=&quot;http://s0.wp.com/latex.php?latex=J+%3A+%5Cmathcal%7BC%7D_T+%5Cto+%5Cmathcal%7BC%7D%5ET&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;J : \mathcal{C}_T \to \mathcal{C}^T&quot; class=&quot;latex&quot; title=&quot;J : \mathcal{C}_T \to \mathcal{C}^T&quot; /&gt; by setting &lt;img src=&quot;http://s0.wp.com/latex.php?latex=JA+%3D+%5Cmu_A+%3D+%28TA%2C+%5Cmu_A%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;JA = \mu_A = (TA, \mu_A)&quot; class=&quot;latex&quot; title=&quot;JA = \mu_A = (TA, \mu_A)&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=A+%5Cin+%7C%5Cmathcal%7BC%7D_T%7C+%3D+%7C%5Cmathcal%7BC%7D%7C&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;A \in |\mathcal{C}_T| = |\mathcal{C}|&quot; class=&quot;latex&quot; title=&quot;A \in |\mathcal{C}_T| = |\mathcal{C}|&quot; /&gt;, and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=Jf+%3D+%5Cmu_B+%5Ccirc+Tf+%3D+f%5E%5Cast+%5Cin+%5Cmathcal%7BC%7D%5ET%28JA%2C+JB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;Jf = \mu_B \circ Tf = f^\ast \in \mathcal{C}^T(JA, JB)&quot; class=&quot;latex&quot; title=&quot;Jf = \mu_B \circ Tf = f^\ast \in \mathcal{C}^T(JA, JB)&quot; /&gt; for every &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%5Cin+%5Cmathcal%7BC%7D_T%28A%2CB%29+%3D+%5Cmathcal%7BC%7D%28A%2C+TB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f \in \mathcal{C}_T(A,B) = \mathcal{C}(A, TB)&quot; class=&quot;latex&quot; title=&quot;f \in \mathcal{C}_T(A,B) = \mathcal{C}(A, TB)&quot; /&gt;. Then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=J&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;J&quot; class=&quot;latex&quot; title=&quot;J&quot; /&gt; is a faithful functor, because if &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f%2Cg+%5Cin+%5Cmathcal%7BC%7D_T%28A%2CB%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f,g \in \mathcal{C}_T(A,B)&quot; class=&quot;latex&quot; title=&quot;f,g \in \mathcal{C}_T(A,B)&quot; /&gt;, then &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%3D+%5Cmu_B+%5Ccirc+%5Ceta_%7BTB%7D+%5Ccirc+f+%3D+%5Cmu_B+%5Ccirc+Tf+%5Ccirc+%5Ceta_A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f = \mu_B \circ \eta_{TB} \circ f = \mu_B \circ Tf \circ \eta_A&quot; class=&quot;latex&quot; title=&quot;f = \mu_B \circ \eta_{TB} \circ f = \mu_B \circ Tf \circ \eta_A&quot; /&gt; and similarly &lt;img src=&quot;http://s0.wp.com/latex.php?latex=g+%3D+%5Cmu_B+%5Ccirc+%5Ceta_%7BTB%7D+%5Ccirc+g+%3D+%5Cmu_B+%5Ccirc+Tg+%5Ccirc+%5Ceta_A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;g = \mu_B \circ \eta_{TB} \circ g = \mu_B \circ Tg \circ \eta_A&quot; class=&quot;latex&quot; title=&quot;g = \mu_B \circ \eta_{TB} \circ g = \mu_B \circ Tg \circ \eta_A&quot; /&gt;, so that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%3D+g&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f = g&quot; class=&quot;latex&quot; title=&quot;f = g&quot; /&gt; if &lt;img src=&quot;http://s0.wp.com/latex.php?latex=Jf+%3D+Jg&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;Jf = Jg&quot; class=&quot;latex&quot; title=&quot;Jf = Jg&quot; /&gt;. But &lt;img src=&quot;http://s0.wp.com/latex.php?latex=J&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;J&quot; class=&quot;latex&quot; title=&quot;J&quot; /&gt; is also full, because if &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%3A+%28TA%2C+%5Cmu_A%29+%5Cto+%28TB%2C+%5Cmu_B%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f : (TA, \mu_A) \to (TB, \mu_B)&quot; class=&quot;latex&quot; title=&quot;f : (TA, \mu_A) \to (TB, \mu_B)&quot; /&gt; is a morphism of free &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;-algebras, then from the laws of monads follows that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f+%3D+J%28f+%5Ccirc+%5Ceta_A%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f = J(f \circ \eta_A)&quot; class=&quot;latex&quot; title=&quot;f = J(f \circ \eta_A)&quot; /&gt;. &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5CBox&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\Box&quot; class=&quot;latex&quot; title=&quot;\Box&quot; /&gt;&lt;/p&gt;
&lt;p&gt;But things get even more interesting than this! Let &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+%28T%2C+%5Ceta%2C+%5Cmu%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = (T, \eta, \mu)&quot; class=&quot;latex&quot; title=&quot;T = (T, \eta, \mu)&quot; /&gt; be a monad: let us consider &lt;em&gt;all&lt;/em&gt; the adjunctions &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2C+G%2C+%5Ceta%2C+%5Cvarepsilon%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F, G, \eta, \varepsilon)&quot; class=&quot;latex&quot; title=&quot;(F, G, \eta, \varepsilon)&quot; /&gt; that generate &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;. What can be a &lt;em&gt;morphism&lt;/em&gt; of such adjunctions? First, if &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2C+G%2C+%5Ceta%2C+%5Cvarepsilon%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F, G, \eta, \varepsilon)&quot; class=&quot;latex&quot; title=&quot;(F, G, \eta, \varepsilon)&quot; /&gt; is a solution with &lt;img src=&quot;http://s0.wp.com/latex.php?latex=F+%3A+%5Cmathcal%7BC%7D+%5Cto+%5Cmathcal%7BD%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F : \mathcal{C} \to \mathcal{D}&quot; class=&quot;latex&quot; title=&quot;F : \mathcal{C} \to \mathcal{D}&quot; /&gt;, and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%27%2C+G%27%2C+%5Ceta%2C+%5Cvarepsilon%27%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F', G', \eta, \varepsilon')&quot; class=&quot;latex&quot; title=&quot;(F', G', \eta, \varepsilon')&quot; /&gt; is a solution with &lt;img src=&quot;http://s0.wp.com/latex.php?latex=F%27+%3A+%5Cmathcal%7BC%7D+%5Cto+%5Cmathcal%7BD%7D%27&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F' : \mathcal{C} \to \mathcal{D}'&quot; class=&quot;latex&quot; title=&quot;F' : \mathcal{C} \to \mathcal{D}'&quot; /&gt;, we may consider a functor &lt;img src=&quot;http://s0.wp.com/latex.php?latex=L+%3A+%5Cmathcal%7BD%7D+%5Cto+%5Cmathcal%7BD%27%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;L : \mathcal{D} \to \mathcal{D'}&quot; class=&quot;latex&quot; title=&quot;L : \mathcal{D} \to \mathcal{D'}&quot; /&gt; as a morphism from &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2C+G%2C+%5Ceta%2C+%5Cvarepsilon%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F, G, \eta, \varepsilon)&quot; class=&quot;latex&quot; title=&quot;(F, G, \eta, \varepsilon)&quot; /&gt; to &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%27%2C+G%27%2C+%5Ceta%2C+%5Cvarepsilon%27%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F', G', \eta, \varepsilon')&quot; class=&quot;latex&quot; title=&quot;(F', G', \eta, \varepsilon')&quot; /&gt;. Next, we want that the equalities &lt;img src=&quot;http://s0.wp.com/latex.php?latex=GF+%3D+T+%3D+G%27F%27&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;GF = T = G'F'&quot; class=&quot;latex&quot; title=&quot;GF = T = G'F'&quot; /&gt; are not affected by mid-way application of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=L&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;L&quot; class=&quot;latex&quot; title=&quot;L&quot; /&gt;: this translates into the two conditions &lt;img src=&quot;http://s0.wp.com/latex.php?latex=L+%5Ccirc+F+%3D+F%27&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;L \circ F = F'&quot; class=&quot;latex&quot; title=&quot;L \circ F = F'&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=G%27+%5Ccirc+L+%3D+G&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G' \circ L = G&quot; class=&quot;latex&quot; title=&quot;G' \circ L = G&quot; /&gt;. Finally, as the previous point yields &lt;img src=&quot;http://s0.wp.com/latex.php?latex=LFG+%3D+F%27G%27L&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;LFG = F'G'L&quot; class=&quot;latex&quot; title=&quot;LFG = F'G'L&quot; /&gt;, we want that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=L&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;L&quot; class=&quot;latex&quot; title=&quot;L&quot; /&gt; does not interfere with the counits: that is, &lt;img src=&quot;http://s0.wp.com/latex.php?latex=L%5Cvarepsilon+%3D+%5Cvarepsilon%27_L&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;L\varepsilon = \varepsilon'_L&quot; class=&quot;latex&quot; title=&quot;L\varepsilon = \varepsilon'_L&quot; /&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Definition 7.&lt;/strong&gt; Let &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+%28T%2C+%5Ceta%2C+%5Cmu%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = (T, \eta, \mu)&quot; class=&quot;latex&quot; title=&quot;T = (T, \eta, \mu)&quot; /&gt; be a monad on a category &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BC%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{C}&quot; class=&quot;latex&quot; title=&quot;\mathcal{C}&quot; /&gt; and let &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2C+G%2C+%5Ceta%2C+%5Cvarepsilon%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F, G, \eta, \varepsilon)&quot; class=&quot;latex&quot; title=&quot;(F, G, \eta, \varepsilon)&quot; /&gt;, &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%27%2C+G%27%2C+%5Ceta%2C+%5Cvarepsilon%27%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F', G', \eta, \varepsilon')&quot; class=&quot;latex&quot; title=&quot;(F', G', \eta, \varepsilon')&quot; /&gt; be two adjunctions that generate &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;, with &lt;img src=&quot;http://s0.wp.com/latex.php?latex=F+%3A+%5Cmathcal%7BC%7D+%5Cto+%5Cmathcal%7BD%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F : \mathcal{C} \to \mathcal{D}&quot; class=&quot;latex&quot; title=&quot;F : \mathcal{C} \to \mathcal{D}&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=F%27+%3A+%5Cmathcal%7BC%7D+%5Cto+%5Cmathcal%7BD%7D%27&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F' : \mathcal{C} \to \mathcal{D}'&quot; class=&quot;latex&quot; title=&quot;F' : \mathcal{C} \to \mathcal{D}'&quot; /&gt;, respectively. A &lt;em&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;-preserving functor&lt;/em&gt; from &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2C+G%2C+%5Ceta%2C+%5Cvarepsilon%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F, G, \eta, \varepsilon)&quot; class=&quot;latex&quot; title=&quot;(F, G, \eta, \varepsilon)&quot; /&gt; to &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%27%2C+G%27%2C+%5Ceta%2C+%5Cvarepsilon%27%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F', G', \eta, \varepsilon')&quot; class=&quot;latex&quot; title=&quot;(F', G', \eta, \varepsilon')&quot; /&gt; is a functor &lt;img src=&quot;http://s0.wp.com/latex.php?latex=L+%3A+%5Cmathcal%7BD%7D+%5Cto+%5Cmathcal%7BD%7D%27&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;L : \mathcal{D} \to \mathcal{D}'&quot; class=&quot;latex&quot; title=&quot;L : \mathcal{D} \to \mathcal{D}'&quot; /&gt; such that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=L+%5Ccirc+F+%3D+F%27&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;L \circ F = F'&quot; class=&quot;latex&quot; title=&quot;L \circ F = F'&quot; /&gt;, &lt;img src=&quot;http://s0.wp.com/latex.php?latex=G%27+%5Ccirc+L+%3D+G&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G' \circ L = G&quot; class=&quot;latex&quot; title=&quot;G' \circ L = G&quot; /&gt;, and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=L%5Cvarepsilon+%3D+%5Cvarepsilon%27_L&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;L\varepsilon = \varepsilon'_L&quot; class=&quot;latex&quot; title=&quot;L\varepsilon = \varepsilon'_L&quot; /&gt;.&lt;/p&gt;
&lt;p&gt;It is straightforward to see that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;-generating adjunctions together with &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T&quot; class=&quot;latex&quot; title=&quot;T&quot; /&gt;-preserving functors form a category &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathrm%7BAdj%7D%28T%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathrm{Adj}(T)&quot; class=&quot;latex&quot; title=&quot;\mathrm{Adj}(T)&quot; /&gt;: composition is provided by the usual composition of functors, while the identity of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2C+G%2C+%5Ceta%2C+%5Cvarepsilon%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F, G, \eta, \varepsilon)&quot; class=&quot;latex&quot; title=&quot;(F, G, \eta, \varepsilon)&quot; /&gt; in &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathrm%7BAdj%7D%28T%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathrm{Adj}(T)&quot; class=&quot;latex&quot; title=&quot;\mathrm{Adj}(T)&quot; /&gt; is the identity functor of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathcal%7BD%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathcal{D}&quot; class=&quot;latex&quot; title=&quot;\mathcal{D}&quot; /&gt; if &lt;img src=&quot;http://s0.wp.com/latex.php?latex=F+%3A+%5Cmathcal%7BC%7D+%5Cto+%5Cmathcal%7BD%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;F : \mathcal{C} \to \mathcal{D}&quot; class=&quot;latex&quot; title=&quot;F : \mathcal{C} \to \mathcal{D}&quot; /&gt;.&lt;/p&gt;
&lt;p&gt;To confirm that our intuition is correct, let us verify that &lt;img src=&quot;http://s0.wp.com/latex.php?latex=J&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;J&quot; class=&quot;latex&quot; title=&quot;J&quot; /&gt; satisfies the three given equations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=J+F_T+A+%3D+JA+%3D+%28T_A%2C+%5Cmu_A%29+%3D+F%5ET+A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;J F_T A = JA = (T_A, \mu_A) = F^T A&quot; class=&quot;latex&quot; title=&quot;J F_T A = JA = (T_A, \mu_A) = F^T A&quot; /&gt;;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=J+F_T+f+%3D+%5Cmu_B+%5Ccirc+T%28%5Ceta_B+%5Ccirc+f%29+%3D+%5Cmu_B+%5Ccirc+T%5Ceta_B+%5Ccirc+Tf+%3D+%5Cmathrm%7Bid%7D_%7BTB%7D+%5Ccirc+Tf+%3D+F%5ETf&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;J F_T f = \mu_B \circ T(\eta_B \circ f) = \mu_B \circ T\eta_B \circ Tf = \mathrm{id}_{TB} \circ Tf = F^Tf&quot; class=&quot;latex&quot; title=&quot;J F_T f = \mu_B \circ T(\eta_B \circ f) = \mu_B \circ T\eta_B \circ Tf = \mathrm{id}_{TB} \circ Tf = F^Tf&quot; /&gt;;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=G%5ET+J+A+%3D+G%5ET+%28TA%2C+%5Cmu_A%29+%3D+TA+%3D+G_T+A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G^T J A = G^T (TA, \mu_A) = TA = G_T A&quot; class=&quot;latex&quot; title=&quot;G^T J A = G^T (TA, \mu_A) = TA = G_T A&quot; /&gt;;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=G%5ET+J+f+%3D+G%5ET+%28%5Cmu_B+%5Ccirc+Tf%29+%3D+%5Cmu_B+%5Ccirc+Tf+%3D+f%5E%5Cast+%3D+G_T+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;G^T J f = G^T (\mu_B \circ Tf) = \mu_B \circ Tf = f^\ast = G_T f&quot; class=&quot;latex&quot; title=&quot;G^T J f = G^T (\mu_B \circ Tf) = \mu_B \circ Tf = f^\ast = G_T f&quot; /&gt;;&lt;/li&gt;
&lt;li&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=J%28%5Cvarepsilon_T%29_S+%3D+J%5Cmathrm%7Bid%7D_%7BTS%7D%3D+%5Cmu_S+%5Ccirc+T%5Cmathrm%7Bid%7D_%7BTS%7D+%3D+%5Cmu_S+%3D+%5Cvarepsilon%5ET_%7B%28TS%2C+%5Cmu_S%29%7D+%3D+%5Cvarepsilon%5ET_%7BJS%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;J(\varepsilon_T)_S = J\mathrm{id}_{TS}= \mu_S \circ T\mathrm{id}_{TS} = \mu_S = \varepsilon^T_{(TS, \mu_S)} = \varepsilon^T_{JS}&quot; class=&quot;latex&quot; title=&quot;J(\varepsilon_T)_S = J\mathrm{id}_{TS}= \mu_S \circ T\mathrm{id}_{TS} = \mu_S = \varepsilon^T_{(TS, \mu_S)} = \varepsilon^T_{JS}&quot; /&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Theorem 4.&lt;/strong&gt; Let &lt;img src=&quot;http://s0.wp.com/latex.php?latex=T+%3D+%28T%2C+%5Ceta%2C+%5Cmu%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;T = (T, \eta, \mu)&quot; class=&quot;latex&quot; title=&quot;T = (T, \eta, \mu)&quot; /&gt; be a monad. Then the Kleisli adjunction is the initial object of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathrm%7BAdj%7D%28T%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathrm{Adj}(T)&quot; class=&quot;latex&quot; title=&quot;\mathrm{Adj}(T)&quot; /&gt;, the Eilenberg-Moore adjunction is the final object. In particular, &lt;img src=&quot;http://s0.wp.com/latex.php?latex=J&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;J&quot; class=&quot;latex&quot; title=&quot;J&quot; /&gt; is the only arrow in &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cmathrm%7BAdj%7D%28T%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\mathrm{Adj}(T)&quot; class=&quot;latex&quot; title=&quot;\mathrm{Adj}(T)&quot; /&gt; from the former to the latter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Proof:&lt;/em&gt; If &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%2C+G%2C+%5Ceta%2C+%5Cvarepsilon%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F, G, \eta, \varepsilon)&quot; class=&quot;latex&quot; title=&quot;(F, G, \eta, \varepsilon)&quot; /&gt; is the Kleisli adjunction, then the only choice for &lt;img src=&quot;http://s0.wp.com/latex.php?latex=L&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;L&quot; class=&quot;latex&quot; title=&quot;L&quot; /&gt; is &lt;img src=&quot;http://s0.wp.com/latex.php?latex=LA+%3D+F%27A&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;LA = F'A&quot; class=&quot;latex&quot; title=&quot;LA = F'A&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=Lf+%3D+%5Cvarepsilon%27_%7BF%27B%7D+%5Ccirc+F%27+f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;Lf = \varepsilon'_{F'B} \circ F' f&quot; class=&quot;latex&quot; title=&quot;Lf = \varepsilon'_{F'B} \circ F' f&quot; /&gt;. If &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%28F%27%2C+G%27%2C+%5Ceta%2C+%5Cvarepsilon%27%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;(F', G', \eta, \varepsilon')&quot; class=&quot;latex&quot; title=&quot;(F', G', \eta, \varepsilon')&quot; /&gt; is the Eilenberg-Moore adjunction, then the only choice for &lt;img src=&quot;http://s0.wp.com/latex.php?latex=L&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;L&quot; class=&quot;latex&quot; title=&quot;L&quot; /&gt; is &lt;img src=&quot;http://s0.wp.com/latex.php?latex=LS+%3D+%28GS%2C+G+%5Cvarepsilon_S%29&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;LS = (GS, G \varepsilon_S)&quot; class=&quot;latex&quot; title=&quot;LS = (GS, G \varepsilon_S)&quot; /&gt; and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=Lf+%3D+Gf&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;Lf = Gf&quot; class=&quot;latex&quot; title=&quot;Lf = Gf&quot; /&gt;. &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5CBox&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\Box&quot; class=&quot;latex&quot; title=&quot;\Box&quot; /&gt;&lt;/p&gt;
&lt;br /&gt;  &lt;a href=&quot;http://feeds.wordpress.com/1.0/gocomments/theorylunch.wordpress.com/885/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/comments/theorylunch.wordpress.com/885/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;img src=&quot;http://stats.wordpress.com/b.gif?host=theorylunch.wordpress.com&amp;amp;blog=43735749&amp;amp;post=885&amp;amp;subd=theorylunch&amp;amp;ref=&amp;amp;feed=1&quot; alt=&quot;&quot; height=&quot;1&quot; border=&quot;0&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Mon, 10 Jun 2013 11:27:02 +0000</pubDate>
</item>
<item>
	<title>Ken T Takusagawa: [sjhltpdo] Unscope symbol</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-6757805.post-3347733635441803703</guid>
	<link>http://kenta.blogspot.com/2013/06/sjhltpdo-unscope-symbol.html</link>
	<description>&lt;p dir=&quot;ltr&quot;&gt;&lt;code&gt;let { foo = ... } in let { hide foo } in foo&lt;/code&gt;&lt;/p&gt;&lt;p dir=&quot;ltr&quot;&gt;This should cause the compiler to signal an error.  We wish to assert that a certain symbol is not used within an inner scope, perhaps to avoid programmer typos of similar symbols.  &quot;hide foo&quot; can also be a statement in &lt;code&gt;do&lt;/code&gt; notation.&lt;/p&gt;&lt;p dir=&quot;ltr&quot;&gt;If we have &lt;a href=&quot;http://kenta.blogspot.com/2013/04/isjnkupe-local-types-and-imports.html&quot;&gt;local imports&lt;/a&gt;, then perhaps syntax like &lt;code&gt;let { import OUTER-SCOPE hiding (foo) }&lt;/code&gt;, where &lt;code&gt;OUTER-SCOPE&lt;/code&gt; is a new keyword.&lt;/p&gt;&lt;p dir=&quot;ltr&quot;&gt;We could also hide everything in the outer scope except a few symbols.  &lt;code&gt;let { import OUTER-SCOPE(foo); import Prelude }&lt;/code&gt;.&lt;/p&gt;</description>
	<pubDate>Mon, 10 Jun 2013 08:49:00 +0000</pubDate>
	<author>noreply@blogger.com (Ken)</author>
</item>
<item>
	<title>wren ng thornton: Dungeon World: Assassin class</title>
	<guid isPermaLink="false">tag:dreamwidth.org,2010-05-25:518115:84469</guid>
	<link>http://winterkoninkje.dreamwidth.org/84469.html</link>
	<description>&lt;p&gt;After far too long, I've finally found a new roleplaying group. We're using &lt;a href=&quot;http://www.dungeon-world.com/&quot;&gt;Dungeon World&lt;/a&gt;, a lightweight system I've never used before. It's a class-based system, which I've never been too fond of, but it does seem like it gets rid of most of the things I hate about class-based systems. The core book only gives the standard D&amp;amp;D-style classes, but they give some guidelines on making up your own classes. For my character I worked with the GM to come up with a new &lt;a href=&quot;http://llama.freegeek.org/~wren/resources/blog/assassin.pdf&quot;&gt;Assassin&lt;/a&gt; class which combines some of the traits of the Thief and the Fighter. I tried to make sure it's balanced against the other classes and doesn't obviate the Thief/Fighter, but not having used DW before it's hard to be sure. If you've used DW and have any comments, I'd be interested in hearing them.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Edit:&lt;/i&gt; I've posted a new version which adjusts the damage option for the Death Dealer and Assassin's Strike moves. Also included is a discussion about how different ways of doing DD/AS would affect DPS, which is necessary for doing a fair comparison against other classes. If you run a game with this class, let me know how it goes.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src=&quot;http://www.dreamwidth.org/tools/commentcount?user=winterkoninkje&amp;amp;ditemid=84469&quot; alt=&quot;comment count unavailable&quot; height=&quot;12&quot; style=&quot;vertical-align: middle;&quot; width=&quot;30&quot; /&gt; comments</description>
	<pubDate>Sun, 09 Jun 2013 00:32:03 +0000</pubDate>
</item>
<item>
	<title>Paul Potts: Objective-C, Day 5</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-21500237.post-2810558068229319785</guid>
	<link>http://praisecurseandrecurse.blogspot.com/2013/06/objective-c-day-5.html</link>
	<description>&lt;p&gt;(Warning: non-FP content). (I've been including this for the benefit of any &quot;Planet X&quot; aggregators who are including my feed, where X is a functional language like Haskell. I'm assuming you've got that by now and either don't care or are unsubscribing/ignoring this series if it bothers you. I expect to get back to more functional language code at some point, maybe even implementing the same problem. But I dabble in different languages and sometimes the digressions go on for a while...)&lt;/p&gt; &lt;p&gt;So, today's topic is &lt;b&gt;dispatch&lt;/b&gt;. I'm starting to design and implement the logic for pieces interacting on the board. The key in object-oriented designs is always to determine who (which object) manages which state. The design I've come up with is a sort of hybrid design, where position on the board is not actually a property of the board tiles. Instead, there's a board which keeps track of them. The instances of, say, a tree on the board don't have any unique state, so I'm not instantiating different objects for each one; they all point to a singleton (which I should properly enforce with a singleton factory method at some point).&lt;/p&gt; &lt;p&gt;There are trade-offs. On paper, my design involved adding a &quot;push&quot; method to the classes for tile pieces. In a language like Dylan, this &quot;push&quot; method would be a generic function, and dispatch on multiple parameter types, so that I could write some very short methods and use the method dispatcher, instead of explicit &lt;b&gt;if-then&lt;/b&gt; or &lt;b&gt;switch&lt;/b&gt; logic to find the right bit of code at run-time according to the types of the interacting objects (either their literal types or an enum, or some such). I could even do this in C++ because it has method overloading based on the parameters -- as long as this is &lt;a href=&quot;http://stackoverflow.com/questions/6897662/matching-an-overloaded-function-to-its-polymorphic-argument&quot;&gt;based on their static type known at compile-time&lt;/a&gt;. Which... isn't true in this case. I miss Dylan's generic function dispatch! &lt;a href=&quot;http://stackoverflow.com/questions/2286312/method-overloading-in-objective-c&quot;&gt;Objective-C is a underpowered&lt;/a&gt; in this respect even compared to C++. For example, I'd like to be able to write methods like this for the bomb class (this is pseudocode):&lt;/p&gt; &lt;pre&gt;bomb::push(mountain)&lt;br /&gt;{&lt;br /&gt;    // blow up the mountain&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;bomb::push(empty)&lt;br /&gt;{&lt;br /&gt;    // slide the bomb onto the tile&lt;br /&gt;}&lt;/pre&gt; &lt;p&gt;But I can't. I can't just use the class construct to organize methods to call without an instance -- there doesn't seem to be the equivalent of C++ static methods. There also is no equivalent of a pure virtual function; I can't declare the need for a push() method in the common base class of the tile pieces and have the compiler demand that I implement in any subclasses to make them instantiable. The closest I can come, I think, is to create a method that generates an error if it itself is called instead of being overridden in a subclass. That seems to lack semantic clarity.  So maybe I could do this, with &lt;a href=&quot;http://en.wikipedia.org/wiki/Double_dispatch&quot;&gt;double dispatch&lt;/a&gt;, but it doesn't seem worth the trouble for a small number of collision behaviors, when the behavior isn't simply supported by the language. I keep telling myself &quot;thin layer on top of C... thin layer on top of C...&quot;&lt;/p&gt; &lt;p&gt;So last night I had my Mac running upstairs in the office, and I used my iPad downstairs to connect to it via VNC, and write some code, running it on the iPad simulator, which I then viewed and controlled with a real iPad (mad scientist laugh). It needs a little tweaking this morning but here's more-or-less what I came up with. Note that I have started using some different naming conventions for file-scope and local variables. I'm not sure they are very standard Objective-C but they are closer to what I've grown comfortable with over the years in C and C++.&lt;/p&gt; &lt;pre&gt;typedef struct {&lt;br /&gt;    int x_idx;&lt;br /&gt;    int y_idx;&lt;br /&gt;} pos_t;&lt;br /&gt;&lt;br /&gt;typedef enum {&lt;br /&gt;    dir_east,&lt;br /&gt;    dir_west,&lt;br /&gt;    dir_south,&lt;br /&gt;    dir_north&lt;br /&gt;} dir_e;&lt;br /&gt;&lt;br /&gt;// A straight C function to return a pos_t updated with a dir_e;&lt;br /&gt;// the result may be out of bounds&lt;br /&gt;pos_t getUpdatedPos( pos_t original_pos, dir_e dir );&lt;br /&gt;BOOL posValid( pos_t pos );&lt;br /&gt;&lt;br /&gt;static const int board_width = 24, board_height = 4;&lt;br /&gt;// The short board design is part of what makes it so&lt;br /&gt;// easy to get sliding objects stuck against the edges&lt;br /&gt;// of the world or in corners where you can't get the&lt;br /&gt;// penguin avatar around to the other side to push them.&lt;br /&gt;// We could consider a bigger board later and maybe&lt;br /&gt;// implement the original puzzles surrounded by water,&lt;br /&gt;// or something like that.&lt;br /&gt;&lt;br /&gt;// Equivalent of C++ class forward declaration&lt;br /&gt;@class ArcticSlideTile;&lt;br /&gt;@class ArcticSlideBomb;&lt;br /&gt;@class ArcticSlideEmpty;&lt;br /&gt;@class ArcticSlideHeart;&lt;br /&gt;@class ArcticSlideHouse;&lt;br /&gt;@class ArcticSlideIceBlock;&lt;br /&gt;@class ArcticSlideMountain;&lt;br /&gt;@class ArcticSlideTree;&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideModel : NSObject&lt;br /&gt;{&lt;br /&gt;    ArcticSlideTile* board[board_height][board_width];&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;- (id)init;&lt;br /&gt;- (id)initWithLevelIndex:(int)level_idx;&lt;br /&gt;- (NSString*) description;&lt;br /&gt;- (ArcticSlideTile*)getTileFromPosition:(pos_t)pos&lt;br /&gt;                            inDirection:(dir_e)dir;&lt;br /&gt;- (void)setTileAtPosition:(pos_t)pos&lt;br /&gt;                       to:(ArcticSlideTile*)type;&lt;br /&gt;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideTile : NSObject&lt;br /&gt;&lt;br /&gt;- (BOOL)pushFromPosition:(pos_t)pos inDirection:(dir_e)dir;&lt;br /&gt;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideBomb : ArcticSlideTile&lt;br /&gt;// Bombs can be pushed and will slide until they hit an&lt;br /&gt;// object and stop. If the object is a mountain, both bomb&lt;br /&gt;// and mountain are destroyed. If another object hits a bomb&lt;br /&gt;// it stops (I think -- I'm not sure it is possible to set &lt;br /&gt;// up a board such that you can slide something into a bomb).&lt;br /&gt;&lt;br /&gt;// push is called when the penguin pushes against a tile.&lt;br /&gt;// It returns YES if the penguin can move onto the tile with&lt;br /&gt;// this action. This is only ever the case for a tree or empty&lt;br /&gt;// tile.&lt;br /&gt;- (BOOL)pushFromPosition:(pos_t)pos inDirection:(dir_e)dir;&lt;br /&gt;- (NSString*) description;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideEmpty : ArcticSlideTile&lt;br /&gt;// The penguin can always step onto an empty tile&lt;br /&gt;- (BOOL)pushFromPosition:(pos_t)pos inDirection:(dir_e)dir;&lt;br /&gt;- (BOOL)slideFromPosition:(pos_t)pos inDirection:(dir_e)dir;&lt;br /&gt;- (NSString*) description;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideHeart : ArcticSlideTile&lt;br /&gt;// When a heart hits a house the heart disappears (getting&lt;br /&gt;// all the hearts into the house is how you win the game).&lt;br /&gt;// Otherwise they cannot be destroyed, and slide like other&lt;br /&gt;// slidable items.&lt;br /&gt;- (BOOL)pushFromPosition:(pos_t)pos inDirection:(dir_e)dir;&lt;br /&gt;- (NSString*) description;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideHouse : ArcticSlideTile&lt;br /&gt;// Houses cannot be pushed and stop other objects except&lt;br /&gt;// hearts. When a heart hits a house the heart disappears&lt;br /&gt;// (getting the hearts into the house is how you win the game).&lt;br /&gt;// So the model should keep track of the number of hearts&lt;br /&gt;// on the board and trigger a &quot;win the level&quot; behavior when&lt;br /&gt;// the last heart is removed.&lt;br /&gt;- (BOOL)pushFromPosition:(pos_t)pos inDirection:(dir_e)dir;&lt;br /&gt;- (NSString*) description;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideIceBlock : ArcticSlideTile&lt;br /&gt;// Ice blocks can be pushed and will slide until they hit&lt;br /&gt;// an object and stop. If they are pushed directly against&lt;br /&gt;// an object they will be crushed (there should be an animation)&lt;br /&gt;// and disappear.&lt;br /&gt;- (BOOL)pushFromPosition:(pos_t)pos inDirection:(dir_e)dir;&lt;br /&gt;- (NSString*) description;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideMountain : ArcticSlideTile&lt;br /&gt;// Mountains cannot be moved and are destroyed by bombs.&lt;br /&gt;- (BOOL)pushFromPosition:(pos_t)pos inDirection:(dir_e)dir;&lt;br /&gt;- (NSString*) description;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideTree : ArcticSlideTile&lt;br /&gt;// Trees cannot be pushed or destroyed and stop all sliding&lt;br /&gt;// objects, but the penguin avatar character can walk through&lt;br /&gt;// them.&lt;br /&gt;- (BOOL)pushFromPosition:(pos_t)pos inDirection:(dir_e)dir;&lt;br /&gt;- (NSString*) description;&lt;br /&gt;@end&lt;/pre&gt; &lt;p&gt;Here's part of the implementation. It seems way too wordy; I need to rethink the amount of code required for each step. At the least, some refactoring seems to be in order. As I mentioned earlier, I'm not sure the tile classes are really earning their keep. I got rid of the singleton instantiation machinery but now there are order of initialization dependencies. I'll need to do further thinking as I consider what communication needs to happen between the &quot;model&quot; part and &quot;controller&quot; part -- how to interact with the GUI, how to indicate that tiles need to be redrawn, or animated transitions should play, or sound effects should play, or that the score is changed, and what to do when a level is completed. There is lots more to think about for such a simple little game! And of course I haven't really even begun to implement the &quot;view&quot; parts.&lt;/p&gt; &lt;pre&gt;static ArcticSlideEmpty* empty_p;&lt;br /&gt;static ArcticSlideTree* tree_p;&lt;br /&gt;static ArcticSlideMountain* mountain_p;&lt;br /&gt;static ArcticSlideHouse* house_p;&lt;br /&gt;static ArcticSlideIceBlock* ice_block_p;&lt;br /&gt;static ArcticSlideHeart* heart_p;&lt;br /&gt;static ArcticSlideBomb* bomb_p;&lt;br /&gt;&lt;br /&gt;static ArcticSlideModel* model_p;&lt;br /&gt;&lt;br /&gt;pos_t getUpdatedPos( pos_t original_pos, dir_e dir )&lt;br /&gt;{&lt;br /&gt;    pos_t updated_pos = original_pos;&lt;br /&gt;    int x_offset = 0;&lt;br /&gt;    int y_offset = 0;&lt;br /&gt;    if ( dir_east == dir )&lt;br /&gt;    {&lt;br /&gt;        x_offset = 1;&lt;br /&gt;        y_offset = 0;&lt;br /&gt;    }&lt;br /&gt;    else if ( dir_west == dir )&lt;br /&gt;    {&lt;br /&gt;        x_offset = -1;&lt;br /&gt;        y_offset = 0;&lt;br /&gt;    }&lt;br /&gt;    else if ( dir_north == dir )&lt;br /&gt;    {&lt;br /&gt;        x_offset = 0;&lt;br /&gt;        y_offset = -1;&lt;br /&gt;    }&lt;br /&gt;    else if ( dir_south == dir )&lt;br /&gt;    {&lt;br /&gt;        x_offset = 0;&lt;br /&gt;        y_offset = +1;&lt;br /&gt;    }&lt;br /&gt;    updated_pos.x_idx += x_offset;;&lt;br /&gt;    updated_pos.y_idx += y_offset;&lt;br /&gt;    return updated_pos;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;BOOL posValid( pos_t pos )&lt;br /&gt;{&lt;br /&gt;    return ( ( ( pos.x_idx &amp;gt;= 0 ) ||&lt;br /&gt;               ( pos.x_idx &amp;lt; board_width  ) ) ||&lt;br /&gt;             ( ( pos.y_idx &amp;gt;= 0 ) ||&lt;br /&gt;               ( pos.y_idx &amp;lt; board_height ) ) );&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@implementation ArcticSlideTile&lt;br /&gt;&lt;br /&gt;- (BOOL)pushFromPosition:(pos_t)pos inDirection:(dir_e)dir&lt;br /&gt;{&lt;br /&gt;    // Should be implemented in subclass&lt;br /&gt;    return NO;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@implementation ArcticSlideBomb&lt;br /&gt;&lt;br /&gt;- (BOOL)pushFromPosition:(pos_t)pos inDirection:(dir_e)dir&lt;br /&gt;{&lt;br /&gt;    // Penguin has pushed bomb in the given direction.&lt;br /&gt;    // Get our own position:&lt;br /&gt;    pos_t bomb_pos = getUpdatedPos( pos, dir );&lt;br /&gt;    // What are we being pushed into?&lt;br /&gt;    ArcticSlideTile *target_tile_p =&lt;br /&gt;    [model_p getTileFromPosition:bomb_pos&lt;br /&gt;                     inDirection:dir];&lt;br /&gt;    &lt;br /&gt;    if ( nil == target_tile_p )&lt;br /&gt;    {&lt;br /&gt;        // Edge of the world. TODO:&lt;br /&gt;        // queue a &quot;boop&quot; sound effect&lt;br /&gt;    }&lt;br /&gt;    else if ( mountain_p == target_tile_p )&lt;br /&gt;    {&lt;br /&gt;        // bomb pushed into mountain&lt;br /&gt;        // TODO: queue animation of bomb moving onto&lt;br /&gt;        // mountain, animate explosion&lt;br /&gt;        // remove bomb and mountain&lt;br /&gt;        pos_t new_bomb_pos = getUpdatedPos( bomb_pos, dir );&lt;br /&gt;        [model_p setTileAtPosition:new_bomb_pos&lt;br /&gt;                                to:empty_p];&lt;br /&gt;        new_bomb_pos = getUpdatedPos( new_bomb_pos, dir );&lt;br /&gt;        [model_p setTileAtPosition:new_bomb_pos&lt;br /&gt;                                to:empty_p];&lt;br /&gt;    }&lt;br /&gt;    else if ( empty_p == target_tile_p )&lt;br /&gt;    {&lt;br /&gt;        // TODO: queue bomb moving into space&lt;br /&gt;        pos_t new_bomb_pos = getUpdatedPos( bomb_pos, dir );&lt;br /&gt;        // Set bomb at new position&lt;br /&gt;        [model_p setTileAtPosition:new_bomb_pos&lt;br /&gt;                                to:bomb_p];&lt;br /&gt;        // Remove bomb from old position&lt;br /&gt;        [model_p setTileAtPosition:bomb_pos&lt;br /&gt;                                to:empty_p];&lt;br /&gt;&lt;br /&gt;        // Bombs will continue to slide until stopped&lt;br /&gt;        ArcticSlideTile *target_tile_p =&lt;br /&gt;        [model_p getTileFromPosition:new_bomb_pos&lt;br /&gt;                         inDirection:dir];&lt;br /&gt;&lt;br /&gt;        while ( empty_p == target_tile_p )&lt;br /&gt;        {&lt;br /&gt;            // TODO: animate bomb moving into space&lt;br /&gt;            pos_t new_bomb_pos = getUpdatedPos( bomb_pos, dir );&lt;br /&gt;            // set bomb at new position&lt;br /&gt;            [model_p setTileAtPosition:new_bomb_pos&lt;br /&gt;                                    to:bomb_p];&lt;br /&gt;            // remove bomb from old position&lt;br /&gt;            [model_p setTileAtPosition:bomb_pos&lt;br /&gt;                                    to:empty_p];&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        if ( mountain_p == target_tile_p )&lt;br /&gt;        {&lt;br /&gt;            // bomb pushed into mountain&lt;br /&gt;            // TODO: queue animation of bomb moving&lt;br /&gt;            // onto mountain, animate explosion&lt;br /&gt;            // remove bomb and mountain&lt;br /&gt;            [model_p setTileAtPosition:new_bomb_pos&lt;br /&gt;                                    to:empty_p];&lt;br /&gt;            new_bomb_pos = getUpdatedPos( new_bomb_pos, dir );&lt;br /&gt;            [model_p setTileAtPosition:new_bomb_pos&lt;br /&gt;                                    to:empty_p];&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    // The penguin cannot actually move in this turn&lt;br /&gt;    return NO;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;- (NSString*) description&lt;br /&gt;{&lt;br /&gt;    return @&quot;Bomb  &quot;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@implementation ArcticSlideEmpty&lt;br /&gt;- (BOOL)pushFromPosition:(pos_t)pos inDirection:(dir_e)dir&lt;br /&gt;{&lt;br /&gt;    // If the penguin pushes onto an empty tile, he can always&lt;br /&gt;    // move there&lt;br /&gt;    return YES;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;- (NSString*) description&lt;br /&gt;{&lt;br /&gt;    return @&quot;      &quot;;&lt;br /&gt;}&lt;br /&gt;@end&lt;/pre&gt; &lt;p&gt;I'm leaving out unfinished tile classes for clarity, but here is the model implementation:&lt;/p&gt; &lt;pre&gt;@implementation ArcticSlideModel&lt;br /&gt;&lt;br /&gt;- (id)init&lt;br /&gt;{&lt;br /&gt;    // Initialize the global tile objects. I messed around&lt;br /&gt;    // with singleton factory methods for creating a single&lt;br /&gt;    // instance of each of these and accessing it everywhere&lt;br /&gt;    // but the resulting code was too wordy to justify this.&lt;br /&gt;&lt;br /&gt;    empty_p = [[ArcticSlideEmpty alloc] init];&lt;br /&gt;    tree_p = [[ArcticSlideTree alloc] init];&lt;br /&gt;    mountain_p = [[ArcticSlideMountain alloc] init];&lt;br /&gt;    house_p = [[ArcticSlideHouse alloc] init];&lt;br /&gt;    ice_block_p = [[ArcticSlideIceBlock alloc] init];&lt;br /&gt;    heart_p = [[ArcticSlideHeart alloc] init];&lt;br /&gt;    bomb_p = [[ArcticSlideBomb alloc] init];&lt;br /&gt;&lt;br /&gt;    self = [super init];&lt;br /&gt;&lt;br /&gt;    for ( unsigned int idx_y = 0;&lt;br /&gt;         idx_y &amp;lt; board_height; idx_y++ )&lt;br /&gt;    {&lt;br /&gt;        for ( unsigned int idx_x = 0;&lt;br /&gt;             idx_x &amp;lt; board_width; idx_x++ )&lt;br /&gt;        {&lt;br /&gt;            board[idx_y][idx_x] = empty_p;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    return self;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;- (id)initWithLevelIndex:(int)level_idx&lt;br /&gt;{&lt;br /&gt;    self = [self init];&lt;br /&gt;&lt;br /&gt;    // Lookup table to decode the original Polar resource&lt;br /&gt;    // data as strings&lt;br /&gt;    ArcticSlideTile *&lt;br /&gt;        polar_data_tile_map[POLAR_DATA_NUM_TILE_VALS] =&lt;br /&gt;    {&lt;br /&gt;        empty_p, tree_p, mountain_p, house_p, ice_block_p,&lt;br /&gt;        heart_p, bomb_p&lt;br /&gt;    };&lt;br /&gt;&lt;br /&gt;    if ( level_idx &amp;gt; ( num_polar_levels - 1) )&lt;br /&gt;    {&lt;br /&gt;        NSLog(@&quot;initWithLevelIndex: bad level_idx %d!\n&quot;,&lt;br /&gt;              level_idx);&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;        unsigned int level_data_idx = 0;&lt;br /&gt;        for ( unsigned int idx_y = 0;&lt;br /&gt;             idx_y &amp;lt; board_height; idx_y++ )&lt;br /&gt;        {&lt;br /&gt;            for ( unsigned int idx_x = 0;&lt;br /&gt;                 idx_x &amp;lt; board_width; idx_x++ )&lt;br /&gt;            {&lt;br /&gt;                int polar_data_tile_val =&lt;br /&gt;                    polar_levels[level_idx]&lt;br /&gt;                                [level_data_idx] - '0';&lt;br /&gt;                if ( ( polar_data_tile_val &amp;lt; 0 ) ||&lt;br /&gt;                     ( polar_data_tile_val &amp;gt; &lt;br /&gt;                       polar_data_max_tile_val ) )&lt;br /&gt;                {&lt;br /&gt;                    NSLog(@&quot;tile value %d out of range!\n&quot;,&lt;br /&gt;                          polar_data_tile_val );&lt;br /&gt;                    self = nil;&lt;br /&gt;                }&lt;br /&gt;                else&lt;br /&gt;                {&lt;br /&gt;                    board[idx_y][idx_x] =&lt;br /&gt;                        polar_data_tile_map[polar_data_tile_val];&lt;br /&gt;                    level_data_idx++;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    return self;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;- (ArcticSlideTile*)getTileFromPosition:(pos_t)pos &lt;br /&gt;                            inDirection:(dir_e)dir&lt;br /&gt;{&lt;br /&gt;    pos_t updated_pos = getUpdatedPos(pos, dir);&lt;br /&gt;    if ( posValid( updated_pos ) )&lt;br /&gt;    {&lt;br /&gt;        return board[updated_pos.y_idx]&lt;br /&gt;                    [updated_pos.x_idx];&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;        return nil;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;- (NSString*)description&lt;br /&gt;{&lt;br /&gt;    NSMutableString *desc_str =[[NSMutableString alloc]init];&lt;br /&gt;    &lt;br /&gt;    [desc_str appendString:@&quot;ArcticSlideModel board state:\n&quot;];&lt;br /&gt;    for ( unsigned int idx_y = 0;&lt;br /&gt;         idx_y &amp;lt; board_height; idx_y++ )&lt;br /&gt;    {&lt;br /&gt;        for ( unsigned int idx_x = 0;&lt;br /&gt;             idx_x &amp;lt; board_width; idx_x++ )&lt;br /&gt;        {&lt;br /&gt;            [desc_str appendString:[board[idx_y][idx_x] &lt;br /&gt;                                    description]];&lt;br /&gt;        }&lt;br /&gt;        [desc_str appendString:@&quot;\n&quot;];&lt;br /&gt;    }&lt;br /&gt;    return desc_str;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;- (void)setTileAtPosition:(pos_t)pos to:(ArcticSlideTile*)type&lt;br /&gt;{&lt;br /&gt;    board[pos.y_idx][pos.x_idx] = type;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@end&lt;/pre&gt; &lt;p&gt;It's not much yet, and it doesn't have any kind of user interface outside of &lt;b&gt;NSLog&lt;/b&gt;, but my code will successfully respond to moving the penguin through trees, through open space, and pushing a bomb, which then moves into an open space, continues to slide until it comes up against a mountain, and destroys the mountain. I'm driving this with a test method like this:&lt;/p&gt; &lt;pre&gt;NSLog(@&quot;%@\n&quot;, model_p);&lt;br /&gt;// Penguin starts at 0,0, on a tree tile&lt;br /&gt;pos_t penguin_pos = { 0, 0 };&lt;br /&gt;&lt;br /&gt;// Walk the penguin south onto another tree tile&lt;br /&gt;ArcticSlideTile* tile_p =&lt;br /&gt;[model_p getTileFromPosition:penguin_pos &lt;br /&gt;                 inDirection:dir_south];&lt;br /&gt;NSLog(@&quot;Penguin is facing: %@\n&quot;, tile_p);&lt;br /&gt;BOOL allowed = [tile_p pushFromPosition:penguin_pos&lt;br /&gt;                            inDirection:dir_south];&lt;br /&gt;NSLog(@&quot;Penguin allowed: %s\n&quot;, ( allowed ? &quot;YES&quot; : &quot;NO&quot; ) );&lt;br /&gt;tile_p = [model_p getTileFromPosition:penguin_pos&lt;br /&gt;                              inDirection:dir_south];&lt;br /&gt;penguin_pos = getUpdatedPos(penguin_pos, dir_south);&lt;br /&gt;NSLog(@&quot;Penguin is facing: %@\n&quot;, tile_p);&lt;br /&gt;    &lt;br /&gt;// Walk the penguin east onto an empty space&lt;br /&gt;tile_p = [model_p getTileFromPosition:penguin_pos&lt;br /&gt;                              inDirection:dir_east];&lt;br /&gt;NSLog(@&quot;Penguin is facing: %@\n&quot;, tile_p);&lt;br /&gt;allowed = [tile_p pushFromPosition:penguin_pos&lt;br /&gt;                       inDirection:dir_east];&lt;br /&gt;NSLog(@&quot;Penguin allowed: %s\n&quot;, ( allowed ? &quot;YES&quot; : &quot;NO&quot; ) );&lt;br /&gt;tile_p = [model_p getTileFromPosition:penguin_pos&lt;br /&gt;                          inDirection:dir_east];&lt;br /&gt;penguin_pos = getUpdatedPos(penguin_pos, dir_east);&lt;br /&gt;&lt;br /&gt;// Try walking into a bomb, which should slide&lt;br /&gt;// and blow up a mountain&lt;br /&gt;tile_p = [model_p getTileFromPosition:penguin_pos&lt;br /&gt;                          inDirection:dir_east];&lt;br /&gt;NSLog(@&quot;Penguin is facing: %@\n&quot;, tile_p);&lt;br /&gt;allowed = [tile_p pushFromPosition:penguin_pos&lt;br /&gt;                           inDirection:dir_east];&lt;br /&gt;NSLog(@&quot;Penguin allowed: %s\n&quot;, ( allowed ? &quot;YES&quot; : &quot;NO&quot; ) );&lt;br /&gt;&lt;br /&gt;NSLog(@&quot;%@\n&quot;, model_p);&lt;/pre&gt; &lt;p&gt;I'll think on this whole model of updates some more. Maybe it can be even simpler. And I have to consider how the penguin state will be managed, including its orientation (in the original, the penguin can face in the cardinal directions). Should I preserve that in a touch-driven game user interface?&lt;/p&gt;</description>
	<pubDate>Fri, 07 Jun 2013 23:17:00 +0000</pubDate>
	<author>noreply@blogger.com (Paul Potts)</author>
</item>
<item>
	<title>FP Complete: FP Complete Launches Haskell in Real World Competition</title>
	<guid isPermaLink="false">https://www.fpcomplete.com/blog/2013/06/call-for-submissions</guid>
	<link>http://feedproxy.google.com/~r/FpComplete/~3/eSA5S2IjGkc/call-for-submissions</link>
	<description>&lt;h4&gt;FP Complete Launches Haskell in Real World Competition with $1,000 Cash Prize Each Month&lt;/h4&gt;&lt;p&gt;I’m very excited to announce our Haskell in Real World contest: a call out for sample Haskell code and tutorials of real-world engineering and business solutions. Each entry consists of a working solution to an applied problem, plus accompanying tutorial material to teach others how to build similar programs.&lt;/p&gt;&lt;p&gt;The winning entry each month will get $1,000 cash prize. There may be multiple prizes each month or none at all if none meet the winning criteria. There are no limits on how many prizes each individual, team or group can win in any month or the duration of the contest.  In other words, you can make some serious play money here! Anyone not affiliated with FP Complete is eligible to enter.&lt;/p&gt;&lt;p&gt;Why are we doing this?  Simple: because the Haskell community needs to vastly expand for Haskell to become a mainstream language. To do this we must show people how to use this amazing language in solving real-world business problems. People tell us they need to see running and documented examples of real-world problems and solutions, so they can quickly see how to design their own working solutions. They also want material to show their colleagues and bosses just how useful Haskell is today.  In essence this contest intends to compile recipes which, taken together, could be seen as an applied Haskell cookbook. &lt;/p&gt;&lt;p&gt;The competition starts in July.  Official Rules and sign-ups will be published on our website www.fpcomplete.com at the end of June / early July.  So check back then and let the games begin!&lt;/p&gt;&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/FpComplete?a=eSA5S2IjGkc:l31aYdR7xbk:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/FpComplete?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.feedburner.com/~ff/FpComplete?a=eSA5S2IjGkc:l31aYdR7xbk:V_sGLiPBpWU&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/FpComplete?i=eSA5S2IjGkc:l31aYdR7xbk:V_sGLiPBpWU&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.feedburner.com/~ff/FpComplete?a=eSA5S2IjGkc:l31aYdR7xbk:qj6IDK7rITs&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/FpComplete?d=qj6IDK7rITs&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.feedburner.com/~ff/FpComplete?a=eSA5S2IjGkc:l31aYdR7xbk:gIN9vFwOqvQ&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/FpComplete?i=eSA5S2IjGkc:l31aYdR7xbk:gIN9vFwOqvQ&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/FpComplete/~4/eSA5S2IjGkc&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Fri, 07 Jun 2013 19:18:00 +0000</pubDate>
</item>
<item>
	<title>Simon Michael: Blog tinkering, hledger.org</title>
	<guid isPermaLink="true">http://joyful.com/blog/2013-06-06-blog-tinkering-hledger.org.html</guid>
	<link>http://joyful.com/blog/2013-06-06-blog-tinkering-hledger.org.html</link>
	<description>&lt;div style=&quot;font-style: italic;&quot;&gt;June  7, 2013&lt;/div&gt;
&lt;h2&gt;Blog tinkering, hledger.org&lt;/h2&gt;
&lt;p&gt;
&lt;/p&gt;&lt;h3 id=&quot;a-better-ghci-fix&quot;&gt;A better ghci fix&lt;/h3&gt;
&lt;p&gt;I forgot to commit &lt;a href=&quot;http://joyful.com/2013-06-05-ghci-fix.html&quot;&gt;last night’s ghci fix&lt;/a&gt;, which is good because I &lt;a href=&quot;https://github.com/simonmichael/hledger/commit/39f6ec9f04a5f077ac8e0f4036d81d5185c23c4a&quot;&gt;improved it&lt;/a&gt; today, fixing the code duplication.&lt;/p&gt;
&lt;h3 id=&quot;blog-tinkering&quot;&gt;Blog tinkering&lt;/h3&gt;
&lt;p&gt;Thanks to help from #hakyll, I spent some time figuring out how to safely update published blog posts without having them reappear as new on Planet Haskell. The problem seemed to be that I wasn’t setting a separate &lt;code&gt;updated&lt;/code&gt; date, which caused the &lt;code&gt;published&lt;/code&gt; date to change, which caused the post to reappear as new, at least in my feed reader.&lt;/p&gt;
&lt;p&gt;Solution: Start blog posts with metadata like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;---
title:     6/6
author:    Simon Michael
published: 2013-06-06 16:00:00PDT
updated:   2013-06-06 17:00:00PDT
---&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;hledger.org&quot;&gt;hledger.org&lt;/h3&gt;
&lt;p&gt;Worked on the next hledger backlog item, improving the website update process. Did some file cleanup and testing.&lt;/p&gt;
&lt;p&gt;I found that I had broken hledger.org several days ago, when I gratuitously enhanced:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;RewriteRule ^/bugs?/?$   https://github.com/simonmichael/hledger/issues [L,NE]&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;to:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;RewriteRule ^/(bugs|issues)?/?$   https://github.com/simonmichael/hledger/issues [L,NE]&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Doh. Fixed it:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;RewriteRule ^/(bugs?|issues)/?$   https://github.com/simonmichael/hledger/issues [L,NE]&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next: yes, the cron job for updating the site is reporting an error - though it seems to successfully update the site all the same:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;From github.com:simonmichael/hledger
   47ebc21..e87f492  master     -&amp;gt; origin/master
Updating 47ebc21..e87f492
Fast-forward
 README.md |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cd site; ghc site.hs -L/usr/lib -package-db ~/src/joyful.com/cabal-dev/packages-*.conf
cd site; ./site build
Initialising...
  Creating store...
  Creating provider...
  Running rules...
Checking for out-of-date items
Compiling
site: _site/README.html: commitBuffer: invalid argument (invalid character)
  updated README.md
make: *** [site] Error 1&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Re-enabling &lt;code&gt;export LANG=en_US.UTF-8&lt;/code&gt; in the Makefile seems to have fixed it. I have a non-ascii character in the site footer. Setting the LANG environment variable is the quick way to configure a locale, which used to be very much required to avoid encoding errors with GHC 6, but which I thought was less necessary with GHC 7. Perhaps not.&lt;/p&gt;</description>
	<pubDate>Fri, 07 Jun 2013 01:00:00 +0000</pubDate>
</item>
<item>
	<title>Paul Potts: Objective-C, Day 4</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-21500237.post-1740262822173408472</guid>
	<link>http://praisecurseandrecurse.blogspot.com/2013/06/objective-c-day-4.html</link>
	<description>&lt;p&gt;(Warning: more non-FP content)&lt;/p&gt; &lt;p&gt;It's time to get the board representation filled out with a real board. I've only got a couple of hours left before I have to pack up my computer to leave my Undisclosed Location, but let's see if I can get a little more done. Let's review what level 1 looks like:&lt;/p&gt; &lt;a href=&quot;http://4.bp.blogspot.com/-7lLSOnNaHd8/Ua4uWuQMFmI/AAAAAAAADBs/8ByFzRW9Ccs/s1600/level_1_blown_up.tiff&quot;&gt;&lt;img src=&quot;http://4.bp.blogspot.com/-7lLSOnNaHd8/Ua4uWuQMFmI/AAAAAAAADBs/8ByFzRW9Ccs/s640/level_1_blown_up.tiff&quot; height=&quot;120&quot; border=&quot;0&quot; width=&quot;512&quot; /&gt;&lt;/a&gt; &lt;p&gt;Level layouts are taken from the original Macintosh Polar game created by Go Endo. These were originally MacOS resources of type 'STGE.' Let's see if we can decode them. Using ResEdit, the raw data for 'STGE' resource ID -16000 looks like:&lt;/p&gt; &lt;pre&gt;0x0000 0x0000 0x0003 0x0001&lt;br /&gt;0x0000 0x0000 0x0000 0x0000&lt;br /&gt;0x0000 0x0000 0x0000 0x0000&lt;br /&gt;0x0000 0x0000 0x0000 0x0000&lt;br /&gt;0x0000 0x0000 0x0001 0x0000&lt;br /&gt;0x0000 0x0000 0x0000 0x0000&lt;br /&gt;0x0004 0x0000 0x0000 0x0001&lt;br /&gt;0x0000 0x0006 0x0000 0x0002&lt;br /&gt;0x0000 0x0005 0x0004 0x0005&lt;br /&gt;0x0000 0x0000 0x0000 0x0000&lt;br /&gt;0x0000 0x0000 0x0000 0x0000&lt;br /&gt;0x0000 0x0001 0x0000 0x0000&lt;br /&gt;0x0001 0x0000 0x0000 0x0001&lt;br /&gt;0x0000 0x0000 0x0000 0x0000&lt;br /&gt;0x0000 0x0000 0x0000 0x0000&lt;br /&gt;0x0000 0x0000 0x0000 0x0000&lt;br /&gt;0x0000 0x0000 0x0000 0x0005&lt;br /&gt;0x0000 0x0000 0x0000 0x0002&lt;br /&gt;0x0003 0x0000 0x0000 0x0001&lt;br /&gt;0x0001 0x0000 0x0000 0x0000&lt;br /&gt;0x0000 0x0001 0x0000 0x0000&lt;br /&gt;0x0000 0x0000 0x0000 0x0000&lt;br /&gt;0x0000 0x0000 0x0000 0x0000&lt;br /&gt;0x0000 0x0000 0x0000 0x0000&lt;br /&gt;0x0000 0x0000 0x0000&lt;/pre&gt; &lt;p&gt;There are 99 16-bit values. My best guess is that this corresponds to the 24x4 grid (96 board positions) plus 3 extras for some kind of of header of footer data (maybe the total number of hearts is indicated, for example). There are 7 unique values, so it seems extremely likely that they correspond almost exactly to our eight different tile types, with zero representing a blank space. But the counts of each type don't _quite_ match up. The first board has 8 trees, 1 bomb, 2 hearts, 2 ice blocks, 2 mountains, 3 hearts, 1 house, and 1 penguin (there is always 1 penguin), while this 'STGE' resource has: 9 ones, 2 twos, 2 threes, 2 fours, 3 fives, and 1 six. The counts are very close, so this has to represent level 1, and the the 5 almost certainly represents a heart, but I'm not clearly seeing the layout. The first vertical column goes penguin, tree, tree, tree. I don't quite see a pattern that looks like that, but resources -1599 and -15996 give me a hint that the &quot;extra&quot; data is at the front: they contain 0x0007 and 0x0008 as their third values. Those don't appear anywhere else so they probably don't indicate tiles. So let's try rearranging resource -16000 without the first 6 bytes, remove redundant zeroes for clarity, and looking at the values aligned by groups of 24 instead of 4:&lt;/p&gt; &lt;pre&gt;1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 4 0 0&lt;br /&gt;1 0 6 0 2 0 5 4 5 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0&lt;br /&gt;1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 2 3 0 0&lt;br /&gt;1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0&lt;/pre&gt; &lt;p&gt;There's the board. The left column is actually all trees -- when the board first appears, the penguin is hiding a tree. There are actually nine trees. So the encoding looks like so: empty space = 0, tree = 1, mountain = 2, home = 3, ice block = 4, heart = 5, and bomb = 6. The penguin doesn't have a value, but his starting position is probably represented by the first two values, (0,0), most likely encoded as row index, column index to correspond to row-major indexing, and there are 3 hearts to count down as the game is solved.&lt;/p&gt; &lt;p&gt;Can we validate this with the second board? Yes, it looks like there's a 4 indicating 4 hearts. In all the boards I've seen so far (the first four), the penguin is in the upper left. The fifth resource has 0, 1 for its first two values, so I'm guessing I can confirm the encoding of the penguin's position if and when I get to that stage.&lt;/p&gt; &lt;p&gt;And now to come up with a quick way to populate the board in my code. As I'm still a complete n00b in Objective-C, and on the general principle that there's no real need to make this any more clever or less obvious than it has to be, let's just use a string with the data from the resource. Let's add an init method to the interface for the model class:&lt;/p&gt; &lt;pre&gt;- (id)initWithLevelIndex:(int)level_idx;&lt;/pre&gt; &lt;p&gt;And here is some data, and an initializer method:&lt;/p&gt; &lt;pre&gt;static const int num_polar_levels = 1;&lt;br /&gt;static const int polar_data_len = 96;&lt;br /&gt;static const int polar_data_num_tile_vals = 7; // 0-6 inclusive&lt;br /&gt;static const int polar_data_max_tile_val = 6;&lt;br /&gt;static const NSString *polar_levels[num_polar_levels] =&lt;br /&gt;{&lt;br /&gt;    @&quot;100000000000000100000400&quot; \&lt;br /&gt;    @&quot;106020545000000000100100&quot; \&lt;br /&gt;    @&quot;100000000000000050002300&quot; \&lt;br /&gt;    @&quot;110000100000000000000000&quot;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;- (id)initWithLevelIndex:(int)level_idx&lt;br /&gt;{&lt;br /&gt;    // Call our own basic initializer. This will &lt;br /&gt;    // result in redundant setting of board values,&lt;br /&gt;    // but maybe I will clean that up later.&lt;br /&gt;    self = [self init];&lt;br /&gt;&lt;br /&gt;    // A simple lookup table to decode the original&lt;br /&gt;    // Polar resource data as strings&lt;br /&gt;    ArcticSlideTile &lt;br /&gt;        *polar_data_tile_map[polar_data_num_tile_vals] = {&lt;br /&gt;        empty, tree, mountain, house,&lt;br /&gt;        ice_block, heart, bomb };&lt;br /&gt;&lt;br /&gt;    if ( level_idx &amp;gt; ( num_polar_levels - 1) )&lt;br /&gt;    {&lt;br /&gt;        NSLog( @&quot;initWithLevelLayout: level %d out of range!\n&quot;,&lt;br /&gt;               level_idx );&lt;br /&gt;        self = nil;&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;        const NSString* level_str = polar_levels[level_idx];&lt;br /&gt;        unsigned int level_data_idx = 0;&lt;br /&gt;        for ( unsigned int idx_y = 0;&lt;br /&gt;             idx_y &amp;lt; board_height; idx_y++ )&lt;br /&gt;        {&lt;br /&gt;            for ( unsigned int idx_x = 0;&lt;br /&gt;                 idx_x &amp;lt; board_width; idx_x++ )&lt;br /&gt;            {&lt;br /&gt;                NSRange range = NSMakeRange(level_data_idx, 1);&lt;br /&gt;                const NSString * item_str =&lt;br /&gt;                    [level_str substringWithRange: range];&lt;br /&gt;                int polar_data_tile_val = [item_str intValue];&lt;br /&gt;                if ( polar_data_tile_val &amp;gt;&lt;br /&gt;                    polar_data_max_tile_val )&lt;br /&gt;                {&lt;br /&gt;                    NSLog(@&quot;tile val %d out of range!\n&quot;,&lt;br /&gt;                        polar_data_tile_val );&lt;br /&gt;                    self = nil;&lt;br /&gt;                }&lt;br /&gt;                else&lt;br /&gt;                {&lt;br /&gt;                    board[idx_y][idx_x] =&lt;br /&gt;                        polar_data_tile_map[polar_data_tile_val];&lt;br /&gt;                    level_data_idx++;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return self;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;Hmmm, that seems overly complicated. There probably is a better, more idiomatic Objective-C way to use NSStrings for that, but I'm tempted to just write it with a basic C string. Using NSString objects in this context didn't even really help me catch bugs or avoid crashes, since I had forgotten to initialize a heart object and the result was hitting a nil object pointer at runtime and crashing, pretty much the same result as dereferencing a null pointer in straight C except with a better error logged. I'm a little disconcerted by Objective-C's inability to allocate objects on the stack, but that comes down to the aforementioned &quot;thin veneer&quot; over C. I don't really care about the overhead in using method dispatch in this simple piece of code operating on such a small amount of data, but I do care about simplicity. Just to compare, here's a more straightforward C implementation of that inner loop:&lt;/p&gt; &lt;pre&gt;static const char *polar_levels[num_polar_levels] =&lt;br /&gt;{&lt;br /&gt;    &quot;100000000000000100000400&quot;&lt;br /&gt;    &quot;106020545000000000100100&quot;&lt;br /&gt;    &quot;100000000000000050002300&quot;&lt;br /&gt;    &quot;110000100000000000000000&quot;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;int polar_data_tile_val = level_str_p[level_data_idx] - '0';&lt;br /&gt;if ( ( polar_data_tile_val &amp;lt; 0 ) || &lt;br /&gt;     ( polar_data_tile_val &amp;gt; &lt;br /&gt;       polar_data_max_tile_val ) )&lt;br /&gt;{&lt;br /&gt;    NSLog(@&quot;polar data tile value %d out of range!\n&quot;,&lt;br /&gt;          polar_data_tile_val );&lt;br /&gt;    self = nil&lt;br /&gt;}&lt;/pre&gt; &lt;p&gt;Maybe the lesson here is &quot;use Objective-C objects only when objects are a win.&quot;&lt;/p&gt; &lt;p&gt;I'm going to continue with this project, developing the code to handle the interactions of objects on the playing field and eventually the user interface. However, now that my week away from home is done, I might not be able to make progress very quickly. Stay tuned -- I'll post what I can, when I can. As always, if you have any comments or questions, I'm happy to have feedback.&lt;/p&gt;</description>
	<pubDate>Thu, 06 Jun 2013 19:06:00 +0000</pubDate>
	<author>noreply@blogger.com (Paul Potts)</author>
</item>
<item>
	<title>Tom Schrijvers: PPDP '13: Last Call for Papers</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-5844006452378085451.post-1756051365640155158</guid>
	<link>http://tomschrijvers.blogspot.com/2013/06/ppdp-13-last-call-for-papers.html</link>
	<description>&lt;div style=&quot;text-align: left;&quot; dir=&quot;ltr&quot;&gt;&lt;br /&gt;=====================================================================&lt;br /&gt;&lt;br /&gt;                        Last Call for papers&lt;br /&gt;               15th International Symposium on&lt;br /&gt;       Principles and Practice of Declarative Programming&lt;br /&gt;                           PPDP 2013&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Special Issue of Science of Computer Programming (SCP)&lt;br /&gt;&lt;br /&gt;            Madrid, Spain, September 16-18, 2013&lt;br /&gt;                 (co-located with LOPSTR 2013)&lt;br /&gt;&lt;br /&gt;              http://users.ugent.be/~tschrijv/PPDP2013/&lt;br /&gt;&lt;br /&gt;======================================================================&lt;br /&gt;&lt;br /&gt;PPDP 2013 is a forum that brings together researchers from the declarative&lt;br /&gt;programming communities, including those working in the logic, constraint and&lt;br /&gt;functional programming paradigms, but also embracing a variety of other&lt;br /&gt;paradigms such as visual programming, executable specification languages,&lt;br /&gt;database languages, and knowledge representation languages.&lt;br /&gt;&lt;br /&gt;The goal is to stimulate research in the use of logical formalisms and methods&lt;br /&gt;for specifying, performing, and analysing computations, including mechanisms&lt;br /&gt;for mobility, modularity, concurrency, object-orientation, security,&lt;br /&gt;verification and static analysis. Papers related to the use of declarative&lt;br /&gt;paradigms and tools in industry and education are especially solicited. Topics&lt;br /&gt;of interest include, but are not limited to:&lt;br /&gt;&lt;br /&gt;*&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Functional programming&lt;br /&gt;*&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Logic programming&lt;br /&gt;*&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Answer-set programming&lt;br /&gt;*&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Functional-logic programming&lt;br /&gt;*&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Declarative visual languages&lt;br /&gt;*&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Constraint Handling Rules&lt;br /&gt;*&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Parallel implementation and concurrency&lt;br /&gt;*&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Monads, type classes and dependent type systems&lt;br /&gt;*&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Declarative domain-specific languages&lt;br /&gt;*&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Termination, resource analysis and the verification of declarative programs&lt;br /&gt;*&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Transformation and partial evaluation of declarative languages&lt;br /&gt;*&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Language extensions for security and tabulation&lt;br /&gt;*&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Probabilistic modelling in a declarative language and modelling reactivity&lt;br /&gt;*&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Memory management and the implementation of declarative systems&lt;br /&gt;*&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Practical experiences and industrial application&lt;br /&gt;&lt;br /&gt;This year the conference will be co-located with the 23nd International&lt;br /&gt;Symposium on Logic-Based Program Synthesis and Transformation (LOPSTR 2013) and&lt;br /&gt;held in cooperation with ACM SIGPLAN.  The conference will be held in Madrid,&lt;br /&gt;Spain. Previous symposia were held at Leuven (Belgium), Odense (Denmark),&lt;br /&gt;Hagenberg (Austria), Coimbra (Portugal), Valencia (Spain), Wroclaw (Poland),&lt;br /&gt;Venice (Italy), Lisboa (Portugal), Verona (Italy), Uppsala (Sweden), Pittsburgh&lt;br /&gt;(USA), Florence (Italy), Montreal (Canada), and Paris (France).&lt;br /&gt;&lt;br /&gt;Papers must describe original work, be written and presented in English, and&lt;br /&gt;must not substantially overlap with papers that have been published or that are&lt;br /&gt;simultaneously submitted to a journal, conference, or workshop with refereed&lt;br /&gt;proceedings. Work that already appeared in unpublished or informally published&lt;br /&gt;workshop proceedings may be submitted (please contact the PC chair in case of&lt;br /&gt;questions).  Proceedings will be published in the ACM International Conference&lt;br /&gt;Proceedings Series.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;After the symposium, a selection of the best papers will be invited to extend&lt;br /&gt;their submissions in the light of the feedback solicited at the symposium.  The&lt;br /&gt;papers are expected to include at least 30% extra material over and above the&lt;br /&gt;PPDP version. Then, after another round of reviewing, these revised papers will&lt;br /&gt;be published in a special issue of SCP with a target publication date by&lt;br /&gt;Elsevier of 2014.&lt;br /&gt;&lt;br /&gt;Important Dates&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Abstract Submission: &lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt;  &lt;/span&gt;June 10, 2013&lt;br /&gt;&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Paper submission: &lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt;  &lt;/span&gt;June 13, 2013&lt;br /&gt;&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Notification: &lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt;   &lt;/span&gt;July 18, 2013&lt;br /&gt;&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Camera-ready: &lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt;   &lt;/span&gt;August 4, 2013&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Symposium: &lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt;   &lt;/span&gt;September 16-18, 2013 &lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Invites for SCP: &lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt;  &lt;/span&gt;October 2, 2013&lt;br /&gt;&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Submission of SCP: &lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt;  &lt;/span&gt;December 11, 2013&lt;br /&gt;&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Notification from SCP: &lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt;  &lt;/span&gt;February 22, 2014&lt;br /&gt;&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;Camera-ready for SCP: &lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt;  &lt;/span&gt;March 14, 2014&lt;br /&gt;&lt;br /&gt;Authors should submit an electronic copy of the paper (written in English) in&lt;br /&gt;PDF.  Each submission must include on its first page the paper title; authors&lt;br /&gt;and their affiliations; abstract; and three to four keywords. The keywords will&lt;br /&gt;be used to assist us in selecting appropriate reviewers for the paper. Papers&lt;br /&gt;should consist of no more than 12 pages, formatted following the ACM SIG&lt;br /&gt;proceedings template (option 1). The 12 page limit must include references but&lt;br /&gt;excludes well-marked appendices not intended for publication. Referees are not&lt;br /&gt;required to read the appendices, and thus papers should be intelligible without&lt;br /&gt;them.&lt;br /&gt;&lt;br /&gt;Program Committee&lt;br /&gt;&lt;br /&gt;Sergio Antoy               Portland State University, USA&lt;br /&gt;Manuel Carro               IMDEA Software Institute, Spain&lt;br /&gt;Iliano Cervesato           Carnegie Mellon University, Qatar&lt;br /&gt;Agostino Dovier            Universita degli Studi di Udine, Italy&lt;br /&gt;Maria Garcia de la Banda   Monash University, Australia&lt;br /&gt;Ralf Hinze                 University of Oxford, UK&lt;br /&gt;Yukiyoshi Kameyama         University of Tsukuba, Japan&lt;br /&gt;Oleg Kiselyov              USA&lt;br /&gt;Yanhong Annie Liu          State University of New York at Stony Brook, USA&lt;br /&gt;Stefan Monnier             Universite de Montreal, Canada&lt;br /&gt;Alan Mycroft               University of Cambrige, UK&lt;br /&gt;Bruno C. d. S. Oliveira&lt;span style=&quot;white-space: pre;&quot; class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;   National University of Singapore, Singapore&lt;br /&gt;Alberto Pettorossi         Universita di Roma Tor Vergata, Italy&lt;br /&gt;Enrico Pontelli            New Mexico State University, USA&lt;br /&gt;Kristoffer Rose            IBM Research, USA&lt;br /&gt;Sukyoung Ryu               KAIST, South Korea&lt;br /&gt;Vitor Santos Costa         University of Porto, Portugal&lt;br /&gt;Torsten Schaub             University Potsdam, Germany&lt;br /&gt;Tom Schrijvers             Ghent University, Belgium&lt;br /&gt;Martin Sulzmann            Hochschule Karlsruhe, Germany&lt;br /&gt;Wouter Swierstra           Universiteit Utrecht, The Netherlands&lt;br /&gt;Tarmo Uustalu              Institute of Cybernetics, Estonia&lt;br /&gt;Janis Voigtlaender         University of Bonn, Germany&lt;br /&gt;Meng Wang                  Chalmers University of Technology, Sweden&lt;br /&gt;Jan Wielemaker             Universiteit van Amsterdam, The Netherlands&lt;br /&gt;&lt;br /&gt;Program Chair&lt;br /&gt;&lt;br /&gt;    Tom Schrijvers&lt;br /&gt;    Department of Applied Mathematics and Computer Science&lt;br /&gt;    Ghent University&lt;br /&gt;    9000 Gent, Belgium&lt;br /&gt;&lt;br /&gt;General Chair&lt;br /&gt;&lt;br /&gt;    Ricardo Pena&lt;br /&gt;    Facultad de Informatica&lt;br /&gt;    Universidad Complutense de Madrid&lt;br /&gt;    28040 Madrid, Spain&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;</description>
	<pubDate>Thu, 06 Jun 2013 15:56:18 +0000</pubDate>
	<author>noreply@blogger.com (Tom Schrijvers)</author>
</item>
<item>
	<title>Simon Michael: GHCI fix</title>
	<guid isPermaLink="true">http://joyful.com/blog/2013-06-05-ghci-fix.html</guid>
	<link>http://joyful.com/blog/2013-06-05-ghci-fix.html</link>
	<description>&lt;div style=&quot;font-style: italic;&quot;&gt;June  6, 2013&lt;/div&gt;
&lt;h2&gt;GHCI fix&lt;/h2&gt;
&lt;p&gt;
&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://joyful.com/2013-06-04-bugfix-planning-autoweb.html&quot;&gt;Yesterday&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Today:&lt;/p&gt;
&lt;p&gt;Next item on the 0.22 backlog: fix &lt;code&gt;make ghci&lt;/code&gt; and &lt;code&gt;make ghciweb&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The goal of these make rules is to bring up a GHCI prompt with as much as possible of the guts of hledger (or hledger-web) loaded, in scope, and visible. This can be really valuable for debugging, sanity-checking, and exploratory development, but it’s quite hard to set up by hand, as the necessary flags tend to proliferate. Hence these rules.&lt;/p&gt;
&lt;p&gt;The latest breakage was due to my using cabal’s MIN_version_* macros (to make Hledger.Cli.Utils compatible with both the versions of directory currently found on user machines). It’s hard to ensure these macros are available in all circumstances (sp/ghc builds, haddock, ghci..) So I’ve made it work with or without them, with this not-very-pretty kludge:&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span class=&quot;fu&quot;&gt;#&lt;/span&gt;ifdef &lt;span class=&quot;dt&quot;&gt;MIN_VERSION_directory&lt;/span&gt;
&lt;span class=&quot;fu&quot;&gt;#&lt;/span&gt;if &lt;span class=&quot;dt&quot;&gt;MIN_VERSION_directory&lt;/span&gt;(&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;)
        utc &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; getModificationTime f
        &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; nom &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; utcTimeToPOSIXSeconds utc
        &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; clo &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TOD&lt;/span&gt; (&lt;span class=&quot;fu&quot;&gt;read&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;takeWhile&lt;/span&gt; (&lt;span class=&quot;ot&quot;&gt;`elem`&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;0123456789&quot;&lt;/span&gt;) &lt;span class=&quot;fu&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;show&lt;/span&gt; nom) &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;-- XXX read&lt;/span&gt;
&lt;span class=&quot;fu&quot;&gt;#&lt;/span&gt;else
        clo &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; getModificationTime f
&lt;span class=&quot;fu&quot;&gt;#&lt;/span&gt;endif
&lt;span class=&quot;fu&quot;&gt;#&lt;/span&gt;else
&lt;span class=&quot;co&quot;&gt;-- cabal macros aren't available, assume the new directory&lt;/span&gt;
        utc &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; getModificationTime f
        &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; nom &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; utcTimeToPOSIXSeconds utc
        &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; clo &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TOD&lt;/span&gt; (&lt;span class=&quot;fu&quot;&gt;read&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;takeWhile&lt;/span&gt; (&lt;span class=&quot;ot&quot;&gt;`elem`&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;0123456789&quot;&lt;/span&gt;) &lt;span class=&quot;fu&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;show&lt;/span&gt; nom) &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;-- XXX read&lt;/span&gt;
&lt;span class=&quot;fu&quot;&gt;#&lt;/span&gt;endif&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And now I’ve got ghci prompts again. It looks like they could do some more importing though:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;~/src/hledger$ make ghciweb
ghci -rtsopts -W -fwarn-tabs -fno-warn-unused-do-bind -fno-warn-name-shadowing  -ihledger-lib -ihledger -ihledger-web -ihledger-web/app -L/usr/lib  -optP-include -optPhledger/dist/build/autogen/cabal_macros.h -DPATCHLEVEL=0 -DDEVELOPMENT -DVERSION='&quot;0.21.2dev&quot;' -XCPP -XMultiParamTypeClasses -XOverloadedStrings -XQuasiQuotes -XRecordWildCards -XTemplateHaskell  hledger-web/app/main.hs
GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[ 1 of 52] Compiling Settings.Development ( hledger-web/Settings/Development.hs, interpreted ) [flags changed]
[ 2 of 52] Compiling Settings         ( hledger-web/Settings.hs, interpreted ) [flags changed]
Loading package array-0.4.0.1 ... linking ... done.
...
[52 of 52] Compiling Main             ( hledger-web/app/main.hs, interpreted )
Ok, modules loaded: Hledger, Hledger.Data, Hledger.Data.Account, Hledger.Data.AccountName, Hledger.Data.Amount, Hledger.Data.Commodity, Hledger.Data.Dates, Hledger.Data.FormatStrings, Hledger.Data.Journal, Hledger.Data.Ledger, Hledger.Data.Posting, Hledger.Data.TimeLog, Hledger.Data.Transaction, Hledger.Data.Types, Hledger.Query, Hledger.Read, Hledger.Read.CsvReader, Hledger.Read.JournalReader, Hledger.Read.TimelogReader, Hledger.Reports, Hledger.Utils, Hledger.Utils.UTF8IOCompat, Hledger.Cli, Hledger.Cli.Options, Hledger.Cli.Utils, Hledger.Cli.Version, Hledger.Cli.Add, Hledger.Cli.Balance, Hledger.Cli.Balancesheet, Hledger.Cli.Cashflow, Hledger.Cli.Histogram, Hledger.Cli.Incomestatement, Hledger.Cli.Print, Hledger.Cli.Register, Hledger.Cli.Stats, Application, Foundation, Import, Settings, Settings.StaticFiles, Settings.Development, Handler.Common, Handler.JournalEditR, Handler.JournalEntriesR, Handler.JournalR, Handler.Post, Handler.RegisterR, Handler.RootR, Handler.Utils, Hledger.Web.Main, Hledger.Web.Options, Main.
&amp;gt;&amp;gt;&amp;gt; :t defbaseurl

&amp;lt;interactive&amp;gt;:1:1: Not in scope: `defbaseurl'
&amp;gt;&amp;gt;&amp;gt; import Settings
&amp;gt;&amp;gt;&amp;gt; :t defbaseurl
defbaseurl :: GHC.Types.Int -&amp;gt; GHC.Base.String
&amp;gt;&amp;gt;&amp;gt; &lt;/code&gt;&lt;/pre&gt;</description>
	<pubDate>Thu, 06 Jun 2013 06:00:00 +0000</pubDate>
</item>
<item>
	<title>Paul Potts: Objective-C, Day 3</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-21500237.post-4074733724618686365</guid>
	<link>http://praisecurseandrecurse.blogspot.com/2013/06/objective-c-day-3.html</link>
	<description>&lt;p&gt;(Warning: non-FP content)&lt;/p&gt; &lt;p&gt;So, this is day 5 in my Undisclosed Location and I haven't gotten much done -- I'm still engaged in a job search, and spent about eight hours working on a &quot;take-home test&quot; for an employer (with a few interruptions), and I'm pursuing more leads, and I'm trying to socialize with my hosts occasionally, so there are some distractions. But I've got enough information to go on to start implementing something original.&lt;/p&gt; &lt;p&gt;What I want to implement is a small game. I'll get as far as I can in the back-end code today (the Model and Controller parts of the MVC &quot;trinity&quot;) and we'll see just how productive I really am in Objective-C. I read most of Objective-C Programming by Aaron Hillegass last night -- it's a very quick read for someone well-versed in C, and it reiterates parts of the iOS Programming book. I haven't covered protocols, categories, blocks, or run loops, but I think I can get by without those things for now.&lt;/p&gt; &lt;p&gt;Many years ago there existed on old-school MacOS a small game called &quot;Polar.&quot; It was a very simple game, written by a guy (Go Endo) who was probably a student at the time, but I was fond of it -- fond enough to save it for 23 years, with the intention of studying its design and re-implementing it in the future. (In fact, I've saved a lot of old bits and bobs like this). I made notes of how to beat the first 3 levels (it was one of those &quot;incredibly simple game play, but maddeningly tricky&quot; games), drew out the levels, made notes on how the objects behaved, etc. I haven't been able to run that game for a long time, but today I just got it working under SheepShaver. Here's what level 1 looks like (blown up a bit):&lt;/p&gt; &lt;a href=&quot;http://4.bp.blogspot.com/-7lLSOnNaHd8/Ua4uWuQMFmI/AAAAAAAADBs/8ByFzRW9Ccs/s1600/level_1_blown_up.tiff&quot;&gt;&lt;img src=&quot;http://4.bp.blogspot.com/-7lLSOnNaHd8/Ua4uWuQMFmI/AAAAAAAADBs/8ByFzRW9Ccs/s640/level_1_blown_up.tiff&quot; height=&quot;120&quot; border=&quot;0&quot; width=&quot;512&quot; /&gt;&lt;/a&gt; &lt;p&gt;The penguin is your avatar. The rest of the objects are ice blocks, trees, hearts, bombs, mountains, and houses. The world is a sheet of ice. You can walk around on the ice. You can walk through trees. Some objects (trees, mountains, and houses) can't be moved. Bombs, hearts, and ice blocks move without friction -- if you push them, they will keep going until they hit the edge of the world or another object. If an sliding ice block hits another object, it stops. If you push it against another object, it crumbles and disappears. The penguin can walk through trees, but other objects can't. Bombs will blow up mountains when they slide into them. Everything else simply stops them. The goal of the game is to slide all the hearts into a house (cute, huh?) But because the ice is frictionless, it's incredibly easy to get objects stuck against walls or corners where you can no longer move them the way you need to. So you have to carefully plan out your moves, and if you get stuck, there's an option to start the level over.&lt;/p&gt; &lt;p&gt;I should mention that the original game had a copyright notice (1990), and was shareware ($2.00). I can't remember if I ever sent the author $2.00. I'm not sure how he would feel about me taking apart and trying to re-implement his game, or whether he'd try to assert that copyright prevented me from doing so, but I'll assume he's a  nice guy and wouldn't care as long as I don't charge for it, and go ahead, on the theory that easier to ask forgiveness than permission. I was not able to find him online -- maybe &quot;Go Endo&quot; was a pseudonym?&lt;/p&gt; &lt;p&gt;Back in 1991 I came up with a C++ class design (actually, it doesn't quite look like C++; I think it was written using THINK C's object-oriented extensions, which are sort of lost in the mists of time to me -- what is that &lt;b&gt;indirect&lt;/b&gt; keyword? What did &lt;b&gt;#pragma options(virtual)&lt;/b&gt; do? I don't remember for sure, but let's see if we can come up with an Objective-C implementation. The objects, other than the penguin, which can face in different directions, don't seem to have any real distinct properties except for their locations in the board, so I'm tempted not to take the obvious route and implement a board full of instances of the objects. I'm more inclined to just define a class for the board, and let it encapsulate most of the game logic. For the objects themselves, I'd like to just make references (pointers) to their classes (the class object) rather than putting them in a container. That's apparently not really possible -- there are no predefined singletons for the class objects that are accessible at run-time the way there are for NSNull. So I have to make some (sort of) singletons.&lt;/p&gt; &lt;p&gt;Here's what I've got today:&lt;/p&gt; &lt;pre&gt;@interface ArcticSlideTile : NSObject&lt;br /&gt;// Not necessarily useful yet, but I am guessing it might&lt;br /&gt;// be helpful to have a separate base class at some point.&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideTileStateless : ArcticSlideTile&lt;br /&gt;// In implementation, there will be a single shared instance.&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideBomb : ArcticSlideTileStateless&lt;br /&gt;// Bombs can be pushed and will slide until they&lt;br /&gt;// hit an object and stop. If the object is a mountain,&lt;br /&gt;// both bomb and mountain are destroyed and&lt;br /&gt;// there should be an animation. If another object&lt;br /&gt;// hits a bomb it stops (I'm not sure you can test this&lt;br /&gt;// combination in the original game with the board layouts&lt;br /&gt;// available&lt;br /&gt;- (NSString*) description;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideEmpty : ArcticSlideTileStateless&lt;br /&gt;// The penguin can walk on empty space. Pushable objects&lt;br /&gt;// on empty space are on ice and they slide until something&lt;br /&gt;// stops them.&lt;br /&gt;- (NSString*) description;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideHeart : ArcticSlideTileStateless&lt;br /&gt;// When a heart hits a house the heart disappears (getting&lt;br /&gt;// all the hearts into the house is how you win the game).&lt;br /&gt;// Otherwise they cannot be destroyed, and slide like other&lt;br /&gt;// slidable items.&lt;br /&gt;- (NSString*) description;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideHouse : ArcticSlideTileStateless&lt;br /&gt;// Houses cannot be pushed and stop other objects&lt;br /&gt;// except hearts. When a heart hits a house the heart&lt;br /&gt;// disappears (getting the hearts into the house is&lt;br /&gt;// how you win the game). So the model should keep track&lt;br /&gt;// of the number of hearts on the board and trigger a&lt;br /&gt;// &quot;win the level&quot; behavior when the last heart is &lt;br /&gt;// destroyed.&lt;br /&gt;- (NSString*) description;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideIceBlock : ArcticSlideTileStateless&lt;br /&gt;// Ice blocks can be pushed and will slide until they&lt;br /&gt;// hit an object and stop. If they are pushed directly&lt;br /&gt;// against an object they will be crushed (there should&lt;br /&gt;// be an animation) and disappear.&lt;br /&gt;- (NSString*) description;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideMountain : ArcticSlideTileStateless&lt;br /&gt;// Mountains cannot be moved and are destroyed by bombs.&lt;br /&gt;- (NSString*) description;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@interface ArcticSlidePenguin : ArcticSlideTile&lt;br /&gt;// The penguin is the avatar. It has the special&lt;br /&gt;// quality of being able to face different directions&lt;br /&gt;// in the original game, although that's because you &lt;br /&gt;// can click near him to turn him and make him walk in&lt;br /&gt;// different directions. In a touchscreen implementation&lt;br /&gt;// I'm not sure how this should be implemented -- maybe&lt;br /&gt;// he can slide in discreet steps. The penguin has the&lt;br /&gt;// ability to walk through trees. We might want to &lt;br /&gt;// implement this temporary state using using an&lt;br /&gt;// &quot;override&quot; object reference in the model. Sliding&lt;br /&gt;// objects might be implemented the same way.&lt;br /&gt;typedef enum {&lt;br /&gt;    north, south, east, west&lt;br /&gt;}penguinDirection_e;&lt;br /&gt;&lt;br /&gt;@property penguinDirection_e facing;&lt;br /&gt;- (NSString*) description;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideTree : ArcticSlideTile&lt;br /&gt;// Trees cannot be pushed or destroyed and stop&lt;br /&gt;// all sliding objects, but the penguin avatar &lt;br /&gt;// character can walk through them.&lt;br /&gt;- (NSString*) description;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;static const int board_width = 24, board_height = 4;&lt;br /&gt;// The short board design is part of what makes it&lt;br /&gt;// so easy to get sliding objects stuck against the&lt;br /&gt;// edges of the world or in corners where you can no longer&lt;br /&gt;// get around to the other side to push them. We could&lt;br /&gt;// consider a bigger board later and maybe implement the&lt;br /&gt;// original puzzles surrounded by impassible water.&lt;br /&gt;&lt;br /&gt;@interface ArcticSlideModel : NSObject&lt;br /&gt;{&lt;br /&gt;    ArcticSlideTile* board[board_height][board_width];&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;- (id)init;&lt;br /&gt;- (NSString*) description;&lt;br /&gt;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@implementation ArcticSlideTile&lt;br /&gt;{&lt;br /&gt;}&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@implementation ArcticSlideTileStateless&lt;br /&gt;{&lt;br /&gt;}&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@implementation ArcticSlideBomb&lt;br /&gt;- (NSString*) description&lt;br /&gt;{&lt;br /&gt;    return @&quot;Bomb&quot;;&lt;br /&gt;}&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@implementation ArcticSlideEmpty&lt;br /&gt;- (NSString*) description&lt;br /&gt;{&lt;br /&gt;    return @&quot;Empty&quot;;&lt;br /&gt;}&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@implementation ArcticSlideHouse&lt;br /&gt;- (NSString*) description&lt;br /&gt;{&lt;br /&gt;    return @&quot;House&quot;;&lt;br /&gt;}&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@implementation ArcticSlideIceBlock&lt;br /&gt;- (NSString*) description&lt;br /&gt;{&lt;br /&gt;    return @&quot;Ice Block&quot;;&lt;br /&gt;}&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@implementation ArcticSlideMountain&lt;br /&gt;- (NSString*) description&lt;br /&gt;{&lt;br /&gt;    return @&quot;Mountain&quot;;&lt;br /&gt;}&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@implementation ArcticSlidePenguin&lt;br /&gt;- (NSString*) description&lt;br /&gt;{&lt;br /&gt;    return @&quot;Mountain&quot;;&lt;br /&gt;}&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@implementation ArcticSlideTree&lt;br /&gt;- (NSString*) description&lt;br /&gt;{&lt;br /&gt;    return @&quot;Tree&quot;;&lt;br /&gt;}&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;@implementation ArcticSlideModel&lt;br /&gt;{&lt;br /&gt;    ArcticSlideBomb *bomb;&lt;br /&gt;    ArcticSlideEmpty *empty;&lt;br /&gt;    ArcticSlideHeart *heart;&lt;br /&gt;    ArcticSlideHouse *house;&lt;br /&gt;    ArcticSlideIceBlock *ice_block;&lt;br /&gt;    ArcticSlideMountain *mountain;&lt;br /&gt;    ArcticSlidePenguin *penguin;&lt;br /&gt;    ArcticSlideTree* tree;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;- (id)init&lt;br /&gt;{&lt;br /&gt;    bomb = [[ArcticSlideBomb alloc] init];&lt;br /&gt;    empty = [[ArcticSlideEmpty alloc] init];&lt;br /&gt;    heart = [[ArcticSlideHeart alloc] init];&lt;br /&gt;    house = [[ArcticSlideHouse alloc] init];&lt;br /&gt;    ice_block = [[ArcticSlideIceBlock alloc] init];&lt;br /&gt;    mountain = [[ArcticSlideMountain alloc] init];&lt;br /&gt;    penguin = [[ArcticSlidePenguin alloc] init];&lt;br /&gt;    tree = [[ArcticSlideTree alloc] init];&lt;br /&gt;&lt;br /&gt;    for ( unsigned int idx_y = 0;&lt;br /&gt;         idx_y &amp;lt; board_height; idx_y++ )&lt;br /&gt;    {&lt;br /&gt;        for ( unsigned int idx_x = 0;&lt;br /&gt;             idx_x &amp;lt; board_width; idx_x++ )&lt;br /&gt;        {&lt;br /&gt;            board[idx_y][idx_x] = empty;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return self;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;- (NSString*)description&lt;br /&gt;{&lt;br /&gt;    NSMutableString *desc_str =&lt;br /&gt;        [[NSMutableString alloc]init];&lt;br /&gt;    &lt;br /&gt;    [desc_str appendString:@&quot;ArcticSlideModel board state:\n&quot;];&lt;br /&gt;    for ( unsigned int idx_y = 0;&lt;br /&gt;         idx_y &amp;lt; board_height; idx_y++ )&lt;br /&gt;    {&lt;br /&gt;        for ( unsigned int idx_x = 0;&lt;br /&gt;             idx_x &amp;lt; board_width; idx_x++ )&lt;br /&gt;        {&lt;br /&gt;            [desc_str&lt;br /&gt;             appendString:[board[idx_y][idx_x] description]];&lt;br /&gt;            [desc_str appendString:@&quot; &quot;];&lt;br /&gt;        }&lt;br /&gt;        [desc_str appendString:@&quot;\n&quot;];&lt;br /&gt;    }&lt;br /&gt;    return desc_str;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@end&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;My quiet time at my undisclosed location ends tomorrow. I'm not sure I'll be able to get back to this for a few days. I haven't gotten nearly as much done with this as I've hoped. I've been distracted by a lot of job-search things. But hey, it's a start!&lt;/p&gt;</description>
	<pubDate>Wed, 05 Jun 2013 05:05:00 +0000</pubDate>
	<author>noreply@blogger.com (Paul Potts)</author>
</item>
<item>
	<title>Tim Docker: Data analysis with Monoids</title>
	<guid isPermaLink="false">http://twdkz.wordpress.com/?p=65</guid>
	<link>http://twdkz.wordpress.com/2013/05/31/data-analysis-with-monoids/</link>
	<description>&lt;p&gt;This post expresses the key ideas of a talk I gave at FP-SYD this week.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html&quot;&gt;Monoids&lt;/a&gt; are a pretty simple concept in haskell. Some years ago I learnt of them through the excellent &lt;a href=&quot;http://www.haskell.org/haskellwiki/Typeclassopedia&quot;&gt;Typeclassopedia&lt;/a&gt;, looked at the examples, and understood them quickly (which is more than can be said for many of the new ideas that one learns in haskell). However that was it. Having learnt the idea, I realised that monoids are everywhere in programming, but I’d not found much use for the Monoid typeclass abstraction itself. Recently, I’ve found they can be a useful tool for data analysis…&lt;/p&gt;
&lt;h2 id=&quot;monoids&quot;&gt;Monoids&lt;/h2&gt;
&lt;p&gt;First a quick recap. A monoid is a type with a binary operation, and an identity element:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;class Monoid a where
  mempty :: a
  mappend :: a -&amp;gt; a -&amp;gt; a&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It must satisfy a simple set of laws, specifically that the binary operation much be associative, and the identity element must actually be the identity for the given operation:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;mappend a (mappend b c) = mappend (mappend a b) c
mappend mempty x = x
mappend x mempty = x&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As is hinted by the names of the typeclass functions, lists are an obvious Monoid instance:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;instance Monoid [a] where
  mempty  = []
  mappend = (++)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, many types can be Monoids. In fact, often a type can be a monoid in multiple ways. Numbers are monoids under both addition and multiplication, with 0 and 1 as their respective identity elements. In the haskell standard libraries, rather than choose one kind of monoid for numbers, newtype declarations are used to given instances for both:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;newtype Sum a = Sum { getSum :: a }
  deriving (Eq, Ord, Read, Show, Bounded)

instance Num a =&amp;gt; Monoid (Sum a) where
  mempty = Sum 0
  Sum x `mappend` Sum y = Sum (x + y)

newtype Product a = Product { getProduct :: a }
  deriving (Eq, Ord, Read, Show, Bounded)

instance Num a =&amp;gt; Monoid (Product a) where
  mempty = Product 1
  Product x `mappend` Product y = Product (x * y)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We’ve now established and codified the common structure for a few monoids, but it’s not yet clear what it has gained us. The Sum and Product instances are unwieldly – you are unlikely to want to use Sum directly to add two numbers:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Prelude&amp;gt; :m Data.Monoid
Prelude Data.Monoid&amp;gt; 5+4
9
Prelude Data.Monoid&amp;gt; getSum (mappend (Sum 5) (Sum 4))
9&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Before we progress, however, let’s define a few more monoid instances, potentially useful for data analysis.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;data Min a = Min a | MinEmpty deriving (Show)
           
data Max a = Max a | MaxEmpty deriving (Show)

newtype Count = Count Int deriving (Show)

instance (Ord a) =&amp;gt; Monoid (Min a) where
  mempty = MinEmpty
  mappend MinEmpty m = m
  mappend m MinEmpty = m
  mappend (Min a) (Min b) = (Min (P.min a b))

instance (Ord a) =&amp;gt; Monoid (Max a) where
  mempty = MaxEmpty
  mappend MaxEmpty m = m
  mappend m MaxEmpty = m
  mappend (Max a) (Max b) = (Max (P.max a b))

instance Monoid Count where
  mempty = Count 0
  mappend (Count n1) (Count n2) = Count (n1+n2)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Also some helper functions to construct values of all these monoid types:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;sum :: (Num a) =&amp;gt; a -&amp;gt; Sum a
sum = Sum

product :: (Num a) =&amp;gt; a -&amp;gt; Product a
product = Product

min :: (Ord a) =&amp;gt; a -&amp;gt; Min a
min = Min

max :: (Ord a) =&amp;gt; a -&amp;gt; Max a
max = Max

count :: a -&amp;gt; Count
count _ = Count 1&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These functions are trivial, but they put a consistent interface on creating monoid values. They all have a signature (a -&amp;gt; m) where m is some monoid. For lack of a better name, I’ll call functions with such signatures &quot;monoid functions&quot;.&lt;/p&gt;
&lt;h2 id=&quot;foldable&quot;&gt;Foldable&lt;/h2&gt;
&lt;p&gt;It’s time to introduce another typeclass, &lt;a href=&quot;http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Foldable.html&quot;&gt;Foldable&lt;/a&gt;. This class abstracts the classic foldr and foldl functions away from lists, making them applicable to arbitrary structures. (There’s a robust debate going on right now about the merits of replacing the list specific fold functions in the standard prelude with the more general versions from Foldable.) Foldable is a large typeclass – here’s the key function of interest to us:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;class Foldable t where
  ...
  foldMap :: Monoid m =&amp;gt; (a -&amp;gt; m) -&amp;gt; t a -&amp;gt; m
  ...&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;foldMap takes a monoid function and a Foldable structure, and reduces the structure down to a single value of the monoid. Lists are, of course, instances of foldable, so we can demo our helper functions:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;*Examples&amp;gt; let as = [45,23,78,10,11,1]
*Examples&amp;gt; foldMap count as
Count 6
*Examples&amp;gt; foldMap sum as
Sum {getSum = 168}
*Examples&amp;gt; foldMap max as
Max 78&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice how the results are all still wrapped with the newtype constructors. We’ll deal with this later.&lt;/p&gt;
&lt;h2 id=&quot;composition&quot;&gt;Composition&lt;/h2&gt;
&lt;p&gt;As it turns out, tuples are already instances of Monoids:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;instance (Monoid a,Monoid b) =&amp;gt; Monoid (a,b) where
  mempty = (mempty,mempty)
  mappend (a1,b1) (a2,b2) = (mappend a1 a2,mappend b1 b2)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A pair is a monoid if it’s elements are monoids. There are similar instances for longer tuples. We need some helper monoid functions for tuples also:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;a2 :: (a -&amp;gt; b) -&amp;gt; (a -&amp;gt; c) -&amp;gt; a -&amp;gt; (b,c)
a2 b c = (,) &amp;lt;$&amp;gt; b &amp;lt;*&amp;gt; c

a3 :: (a -&amp;gt; b) -&amp;gt; (a -&amp;gt; c) -&amp;gt; (a -&amp;gt; d) -&amp;gt; a -&amp;gt; (b,c,d)
a3 b c d = (,,) &amp;lt;$&amp;gt; b &amp;lt;*&amp;gt; c &amp;lt;*&amp;gt; d&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These are implemented above using &lt;a href=&quot;http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html&quot;&gt;Applicative&lt;/a&gt; operators, though I’ve given them more restrictive types to make their intended use here clearer. Now I can compose monoid functions:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;*Examples&amp;gt; let as = [45,23,78,10,11,1]
*Examples&amp;gt; :t (a2 min max)
(a2 min max) :: Ord a =&amp;gt; a -&amp;gt; (Min a, Max a)
*Examples&amp;gt; foldMap (a2 min max) as
(Min 1,Max 78)
*Examples&amp;gt; :t (a3 count (a2 min max) (a2 sum product))
(a3 count (a2 min max) (a2 sum product))
  :: (Num a, Ord a) =&amp;gt;
     a -&amp;gt; (Count, (Min a, Max a), (Sum a, Product a))
*Examples&amp;gt; foldMap (a3 count (a2 min max) (a2 sum product)) as
(Count 6,(Min 1,Max 78),(Sum {getSum = 168},Product {getProduct = 8880300}))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It’s worth noting here that the composite computations are done in a single traversal of the input list.&lt;/p&gt;
&lt;h2 id=&quot;more-complex-calculations&quot;&gt;More complex calculations&lt;/h2&gt;
&lt;p&gt;Happy with this, I decide to extend my set of basic computations with the arithmetic mean. There is a problem, however. The arithmetic mean doesn’t &quot;fit&quot; as a monoid – there’s no binary operation such that a mean for a combined set of data can be calculated from the mean of two subsets.&lt;/p&gt;
&lt;p&gt;What to do? Well, the mean is the sum divided by the count, both of which are monoids:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;newtype Mean a = Mean (Sum a,Count) deriving (Show)

instance (Num a) =&amp;gt; Monoid (Mean a) where
  mempty = Mean mempty
  mappend (Mean m1) (Mean m2) = Mean (mappend m1 m2)

mean v = Mean (Sum v,Count 1)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So I can calculate the mean if I am prepared to do a calculation after the foldMap:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;*Examples&amp;gt; let as = [45,23,78,10,11,1.5]
*Examples&amp;gt; foldMap mean as
Mean (Sum {getSum = 168.5},Count 6)
*Examples&amp;gt; let (Mean (Sum t,Count n)) = foldMap mean as in t / fromIntegral n
28.083333333333332&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;the-aggregation-type-class&quot;&gt;The Aggregation type class&lt;/h2&gt;
&lt;p&gt;For calculations like &lt;code&gt;mean&lt;/code&gt;, I need something more than a monoid. I need a monoid for accumulating the values, and then, once the accumulation is complete, a postprocessing function to compute the final result. Hence a new typeclass to extend Monoid:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;{-# LANGUAGE TypeFamilies #-}

class (Monoid a) =&amp;gt; Aggregation a where
  type AggResult a :: *
  aggResult :: a -&amp;gt; AggResult a&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This makes use of the &lt;a href=&quot;http://www.haskell.org/haskellwiki/GHC/Type_families&quot;&gt;type families ghc extension&lt;/a&gt;. We need this to express the fact that our postprocessing function aggResult has a different return type to the type of the monoid. In the above definition:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;aggResult is a function that gives you the &lt;em&gt;value&lt;/em&gt; of the final result from the &lt;em&gt;value&lt;/em&gt; of the monoid&lt;/li&gt;
&lt;li&gt;AggResult is a &lt;em&gt;type&lt;/em&gt; function that gives you the &lt;em&gt;type&lt;/em&gt; of the final result from the &lt;em&gt;type&lt;/em&gt; of the monoid&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We can write an instance of Aggregation for Mean:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;instance (Fractional a) =&amp;gt; Aggregation (Mean a) where
  type AggResult (Mean a) = a
  aggResult (Mean (Sum t,Count n)) = t/fromIntegral n&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and test it out:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;*Examples&amp;gt; let as = [45,23,78,10,11,1.5]
*Examples&amp;gt; aggResult (foldMap mean as)
28.083333333333332
*Examples&amp;gt; &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Nice. Given that &lt;code&gt;aggResult (foldMap ...)&lt;/code&gt; will be a common pattern, lets write a helper:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;afoldMap :: (Foldable t, Aggregation a) =&amp;gt; (v -&amp;gt; a) -&amp;gt; t v -&amp;gt; AggResult a
afoldMap f vs = aggResult (foldMap f vs)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In order to use the monoids we defined before (sum,product etc) we need to define Aggregation instances for them also. Even though they are trivial, it turns out to be useful, as we can make the aggResult function strip off the newtype constructors that were put there to enable the Monoid typeclass:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;instance (Num a) =&amp;gt; Aggregation (Sum a)  where
  type AggResult (Sum a) = a
  aggResult (Sum a) = a
    
instance (Num a) =&amp;gt; Aggregation (Product a)  where
  type AggResult (Product a) = a
  aggResult (Product a) = a

instance (Ord a) =&amp;gt; Aggregation (Min a)  where
  type AggResult (Min a) = a
  aggResult (Min a) = a

instance (Ord a) =&amp;gt; Aggregation (Max a)  where
  type AggResult (Max a) = a
  aggResult (Max a) = a

instance Aggregation Count where
  type AggResult Count = Int
  aggResult (Count n) = n

instance (Aggregation a, Aggregation b) =&amp;gt; Aggregation (a,b) where
  type AggResult (a,b) = (AggResult a, AggResult b)
  aggResult (a,b) = (aggResult a, aggResult b)

instance (Aggregation a, Aggregation b, Aggregation c) =&amp;gt; Aggregation (a,b,c) where
  type AggResult (a,b,c) = (AggResult a, AggResult b, AggResult c)
  aggResult (a,b,c) = (aggResult a, aggResult b, aggResult c)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is mostly boilerplate, though notice how the tuple instances delve into their components in order to postprocess the results. Now everything fits together cleanly:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;*Examples&amp;gt; let as = [45,23,78,10,11,1.5]
*Examples&amp;gt; :t (a3 count (a2 min max) mean)
(a3 count (a2 min max) mean)
  :: Ord a =&amp;gt; a -&amp;gt; (Count, (Min a, Max a), Mean a)
*Examples&amp;gt; afoldMap (a3 count (a2 min max) mean) as
(6,(1.5,78.0),28.083333333333332)
*Examples&amp;gt; &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The 4 computations have been calculated all in a single pass over the input list, and the results are free of the type constructors that are no longer required once the aggregation is complete.&lt;/p&gt;
&lt;p&gt;Another example of an Aggregation where we need to postprocess the result is counting the number of unique items. For this we will keep a set of the items seen, and then return the size of this set at the end:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;newtype CountUnique a = CountUnique (Set.Set a)

instance Ord a =&amp;gt; Monoid (CountUnique a) where
  mempty = CountUnique Set.empty
  mappend (CountUnique s1) (CountUnique s2) = CountUnique (Set.union s1 s2)

instance Ord a =&amp;gt; Aggregation (CountUnique a) where
  type AggResult (CountUnique a) = Int
  aggResult (CountUnique s1) = Set.size s1

countUnique :: Ord a =&amp;gt; a -&amp;gt; CountUnique a
countUnique a = CountUnique (Set.singleton a)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;.. in use:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;*Examples&amp;gt; let as = [5,7,8,7,11,10,11]
*Examples&amp;gt; afoldMap (a2 countUnique count) as
(5,7)&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;higher-order-aggregation-functions&quot;&gt;Higher order aggregation functions&lt;/h2&gt;
&lt;p&gt;All of the calculations seen so far have worked consistently across all values in the source data structure. We can make use of the &lt;code&gt;mempty&lt;/code&gt; monoid value in order to filter our data set, and or aggregate in groups. Here’s a couple of higher order monoid functions for this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;afilter :: Aggregation m =&amp;gt; (a -&amp;gt; Bool) -&amp;gt; (a -&amp;gt; m) -&amp;gt; (a -&amp;gt; m)
afilter match mf = \a -&amp;gt; if match a then mf a else mempty

newtype MMap k v = MMap (Map.Map k v)
  deriving Show

instance (Ord k, Monoid v) =&amp;gt; Monoid (MMap k v) where
  mempty = MMap (Map.empty)
  mappend (MMap m1) (MMap m2) = MMap (Map.unionWith mappend m1 m2)

instance (Ord k, Aggregation v) =&amp;gt; Aggregation (MMap k v) where
  type AggResult (MMap k v) = Map.Map k (AggResult v)
  aggResult (MMap m) = Map.map aggResult m

groupBy :: (Ord k, Aggregation m) =&amp;gt; (a -&amp;gt; k) -&amp;gt; (a -&amp;gt; m) -&amp;gt; (a -&amp;gt; MMap k m)
groupBy keyf valuef = \a -&amp;gt; MMap (Map.singleton (keyf a) (valuef a))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;afilter&lt;/code&gt; restricts the application of a monoid function to a subset of the input data. eg to calculate the sum of all the values, and the sum of values less than 20:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;*Examples&amp;gt; let as = [5,10,20,45.4,35,1,3.4]
*Examples&amp;gt; afoldMap (a2 sum (afilter (&amp;lt;=20) sum)) as
(119.8,39.4)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;groupBy&lt;/code&gt; takes a key function and a monoid function. It partitions the data set using the key function, and applies a monoid function to each subset, returning all of the results in a map. Non-numeric data works better as an example here. Let’s take a set of words as input, and for each starting letter, calculate the number of words with that letter, the length of the shortest word, and and the length of longest word:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;*Examples&amp;gt; let as = words &quot;monoids are a pretty simple concept in haskell some years ago i learnt of them through the excellent typeclassopedia looked at the examples and understood them straight away which is more than can be said for many of the new ideas that one learns in haskell&quot;
*Examples&amp;gt; :t groupBy head (a3 count (min.length) (max.length))
groupBy head (a3 count (min.length) (max.length))
  :: Ord k =&amp;gt; [k] -&amp;gt; MMap k (Count, Min Int, Max Int)
*Examples&amp;gt; afoldMap (groupBy head (a3 count (min.length) (max.length))) as
fromList [('a',(6,1,4)),('b',(1,2,2)),('c',(2,3,7)),('e',(2,8,9)),('f',(1,3,3)),('h',(2,7,7)),('i',(5,1,5)),('l',(3,6,6)),('m',(3,4,7)),('n',(1,3,3)),('o',(3,2,3)),('p',(1,6,6)),('s',(4,4,8)),('t',(9,3,15)),('u',(1,10,10)),('w',(1,5,5)),('y',(1,5,5))]&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Many useful data analysis functions can be written through simple function application and composition using these primitive monoid functions, the product combinators a2 and a3 and these new filtering and grouping combinators.&lt;/p&gt;
&lt;h2 id=&quot;disk-based-data&quot;&gt;Disk-based data&lt;/h2&gt;
&lt;p&gt;As pointed out before, regardless of the complexity of the computation, it’s done with a single traversal of the input data. This means that we don’t need to limit ourselves to lists and other in memory Foldable data structures. Here’s a function similar to foldMap, but that works over the lines in a file:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;foldFile :: Monoid m =&amp;gt; FilePath -&amp;gt; (BS.ByteString -&amp;gt; Maybe a) -&amp;gt; (a -&amp;gt; m) -&amp;gt; IO m
foldFile fpath pf mf = do
  h &amp;lt;- openFile fpath ReadMode
  m &amp;lt;- loop h mempty
  return m
  where
    loop h m = do
      eof &amp;lt;- hIsEOF h
      if eof
        then (return m)
        else do
          l &amp;lt;- BS.hGetLine h
          case pf l of
            Nothing -&amp;gt; loop h m
            (Just a) -&amp;gt; let m' = mappend m (mf a)
                        in loop h m'

afoldFile :: Aggregation m =&amp;gt; FilePath -&amp;gt; (BS.ByteString -&amp;gt; Maybe a) -&amp;gt; (a -&amp;gt; m) -&amp;gt; IO (AggResult m)
afoldFile fpath pf mf = fmap aggResult (foldFile fpath pf mf)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;foldFile take two parameters – a function to parse each line of the file, the other is the monoid function to do the aggregation. Lines that fail to parse are skipped. (I can here questions in the background &quot;What about strictness and space leaks?? – I’ll come back to that). As an example usage of aFoldFile, I’ll analyse some stock data. Assume that I have it in a CSV file, and I’ve got a function to parse one CSV line into a sensible data value:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import qualified Data.ByteString.Char8 as BS
import Data.Time.Calendar

data Prices = Prices {
  pName :: String,          -- The stock code
  pDate :: Day,             -- The historic date
  pOpen :: Double,          -- The price at market open
  pHigh :: Double,          -- The highest price on the date
  pLow :: Double,           -- The lowest price on the date
  pClose :: Double,         -- The price at market close
  pVolume :: Double         -- How many shares were traded
  } deriving (Show)

  parsePrices :: BS.ByteString -&amp;gt; Maybe Prices
  parsePrices = ...&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now I can use my monoid functions to analyse the file based data. How many google prices do I have, over what date range:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;*Examples&amp;gt; let stats =  afilter ((&quot;GOOG&quot;==).pName) (a3 count (min.pDate) (max.pDate))
*Examples&amp;gt; :t stats
stats
  :: Prices
     -&amp;gt; (Count,
         Min time-1.4:Data.Time.Calendar.Days.Day,
         Max time-1.4:Data.Time.Calendar.Days.Day)
*Examples&amp;gt; afoldFile &quot;prices.csv&quot; parsePrices stats
(1257,2008-05-29,2013-05-24)
*Examples&amp;gt; &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Perhaps I want to aggregate my data per month, getting traded price range and total volume. We need a helper function to work out the month of each date:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;startOfMonth :: Day -&amp;gt; Day
startOfMonth t = let (y,m,d) = toGregorian t
                 in fromGregorian y m 1&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then we can use groupBy to collect data monthly:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;:*Examples&amp;gt; let stats =  afilter ((&quot;GOOG&quot;==).pName) (groupBy (startOfMonth.pDate) (a3 (min.pLow) (max.pHigh) (sum.pVolume)))
*Examples&amp;gt; :t stats
stats
  :: Prices
     -&amp;gt; MMap
          time-1.4:Data.Time.Calendar.Days.Day
          (Min Double, Max Double, Sum Double)
*Examples&amp;gt; results &amp;lt;- afoldFile &quot;prices.csv&quot; parsePrices stats
*Examples&amp;gt; mapM_ print (Map.toList results)
(2008-05-01,(573.2,589.92,8073107.0))
(2008-06-01,(515.09,588.04,9.3842716e7))
(2008-07-01,(465.6,555.68,1.04137619e8))
...
(2013-03-01,(793.3,844.0,4.2559856e7))
(2013-04-01,(761.26,827.64,5.3574633e7))
(2013-05-01,(816.36,920.6,4.1080028e7))&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;So, I hope I’ve shown that monoids are useful indeed. They can form the core of a framework for cleanly specifing quite complex data analysis tasks.&lt;/p&gt;
&lt;p&gt;An additional typeclass which I called &quot;Aggregation&quot; extends Monoid and provides for a broader range of computations and also cleaner result types (thanks to type families). There was some discussion when I presented this talk as to whether a single method typeclass like Aggregation was a &quot;true&quot; abstraction, given it has no associated laws. This is a valid point, however using it simplifies the syntax and usage of monoidal calculations significantly, and for me, this makes it worth having.&lt;/p&gt;
&lt;p&gt;There remains an elephant in the room, however, and this is space leakage. Lazy evalulation means that, as written, most of the calculations shown run in space proportional to the input data set. Appropriate strictness annotations and related modifications will fix this, but it turns out to be slightly irritating. This blog post is already long enough, so I’ll address space leaks in in a subsequent post…&lt;/p&gt;
&lt;br /&gt;  &lt;a href=&quot;http://feeds.wordpress.com/1.0/gocomments/twdkz.wordpress.com/65/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/comments/twdkz.wordpress.com/65/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;img src=&quot;http://stats.wordpress.com/b.gif?host=twdkz.wordpress.com&amp;amp;blog=33863535&amp;amp;post=65&amp;amp;subd=twdkz&amp;amp;ref=&amp;amp;feed=1&quot; alt=&quot;&quot; height=&quot;1&quot; border=&quot;0&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Tue, 04 Jun 2013 22:05:36 +0000</pubDate>
</item>
<item>
	<title>Simon Michael: Bugfix, planning, autoweb</title>
	<guid isPermaLink="true">http://joyful.com/blog/2013-06-04-bugfix-planning-autoweb.html</guid>
	<link>http://joyful.com/blog/2013-06-04-bugfix-planning-autoweb.html</link>
	<description>&lt;div style=&quot;font-style: italic;&quot;&gt;June  4, 2013&lt;/div&gt;
&lt;h2&gt;Bugfix, planning, autoweb&lt;/h2&gt;
&lt;p&gt;
&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://joyful.com/2013-06-03-chart-fix.html&quot;&gt;Yesterday&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Today:&lt;/p&gt;
&lt;p&gt;Fixed a build failure from last night’s late session, caught by &lt;a href=&quot;http://hydra.cryp.to:8080/project/hledger&quot;&gt;Peter’s build bot&lt;/a&gt;, which is being very helpful. It’s like having R2D2 at my back! I do worry a little about the gratuitous added carbon footprint from a bunch of builds on every github commit.&lt;/p&gt;
&lt;p&gt;Loaded up the 0.22 release backlog with a bunch of items on the &lt;a href=&quot;http://hledger.org/trello&quot;&gt;planning board&lt;/a&gt;. (Scroll over to the right).&lt;/p&gt;
&lt;p&gt;Did the first backlog item, removing some troublesome friction in hledger-web development. When I’m working on hledger[-web] and need rapid compiler feedback, I use the &lt;a href=&quot;https://github.com/simonmichael/hledger/blob/master/Makefile#L168&quot;&gt;“auto” rules&lt;/a&gt; - &lt;code&gt;make auto&lt;/code&gt;, &lt;code&gt;make auto-test&lt;/code&gt;, or &lt;code&gt;make autoweb&lt;/code&gt;. These watch for file changes and recompile as needed, using &lt;a href=&quot;http://hub.darcs.net/simon/searchpath&quot;&gt;searchpath&lt;/a&gt;. Searchpath is old, but still works well and can build from multiple packages, unlike cabal-based autobuilders. So it’s very useful when I’m tweaking things in hledger-lib for hledger-web. (I’d use &lt;code&gt;yesod devel&lt;/code&gt; if I was just working just in hledger-web, and changing routes, templates, or the cabal file).&lt;/p&gt;
&lt;p&gt;The problem: when compiling hledger-web, hledger and hledger-lib together, how to enable the hairy yesodish language extensions only for hledger-web code, and not for hledger-lib and hledger, which don’t compile with them (and seem to get obfuscated if I make them compile).&lt;/p&gt;
&lt;p&gt;The solution: wrestle with ghc, sp, cabal; identify required and incompatible language extensions by trial and error; specify the compatible ones in the makefile, and add the rest as source file pragmas. Now my trusty autoweb runs again!&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;~/src/hledger$ make autoweb
sp --no-exts --no-default-map ghc  -O0  hledger-web/app/main.hs -o bin/hledger-webdev -rtsopts -W -fwarn-tabs -fno-warn-unused-do-bind -fno-warn-name-shadowing  -ihledger-lib -ihledger -ihledger-web -ihledger-web/app -L/usr/lib  -optP-include -optPhledger/dist/build/autogen/cabal_macros.h -DPATCHLEVEL=2 -DDEVELOPMENT -DVERSION='\&quot;0.21.1dev\&quot;'  -XCPP -XMultiParamTypeClasses -XOverloadedStrings -XQuasiQuotes -XRecordWildCards -XTemplateHaskell  --run -B --port 5001 --base-url http://localhost:5001 -f webtest.j
[ 1 of 52] Compiling Hledger.Data.Types ( hledger-lib/Hledger/Data/Types.hs, hledger-lib/Hledger/Data/Types.o )
[ 2 of 52] Compiling Hledger.Data.FormatStrings ( hledger-lib/Hledger/Data/FormatStrings.hs, hledger-lib/Hledger/Data/FormatStrings.o )
...
Loading package yesod-static-1.2.0 ... linking ... done.
[40 of 52] Compiling Foundation       ( hledger-web/Foundation.hs, hledger-web/Foundation.o )

hledger-web/Foundation.hs:106:37: Not in scope: `css_bootstrap_css'&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Hmm. That didn’t happen on the other machine. Never mind, something for tomorrow.&lt;/p&gt;
&lt;p&gt;Finally, realised I need to do &lt;a href=&quot;http://hackage.haskell.org/package/hledger-0.21.2&quot;&gt;yet another release&lt;/a&gt; including this morning’s build fix. This time I waited to see &lt;a href=&quot;http://hydra.cryp.to:8080/eval/211&quot;&gt;green lights&lt;/a&gt; on the buildbot before uploading.&lt;/p&gt;</description>
	<pubDate>Tue, 04 Jun 2013 19:00:00 +0000</pubDate>
</item>
<item>
	<title>JP Moresmau: Eclipse Hate and Haskell IDEs</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-37404288.post-3053784854205754325</guid>
	<link>http://jpmoresmau.blogspot.com/2013/06/eclipse-hate-and-haskell-ides.html</link>
	<description>A few weeks ago I was &lt;a href=&quot;http://www.reddit.com/r/programming/comments/1eeoqu/android_studio/&quot;&gt;reading the comments about the release of Android Studio on reddit&lt;/a&gt;. I was a bit shocked by all the Eclipse hate. But hey, I suppose I use &lt;a href=&quot;http://eclipse.org/&quot;&gt;Eclipse&lt;/a&gt; and not &lt;a href=&quot;http://www.jetbrains.com/idea/&quot;&gt;IntelliJ&lt;/a&gt;, so I don't know what I'm missing and how great it would be to work on IDEA. But then I was surprised to see that there has been no release of &lt;a href=&quot;http://code.google.com/p/ideah/&quot;&gt;ideah&lt;/a&gt;, the Haskell plug-in for IDEA, for &lt;a href=&quot;http://ideah-plugin.blogspot.fr/&quot;&gt;a year and a half&lt;/a&gt;. How come there isn't more momentum if IntelliJ is such a great IDE to build on and work with?&lt;br /&gt;&lt;br /&gt;Maybe we're just suffering from too much dispersion in the &lt;a href=&quot;http://www.haskell.org/haskellwiki/IDE&quot;&gt;Haskell IDE space&lt;/a&gt;. We have plug-ins for the major IDEs, but none of them can probably called great (I know, some people positively hate EclipseFP, but for my defence I'll say I get much more bug reports and feature requests than pull requests, I probably need a course on &quot;building a passionate programming community around your open source project&quot;). They however have the massive advantage of being able to reuse a wealth of existing code (I can use the &lt;a href=&quot;http://www.eclipse.org/egit/&quot;&gt;Eclipse Git plugin&lt;/a&gt; to work with Github, I don't need somebody to write the Haskell version). An IDE in Haskell like &lt;a href=&quot;http://leksah.org/&quot;&gt;Leksah&lt;/a&gt; or something built on top of &lt;a href=&quot;https://github.com/yi-editor/yi&quot;&gt;Yi&lt;/a&gt; would be amazing to showcase that you can do real applications in Haskell (since you still get people saying that Haskell is only for toying with), but then you need to build everything, which requires people (see note above on building a community). Then we have web based editors like the offering from &lt;a href=&quot;https://www.fpcomplete.com/school/how-to-use-the-school-of-haskell/creating-your-own-content&quot;&gt;FPComplete&lt;/a&gt;, but for developers to use them on real projects we need to think on how to package a web based interface and what it means for a development work-flow: I'm not sure developers would embrace developing using a browser. Of course we have plug-ins for the Unixy editors, &lt;a href=&quot;https://github.com/haskell/haskell-mode&quot;&gt;emacs&lt;/a&gt; and &lt;a href=&quot;http://projects.haskell.org/haskellmode-vim/&quot;&gt;vim&lt;/a&gt;, but if we want to open up Haskell to non hackers, maybe we need something more...&lt;br /&gt;&lt;br /&gt;So can we continue like that and hope to have a few decent environments? Or shall we all agree on a direction and unite to provide the one true development environment for Haskell? Sometimes people say &quot;Haskell is so different and advanced as a programming language, it needs a new type of editor/IDE&quot;. I don't disagree with it, but who has the vision of what the Haskell IDE should be?</description>
	<pubDate>Tue, 04 Jun 2013 18:14:33 +0000</pubDate>
	<author>noreply@blogger.com (JP Moresmau)</author>
</item>
<item>
	<title>Simon Michael: Chart fix</title>
	<guid isPermaLink="true">http://joyful.com/blog/2013-06-03-chart-fix.html</guid>
	<link>http://joyful.com/blog/2013-06-03-chart-fix.html</link>
	<description>&lt;div style=&quot;font-style: italic;&quot;&gt;June  4, 2013&lt;/div&gt;
&lt;h2&gt;Chart fix&lt;/h2&gt;
&lt;p&gt;
&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://joyful.com/2013-06-02-earth-nap.html&quot;&gt;Yesterday&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Here’s the developer diary entry for monday.&lt;/p&gt;
&lt;p&gt;Peter Simons &lt;a href=&quot;http://thread.gmane.org/gmane.comp.finance.ledger.hledger/935&quot;&gt;argued convincingly&lt;/a&gt; against depending on yesod-platform.&lt;/p&gt;
&lt;p&gt;I released hledger 0.21.1, fixing a &lt;a href=&quot;https://github.com/simonmichael/hledger/issues/122&quot;&gt;regression&lt;/a&gt; in hledger-web 0.21 where it showed the wrong chart Y-values when filtering by date. The chart appears at the top of the &lt;a href=&quot;http://demo.hledger.org/register&quot;&gt;register view&lt;/a&gt;, and simply shows the register’s running total in graphical form. Here’s the report type which provides values for the chart:&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span class=&quot;co&quot;&gt;-- | A transactions report includes a list of transactions&lt;/span&gt;
&lt;span class=&quot;co&quot;&gt;-- (posting-filtered and unfiltered variants), a running balance, and some&lt;/span&gt;
&lt;span class=&quot;co&quot;&gt;-- other information helpful for rendering a register view (a flag&lt;/span&gt;
&lt;span class=&quot;co&quot;&gt;-- indicating multiple other accounts and a display string describing&lt;/span&gt;
&lt;span class=&quot;co&quot;&gt;-- them) with or without a notion of current account(s).&lt;/span&gt;
&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TransactionsReport&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;                   &lt;span class=&quot;co&quot;&gt;-- label for the balance column, eg &quot;balance&quot; or &quot;total&quot;&lt;/span&gt;
                          ,[&lt;span class=&quot;dt&quot;&gt;TransactionsReportItem&lt;/span&gt;] &lt;span class=&quot;co&quot;&gt;-- line items, one per transaction&lt;/span&gt;
                          )
&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TransactionsReportItem&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Transaction&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;-- the corresponding transaction&lt;/span&gt;
                              ,&lt;span class=&quot;dt&quot;&gt;Transaction&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;-- the transaction with postings to the current account(s) removed&lt;/span&gt;
                              ,&lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt;        &lt;span class=&quot;co&quot;&gt;-- is this a split, ie more than one other account posting&lt;/span&gt;
                              ,&lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;      &lt;span class=&quot;co&quot;&gt;-- a display string describing the other account(s), if any&lt;/span&gt;
                              ,&lt;span class=&quot;dt&quot;&gt;MixedAmount&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;-- the amount posted to the current account(s) (or total amount posted)&lt;/span&gt;
                              ,&lt;span class=&quot;dt&quot;&gt;MixedAmount&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;-- the running balance for the current account(s) after this transaction&lt;/span&gt;
                              )

triDate (t,_,_,_,_,_) &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; tdate t
triAmount (_,_,_,_,a,_) &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; a
triBalance (_,_,_,_,_,a) &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; a
triSimpleBalance (_,_,_,_,_,&lt;span class=&quot;dt&quot;&gt;Mixed&lt;/span&gt; a) &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; a &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt; [] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;0&quot;&lt;/span&gt;
                                                 (&lt;span class=&quot;dt&quot;&gt;Amount&lt;/span&gt;{aquantity&lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt;q})&lt;span class=&quot;fu&quot;&gt;:&lt;/span&gt;_ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;show&lt;/span&gt; q&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and here’s the slightly tricky new code to split it into separate reports, each covering one commodity:&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span class=&quot;co&quot;&gt;-- Split a transactions report whose items may involve several commodities,&lt;/span&gt;
&lt;span class=&quot;co&quot;&gt;-- into one or more single-commodity transactions reports.&lt;/span&gt;
&lt;span class=&quot;ot&quot;&gt;transactionsReportByCommodity ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TransactionsReport&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [&lt;span class=&quot;dt&quot;&gt;TransactionsReport&lt;/span&gt;]
transactionsReportByCommodity tr &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt;
  [filterTransactionsReportByCommodity c tr &lt;span class=&quot;fu&quot;&gt;|&lt;/span&gt; c &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; transactionsReportCommodities tr]
  &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;
    transactionsReportCommodities (_,items) &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt;
      nub &lt;span class=&quot;fu&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;sort&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; acommodity &lt;span class=&quot;fu&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;concatMap&lt;/span&gt; (amounts &lt;span class=&quot;fu&quot;&gt;.&lt;/span&gt; triAmount) items

&lt;span class=&quot;co&quot;&gt;-- Remove transaction report items and item amount (and running&lt;/span&gt;
&lt;span class=&quot;co&quot;&gt;-- balance amount) components that don't involve the specified&lt;/span&gt;
&lt;span class=&quot;co&quot;&gt;-- commodity. Other item fields such as the transaction are left unchanged.&lt;/span&gt;
&lt;span class=&quot;ot&quot;&gt;filterTransactionsReportByCommodity ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Commodity&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TransactionsReport&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TransactionsReport&lt;/span&gt;
filterTransactionsReportByCommodity c (label,items) &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt;
  (label, fixTransactionsReportItemBalances &lt;span class=&quot;fu&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;concat&lt;/span&gt; [filterTransactionsReportItemByCommodity c i &lt;span class=&quot;fu&quot;&gt;|&lt;/span&gt; i &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; items])
  &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;
    filterTransactionsReportItemByCommodity c (t,t2,s,o,a,bal)
      &lt;span class=&quot;fu&quot;&gt;|&lt;/span&gt; c &lt;span class=&quot;ot&quot;&gt;`elem`&lt;/span&gt; cs &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; [item']
      &lt;span class=&quot;fu&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;otherwise&lt;/span&gt;   &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; []
      &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;
        cs &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; acommodity as
        item' &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; (t,t2,s,o,a',bal)
        a' &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; filterMixedAmountByCommodity c a

    fixTransactionsReportItemBalances [] &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; []
    fixTransactionsReportItemBalances [i] &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; [i]
    fixTransactionsReportItemBalances items &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;reverse&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;$&lt;/span&gt; i&lt;span class=&quot;fu&quot;&gt;:&lt;/span&gt;(go startbal is)
      &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;
        i&lt;span class=&quot;fu&quot;&gt;:&lt;/span&gt;is &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;reverse&lt;/span&gt; items
        startbal &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; filterMixedAmountByCommodity c &lt;span class=&quot;fu&quot;&gt;$&lt;/span&gt; triBalance i
        go _ [] &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; []
        go bal ((t,t2,s,o,amt,_)&lt;span class=&quot;fu&quot;&gt;:&lt;/span&gt;is) &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; (t,t2,s,o,amt,bal')&lt;span class=&quot;fu&quot;&gt;:&lt;/span&gt;go bal' is
          &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt; bal' &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; bal &lt;span class=&quot;fu&quot;&gt;+&lt;/span&gt; amt

&lt;span class=&quot;co&quot;&gt;-- | Filter out all but the specified commodity from this amount.&lt;/span&gt;
&lt;span class=&quot;ot&quot;&gt;filterMixedAmountByCommodity ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Commodity&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MixedAmount&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MixedAmount&lt;/span&gt;
filterMixedAmountByCommodity c (&lt;span class=&quot;dt&quot;&gt;Mixed&lt;/span&gt; as) &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Mixed&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;filter&lt;/span&gt; ((&lt;span class=&quot;fu&quot;&gt;==&lt;/span&gt;c)&lt;span class=&quot;fu&quot;&gt;.&lt;/span&gt; acommodity) as&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;so that we can render one line for each commodity:&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span class=&quot;ot&quot;&gt;registerChartHtml ::&lt;/span&gt; [[&lt;span class=&quot;dt&quot;&gt;TransactionsReportItem&lt;/span&gt;]] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;HtmlUrl&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;AppRoute&lt;/span&gt;
registerChartHtml itemss &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt;
&lt;span class=&quot;fu&quot;&gt;...&lt;/span&gt;
     \&lt;span class=&quot;fu&quot;&gt;$.&lt;/span&gt;plot(chartdiv,
             [
              &lt;span class=&quot;fu&quot;&gt;$&lt;/span&gt;forall items &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; itemss
               [
                &lt;span class=&quot;fu&quot;&gt;$&lt;/span&gt;forall i &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;reverse&lt;/span&gt; items
                 [&lt;span class=&quot;fu&quot;&gt;#&lt;/span&gt;{dayToJsTimestamp &lt;span class=&quot;fu&quot;&gt;$&lt;/span&gt; triDate i}, &lt;span class=&quot;fu&quot;&gt;#&lt;/span&gt;{triSimpleBalance i}],
                []
               ],
              []
             ],
             {
               xaxis&lt;span class=&quot;fu&quot;&gt;:&lt;/span&gt; {
                mode&lt;span class=&quot;fu&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;time&quot;&lt;/span&gt;,
                timeformat&lt;span class=&quot;fu&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;%y/%m/%d&quot;&lt;/span&gt;
               }
             }
             );
&lt;span class=&quot;fu&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</description>
	<pubDate>Tue, 04 Jun 2013 05:30:00 +0000</pubDate>
</item>
<item>
	<title>Paul Potts: Objective-C, Day 2</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-21500237.post-839798927932946107</guid>
	<link>http://praisecurseandrecurse.blogspot.com/2013/06/objective-c-day-2.html</link>
	<description>&lt;p&gt;(Warning: non-FP content)&lt;/p&gt; &lt;p&gt;So today I'm working on chapter 3 in &lt;i&gt;iOS Programming&lt;/i&gt;. This is about memory management. I have vague memories of manual reference-counting using retain and release in earlier experiments with Objective-C, but this book teaches ARM (Automatic Reference Counting). You enable ARM when you configure an XCode project of iOS.&lt;/p&gt; &lt;p&gt;The idea behind object references (well, pointers in iOS) and trees of objects and their owners is not new to me. And here they they still do not clarify whether a statement like &lt;b&gt;items = nil&lt;/b&gt; actually invokes runtime behavior to destroy objects. I believe it does not. When I override a &lt;b&gt;dealloc&lt;/b&gt; method to log, I see that the &lt;b&gt;dealloc&lt;/b&gt; methods are called when the curly brace at the end of @autoreleasepool is reached. This makes sense, as in C it is the point where variables go out of scope. So reference-counting bookkeeping must be invoked then. It does not seem to be possible to step in with the debugger at this point to view what is happening, though -- Apple's Objective-C runtime is proprietary software.&lt;/p&gt; &lt;p&gt;We next get into weak references. There's a &lt;b&gt;__weak&lt;/b&gt; specifier that can appear as part of an object pointer declaration, and &lt;i&gt;iOS Programming&lt;/i&gt; says that &quot;an interesting property of weak references is that they know when the object they reference is destroyed... the [parent object] automatically sets its [child object] instance variable to &lt;b&gt;nil&lt;/b&gt;.&quot; I'm going to have to read more about that. I really wish this book were more precise in its language. There's another specifier, &lt;b&gt;__unsafe_unretained&lt;/b&gt;, which is not set -- and so a pointer to a destroyed object could still be dereferenced. I suppose I should just be happy that this works and use it, but I'm the type to want to know what is going on at the register level. Apparently ARC (the &quot;automatic&quot; in reference-counting) is all due to some amazing work done in clang, where the static analysis actually figures out all the possible points of object creation and deletion and can wrap up your reference-counting for you. That seems like a great development -- because I've never quite liked how garbage collectors (at least, those without hardware support), no matter how efficient they are, had to actually be implemented, looking at dumb machine pointers. It seems to me that there could be room in purely-GC'ed languages for this kind of static analysis. But I haven't thought very hard about that yet.&lt;/p&gt; &lt;p&gt;&lt;i&gt;Properties&lt;/i&gt; seem to be a shorthand, asking the compiler to provide getters and setters. There's a great example as to how much code this can eliminate. There's a strange wart in that we are asked to specify (nonatomic) in every property, because that is not the default setting. Presumably this is to support multiple threads accessing the object. Again, a dark corner to look into.&lt;/p&gt; &lt;p&gt;For a little refresher, I took a break by reading the first few chapters of Brad Cox's book. He outlines his vision of a software IC, along the lines of a hardware C, that would support reusability primarily through late binding, limited interfaces, and wrapping up functionality with data so that clients don't have to write the operations on an object's data type. It's an interesting vision.&lt;/p&gt; &lt;p&gt;Cox writes of early Objective-C:&lt;/p&gt; &lt;blockquote&gt;Objective-C adds precisely one new data type, the object, to those C provides already, and precisely one new operation, the message expression.&lt;/blockquote&gt; &lt;p&gt;Cox's book indicates that early versions of Objective-C used an intermediate preprocessor step, between the C macro-preprocessing step and the regular C compiler. If you are my age you might recall that, early on, C++ was treated similarly, with a program called &lt;a href=&quot;http://en.wikipedia.org/wiki/Cfront&quot;&gt;CFront&lt;/a&gt;. This is no longer the case with either Objective-C or C++, although this approach lives on in tools like &lt;a href=&quot;http://en.wikipedia.org/wiki/Qt_(framework)&quot;&gt;Qt, with its Meta Object Compiler&lt;/a&gt;, or moc).&lt;/p&gt; &lt;p&gt;Cox describes the pragmatic design of Objective-C:&lt;/p&gt; &lt;blockquote&gt;One of Objective-C's key features is that it is always possible to bypass the object-oriented machinery to access an object's private information directly. This is one of a hybrid language's greatest theoretical weaknesses and one of its greatest pragmatic strengths. Low-level code is often best developed in a conventional manner, exploiting static binding to obtain high machine efficiency and strong type checking. Conversely, user-level code is often best written as objects. It is important that these levels interface smoothly and efficiently.&lt;/blockquote&gt; &lt;p&gt;There is an interesting passage about object identifiers. In a slight muddling of his earlier statement he writes that &quot;objects are identified by a new Objective-C data type called an id, or object identifier.&quot; In practice, though, this isn't really a new data type &lt;i&gt;per se&lt;/i&gt;. The address (a pointer to) an object is this identifier. The veneer over C is so thin that, he writes, it would have been perfectly feasible to implement the dynamic message sends using straightforward C language calls like &lt;/p&gt;&lt;pre&gt;reply = _msg_(aReceiver, &quot;aMessage&quot;, argument1, ...)&lt;/pre&gt; (I assume he'd use C's &lt;a href=&quot;http://en.wikipedia.org/wiki/Varargs.h#.3Cvarargs.h.3E&quot;&gt;variable-length argument list&lt;/a&gt; mechanism here), but for efficiency he wanted to avoid string comparison in the dispatch mechanism. Cox was quite aware of the reaction that messages like [anObject do:arg1 with:arg2] would inspire in C programmers, writing &quot;the convention seems strange to those accustomed to conventional function call syntax, and frankly, I find it a mixed blessing.&quot;&lt;p&gt;&lt;/p&gt; &lt;p&gt;In this formulation, Objective-C classes were still objects, and class methods were called &quot;factory methods&quot; (but I'm peeking ahead a number of pages, so there might be some differences from their current incarnation), as distinct from instance methods. Cox writes &quot;...the programmer conceives the initial object... the loader gives it life by installing it in memory... this is done once for each class in the system. These primal objects are called factory objects... every factory's name is published in a global variable.&quot;&lt;/p&gt; &lt;p&gt;Cox is a very thoughtful writer and he presents a minimalist view of what an object-oriented language requires in order to provide the most basic advantage of OOP. He writes:&lt;/p&gt; &lt;blockquote&gt;One last time: the only substantive difference between conventional programming and object-oriented programming is the selection mechanism. This is not to minimize its importance, but to demystify what is basically a very simple notion. Its significance is that it moves a single responsibility across the boundary between the consumer of a service and the supplier of that service. In conventional programming, the consumer is responsible for choosing functions that operate properly on the data managed by that service. The selection mechanism moves that single responsibility off the consumer and onto the supplier. In this small change lies all the power of the technique.&lt;/blockquote&gt; &lt;p&gt;That really caused me to, as they say, nearly drop my monocle. Could this really be where most of the advantage, such as it is, imperfectly realized, understood, and applied, of OOP comes from?&lt;/p&gt; &lt;p&gt;The mainstream business world got C++ instead, a mash-up of C and &lt;a href=&quot;http://en.wikipedia.org/wiki/Simula&quot;&gt;Simula&lt;/a&gt;, and then Java instead, &lt;a href=&quot;http://www.infoworld.com/d/developer-world/java-becoming-new-cobol-204&quot;&gt;the Cobol of object-oriented programming languages&lt;/a&gt;. I was not pleased. As I consider my career I also consider which implementation languages I should focus my efforts on, for the next decade. After exposure to Haskell it's hard to believe that, ultimately, functions -- without explicit state -- won't prove to be a cleaner reusable element than classes, whatever kind of binding they use. And don't get me wrong -- I like classes. I'm pretty certain that I'll still be writing C code, at least occasionally, in ten years. I'd prefer to be writing less C++. But what I'd really like is to move on -- to pick a &quot;&lt;a href=&quot;http://skillsmatter.com/podcast/agile-testing/bobs-last-language&quot;&gt;last programming language&lt;/a&gt;.&quot; Objective-C isn't that, for me -- it's too imperative, and can't truly be made safe, just &lt;i&gt;safer&lt;/i&gt;. I'm enjoying it in this context, but would Objective-C even be an viable option outside of the Apple ecosystem? It doesn't seem to have gained much adoption. The state of GnuStep doesn't seem terribly robust. And so my career-long quest for a Better Way continues, while at the same time trying to gain facility with all the Good Ways along the way.&lt;/p&gt;</description>
	<pubDate>Tue, 04 Jun 2013 02:57:00 +0000</pubDate>
	<author>noreply@blogger.com (Paul Potts)</author>
</item>
<item>
	<title>Luke Palmer: The Plan</title>
	<guid isPermaLink="false">http://lukepalmer.wordpress.com/?p=2154</guid>
	<link>http://lukepalmer.wordpress.com/2013/04/01/the-plan/</link>
	<description>&lt;p&gt;Last September, I decided that it was time to get a programming job again.  After two months of trying to find paid work (of any kind, $10 would have been great!) as a composer, I realized that it’s really hard.  There are a lot of people willing to work for free, and without much of a scoring portfolio (as opposed to the “pure music” I do) I have no way to distinguish myself to the studios that have a budget.  Also, a lot of games want orchestral scores, and I don’t have the hardware and software I need to make convincing-sounding synthetic orchestral scores.  Also, I’m sure once I get the necessary hardware and software, I will need time to practice with it.  In short, I needed money and time.  I am extremely fortunate to have, in my free-flowing way, stumbled onto a skill that is valued by the economy, and so I decided it was once again time to utilize that skill to achieve my other goals.  I planned to live reasonably cheaply, save up money so that I can buy equipment and support myself for enough time to build up a portfolio by doing free projects.&lt;/p&gt;
&lt;p&gt;Now I have been programming for Clozure for almost six months.  As far as jobs go, it’s great.  I get to work in my favorite language, Haskell, and they give me enough freedom to experiment with designs and come up with solutions that not only work, but that I would even consider &lt;i&gt;good&lt;/i&gt;.  My fear of programming jobs was based on having jobs where I constantly have to compromise my values, either by working in crappy languages or on startup-style timelines where there is no time to lose.  With this job, I feel reunited with my love of software, and my inspirations for developer support tools have been once again ignited.&lt;/p&gt;
&lt;p&gt;And so I have amended the plan: after I have saved enough money to support myself for several years, I will not only attempt to bootstrap a career composing, but dedicate my current work week to making a reality the software ideas which have been floating around in my head for half a decade.  This prospect &lt;i&gt;really&lt;/i&gt; excites me — the reason I have not been able to make my ideas is mostly the time pressure: there’s was always something else I &lt;i&gt;should&lt;/i&gt; be doing, and so I always felt guilty working on my pet projects.  I wonder, what am I capable of if my pet projects are the main thing?&lt;/p&gt;
&lt;p&gt;I want to revive &lt;a href=&quot;https://github.com/luqui/CodeCatalog&quot;&gt;CodeCatalog&lt;/a&gt;.  Max and I lost steam on that project for a number of reasons.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Due to family pressure, I returned to school.&lt;/li&gt;
&lt;li&gt;I fell in love with a girl and got my heart all broken.  That can be kind of a downer.&lt;/li&gt;
&lt;li&gt;The priorities of the project compromised my vision.  We were attempting to use modern wisdom to make the project successful: first impressions and intuitive usability came first.  Our focus was on making it pretty and satisfying to use (which took a long time since neither of us were experienced web front-end developers), and that required me to strip off the most interesting parts of the project because noobs wouldn’t immediately understand it.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;So I want to re-orient (3) to make it more satisfying for me.  I want to allow myself to make the large strides that I envisage rather than baby-stepping toward success — to encourage myself to use my own talents in design and abstraction rather than trying to be a front-end person, to emphasize the exciting parts (what Audrey Tang calles &lt;tt&gt;-Ofun&lt;/tt&gt;).  By funding myself, I will not feel the guilt that comes with working on a project at the same time as (1).  I can do no more than hope that something like (2) doesn’t happen.  (I have a wonderful, stable and supportive relationship right now, so if that continues, that’d cover it :-)&lt;/p&gt;
&lt;p&gt;I have many ideas; the reason I want to return to CodeCatalog in particular is mainly because I have identified most of my ideas as aspects of this project.  My specific fancies change frequently (usually to things I have thought about before but never implemented), and so by focusing on this project in a researchy rather than producty way, I can entertain them while still working toward a larger goal and eventually benefitting the community.  &lt;/p&gt;
&lt;p&gt;Here is a summary of some ideas that fit in the CodeCatalog umbrella (just because I’m excited and want to remember):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://lukepalmer.wordpress.com/2008/11/12/sketch-of-udon-version-controlpackaging-system/&quot;&gt;Inter-project version control&lt;/a&gt; — I have always been frustrated by the inability of git and hg to merge two projects while still allowing interoperation with where they came from.  The “project” quantum seems arbitrary, and I want to globalize it.&lt;/li&gt;
&lt;li&gt;Package adapters — evolving the interface of a package without breaking users of the old interface by rewriting the old package in terms of the new one.  There is a great deal that can be done automatically in this area with sufficient knowledge about the meaning of changes. I talked with Michael Sloan about this some, and some of the resulting ideas are contained in &lt;a href=&quot;http://www.mgsloan.com/wordpress/?p=219&quot;&gt;this writeup&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Informal checked documentation — documenting the assumptions of code in a machine-readable semi-formal language, to get the computer to pair-program with you (e.g. you write a division &lt;tt&gt;x/y&lt;/tt&gt; and you have no &lt;tt&gt;y /= 0&lt;/tt&gt; assumption in scope, you’d get a “documentation obligation” to explain in english why &lt;tt&gt;y&lt;/tt&gt; can’t be 0).&lt;/li&gt;
&lt;li&gt;Structural editing — coding by transforming valid syntax trees.  Yes it’d be cool, but the main reason it’s compelling to me is in its synergy with other features.  Once you have the notion of focusing on expressions, holes with contextual information (a la Agda), semi-automatic creation of package and data-type adapters, smarter version control (e.g. a change might rename &lt;i&gt;all&lt;/i&gt; references to an identifier, even the ones that weren’t there when the change was made) all come as natural extensions to the idea.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I think the challenge for me will be to focus on one of these for long enough to make it cool before getting distracted by another.  My plan for that is to set short-term goals here on my blog and use it to keep myself in check.  I am considering involving other people in my project as a way to keep myself focused (i.e. maybe I can make a little mini-kickstarter in which my devotees can pledge small amounts in exchange for me completing a specific goal on time).&lt;/p&gt;
&lt;p&gt;This is all two years away or more, which feels like a long time, but in the grand scheme is not that long in exchange for what I see as the potential of this endeavor.  I’m just excited and couldn’t help but to think about it and get pumped up.  Thanks for reading!&lt;/p&gt;
&lt;p&gt;Oh, despite the date, this is totally not an April Fools joke (as far as I know ;-).&lt;/p&gt;
&lt;br /&gt;  &lt;img src=&quot;http://stats.wordpress.com/b.gif?host=lukepalmer.wordpress.com&amp;amp;blog=5292379&amp;amp;post=2154&amp;amp;subd=lukepalmer&amp;amp;ref=&amp;amp;feed=1&quot; alt=&quot;&quot; height=&quot;1&quot; border=&quot;0&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Mon, 03 Jun 2013 20:49:26 +0000</pubDate>
</item>
<item>
	<title>Mike Izbicki: HLearn cross-validates &gt;400x faster than Weka</title>
	<guid isPermaLink="false">http://izbicki.me/blog/?p=2468</guid>
	<link>http://izbicki.me/blog/hlearn-cross-validates-400x-faster-than-weka?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=hlearn-cross-validates-400x-faster-than-weka</link>
	<description>&lt;p&gt;&lt;img src=&quot;http://izbicki.me/blog/wp-content/uploads/2013/05/weka-lambda-haskell-300x150.png&quot; alt=&quot;weka-lambda-haskell&quot; height=&quot;120&quot; class=&quot;alignright  wp-image-2478&quot; width=&quot;240&quot; /&gt;&lt;a href=&quot;http://www.cs.waikato.ac.nz/~ml/weka/&quot;&gt;Weka&lt;/a&gt; is one of the most popular tools for data analysis.  But Weka takes &lt;strong&gt;70 minutes&lt;/strong&gt; to perform leave-one-out cross-validate using a simple &lt;a href=&quot;https://en.wikipedia.org/wiki/Naive_Bayes_classifier&quot;&gt;naive bayes classifier&lt;/a&gt; on the &lt;a href=&quot;http://archive.ics.uci.edu/ml/datasets/Census-Income+(KDD)&quot;&gt;census income&lt;/a&gt; data set, whereas Haskell’s &lt;a href=&quot;https://github.com/mikeizbicki/HLearn&quot;&gt;HLearn&lt;/a&gt; library only takes &lt;strong&gt;9 seconds&lt;/strong&gt;.  Weka is 465x slower!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Code and instructions for reproducing these experiments are &lt;a href=&quot;https://github.com/mikeizbicki/HLearn/tree/master/HLearn-classification/src/examples/weka-cv#readme&quot;&gt;available on github&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;span id=&quot;more-2468&quot;&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Why is HLearn so much faster?&lt;/p&gt;
&lt;p&gt;Well, it turns out that the bayesian classifier has the algebraic structure of a &lt;a href=&quot;https://en.wikipedia.org/wiki/Monoid&quot;&gt;monoid&lt;/a&gt;, a &lt;a href=&quot;https://en.wikipedia.org/wiki/Abelian_group&quot;&gt;group&lt;/a&gt;, and a &lt;a href=&quot;https://en.wikipedia.org/wiki/Vector_space&quot;&gt;vector space&lt;/a&gt;.  HLearn uses a new cross-validation algorithm that can exploit these algebraic structures.  The standard algorithm runs in time &lt;span id=&quot;tex_7479&quot;&gt;&lt;/span&gt;, where &lt;span id=&quot;tex_3881&quot;&gt;&lt;/span&gt; is the number of “folds” and &lt;span id=&quot;tex_4131&quot;&gt;&lt;/span&gt; is the number of data points.  The algebraic algorithms, however, run in time &lt;span id=&quot;tex_1491&quot;&gt;&lt;/span&gt;.  In other words, it doesn’t matter how many folds we do, the run time is constant!  And not only are we faster, but we get the &lt;em&gt;exact same answer&lt;/em&gt;.  Algebraic cross-validation is not an approximation, it’s just fast.&lt;/p&gt;
&lt;p&gt;Here’s some run times for k-fold cross-validation on the census income data set.  Notice that HLearn’s run time is constant as we add more folds.&lt;i&gt;&lt;br /&gt;
&lt;/i&gt;&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;&lt;img src=&quot;http://izbicki.me/blog/wp-content/uploads/2013/05/k-fold-cross-validation-weka1.png&quot; alt=&quot;k-fold-cross-validation-weka&quot; height=&quot;336&quot; class=&quot;aligncenter size-full wp-image-2479&quot; width=&quot;555&quot; /&gt;&lt;/p&gt;
&lt;p&gt;And when we set k=n, we have leave-one-out cross-validation.  Notice that Weka’s cross-validation has quadratic run time, whereas HLearn has linear run time.&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;&lt;img src=&quot;http://izbicki.me/blog/wp-content/uploads/2013/05/leave-one-out-fast-cross-validation-weka1.png&quot; alt=&quot;leave-one-out-fast-cross-validation-weka&quot; height=&quot;333&quot; class=&quot;aligncenter size-full wp-image-2480&quot; width=&quot;553&quot; /&gt;&lt;/p&gt;
&lt;p&gt;HLearn certainly isn’t going to replace Weka any time soon, but it’s got a number of cool tricks like this going on inside.  If you want to read more, you should check out these two recent papers:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;(ICML13) &lt;a href=&quot;http://izbicki.me/public/papers/icml2013-algebraic-classifiers.pdf&quot;&gt;Algebraic Classifiers: a generic approach to fast cross-validation, online training, and parallel training&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;line-height: 12px;&quot; class=&quot;Apple-style-span&quot;&gt;(TFP13) &lt;a href=&quot;http://izbicki.me/public/papers/tfp2013-hlearn-a-machine-learning-library-for-haskell.pdf&quot;&gt;HLearn: a machine learning library for Haskell&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I’ll continue to write more about these tricks in future blog posts.&lt;/p&gt;
&lt;p&gt;Subscribe to the &lt;a href=&quot;http://izbicki.me/blog/feed&quot;&gt;RSS feed&lt;/a&gt; to stay tuned.&lt;/p&gt;
 &lt;img src=&quot;http://izbicki.me/blog/?feed-stats-post-id=2468&quot; style=&quot;display: none;&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Mon, 03 Jun 2013 15:33:16 +0000</pubDate>
</item>
<item>
	<title>Simon Michael: Earth Nap</title>
	<guid isPermaLink="true">http://joyful.com/blog/2013-06-02-earth-nap.html</guid>
	<link>http://joyful.com/blog/2013-06-02-earth-nap.html</link>
	<description>&lt;div style=&quot;font-style: italic;&quot;&gt;June  3, 2013&lt;/div&gt;
&lt;h2&gt;Earth Nap&lt;/h2&gt;
&lt;p&gt;
&lt;/p&gt;&lt;p&gt;Let’s take a break from all this &lt;a href=&quot;http://joyful.com/2013-06-01-hledger-0.21-released.html&quot;&gt;hledger stuff&lt;/a&gt;. I’ve been programming for about 30 years, and I’ve acquired - often the hard way - some tricks, tools and habits helpful for balance and productivity as a working programmer. We are all different, but I bet some of you could also use these. Here’s one - I’ll call it &lt;strong&gt;Earth Nap&lt;/strong&gt;. It’s very simple:&lt;/p&gt;
&lt;ol style=&quot;&quot;&gt;
&lt;li&gt;&lt;p&gt;Find a patch of earth where you can lie down without being disturbed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lie down. If possible, cover your eyes or head.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rest with eyes closed, doze, or nap for 5-30 minutes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reactivate gently. Stretch, rub your face/hands/feet, roll up, maybe sing, shake out the cobwebs or walk a little.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;A good time is after lunch, or early/mid/late afternoon. Or any time you feel fatigued, over-pressured, or burnt out. There is always time for a 5m break.&lt;/p&gt;
&lt;p&gt;Direct contact between your skin and the earth is ideal. Lately I’ve done it on the beach and under a tree in a nearby quiet park - perfect. At my last client gig, I was onsite all day and there was no park and no quiet.. but there was an unused stair leading up to a small concrete landing where no one went. A few minutes in my secret lair made a big difference!&lt;/p&gt;
&lt;p&gt;If you’re likely to sleep longer than 30m, set a gentle alarm (a deeper sleep rhythm kicks in after about 45 minutes which would leave you feeling groggy). Or, just watch/trust your body/mind’s natural napping cycle. It’s not necessary to fall asleep. You may notice yourself sink into a dozing/free association/dreaming state, then return to wakefulness.&lt;/p&gt;
&lt;p&gt;Each time you do this, observe the effects. You might notice:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;more calmness/cheerfulness&lt;/li&gt;
&lt;li&gt;less mental noise&lt;/li&gt;
&lt;li&gt;better concentration&lt;/li&gt;
&lt;li&gt;better coping skills and emotional resilience&lt;/li&gt;
&lt;li&gt;slower, more powerful energy&lt;/li&gt;
&lt;li&gt;less addiction/aversion to pleasant/unpleasant tasks&lt;/li&gt;
&lt;li&gt;less vulnerability to “rabbit holes” (mentally enticing, demanding tasks which fan out endlessly and lead nowhere)&lt;/li&gt;
&lt;li&gt;new ideas and solutions appearing effortlessly&lt;/li&gt;
&lt;li&gt;more energy left over at end of day&lt;/li&gt;
&lt;/ul&gt;</description>
	<pubDate>Mon, 03 Jun 2013 06:45:00 +0000</pubDate>
</item>
<item>
	<title>Tom Moertel: Tricks of the trade: Recursion to Iteration, Part 3: Recursive Data Structures</title>
	<guid isPermaLink="true">http://blog.moertel.com/posts/2013-06-03-recursion-to-iteration-3.html</guid>
	<link>http://blog.moertel.com/posts/2013-06-03-recursion-to-iteration-3.html</link>
	<description>&lt;div class=&quot;info&quot;&gt;Posted on June  3, 2013&lt;/div&gt;
&lt;div class=&quot;tags&quot;&gt;Tags: &lt;a href=&quot;http://blog.moertel.com/tags/programming.html&quot;&gt;programming&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/recursion.html&quot;&gt;recursion&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/iteration.html&quot;&gt;iteration&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/python.html&quot;&gt;python&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/recursion-to-iteration series.html&quot;&gt;recursion-to-iteration series&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/tail calls.html&quot;&gt;tail calls&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/data structures.html&quot;&gt;data structures&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;This is the third article in &lt;a href=&quot;http://blog.moertel.com/tags/recursion-to-iteration%20series.html&quot;&gt;a series on converting recursive algorithms into iterative algorithms&lt;/a&gt;. If any of what follows seems confusing, you may want to read the earlier articles first.&lt;/p&gt;
&lt;p&gt;This is an extra article that I hadn’t planned. I’m writing it because in a comment on the previous article a reader asked me to show a less mathematical example and suggested tree traversal. So that’s the subject of this article: We’ll take a binary tree and flatten it into a list, first recursively, then iteratively.&lt;/p&gt;
&lt;h3 id=&quot;the-challenge&quot;&gt;The challenge&lt;/h3&gt;
&lt;p&gt;First, let’s define a binary tree to be either empty or given by a node having three parts: (1) a value, (2) a left subtree, and (3) a right subtree, where both of the subtrees are themselves binary trees. In Haskell, we might define it like so:&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BinaryTree&lt;/span&gt; a &lt;span class=&quot;fu&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Empty&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Node&lt;/span&gt; a (&lt;span class=&quot;dt&quot;&gt;BinaryTree&lt;/span&gt; a) (&lt;span class=&quot;dt&quot;&gt;BinaryTree&lt;/span&gt; a)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In Python, which we’ll use for the rest of this article, we’ll say that &lt;code&gt;None&lt;/code&gt; represents an empty tree and that the following class represents a node:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;ch&quot;&gt;import&lt;/span&gt; collections
Node = collections.namedtuple(&lt;span class=&quot;st&quot;&gt;'Node'&lt;/span&gt;, &lt;span class=&quot;st&quot;&gt;'val left right'&lt;/span&gt;)

&lt;span class=&quot;co&quot;&gt;# some sample trees having various node counts&lt;/span&gt;
tree0 = &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;  &lt;span class=&quot;co&quot;&gt;# empty tree&lt;/span&gt;
tree1 = Node(&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;, &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;, &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;)
tree2 = Node(&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;, tree1, &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;)
tree3 = Node(&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;, tree1, Node(&lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;, &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;, &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;))
tree4 = Node(&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;, tree3)
tree5 = Node(&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;, Node(&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;, &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;), tree3)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let us now define a function to flatten a tree using an &lt;a href=&quot;http://en.wikipedia.org/wiki/Tree_traversal#In-order&quot;&gt;in-order traversal&lt;/a&gt;. The recursive definition is absurdly simple, the data type having only two cases to consider:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; flatten(bst):
    &lt;span class=&quot;co&quot;&gt;# empty case&lt;/span&gt;
    &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; bst is &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; []
    &lt;span class=&quot;co&quot;&gt;# node case&lt;/span&gt;
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; flatten(bst.left) + [bst.val] + flatten(bst.right)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A few tests to check that it does what we expect:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; check_flattener(f):
    &lt;span class=&quot;kw&quot;&gt;assert&lt;/span&gt; f(tree0) == []
    &lt;span class=&quot;kw&quot;&gt;assert&lt;/span&gt; f(tree1) == [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;]
    &lt;span class=&quot;kw&quot;&gt;assert&lt;/span&gt; f(tree2) == [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]
    &lt;span class=&quot;kw&quot;&gt;assert&lt;/span&gt; f(tree3) == [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;]
    &lt;span class=&quot;kw&quot;&gt;assert&lt;/span&gt; f(tree4) == [&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;]
    &lt;span class=&quot;kw&quot;&gt;assert&lt;/span&gt; f(tree5) == [&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;]
    &lt;span class=&quot;kw&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;'ok'&lt;/span&gt;

check_flattener(flatten)  &lt;span class=&quot;co&quot;&gt;# ok&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our challenge for today is to convert &lt;code&gt;flatten&lt;/code&gt; into an iterative version. Other than a new trick – partial evaluation – the transformation is straightforward, so I’ll move quickly.&lt;/p&gt;
&lt;p&gt;Let’s do this!&lt;/p&gt;
&lt;h3 id=&quot;eliminating-the-first-recursive-call&quot;&gt;Eliminating the first recursive call&lt;/h3&gt;
&lt;p&gt;First, let’s separate the base case from the incremental work:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; step(bst):
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; flatten(bst.left) + [bst.val] + flatten(bst.right)

&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; flatten(bst):
    &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; bst is &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; []
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; step(bst)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And let’s break the incremental work into smaller pieces to see what’s going on.&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; step(bst):
    left = flatten(bst.left)
    left.append(bst.val)
    right = flatten(bst.right)
    left.extend(right)
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; left

&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; flatten(bst):
    &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; bst is &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; []
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; step(bst)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s try to get rid of the first recursive call by assuming that somebody has passed us its result via a secret argument &lt;code&gt;left&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; step(bst, left=&lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;):
    &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; left is &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        left = flatten(bst.left)
    left.append(bst.val)
    right = flatten(bst.right)
    left.extend(right)
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; left

&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; flatten(bst):
    &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; bst is &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; []
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; step(bst)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And now we’ll make &lt;code&gt;step&lt;/code&gt; return values that parallel its input arguments:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; step(bst, left=&lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;):
    &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; left is &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        left = flatten(bst.left)
    left.append(bst.val)
    right = flatten(bst.right)
    left.extend(right)
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; bst, left  &lt;span class=&quot;co&quot;&gt;# &amp;lt;-- add bst&lt;/span&gt;

&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; flatten(bst):
    &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; bst is &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; []
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; step(bst)[-&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;]  &lt;span class=&quot;co&quot;&gt;# &amp;lt;-- note [-1]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the first recursive call, the transformation applied to &lt;code&gt;bst&lt;/code&gt; is &lt;code&gt;.left&lt;/code&gt;, so we want to apply the opposite transformation to &lt;code&gt;bst&lt;/code&gt; in the returned values. And what’s the opposite of descending to a node’s left subtree? It’s ascending to the node’s parent. So we want something like this:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;co&quot;&gt;# this code does not work!&lt;/span&gt;

&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; step(bst, left=&lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;):
    &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; left is &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        left = flatten(bst.left)
    left.append(bst.val)
    right = flatten(bst.right)
    left.extend(right)
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; get_parent(bst), left  &lt;span class=&quot;co&quot;&gt;# &amp;lt;-- need get_parent&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But we’re stuck. We can’t define &lt;code&gt;get_parent&lt;/code&gt; because our tree data structure doesn’t keep track of parents, only children.&lt;/p&gt;
&lt;p&gt;New plan: Maybe we can assume that someone has &lt;em&gt;passed us&lt;/em&gt; the node’s parent and go from there?&lt;/p&gt;
&lt;p&gt;But this plan hits the same brick wall: If we add a new argument to accept the parent, we must for parallelism add a new return value to emit the transformed parent, which is the parent of the parent. But we can’t compute the parent of the parent because, as before, we have no way of implementing &lt;code&gt;get_parent&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;So we do what mathematicians do when their assumptions hit a brick wall: we strengthen our assumption! Now we assume that someone has passed us &lt;em&gt;all of the parents&lt;/em&gt;, right up to the tree’s root. And that assumption gives us what we need:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; step(bst, parents, left=&lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;):
    &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; left is &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        left = flatten(bst.left)
    left.append(bst.val)
    right = flatten(bst.right)
    left.extend(right)
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; parents[-&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;], parents[:-&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;], left&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that we’re using the Python stack convention for &lt;code&gt;parents&lt;/code&gt;; thus the immediate parent of &lt;code&gt;bst&lt;/code&gt; is given by the final element &lt;code&gt;parents[-1]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;As a simplification, we can eliminate the &lt;code&gt;bst&lt;/code&gt; argument by considering it the final parent pushed onto the stack:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; step(parents, left=&lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;):
    bst = parents.pop()  &lt;span class=&quot;co&quot;&gt;# &amp;lt;-- bst = top of parents stack&lt;/span&gt;
    &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; left is &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        left = flatten(bst.left)
    left.append(bst.val)
    right = flatten(bst.right)
    left.extend(right)
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; parents, left&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now that &lt;code&gt;step&lt;/code&gt; requires the &lt;code&gt;parents&lt;/code&gt; stack as an argument, the base function must provide it:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; flatten(bst):
    &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; bst is &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; []
    parents = [bst]
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; step(parents)[-&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;]&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But we still haven’t eliminated the first recursive call. To do that, we’ll need to pass the &lt;code&gt;step&lt;/code&gt; function a value for its &lt;code&gt;left&lt;/code&gt; argument, which will cause the recursive call to be skipped.&lt;/p&gt;
&lt;p&gt;But we only know what that value should be for one case, the base case, when &lt;code&gt;bst&lt;/code&gt; is &lt;code&gt;None&lt;/code&gt;; then &lt;code&gt;left&lt;/code&gt; must be &lt;code&gt;[]&lt;/code&gt;. To get to that case from the tree’s root, where &lt;code&gt;bst&lt;/code&gt; is definitely not &lt;code&gt;None&lt;/code&gt;, we must iteratively replicate the normal recursive calls on &lt;code&gt;bst.left&lt;/code&gt; until we hit the leftmost leaf node. And then, to compute the desired result, we must reverse the trip, iterating the &lt;code&gt;step&lt;/code&gt; function until we have returned to the tree’s root, where the &lt;code&gt;parents&lt;/code&gt; stack must be empty:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; flatten(bst):
    &lt;span class=&quot;co&quot;&gt;# find initial conditions for secret-feature &quot;left&quot;&lt;/span&gt;
    left = []
    parents = []
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; bst is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        parents.append(bst)
        bst = bst.left
    &lt;span class=&quot;co&quot;&gt;# iterate to compute the result&lt;/span&gt;
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; parents:
        parents, left = step(parents, left)
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; left&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And just like that, one of the recursive calls has been transformed into iteration. We’re halfway to the finish line!&lt;/p&gt;
&lt;h3 id=&quot;eliminating-the-second-recursive-call&quot;&gt;Eliminating the second recursive call&lt;/h3&gt;
&lt;p&gt;But we still have to eliminate that final recursive call to &lt;code&gt;flatten&lt;/code&gt;, now sequestered in &lt;code&gt;step&lt;/code&gt;. Let’s take a closer look at that function after we make its &lt;code&gt;left&lt;/code&gt; argument required since it always gets called with a value now:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; step(parents, left):
    bst = parents.pop()
    left.append(bst.val)
    right = flatten(bst.right)
    left.extend(right)
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; parents, left&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To get rid of the recursive call to &lt;code&gt;flatten&lt;/code&gt;, we’re going to use a new trick: partial evaluation. Basically, we’re going to replace the call to &lt;code&gt;flatten&lt;/code&gt; with the function body of &lt;code&gt;flatten&lt;/code&gt;, after we rename all its variables to prevent conflicts. So let’s make a copy of &lt;code&gt;flatten&lt;/code&gt; and suffix all its variables with &lt;code&gt;1&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; flatten1(bst1):
    left1 = []
    parents1 = []
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; bst1 is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        parents1.append(bst1)
        bst1 = bst1.left
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; parents1:
        parents1, left1 = step(parents1, left1)
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; left1&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then let’s make its arguments and return values explicit:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;    (bst1, ) = ARGUMENTS
    left1 = []
    parents1 = []
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; bst1 is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        parents1.append(bst1)
        bst1 = bst1.left
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; parents1:
        parents1, left1 = step(parents1, left1)
    RETURNS = (left1, )&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then we’ll drop this expansion into &lt;code&gt;step&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; step(parents, left):
    bst = parents.pop()
    left.append(bst.val)
    &lt;span class=&quot;co&quot;&gt;# -- begin partial evaluation --&lt;/span&gt;
    (bst1, ) = (bst.right, )
    left1 = []
    parents1 = []
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; bst1 is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        parents1.append(bst1)
        bst1 = bst1.left
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; parents1:
        parents1, left1 = step(parents1, left1)
    (right, ) = (left1, )
    &lt;span class=&quot;co&quot;&gt;# -- end partial evaluation --&lt;/span&gt;
    left.extend(right)
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; parents, left&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can eliminate code by fusion across the partial-evaluation boundary.&lt;/p&gt;
&lt;p&gt;First up: &lt;code&gt;left1&lt;/code&gt;. We can now see that this variable accumulates values that, in the end, get appended to &lt;code&gt;left&lt;/code&gt; (via the return variable &lt;code&gt;right&lt;/code&gt;). But we can just as well append those values to &lt;code&gt;left&lt;/code&gt; directly, eliminating &lt;code&gt;left1&lt;/code&gt; within the boundary and the call to &lt;code&gt;left.extend(right)&lt;/code&gt; without:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; step(parents, left):
    bst = parents.pop()
    left.append(bst.val)
    &lt;span class=&quot;co&quot;&gt;# -- begin partial evaluation --&lt;/span&gt;
    (bst1, ) = (bst.right, )
    &lt;span class=&quot;co&quot;&gt;# left1 = []  # &amp;lt;-- eliminate and use left instead&lt;/span&gt;
    parents1 = []
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; bst1 is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        parents1.append(bst1)
        bst1 = bst1.left
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; parents1:
        parents1, left = step(parents1, left)
    &lt;span class=&quot;co&quot;&gt;# (right, ) = (left, )  # &amp;lt;-- eliminated&lt;/span&gt;
    &lt;span class=&quot;co&quot;&gt;# -- end partial evaluation --&lt;/span&gt;
    &lt;span class=&quot;co&quot;&gt;# left.extend(right)  # &amp;lt;-- eliminated&lt;/span&gt;
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; parents, left&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For this next fusion, we’re going to need to recall our base function to get the necessary outside scope:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; step(parents, left):
    bst = parents.pop()
    left.append(bst.val)
    &lt;span class=&quot;co&quot;&gt;# -- begin partial evaluation --&lt;/span&gt;
    (bst1, ) = (bst.right, )
    parents1 = []
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; bst1 is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        parents1.append(bst1)
        bst1 = bst1.left
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; parents1:
        parents1, left = step(parents1, left)
    &lt;span class=&quot;co&quot;&gt;# -- end partial evaluation --&lt;/span&gt;
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; parents, left

&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; flatten(bst):
    left = []
    parents = []
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; bst is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        parents.append(bst)
        bst = bst.left
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; parents:
        parents, left = step(parents, left)
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; left&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When &lt;code&gt;flatten&lt;/code&gt; calls &lt;code&gt;step&lt;/code&gt; and the code within the partially evaluated region executes, it builds up a stack of nodes &lt;code&gt;parents1&lt;/code&gt; and then calls &lt;code&gt;step&lt;/code&gt; iteratively to pop values off of that stack and process them. When it’s finished, control returns to &lt;code&gt;step&lt;/code&gt; proper, which then returns to its caller, &lt;code&gt;flatten&lt;/code&gt;, with the values (&lt;code&gt;parents&lt;/code&gt;, &lt;code&gt;left&lt;/code&gt;). But look at what &lt;code&gt;flatten&lt;/code&gt; then does with &lt;code&gt;parents&lt;/code&gt;: it calls &lt;code&gt;step&lt;/code&gt; iteratively to pop values off of that stack and process them in exactly the same way.&lt;/p&gt;
&lt;p&gt;So we can eliminate the &lt;code&gt;while&lt;/code&gt; loop in &lt;code&gt;step&lt;/code&gt; – and the recursive call! – by returning not &lt;code&gt;parents&lt;/code&gt; but &lt;code&gt;parents + parents1&lt;/code&gt;, which will make the &lt;code&gt;while&lt;/code&gt; loop in &lt;code&gt;flatten&lt;/code&gt; do the exact same work.&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; step(parents, left):
    bst = parents.pop()
    left.append(bst.val)
    &lt;span class=&quot;co&quot;&gt;# -- begin partial evaluation --&lt;/span&gt;
    (bst1, ) = (bst.right, )
    parents1 = []
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; bst1 is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        parents1.append(bst1)
        bst1 = bst1.left
    &lt;span class=&quot;co&quot;&gt;# while parents1:                            # &amp;lt;-- eliminated&lt;/span&gt;
    &lt;span class=&quot;co&quot;&gt;#     parents1, left = step(parents1, left)  #&lt;/span&gt;
    &lt;span class=&quot;co&quot;&gt;# -- end partial evaluation --&lt;/span&gt;
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; parents + parents1, left  &lt;span class=&quot;co&quot;&gt;# parents -&amp;gt; parents + parents1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then we can eliminate &lt;code&gt;parents1&lt;/code&gt; completely by taking the values we would have appended to it and appending them directly to &lt;code&gt;parents&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; step(parents, left):
    bst = parents.pop()
    left.append(bst.val)
    &lt;span class=&quot;co&quot;&gt;# -- begin partial evaluation --&lt;/span&gt;
    (bst1, ) = (bst.right, )
    &lt;span class=&quot;co&quot;&gt;# parents1 = []  # &amp;lt;-- eliminated&lt;/span&gt;
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; bst1 is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        parents.append(bst1)  &lt;span class=&quot;co&quot;&gt;# parents1 -&amp;gt; parents&lt;/span&gt;
        bst1 = bst1.left
    &lt;span class=&quot;co&quot;&gt;# -- end partial evaluation --&lt;/span&gt;
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; parents, left  &lt;span class=&quot;co&quot;&gt;# parents + parents1 -&amp;gt; parents&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And now, once we remove our partial-evaluation scaffolding, our &lt;code&gt;step&lt;/code&gt; function is looking simple again:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; step(parents, left):
    bst = parents.pop()
    left.append(bst.val)
    bst1 = bst.right
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; bst1 is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        parents.append(bst1)
        bst1 = bst1.left
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; parents, left&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For the final leg of our journey – simplification – let’s inline the &lt;code&gt;step&lt;/code&gt; logic back into the base function:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; flatten(bst):
    left = []
    parents = []
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; bst is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        parents.append(bst)
        bst = bst.left
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; parents:
        parents, left = parents, left
        bst = parents.pop()
        left.append(bst.val)
        bst1 = bst.right
        &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; bst1 is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
            parents.append(bst1)
            bst1 = bst1.left
        parents, left = parents, left
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; left&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s eliminate the trivial argument-binding and return-value assignments:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; flatten(bst):
    left = []
    parents = []
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; bst is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
        parents.append(bst)
        bst = bst.left
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; parents:
        &lt;span class=&quot;co&quot;&gt;# parents, left = parents, left  # = no-op&lt;/span&gt;
        bst = parents.pop()
        left.append(bst.val)
        bst1 = bst.right
        &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; bst1 is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
            parents.append(bst1)
            bst1 = bst1.left
        &lt;span class=&quot;co&quot;&gt;# parents, left = parents, left  # = no-op&lt;/span&gt;
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; left&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And, finally, factor out the duplicated &lt;code&gt;while&lt;/code&gt; loop into a local function:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; flatten(bst):
    left = []
    parents = []
    &lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; descend_left(bst):
        &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; bst is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
            parents.append(bst)
            bst = bst.left
    descend_left(bst)
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; parents:
        bst = parents.pop()
        left.append(bst.val)
        descend_left(bst.right)
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; left&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And that’s it! We now have a tight, efficient, and iterative version of our original function. Further, the code is close to idiomatic.&lt;/p&gt;
&lt;p&gt;That’s it for this time. If you have any questions or comments, just hit me at &lt;a href=&quot;https://twitter.com/tmoertel&quot;&gt;@tmoertel&lt;/a&gt; or use the comment form below.&lt;/p&gt;
&lt;p&gt;Thanks for reading!&lt;/p&gt;</description>
	<pubDate>Mon, 03 Jun 2013 00:00:00 +0000</pubDate>
</item>
<item>
	<title>FP Complete: Haskell from C: Where are the for Loops?</title>
	<guid isPermaLink="false">https://www.fpcomplete.com/blog/2013/06/haskell-from-c</guid>
	<link>http://feedproxy.google.com/~r/FpComplete/~3/2D5iyiU03QM/haskell-from-c</link>
	<description>&lt;p&gt;This post contains fragments of active Haskell code, best viewed and executed at
&lt;a href=&quot;https://www.fpcomplete.com/blog/2013/06/haskell-from-c&quot;&gt;https://www.fpcomplete.com/blog/2013/06/haskell-from-c&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;If you're coming from a language like C, Haskell can take some getting used to. It's typical for a new language to feel a little different, but in Haskell the differences are more dramatic, and more fundamental. In particular...&lt;/p&gt;&lt;h2&gt;Where are the &lt;code&gt;for&lt;/code&gt; loops?&lt;/h2&gt;&lt;p&gt;In most imperative languages, &lt;code&gt;for&lt;/code&gt; loops are all over the place, and are used for a wide variety of &lt;i&gt;different things&lt;/i&gt;. Whether you're squaring every value of an array or finding its sum, you're probably using a &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;&lt;p&gt;In Haskell, control structures are more expressive. Sure, there's a counterpart to C's &lt;code&gt;for&lt;/code&gt; (Haskell's &lt;code&gt;forM_&lt;/code&gt;). But that's a discussion for another time. Today, we'll see some &lt;code&gt;for&lt;/code&gt; loops in C that can be written very differently in Haskell, and why that's a good thing.&lt;/p&gt;&lt;p&gt;Consider the simple example of computing the norm of a vector. For a vector &lt;b&gt;x&lt;/b&gt;∈ℝ&lt;sup&gt;n&lt;/sup&gt;, this is just&lt;/p&gt;

&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;
&lt;mtable style=&quot;width: 98%; margin-top: 1.0em; margin-right: 1.0em; margin-bottom: 2.0em; margin-left: 2.0em;&quot; displaystyle=&quot;true&quot; class=&quot;m-equation-square&quot;&gt;
    &lt;mtr&gt;
        &lt;mtd columnalign=&quot;left&quot;&gt;
            &lt;mrow&gt;
                &lt;mo&gt;|&lt;/mo&gt;
                &lt;mo&gt;|&lt;/mo&gt;
            &lt;/mrow&gt;
            &lt;mi mathvariant=&quot;bold&quot;&gt;x&lt;/mi&gt;
            &lt;mrow&gt;
                &lt;mo&gt;|&lt;/mo&gt;
                &lt;mo&gt;|&lt;/mo&gt;
            &lt;/mrow&gt;
            &lt;mo&gt;=&lt;/mo&gt;
            &lt;msqrt&gt;
                &lt;mrow&gt;
                    &lt;mstyle displaystyle=&quot;true&quot;&gt;
                        &lt;munderover&gt;
                            &lt;mo&gt;∑&lt;/mo&gt;
                            &lt;mrow&gt;
                                &lt;mi&gt;i&lt;/mi&gt;
                                &lt;mo&gt;=&lt;/mo&gt;
                                &lt;mn&gt;0&lt;/mn&gt;
                            &lt;/mrow&gt;
                            &lt;mrow&gt;
                                &lt;mi&gt;n&lt;/mi&gt;
                                &lt;mo&gt;-&lt;/mo&gt;
                                &lt;mn&gt;1&lt;/mn&gt;
                            &lt;/mrow&gt;
                        &lt;/munderover&gt;
                    &lt;/mstyle&gt;
                    &lt;msubsup&gt;
                        &lt;mi mathvariant=&quot;bold&quot;&gt;x&lt;/mi&gt;
                        &lt;mi&gt;i&lt;/mi&gt;
                        &lt;mn&gt;2&lt;/mn&gt;
                    &lt;/msubsup&gt;
                &lt;/mrow&gt;
            &lt;/msqrt&gt;
        &lt;/mtd&gt;
    &lt;/mtr&gt;
&lt;/mtable&gt;
&lt;/math&gt;
&lt;p&gt;Conceptually, there are three stages to this computation:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;&lt;b&gt;&lt;code&gt;mapSq&lt;/code&gt;&lt;/b&gt;: Square each element&lt;/li&gt;&lt;li&gt;&lt;b&gt;&lt;code&gt;sum&lt;/code&gt;&lt;/b&gt;: Compute the sum&lt;/li&gt;&lt;li&gt;&lt;b&gt;&lt;code&gt;sqrt&lt;/code&gt;&lt;/b&gt;: Compute the square root&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;We can think of the first step as building (at least abstractly) a new array whose &lt;code&gt;i&lt;/code&gt;th element is &lt;code&gt;y[i] = x[i] * x[i]&lt;/code&gt;. In functional programming, we refer to this as &lt;i&gt;mapping&lt;/i&gt; the square function over the array. &lt;/p&gt;&lt;p&gt;Putting everything in terms of functions, we can write this (in Haskell-like pseudocode) as&lt;/p&gt;&lt;pre&gt;&lt;code&gt;norm(x) = sqrt(sum(mapSq(x))) ,&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To clean up the syntax a bit, we can instead use the notation for function composition and write&lt;/p&gt;&lt;pre&gt;&lt;code&gt;norm(x) = (sqrt ○ sum ○ mapSq)(x) ,&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;or just &lt;/p&gt;&lt;pre&gt;&lt;code&gt;norm = sqrt ○ sum ○ mapSq .&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Computing Norms in C&lt;/h2&gt;&lt;p&gt;In C, this modular approach leads to something like&lt;/p&gt;&lt;pre&gt;&lt;code&gt;void mapSq(double *x, double *y, int n) {
  int i;
  for(i=0; i&amp;lt;n; i++) {
    y[i] = x[i] * x[i];
  }
}

double sum(double *x, int n) {
  double result = 0;
  int i;
  for(i=0; i&amp;lt;n; i++) {
    result += x[i];
  }
  return result;
}

double norm1(double *x, int n) {
  double *y = malloc(n * sizeof(double));
  mapSq(x, y, n);
  theSum = sum(y, n);
  free(y);
  return sqrt(theSum);
}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We'd probably never do it this way, for a few reasons:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;For such a simple computation, it's way too verbose.&lt;/li&gt;&lt;li&gt;It uses two separate &lt;code&gt;for&lt;/code&gt; loops, where only one is needed.&lt;/li&gt;&lt;li&gt;There's an unnecessary &lt;code&gt;malloc&lt;/code&gt; that's unlikely to be removed by the compiler.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;If it weren't for these issues, we probably would write code like the above. It has the advantage of being more modular and of faithfully representing the concepts. But the problems are too big to ignore, so we typically make some code transformations in our head, and instead write&lt;/p&gt;&lt;pre&gt;&lt;code&gt;double norm2(double *x, int n) {
  double theSum = 0.0;
  for (int i = 0; i &amp;lt; n; i++) { 
    theSum += x[i]*x[i]; }
  return sqrt(theSum);
}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This code is very clear, and performs well, but we've entirely lost modularity. In this case the code is very short, so we don't give the compromise a second thought. But at a larger scale, this kind of manual optimization reduces code reuse and makes components more complex. Code becomes harder to understand, and harder to test for correctness.&lt;/p&gt;&lt;h2&gt;Computing Norms in Haskell&lt;/h2&gt;&lt;p&gt;In Haskell, we could write &lt;code&gt;norm&lt;/code&gt; as&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;active haskell&quot;&gt;import Prelude hiding (sum)
import Data.List (foldl')

-- show
mapSq x = map sq x
  where
  sq xi = xi * xi

sum x = foldl' (+) 0.0 x

norm x = (sqrt . sum . mapSq) x

main = print (norm [1,2,3,4,5])&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;While the C example is in terms of arrays, this Haskell example instead uses &lt;i&gt;lazy linked lists&lt;/i&gt;. We'll change this in a bit.&lt;/p&gt;&lt;p&gt;Let's step through this code. The first function, &lt;code&gt;mapSq&lt;/code&gt;, is defined in terms of the &lt;code&gt;map&lt;/code&gt; function, and produces a new list by squaring every element of the original list. The squaring function &lt;code&gt;sq&lt;/code&gt; can also be written as &lt;code&gt;(\x -&amp;gt; x*x)&lt;/code&gt;, allowing us to write the function as&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;haskell&quot;&gt;mapSq x = map (\x -&amp;gt; x*x) x&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;or simply&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;haskell&quot;&gt;mapSq = map (\x -&amp;gt; x*x)&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Next, the &lt;code&gt;sum&lt;/code&gt; function is defined in terms of &lt;code&gt;foldl'&lt;/code&gt;. Before you worry too much about the name, you should know that a &lt;i&gt;fold&lt;/i&gt; is just a function that traverses some data structure, using a given function to update some &lt;i&gt;accumulator&lt;/i&gt; as it goes. In the current case, the accumulator is the counterpart of &quot;&lt;code&gt;theSum&lt;/code&gt;&quot; in the C version. Accumulation is via addition, and starts at 0.0.&lt;/p&gt;&lt;p&gt;Lists can be folded from either side. In our case, we're using &lt;code&gt;foldl'&lt;/code&gt;. The &quot;&lt;code&gt;l&lt;/code&gt;&quot; in this function name is for &quot;left&quot;, the side we're starting on, and the &quot;&lt;code&gt;'&lt;/code&gt;&quot; indicates that this fold is &lt;i&gt;strict&lt;/i&gt;. Haskell is lazy by default, but for numeric there's no point in delaying evaluation of the accumulator (and for large lists it can lead to a stack overflow), so in this case we prefer to evaluate it at every step.&lt;/p&gt;&lt;p&gt;As it turns out, our &lt;code&gt;sum&lt;/code&gt; function is common enough that it's included in Haskell's Prelude; we can use it exactly as described with no need to define it.&lt;sup&gt;1&lt;/sup&gt; Our &lt;code&gt;norm&lt;/code&gt; function is now simple:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;active haskell&quot;&gt;norm = sqrt . sum . map (\x -&amp;gt; x*x)

main = print (norm [1,2,3,4,5])&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This looks remarkably like the mathematical specification; some have affectionately refered to Haskell as an &lt;i&gt;executable specification language&lt;/i&gt;.&lt;/p&gt;&lt;h2&gt;But what about performance?&lt;/h2&gt;&lt;p&gt;Let's compare performance with C. In order to measure easily across languages, we'll make the vector much bigger - lets say a billion elements. We'll start with a standard list implementation in Haskell and compare this to an implementation in C using arrays. Then we'll make a couple of minor changes in Haskell that allow us to use unboxed vectors.&lt;/p&gt;&lt;p&gt;We'll test using an AMD A6-3670 APU running Ubuntu 13.04. For compilation, we'll use GHC 7.6.3 (with &lt;code&gt;-O2&lt;/code&gt;) and GCC 4.7.3 (with &lt;code&gt;-O3&lt;/code&gt;). &lt;/p&gt;&lt;h3&gt;Haskell - Lists&lt;/h3&gt;&lt;p&gt;Our Haskell version is certainly elegant, but using linked lists in this way is not the best approach for performance. For a billion elements, the running time is &lt;b&gt;over 2 minutes&lt;/b&gt;. What's going on here?&lt;/p&gt;&lt;p&gt;Haskell lists are &lt;i&gt;lazy&lt;/i&gt; (only those elements required by later calculations are computed) and &lt;i&gt;polymorphic&lt;/i&gt; (you can have a list of elements of any type, even if elements of that type don't take up a fixed number of bytes). In order to implement this, a list in Haskell is really a &lt;i&gt;list of pointers to elements&lt;/i&gt;. In cases like this, all that pointer chasing adds up.&lt;/p&gt;&lt;p&gt;We'll soon see an easy way to improve this and get Haskell running really fast.&lt;/p&gt;&lt;h3&gt;C - Arrays&lt;/h3&gt;&lt;p&gt;Before we get Haskell zipping along, let's look at how we would do this in C. We'll use the faster version of our norm in C (&lt;code&gt;norm2&lt;/code&gt; above). Here's the plan:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;&lt;code&gt;malloc&lt;/code&gt; an array of a billion elements&lt;/li&gt;&lt;li&gt;Fill the array so that &lt;code&gt;x[i] = i&lt;/code&gt;&lt;/li&gt;&lt;li&gt;Find the norm of the vector&lt;/li&gt;&lt;li&gt;Free the array and print the result&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;We'll use the OS to measure the time of the whole process, and insert some code to measure step (3) alone. So all together we have this:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;#include &amp;lt;time.h&amp;gt;
#include &amp;lt;math.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#define x_size 1000000000

double norm(double *x, int n) {
  double theSum = 0.0;
  int i;
  for (i=0; i&amp;lt;n; i++) { 
    theSum += x[i]*x[i]; 
  }
  return sqrt(theSum);
}

int main() {
  double *x = malloc(x_size * sizeof(double));
  int i;
  for(i=0; i&amp;lt;x_size; i++) {
    x[i] = (double) i;
  }
  clock_t start = clock();
  double result = norm(x, x_size);
  clock_t end = clock();
  printf(&quot;%3.2f\n&quot;, result);
  printf(&quot;%3.2f sec\n&quot;, (end-start)/(double)CLOCKS_PER_SEC);
  free(x);
  return 0;
}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The whole thing takes about &lt;b&gt;7 seconds&lt;/b&gt; to run, with the &lt;code&gt;norm&lt;/code&gt; function itself taking &lt;b&gt;1.9 seconds&lt;/b&gt;.&lt;/p&gt;&lt;h3&gt;Haskell - Unboxed Vectors&lt;/h3&gt;&lt;p&gt;To close in on C, let's make some minor changes. Instead of using lists, we'll use  unboxed &lt;i&gt;vector&lt;/i&gt;s. The &quot;unboxed&quot; qualifier just means that the elements are available directly, without the need to follow a pointer. &lt;/p&gt;&lt;p&gt;From a data structure standpoint, Haskell's vectors are a lot like C's arrays. There is an important difference, though; many operations on vectors (even boxed ones) are subject to &lt;i&gt;fusion&lt;/i&gt;. A &lt;code&gt;map&lt;/code&gt; followed by a &lt;code&gt;sum&lt;/code&gt; will be represented as a tight loop, but the code can remain modular. Remember the malloc in our original C code? Through fusion, it's eliminated entirely. &lt;/p&gt;&lt;p&gt;Updating the original code to operate on vectors is straightforward. And To stay entirely outside the realm of lists, we can generate our vector using &lt;code&gt;iterateN&lt;/code&gt;, which plays the role of our data-filling loop in C.&lt;/p&gt;&lt;p&gt;[EDIT: Because School of Haskell compilation does not use &lt;code&gt;-O2&lt;/code&gt;, there is no fusion, so we'll need to keep the values a bit smaller. In this case, I'll use an unusually small value for &lt;code&gt;oneBillion&lt;/code&gt;, though for benchmarking I still use 10&lt;sup&gt;9&lt;/sup&gt;]&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;active haskell&quot;&gt;import Data.Vector.Unboxed as V

norm ::  Vector Double -&amp;gt; Double
norm = sqrt . V.sum . V.map (\x -&amp;gt; x*x)

oneBillion = 100

main = print $ norm $ V.iterateN oneBillion (+1) 0.0 &lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This code runs in about &lt;b&gt;2.5 seconds&lt;/b&gt;. It's just a bit longer than C's inner loop alone, but much less than the 7 seconds if the malloc is included.&lt;/p&gt;&lt;p&gt;EDIT: As Pedro Vasconcelos points out, tight numerical loops like this can often benefit from using the LLVM back-end, via &lt;code&gt;-fllvm&lt;/code&gt;. This brings the time for the entire Haskell run down to &lt;b&gt;1.9 seconds&lt;/b&gt;, the same as the inner loop alone in C!&lt;/p&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;Haskell's control structures express that a reduction (a &lt;i&gt;fold&lt;/i&gt; in Haskell) is very different than a &lt;i&gt;map&lt;/i&gt;.&lt;sup&gt;2&lt;/sup&gt; Libraries like &lt;b&gt;vector&lt;/b&gt; implement powerful fusion techniques to combine loops and eliminate intermediate data structures. And &lt;i&gt;unboxing&lt;/i&gt; elminates excessive pointer chasing required by built-in lists.&lt;/p&gt;&lt;p&gt;All in all, this lets us write &lt;i&gt;high-level code without sacrificing high performance&lt;/i&gt;. The need for any compromise between the two is becoming more rare every day.&lt;/p&gt;&lt;hr /&gt;&lt;p&gt; &lt;sup&gt;1&lt;/sup&gt; The Prelude's &lt;code&gt;sum&lt;/code&gt; function is not defined in terms of &lt;code&gt;foldl'&lt;/code&gt;, but instead relies on GHC's strictness analysis. In GHC 7.6.3 there's a regression, as described in &lt;a href=&quot;http://hackage.haskell.org/trac/ghc/ticket/7954&quot;&gt;this ticket&lt;/a&gt;. Because of this, we stuck with &lt;code&gt;foldl' (+) 0.0&lt;/code&gt; in timing our Haskell version using lists.&lt;/p&gt;&lt;br /&gt;
 &lt;sup&gt;2&lt;/sup&gt;  EDIT: Michael Sloan has pointed out that the difference is not so fundamental, since `map f = foldr ((:) . f) []`. &lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/FpComplete?a=2D5iyiU03QM:TrJEQ8HeCec:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/FpComplete?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.feedburner.com/~ff/FpComplete?a=2D5iyiU03QM:TrJEQ8HeCec:V_sGLiPBpWU&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/FpComplete?i=2D5iyiU03QM:TrJEQ8HeCec:V_sGLiPBpWU&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.feedburner.com/~ff/FpComplete?a=2D5iyiU03QM:TrJEQ8HeCec:qj6IDK7rITs&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/FpComplete?d=qj6IDK7rITs&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.feedburner.com/~ff/FpComplete?a=2D5iyiU03QM:TrJEQ8HeCec:gIN9vFwOqvQ&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/FpComplete?i=2D5iyiU03QM:TrJEQ8HeCec:gIN9vFwOqvQ&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/FpComplete/~4/2D5iyiU03QM&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Sun, 02 Jun 2013 18:00:00 +0000</pubDate>
</item>
<item>
	<title>language-puppet: Hruby package released</title>
	<guid isPermaLink="false">http://lpuppet.banquise.net/blog/2013/06/01/hruby-package-released</guid>
	<link>http://lpuppet.banquise.net/blog/2013/06/01/hruby-package-released/</link>
	<description>&lt;p&gt;I finally released the &lt;a href=&quot;http://hackage.haskell.org/package/hruby&quot;&gt;hruby&lt;/a&gt; package, along with an updated version of the &lt;a href=&quot;http://hackage.haskell.org/package/language-puppet&quot;&gt;language-puppet&lt;/a&gt; package. It is very unfortunate this package will never get proper haddocks on Hackage, as the documentation is quite useful. If someone has a suggestion for getting haddocks without having the ruby1.8 library installed, I am interested. Also the path to the Ruby include files is hardcoded, meaning it might require manual tweaking to get it right.&lt;/p&gt;

&lt;p&gt;Both libraries now have build flags :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hruby has a flag for ruby1.9. This flag is mostly cosmetic as I didn’t even test it, and just copied the files for ruby1.8.&lt;/li&gt;
&lt;li&gt;Language-puppet now has a &lt;code&gt;-fhruby&lt;/code&gt; option, to build with this library.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;The immediate result is a two-fold speed increase for single runs of &lt;code&gt;puppetresource&lt;/code&gt;, and a six-time speed-up for scripts computing several catalogs. The
reason is that the parser is not too fast, but its results get cached. Also, the language-puppet daemon infrastructure still let you define the number of
threads that should be spawned to compute templates. &lt;em&gt;This should be set to 1&lt;/em&gt;. The ruby interpreter cannot be used in a thread-safe way.&lt;/p&gt;

&lt;p&gt;There are still several issues to address. The first is related to the multiple variable assignment problem. In Puppet all variables are immutable, and can’t be
reassigned. Well, &lt;em&gt;except&lt;/em&gt; when overwriting variables belonging to an inherited class. I wish they never introduced inheritance, as it introduces all kind of
special rules, and seems generally fragile. Moreover, given how I (have not) implemented scopes, it is not trivial to have a robust way to check if the overwrite is valid
or not.&lt;/p&gt;

&lt;p&gt;The second most important issue is the fact that the dependency system isn’t working as it should be. I still get dependency loops in Puppet that are not
catched by language-puppet. This is a show-stopper, and must be fixed soon. It is however a big challenge.&lt;/p&gt;

&lt;p&gt;Finally, as language-puppet is Linux only for now, I would like to start using the inotify feature. The current caching mechanism works by issuing &lt;code&gt;stat&lt;/code&gt; system
calls on all files that might have changed. Inotify would greatly reduce the number of system calls, which is always a good thing. I am not sure this would lead
to a big speed increase however.&lt;/p&gt;</description>
	<pubDate>Sat, 01 Jun 2013 06:42:00 +0000</pubDate>
</item>
<item>
	<title>Ken T Takusagawa: [spovtkwt] Equiangular pentagon</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-6757805.post-1215930254838482551</guid>
	<link>http://kenta.blogspot.com/2012/05/spovtkwt-equiangular-pentagon.html</link>
	<description>&lt;p&gt;Finding not many good pictures of a pentagon with equal angles but unequal sides, I drew one.  For reference, inside it is a regular pentagon; corresponding sides are parallel.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;http://mit.edu/kenta/www/three/pentagon-equiangular/spovtkwt/equiangular-pentagon.png&quot; alt=&quot;Equiangular Pentagon&quot; height=&quot;572&quot; width=&quot;699&quot; /&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://mit.edu/kenta/www/three/pentagon-equiangular/spovtkwt&quot;&gt;Haskell source code is here&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;And in Scalable Vector Graphics (SVG) format:&lt;br /&gt;
&lt;/p&gt;&lt;div style=&quot;background: white;&quot;&gt;&amp;lt;object data=&quot;http://mit.edu/kenta/www/three/pentagon-equiangular/spovtkwt/pentagon.svg&quot; height=&quot;473&quot; type=&quot;image/svg+xml&quot; width=&quot;600&quot;&amp;gt;&amp;lt;/object&amp;gt;&lt;/div&gt;&lt;a href=&quot;http://mit.edu/kenta/www/three/pentagon-equiangular/spovtkwt/pentagon.svg&quot;&gt;Equiangular Pentagon in SVG&lt;/a&gt;&lt;br /&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;Someday, an amoeba-like animation which explores possible shapes of an equiangular pentagon of a constant area.&lt;/p&gt;</description>
	<pubDate>Fri, 31 May 2013 17:56:36 +0000</pubDate>
	<author>noreply@blogger.com (Ken)</author>
</item>
<item>
	<title>Philip Wadler: The EFF, Oracle, Google, and me</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-9757377.post-499289867012997451</guid>
	<link>http://wadler.blogspot.com/2013/05/the-eff-oracle-google-and-me.html</link>
	<description>&lt;div style=&quot;clear: both; text-align: center;&quot; class=&quot;separator&quot;&gt;&lt;a style=&quot;margin-left: 1em; margin-right: 1em;&quot; href=&quot;http://2.bp.blogspot.com/-oUdfjPrpGnk/UajDtRgxfqI/AAAAAAAACDI/Tshk9t5yiY4/s1600/EFF.png&quot;&gt;&lt;img src=&quot;http://2.bp.blogspot.com/-oUdfjPrpGnk/UajDtRgxfqI/AAAAAAAACDI/Tshk9t5yiY4/s320/EFF.png&quot; height=&quot;222&quot; border=&quot;0&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style=&quot;clear: both; text-align: center;&quot; class=&quot;separator&quot;&gt;&lt;a style=&quot;margin-left: 1em; margin-right: 1em;&quot; href=&quot;http://2.bp.blogspot.com/-JymkWK6ziMQ/UajBL4yXzhI/AAAAAAAACC4/m41WF6Ms4Bw/s1600/oracle-v-google.jpg&quot;&gt;&lt;img src=&quot;http://2.bp.blogspot.com/-JymkWK6ziMQ/UajBL4yXzhI/AAAAAAAACC4/m41WF6Ms4Bw/s1600/oracle-v-google.jpg&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;clear: both; text-align: center;&quot; class=&quot;separator&quot;&gt;&lt;br /&gt;&lt;/div&gt;The Electronic Frontier Foundation has &lt;a href=&quot;https://www.eff.org/press/releases/computer-scientists-urge-court-block-copyright-claims-oracle-v-google-api-fight&quot;&gt;submitted an amicus brief&lt;/a&gt; for the case of Oracle vs. Google, arguing that copyright should apply to code that implements an API, but not to the API itself, which anyone should be free to implement.  The brief details a number of cases where the freedom to implement an API has led to social benefits.  I am pleased to be a signatory of the brief, in the company of luminaries including John Parry Barlow, Jon Bentley, Frederick Brooks, David Dill, Les Earnest, Doug Lea, Martin Odersky, Bruce Schneier, Bjarne Stoustroup, and others.&lt;br /&gt;&lt;br /&gt;</description>
	<pubDate>Fri, 31 May 2013 15:39:13 +0000</pubDate>
	<author>noreply@blogger.com (Philip Wadler)</author>
</item>
<item>
	<title>Dominic Steinitz: Neural Networks and Automated Differentiation</title>
	<guid isPermaLink="false">http://idontgetoutmuch.wordpress.com/?p=460</guid>
	<link>http://idontgetoutmuch.wordpress.com/2013/05/31/neural-networks-and-automated-differentiation-3/</link>
	<description>&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Neural networks are a method for classifying data based on a theory of how biological systems operate. They can also be viewed as a generalization of logistic regression. A method for determining the coefficients of a given model, backpropagation, was developed in the 1970’s and rediscovered in the 1980’s.&lt;/p&gt;
&lt;p&gt;The article “A Functional Approach to Neural Networks” in the &lt;a href=&quot;http://themonadreader.files.wordpress.com/2013/03/issue214.pdf&quot;&gt;Monad Reader&lt;/a&gt; shows how to use a neural network to classify handwritten digits in the &lt;a href=&quot;http://yann.lecun.com/exdb/mnist/&quot;&gt;MNIST database&lt;/a&gt; using backpropagation.&lt;/p&gt;
&lt;p&gt;The reader is struck by how similar &lt;a href=&quot;http://en.wikipedia.org/wiki/Backpropagation&quot;&gt;backpropagation&lt;/a&gt; is to &lt;a href=&quot;http://en.wikipedia.org/wiki/Automatic_differentiation&quot;&gt;automatic differentiation&lt;/a&gt;. The reader may not therefore be surprised to find that this observation had been made before: &lt;a href=&quot;http://justindomke.wordpress.com/2009/02/17/automatic-differentiation-the-most-criminally-underused-tool-in-the-potential-machine-learning-toolbox/&quot;&gt;Domke2009a&lt;/a&gt;. Indeed as Dan Piponi observes: “the grandaddy machine-learning algorithm of them all, back-propagation, is nothing but steepest descent with reverse mode automatic differentiation”.&lt;/p&gt;
&lt;h2 id=&quot;neural-networks&quot;&gt;Neural Networks&lt;/h2&gt;
&lt;p&gt;We can view neural nets or at least a multi layer perceptron as a generalisation of (multivariate) linear logistic regression.&lt;/p&gt;
&lt;p&gt;We follow &lt;span class=&quot;citation&quot;&gt;(Rojas 1996; Bishop 2006)&lt;/span&gt;. We are given a training set:&lt;/p&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5C%7B%28%5Cboldsymbol%7Bx%7D_0%2C+%5Cboldsymbol%7By%7D_0%29%2C+%28%5Cboldsymbol%7Bx%7D_1%2C+%5Cboldsymbol%7By%7D_1%29%2C+%5Cldots%2C+%28%5Cboldsymbol%7Bx%7D_p%2C+%5Cboldsymbol%7By%7D_p%29%5C%7D++&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\displaystyle  \{(\boldsymbol{x}_0, \boldsymbol{y}_0), (\boldsymbol{x}_1, \boldsymbol{y}_1), \ldots, (\boldsymbol{x}_p, \boldsymbol{y}_p)\}  &quot; class=&quot;latex&quot; title=&quot;\displaystyle  \{(\boldsymbol{x}_0, \boldsymbol{y}_0), (\boldsymbol{x}_1, \boldsymbol{y}_1), \ldots, (\boldsymbol{x}_p, \boldsymbol{y}_p)\}  &quot; /&gt;&lt;/div&gt;
&lt;p&gt;of pairs of &lt;img src=&quot;http://s0.wp.com/latex.php?latex=n&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;n&quot; class=&quot;latex&quot; title=&quot;n&quot; /&gt;-dimensional and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=m&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;m&quot; class=&quot;latex&quot; title=&quot;m&quot; /&gt;-dimensional vectors called the input and output patterns in Machine Learning parlance. We wish to build a neural network model using this training set.&lt;/p&gt;
&lt;p&gt;A neural network model (or at least the specific model we discuss: the &lt;a href=&quot;http://en.wikipedia.org/wiki/Multilayer_perceptron&quot;&gt;multi-layer perceptron&lt;/a&gt;) consists of a sequence of transformations. The first transformation creates weighted sums of the inputs.&lt;/p&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++a_j%5E%7B%281%29%7D+%3D+%5Csum_%7Bi%3D1%7D%5E%7BK_0%7D+w%5E%7B%281%29%7D_%7Bij%7Dx_i+%2B+w_%7B0j%7D%5E%7B%281%29%7D++&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\displaystyle  a_j^{(1)} = \sum_{i=1}^{K_0} w^{(1)}_{ij}x_i + w_{0j}^{(1)}  &quot; class=&quot;latex&quot; title=&quot;\displaystyle  a_j^{(1)} = \sum_{i=1}^{K_0} w^{(1)}_{ij}x_i + w_{0j}^{(1)}  &quot; /&gt;&lt;/div&gt;
&lt;p&gt;where &lt;img src=&quot;http://s0.wp.com/latex.php?latex=K_0+%5Cequiv+n&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;K_0 \equiv n&quot; class=&quot;latex&quot; title=&quot;K_0 \equiv n&quot; /&gt; is the size of the input vector and there are &lt;img src=&quot;http://s0.wp.com/latex.php?latex=j+%3D+1%2C%5Cldots%2CK_1&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;j = 1,\ldots,K_1&quot; class=&quot;latex&quot; title=&quot;j = 1,\ldots,K_1&quot; /&gt; neurons in the so called first hidden layer of the network. The weights are unknown.&lt;/p&gt;
&lt;p&gt;The second transformation then applies a non-linear activation function &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f&quot; class=&quot;latex&quot; title=&quot;f&quot; /&gt; to each &lt;img src=&quot;http://s0.wp.com/latex.php?latex=a_j&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;a_j&quot; class=&quot;latex&quot; title=&quot;a_j&quot; /&gt; to give the output from the &lt;img src=&quot;http://s0.wp.com/latex.php?latex=j&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;j&quot; class=&quot;latex&quot; title=&quot;j&quot; /&gt;-th neuron in the first hidden layer.&lt;/p&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++z_j%5E%7B%281%29%7D+%3D+f%28a_j%5E%7B%281%29%7D%29++&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\displaystyle  z_j^{(1)} = f(a_j^{(1)})  &quot; class=&quot;latex&quot; title=&quot;\displaystyle  z_j^{(1)} = f(a_j^{(1)})  &quot; /&gt;&lt;/div&gt;
&lt;p&gt;Typically, &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f&quot; class=&quot;latex&quot; title=&quot;f&quot; /&gt; is chosen to be &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Ctanh&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\tanh&quot; class=&quot;latex&quot; title=&quot;\tanh&quot; /&gt; or the logistic function. Note that if it were chosen to be the identity then our neural network would be the same as a multivariate linear logistic regression.&lt;/p&gt;
&lt;p&gt;We now repeat these steps for the second hidden layer:&lt;/p&gt;
&lt;p&gt;Ultimately after we applied &lt;img src=&quot;http://s0.wp.com/latex.php?latex=L-1&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;L-1&quot; class=&quot;latex&quot; title=&quot;L-1&quot; /&gt; transformations (through &lt;img src=&quot;http://s0.wp.com/latex.php?latex=L-1&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;L-1&quot; class=&quot;latex&quot; title=&quot;L-1&quot; /&gt; hidden layers) we produce some output:&lt;/p&gt;
&lt;p&gt;We show an example neural in the diagram below.&lt;/p&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;p&gt;&lt;img src=&quot;http://idontgetoutmuch.files.wordpress.com/2013/05/3e46ca17f20285d1315961aa7dc6196f.png?w=450&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The input layer has 7 nodes. There are 2 hidden layers, the first has 3 nodes and the second has 5. The output layer has 3 nodes.&lt;/p&gt;
&lt;p&gt;We are also given a cost function:&lt;/p&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++E%28%5Cboldsymbol%7Bw%7D%3B+%5Cboldsymbol%7Bx%7D%2C+%5Cboldsymbol%7By%7D%29+%3D+%5Cfrac%7B1%7D%7B2%7D%5C%7C%28%5Chat%7B%5Cboldsymbol%7By%7D%7D+-+%5Cboldsymbol%7By%7D%29%5C%7C%5E2++&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\displaystyle  E(\boldsymbol{w}; \boldsymbol{x}, \boldsymbol{y}) = \frac{1}{2}\|(\hat{\boldsymbol{y}} - \boldsymbol{y})\|^2  &quot; class=&quot;latex&quot; title=&quot;\displaystyle  E(\boldsymbol{w}; \boldsymbol{x}, \boldsymbol{y}) = \frac{1}{2}\|(\hat{\boldsymbol{y}} - \boldsymbol{y})\|^2  &quot; /&gt;&lt;/div&gt;
&lt;p&gt;where &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Chat%7B%5Cboldsymbol%7By%7D%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\hat{\boldsymbol{y}}&quot; class=&quot;latex&quot; title=&quot;\hat{\boldsymbol{y}}&quot; /&gt; is the predicted output of the neural net and &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cboldsymbol%7By%7D&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\boldsymbol{y}&quot; class=&quot;latex&quot; title=&quot;\boldsymbol{y}&quot; /&gt; is the observed output.&lt;/p&gt;
&lt;p&gt;As with logistic regression, our goal is to find weights for the neural network which minimises this cost function. We initialise the weights to some small non-zero amount and then use the method of steepest descent (aka gradient descent). The idea is that if &lt;img src=&quot;http://s0.wp.com/latex.php?latex=f&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;f&quot; class=&quot;latex&quot; title=&quot;f&quot; /&gt; is a function of several variables then to find its minimum value, one ought to take a small step in the direction in which it is decreasing most quickly and repeat until no step in any direction results in a decrease. The analogy is that if one is walking in the mountains then the quickest way down is to walk in the direction which goes down most steeply. Of course one get stuck at a local minimum rather than the global minimum but from a machine learning point of view this may be acceptable; alternatively one may start at random points in the search space and check they all give the same minimum.&lt;/p&gt;
&lt;p&gt;We therefore need calculate the gradient of the loss function with respect to the weights (since we need to minimise the cost function). In other words we need to find:&lt;/p&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cnabla+E%28%5Cboldsymbol%7Bx%7D%29+%5Cequiv+%28%5Cfrac%7B%5Cpartial+E%7D%7B%5Cpartial+w_1%7D%2C+%5Cldots%2C+%5Cfrac%7B%5Cpartial+E%7D%7B%5Cpartial+w_n%7D%29++&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\displaystyle  \nabla E(\boldsymbol{x}) \equiv (\frac{\partial E}{\partial w_1}, \ldots, \frac{\partial E}{\partial w_n})  &quot; class=&quot;latex&quot; title=&quot;\displaystyle  \nabla E(\boldsymbol{x}) \equiv (\frac{\partial E}{\partial w_1}, \ldots, \frac{\partial E}{\partial w_n})  &quot; /&gt;&lt;/div&gt;
&lt;p&gt;Once we have this we can take our random starting position and move down the steepest gradient:&lt;/p&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;&lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++w%27_i+%3D+w_i+-+%5Cgamma%5Cfrac%7B%5Cpartial+E%7D%7B%5Cpartial+w_i%7D++&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\displaystyle  w'_i = w_i - \gamma\frac{\partial E}{\partial w_i}  &quot; class=&quot;latex&quot; title=&quot;\displaystyle  w'_i = w_i - \gamma\frac{\partial E}{\partial w_i}  &quot; /&gt;&lt;/div&gt;
&lt;p&gt;where &lt;img src=&quot;http://s0.wp.com/latex.php?latex=%5Cgamma&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;\gamma&quot; class=&quot;latex&quot; title=&quot;\gamma&quot; /&gt; is the step length known in machine learning parlance as the learning rate.&lt;/p&gt;
&lt;h2 id=&quot;haskell-foreword&quot;&gt;Haskell Foreword&lt;/h2&gt;
&lt;p&gt;Some pragmas and imports required for the example code.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; &lt;span style=&quot;color: green;&quot;&gt;{-# LANGUAGE RankNTypes                #-}&lt;/span&gt;
&amp;gt; &lt;span style=&quot;color: green;&quot;&gt;{-# LANGUAGE DeriveFunctor             #-}&lt;/span&gt;
&amp;gt; &lt;span style=&quot;color: green;&quot;&gt;{-# LANGUAGE DeriveFoldable            #-}&lt;/span&gt;
&amp;gt; &lt;span style=&quot;color: green;&quot;&gt;{-# LANGUAGE DeriveTraversable         #-}&lt;/span&gt;
&amp;gt; &lt;span style=&quot;color: green;&quot;&gt;{-# LANGUAGE ScopedTypeVariables       #-}&lt;/span&gt;
&amp;gt; &lt;span style=&quot;color: green;&quot;&gt;{-# LANGUAGE TupleSections             #-}&lt;/span&gt;
&amp;gt; &lt;span style=&quot;color: green;&quot;&gt;{-# LANGUAGE NoMonomorphismRestriction #-}&lt;/span&gt;
&lt;/code&gt;
&lt;code&gt;&amp;gt; &lt;span style=&quot;color: green;&quot;&gt;{-# OPTIONS_GHC -Wall                     #-}&lt;/span&gt;
&amp;gt; &lt;span style=&quot;color: green;&quot;&gt;{-# OPTIONS_GHC -fno-warn-name-shadowing  #-}&lt;/span&gt;
&amp;gt; &lt;span style=&quot;color: green;&quot;&gt;{-# OPTIONS_GHC -fno-warn-type-defaults   #-}&lt;/span&gt;
&amp;gt; &lt;span style=&quot;color: green;&quot;&gt;{-# OPTIONS_GHC -fno-warn-unused-do-bind  #-}&lt;/span&gt;
&amp;gt; &lt;span style=&quot;color: green;&quot;&gt;{-# OPTIONS_GHC -fno-warn-missing-methods #-}&lt;/span&gt;
&lt;/code&gt;
&lt;code&gt;&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;module&lt;/span&gt; NeuralNet
&amp;gt;        &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt; test1
&amp;gt;        &lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; test2
&amp;gt;        &lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; test3
&amp;gt;        &lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&lt;/code&gt;
&lt;code&gt;&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; Numeric.AD
&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; Numeric.AD.Types
&lt;/code&gt;
&lt;code&gt;&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; Data.Traversable &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Traversable&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; Data.Foldable &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Foldable&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; Data.List
&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; Data.List.Split
&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; System.Random
&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;qualified&lt;/span&gt; Data.Vector &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;as&lt;/span&gt; V
&lt;/code&gt;
&lt;code&gt;&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; Control.Monad
&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; Control.Monad.State
&lt;/code&gt;
&lt;code&gt;&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; Data.Random ()
&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; Data.Random.Distribution.Beta
&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; Data.Random.Distribution.Uniform
&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; Data.RVar
&lt;/code&gt;
&lt;code&gt;&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; Text.Printf
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;logistic-regression-redux&quot;&gt;Logistic Regression Redux&lt;/h2&gt;
&lt;p&gt;Let us first implement logistic regression. This will give us a reference against which to compare the equivalent solution expressed as a neural network.&lt;/p&gt;
&lt;p&gt;Instead of maximimizing the log likelihood, we will minimize a cost function.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; cost &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Floating a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; V.Vector a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; V.Vector a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a
&amp;gt; cost theta y x &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;0.5&lt;/span&gt; * &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;y &lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt; yhat&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;^&lt;span class=&quot;hs-num&quot;&gt;2&lt;/span&gt;
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&amp;gt;     yhat &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; logit $ V.sum $ V.zipWith &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;*&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; theta x
&lt;/code&gt;
&lt;code&gt;&amp;gt; logit &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Floating a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt;
&amp;gt;          a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a
&amp;gt; logit x &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt; / &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt; + exp &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;negate x&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We add a regularization term into the total cost so that the parameters do not grow too large. Note that we do not regularize over the bias.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; delta &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Floating a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a
&amp;gt; delta &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;0.01&lt;/span&gt;
&lt;/code&gt;
&lt;code&gt;&amp;gt; totalCost &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Floating a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt;
&amp;gt;              V.Vector a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;              V.Vector a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;              V.Vector &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;V.Vector a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;              a
&amp;gt; totalCost theta y x &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;a + delta * b&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; / l
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&amp;gt;     l &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; fromIntegral $ V.length y
&amp;gt;     a &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; V.sum $ V.zipWith &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;cost theta&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; y x
&amp;gt;     b &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;/&lt;span class=&quot;hs-num&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; $ V.sum $ V.map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;^&lt;span class=&quot;hs-num&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; $ V.drop &lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt; theta
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We determine the gradient of the regularized cost function.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; delTotalCost &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Floating a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt;
&amp;gt;                 V.Vector a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                 V.Vector &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;V.Vector a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                 V.Vector a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                 V.Vector a
&amp;gt; delTotalCost y x &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; grad f
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&amp;gt;     f theta &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; totalCost theta &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;V.map auto y&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;V.map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;V.map auto&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; x&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And finally we can apply &lt;a href=&quot;http://en.wikipedia.org/wiki/Gradient_descent&quot;&gt;gradient descent&lt;/a&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; gamma &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Double
&amp;gt; gamma &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;0.4&lt;/span&gt;
&lt;/code&gt;
&lt;code&gt;&amp;gt; stepOnceCost &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Floating a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt;
&amp;gt;                  a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                  V.Vector a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                  V.Vector &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;V.Vector a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                  V.Vector a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                  V.Vector a
&amp;gt; stepOnceCost gamma y x theta &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt;
&amp;gt;   V.zipWith &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; theta &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;V.map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;* gamma&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; $ del theta&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;     &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&amp;gt;       del &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; delTotalCost y x
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;neural-network-representation&quot;&gt;Neural Network Representation&lt;/h2&gt;
&lt;p&gt;Let us borrow, generalize and prune the data structures used in &lt;a href=&quot;http://themonadreader.files.wordpress.com/2013/03/issue214.pdf&quot;&gt;“A Functional Approach to Neural Networks”&lt;/a&gt;. Some of the fields in the borrowed data structures are probably no longer necessary given that we are going to use automated differentiation rather than backpropagation. Caveat lector!&lt;/p&gt;
&lt;p&gt;The activation function itself is a function which takes any type in the &lt;em&gt;Floating&lt;/em&gt; class to the same type in the &lt;em&gt;Floating&lt;/em&gt; class e.g. &lt;em&gt;Double&lt;/em&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;newtype&lt;/span&gt; ActivationFunction &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt;
&amp;gt;   ActivationFunction
&amp;gt;   &lt;span style=&quot;color: red;&quot;&gt;{&lt;/span&gt;
&amp;gt;     activationFunction &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Floating a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a
&amp;gt;   &lt;span style=&quot;color: red;&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A neural network is a collection of layers.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;data&lt;/span&gt; Layer a &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt;
&amp;gt;   Layer
&amp;gt;   &lt;span style=&quot;color: red;&quot;&gt;{&lt;/span&gt;
&amp;gt;     layerWeights  &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;     layerFunction &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; ActivationFunction
&amp;gt;   &lt;span style=&quot;color: red;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;deriving&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Functor&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Foldable&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Traversable&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;
&lt;code&gt;&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;data&lt;/span&gt; BackpropNet a &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; BackpropNet
&amp;gt;     &lt;span style=&quot;color: red;&quot;&gt;{&lt;/span&gt;
&amp;gt;       layers       &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Layer a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;       learningRate &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Double
&amp;gt;     &lt;span style=&quot;color: red;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;deriving&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Functor&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Foldable&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Traversable&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We need some helper functions to build our neural network and to extract information from it.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; buildBackpropNet &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt;
&amp;gt;   Double &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;   &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;   ActivationFunction &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;   BackpropNet a
&amp;gt; buildBackpropNet learningRate ws f &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt;
&amp;gt;   BackpropNet &lt;span style=&quot;color: red;&quot;&gt;{&lt;/span&gt;
&amp;gt;       layers       &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; map buildLayer checkedWeights
&amp;gt;     &lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; learningRate &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; learningRate
&amp;gt;     &lt;span style=&quot;color: red;&quot;&gt;}&lt;/span&gt;
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt; checkedWeights &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; scanl1 checkDimensions ws
&amp;gt;         buildLayer w   &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; Layer &lt;span style=&quot;color: red;&quot;&gt;{&lt;/span&gt; layerWeights  &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; w
&amp;gt;                                 &lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; layerFunction &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; f
&amp;gt;                                 &lt;span style=&quot;color: red;&quot;&gt;}&lt;/span&gt;
&amp;gt;         checkDimensions &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt;         checkDimensions w1 w2 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt;
&amp;gt;           &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt; + length w1 == length &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;head w2&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;           &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;then&lt;/span&gt; w2
&amp;gt;           &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;else&lt;/span&gt; error $ &lt;span style=&quot;color: teal;&quot;&gt;&quot;Inconsistent dimensions in weight matrix\n&quot;&lt;/span&gt; ++
&amp;gt;                         show &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;length w1&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;        ++ &lt;span style=&quot;color: teal;&quot;&gt;&quot;\n&quot;&lt;/span&gt; ++
&amp;gt;                         show &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;length w2&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;        ++ &lt;span style=&quot;color: teal;&quot;&gt;&quot;\n&quot;&lt;/span&gt; ++
&amp;gt;                         show &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;length $ head w1&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; ++ &lt;span style=&quot;color: teal;&quot;&gt;&quot;\n&quot;&lt;/span&gt; ++
&amp;gt;                         show &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;length $ head w2&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;
&lt;code&gt;&amp;gt; extractWeights &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; BackpropNet a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; extractWeights x &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; map layerWeights $ layers x
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In order to undertake gradient descent on the data structure in which we store a neural network, &lt;em&gt;BackpropNet&lt;/em&gt;, it will be convenient to be able to add such structures together point-wise.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;instance&lt;/span&gt; Num a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; Num &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Layer a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&amp;gt;   &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;+&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; addLayer
&amp;gt; 
&amp;gt; addLayer &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Num a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; Layer a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Layer a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Layer a
&amp;gt; addLayer x y &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt;
&amp;gt;   Layer &lt;span style=&quot;color: red;&quot;&gt;{&lt;/span&gt; layerWeights  &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; zipWith &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;zipWith &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;+&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;                                   &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;layerWeights x&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;                                   &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;layerWeights y&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;         &lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; layerFunction &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; layerFunction x
&amp;gt;         &lt;span style=&quot;color: red;&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;
&lt;code&gt;&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;instance&lt;/span&gt; Num a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; Num &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;BackpropNet a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&amp;gt;   &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;+&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; addBPN
&lt;/code&gt;
&lt;code&gt;&amp;gt; addBPN &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Num a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; BackpropNet a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; BackpropNet a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; BackpropNet a
&amp;gt; addBPN x y &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; BackpropNet &lt;span style=&quot;color: red;&quot;&gt;{&lt;/span&gt; layers &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; zipWith &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;+&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;layers x&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;layers y&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;                           &lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; learningRate &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; learningRate x
&amp;gt;                           &lt;span style=&quot;color: red;&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We store information about updating of output values in each layer in the neural network as we move forward through the network (aka forward propagation).&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;data&lt;/span&gt; PropagatedLayer a
&amp;gt;     &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; PropagatedLayer
&amp;gt;         &lt;span style=&quot;color: red;&quot;&gt;{&lt;/span&gt;
&amp;gt;           propLayerIn         &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;           propLayerOut        &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;           propLayerWeights    &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;           propLayerActFun     &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; ActivationFunction
&amp;gt;         &lt;span style=&quot;color: red;&quot;&gt;}&lt;/span&gt;
&amp;gt;     &lt;span style=&quot;color: red;&quot;&gt;|&lt;/span&gt; PropagatedSensorLayer
&amp;gt;         &lt;span style=&quot;color: red;&quot;&gt;{&lt;/span&gt;
&amp;gt;           propLayerOut &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt;         &lt;span style=&quot;color: red;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;deriving&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Functor&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Foldable&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Traversable&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Sadly we have to use an inefficient calculation to multiply matrices; see this &lt;a href=&quot;http://www.haskell.org/pipermail/haskell-cafe/2013-April/107543.html&quot;&gt;email&lt;/a&gt; for further details.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; matMult &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Num a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; matMult m v &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; result
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&amp;gt;     lrs &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; map length m
&amp;gt;     l   &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; length v
&amp;gt;     result &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;if&lt;/span&gt; all &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;== l&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; lrs
&amp;gt;              &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;then&lt;/span&gt; map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;\&lt;/span&gt;r &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; sum $ zipWith &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;*&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; r v&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; m
&amp;gt;              &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;else&lt;/span&gt; error $ &lt;span style=&quot;color: teal;&quot;&gt;&quot;Matrix has rows of length &quot;&lt;/span&gt; ++ show lrs ++
&amp;gt;                           &lt;span style=&quot;color: teal;&quot;&gt;&quot; but vector is of length &quot;&lt;/span&gt; ++ show l
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can propagate forwards. Note that the code from which this is borrowed assumes that the inputs are images which are &lt;img src=&quot;http://s0.wp.com/latex.php?latex=m+%5Ctimes+m&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;m \times m&quot; class=&quot;latex&quot; title=&quot;m \times m&quot; /&gt; pixels each encoded using a grayscale, hence the references to bits and the check that values lie in the range &lt;img src=&quot;http://s0.wp.com/latex.php?latex=0+%5Cleq+x+%5Cleq+1&amp;amp;bg=ffffff&amp;amp;fg=333333&amp;amp;s=0&quot; alt=&quot;0 \leq x \leq 1&quot; class=&quot;latex&quot; title=&quot;0 \leq x \leq 1&quot; /&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; propagateNet &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Floating a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Ord a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Show a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt;
&amp;gt;                 &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                 BackpropNet a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                 &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;PropagatedLayer a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; propagateNet input net &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; tail calcs
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt; calcs &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; scanl propagate layer0 &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;layers net&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;         layer0 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; PropagatedSensorLayer $ validateInput net input
&amp;gt; 
&amp;gt;         validateInput net &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; validateInputValues .
&amp;gt;                             validateInputDimensions net
&amp;gt; 
&amp;gt;         validateInputDimensions net input &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt;
&amp;gt;           &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;if&lt;/span&gt; got == expected
&amp;gt;           &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;then&lt;/span&gt; input
&amp;gt;           &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;else&lt;/span&gt; error &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&quot;Input pattern has &quot;&lt;/span&gt; ++ show got ++
&amp;gt;                       &lt;span style=&quot;color: teal;&quot;&gt;&quot; bits, but &quot;&lt;/span&gt; ++
&amp;gt;                       show expected ++ &lt;span style=&quot;color: teal;&quot;&gt;&quot; were expected&quot;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;           &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt; got      &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; length input
&amp;gt;                 expected &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;+&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;negate &lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; $
&amp;gt;                            length $
&amp;gt;                            head $
&amp;gt;                            layerWeights $
&amp;gt;                            head $
&amp;gt;                            layers net
&amp;gt; 
&amp;gt;         validateInputValues input &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt;
&amp;gt;           &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;minimum input &amp;gt;= &lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &amp;amp;&amp;amp; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;maximum input &amp;lt;= &lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;           &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;then&lt;/span&gt; input
&amp;gt;           &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;else&lt;/span&gt; error &lt;span style=&quot;color: teal;&quot;&gt;&quot;Input bits outside of range [0,1]&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that we add a 1 to the inputs to each layer to give the bias.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; propagate &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Floating a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Show a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt;
&amp;gt;              PropagatedLayer a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;              Layer a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;              PropagatedLayer a
&amp;gt; propagate layerJ layerK &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; result
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&amp;gt;     result &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt;
&amp;gt;       PropagatedLayer
&amp;gt;         &lt;span style=&quot;color: red;&quot;&gt;{&lt;/span&gt;
&amp;gt;           propLayerIn         &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; layerJOut&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;           propLayerOut        &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; map f a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;           propLayerWeights    &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; weights&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;           propLayerActFun     &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; layerFunction layerK
&amp;gt;         &lt;span style=&quot;color: red;&quot;&gt;}&lt;/span&gt;
&amp;gt;     layerJOut &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; propLayerOut layerJ
&amp;gt;     weights   &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; layerWeights layerK
&amp;gt;     a &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; weights `matMult` &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;:layerJOut&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;     f &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Floating a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a
&amp;gt;     f &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; activationFunction $ layerFunction layerK
&lt;/code&gt;
&lt;code&gt;&amp;gt; evalNeuralNet &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Floating a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Ord a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Show a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt;
&amp;gt;                  BackpropNet a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; evalNeuralNet net input &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; propLayerOut $ last calcs
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt; calcs &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; propagateNet input net
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We define a cost function.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; costFn &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Floating a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Ord a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Show a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt;
&amp;gt;           Int &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;           Int &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;           &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;           BackpropNet a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;           a
&amp;gt; costFn nDigits expectedDigit input net &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;0.5&lt;/span&gt; * sum &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;^&lt;span class=&quot;hs-num&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; diffs&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&amp;gt;     predicted &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; evalNeuralNet net input
&amp;gt;     diffs &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; zipWith &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;targets nDigits&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;!!expectedDigit&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; predicted
&lt;/code&gt;
&lt;code&gt;&amp;gt; targets &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Floating a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; Int &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; targets nDigits &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; map row &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;..&lt;/span&gt; nDigits &lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&amp;gt;     row m &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; concat &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;x&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;1.0&lt;/span&gt; : y&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt;       &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&amp;gt;         &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;x&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; y&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; splitAt m &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;take &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;nDigits &lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; $ repeat &lt;span class=&quot;hs-num&quot;&gt;0.0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If instead we would rather perform gradient descent over the whole training set (rather than stochastically) then we can do so. Note that we do not regularize the weights for the biases.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; totalCostNN &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Floating a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Ord a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Show a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt;
&amp;gt;                Int &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                V.Vector Int &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                V.Vector &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                BackpropNet a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                a
&amp;gt; totalCostNN nDigits expectedDigits inputs net &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; cost
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&amp;gt;     cost &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;a + delta * b&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; / l
&amp;gt; 
&amp;gt;     l &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; fromIntegral $ V.length expectedDigits
&amp;gt; 
&amp;gt;     a &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; V.sum $ V.zipWith &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;\&lt;/span&gt;expectedDigit input &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                             costFn nDigits expectedDigit input net&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;                           expectedDigits inputs
&amp;gt; 
&amp;gt;     b &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;/&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;2&lt;/span&gt; * m&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; $ sum $ map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;^&lt;span class=&quot;hs-num&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; ws
&amp;gt; 
&amp;gt;     m &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; fromIntegral $ length ws
&amp;gt; 
&amp;gt;     ws &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; concat $ concat $
&amp;gt;          map stripBias $
&amp;gt;          extractWeights net
&amp;gt; 
&amp;gt;     stripBias xss &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;drop &lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; xss
&lt;/code&gt;
&lt;code&gt;&amp;gt; delTotalCostNN &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Floating a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Ord a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Show a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt;
&amp;gt;                   Int &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                   V.Vector Int &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                   V.Vector &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                   BackpropNet a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                   BackpropNet a
&amp;gt; delTotalCostNN nDigits expectedDigits inputs &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; grad f
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&amp;gt;     f net &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; totalCostNN nDigits expectedDigits
&amp;gt;                         &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;V.map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;map auto&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; inputs&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; net
&lt;/code&gt;
&lt;code&gt;&amp;gt; stepOnceTotal &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Int &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                  Double &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                  V.Vector Int &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                  V.Vector &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Double&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                  BackpropNet Double &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;                  BackpropNet Double
&amp;gt; stepOnceTotal nDigits gamma y x net &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt;
&amp;gt;   net + fmap &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;* &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;negate gamma&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;delTotalCostNN nDigits y x net&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;example-i&quot;&gt;Example I&lt;/h2&gt;
&lt;p&gt;Let’s try it out. First we need to generate some data. Rather arbitrarily let us create some populations from the &lt;code&gt;beta&lt;/code&gt; distribution.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; betas &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Int &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Double &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Double &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Double&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; betas n a b &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt;
&amp;gt;   fst $ runState &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;replicateM n &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;sampleRVar &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;beta a b&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;mkStdGen seed&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;     &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&amp;gt;       seed &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can plot the populations we wish to distinguish by sampling.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; b &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Double
&amp;gt; a          &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;15&lt;/span&gt;
&amp;gt; b          &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;6&lt;/span&gt;
&amp;gt; nSamples &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Int
&amp;gt; nSamples   &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;100000&lt;/span&gt;
&amp;gt; 
&amp;gt; sample0&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; sample1 &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Double&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; sample0 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; betas nSamples a b
&amp;gt; sample1 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; betas nSamples b a
&lt;/code&gt;
&lt;code&gt;&amp;gt; mixSamples &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Double&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Double&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Double&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Double&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; mixSamples xs ys &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; unfoldr g &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; xs&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; ys&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&amp;gt;     g &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;[]&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; []&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;         &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; Nothing
&amp;gt;     g &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;[]&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;  &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;         &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; Nothing
&amp;gt;     g &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; []&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;         &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; Nothing
&amp;gt;     g &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;x:xs&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;y:ys&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; Just $ &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;x&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;y:ys&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; xs&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;
&lt;code&gt;&amp;gt; createSample &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; V.Vector &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Double&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Double&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt; createSample &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; V.fromList $ take &lt;span class=&quot;hs-num&quot;&gt;100&lt;/span&gt; $ mixSamples sample1 sample0
&lt;/code&gt;
&lt;code&gt;&amp;gt; lRate &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Double
&amp;gt; lRate &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;0.01&lt;/span&gt;
&amp;gt; actualTheta &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; V.Vector Double
&amp;gt; actualTheta &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; V.fromList &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;1.0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; initTheta &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; V.Vector Double
&amp;gt; initTheta &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; V.replicate &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;V.length actualTheta&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;0.1&lt;/span&gt;
&lt;/code&gt;
&lt;code&gt;&amp;gt; logitAF &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; ActivationFunction
&amp;gt; logitAF &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; ActivationFunction logit
&lt;/code&gt;
&lt;code&gt;&amp;gt; test1 &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; IO ()
&amp;gt; test1 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;do&lt;/span&gt;
&amp;gt; 
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;let&lt;/span&gt; testNet &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; buildBackpropNet lRate &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;0.1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;0.1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; logitAF
&lt;/code&gt;
&lt;code&gt;&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;let&lt;/span&gt; vals &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; V.Vector &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Double&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; V.Vector Double&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;       vals &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; V.map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;\&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;y&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; x&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;y&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; V.fromList &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1.0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; x&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; $ createSample
&amp;gt; 
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;let&lt;/span&gt; gs &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; iterate &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;stepOnceCost gamma &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;V.map fst vals&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;V.map snd vals&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;                    initTheta
&amp;gt;       theta &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; head $ drop &lt;span class=&quot;hs-num&quot;&gt;1000&lt;/span&gt; gs
&amp;gt;   printf &lt;span style=&quot;color: teal;&quot;&gt;&quot;Logistic regression: theta_0 = %5.3f, theta_1 = %5.3f\n&quot;&lt;/span&gt;
&amp;gt;          &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;theta V.! &lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;theta V.! &lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt; 
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;let&lt;/span&gt; us &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; V.map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;round . fst&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; createSample
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;let&lt;/span&gt; vs &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; V.map snd createSample
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;let&lt;/span&gt; fs &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; iterate &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;stepOnceTotal &lt;span class=&quot;hs-num&quot;&gt;2&lt;/span&gt; gamma us &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;V.map return vs&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; testNet
&amp;gt;       phi &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; extractWeights $ head $ drop &lt;span class=&quot;hs-num&quot;&gt;1000&lt;/span&gt; fs
&amp;gt;   printf &lt;span style=&quot;color: teal;&quot;&gt;&quot;Neural network: theta_00 = %5.3f, theta_01 = %5.3f\n&quot;&lt;/span&gt;
&amp;gt;          &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;phi!!&lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;!!&lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;!!&lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;phi!!&lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;!!&lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;!!&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;   printf &lt;span style=&quot;color: teal;&quot;&gt;&quot;Neural network: theta_10 = %5.3f, theta_11 = %5.3f\n&quot;&lt;/span&gt;
&amp;gt;          &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;phi!!&lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;!!&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;!!&lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;phi!!&lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;!!&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;!!&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;
&lt;code&gt;&lt;span style=&quot;color: gray;&quot;&gt;ghci&amp;gt; &lt;/span&gt;test1
  Logistic regression: theta_0 = -2.383, theta_1 = 4.852
  Neural network: theta_00 = 2.386, theta_01 = -4.861
  Neural network: theta_10 = -2.398, theta_11 = 4.886
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;example-ii&quot;&gt;Example II&lt;/h2&gt;
&lt;p&gt;Now let’s try a neural net with 1 hidden layer using the data we prepared earlier.&lt;/p&gt;
&lt;p&gt;We seed the weights in the neural with small random values; if we set all the weights to 0 then the gradient descent algorithm might get stuck.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; uniforms &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Int &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Double&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; uniforms n &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt;
&amp;gt;   fst $ runState &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;replicateM n &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;sampleRVar stdUniform&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;mkStdGen seed&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;     &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&amp;gt;       seed &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt;
&lt;/code&gt;
&lt;code&gt;&amp;gt; randomWeightMatrix &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Int &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Int &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Double&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; randomWeightMatrix numInputs numOutputs &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; y
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
&amp;gt;     y &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; chunksOf numInputs weights
&amp;gt;     weights &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;/ &lt;span class=&quot;hs-num&quot;&gt;100.0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; $ uniforms &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;numOutputs * numInputs&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;
&lt;code&gt;&amp;gt; w1&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; w2 &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Double&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; w1  &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; randomWeightMatrix &lt;span class=&quot;hs-num&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;2&lt;/span&gt;
&amp;gt; w2  &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; randomWeightMatrix &lt;span class=&quot;hs-num&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;2&lt;/span&gt;
&lt;/code&gt;
&lt;code&gt;&amp;gt; initNet2 &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; BackpropNet Double
&amp;gt; initNet2 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; buildBackpropNet lRate &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;w1&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; w2&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; logitAF
&amp;gt; 
&amp;gt; labels &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; V.Vector Int
&amp;gt; labels &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; V.map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;round . fst&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; createSample
&lt;/code&gt;
&lt;code&gt;&amp;gt; inputs &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; V.Vector &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Double&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; inputs &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; V.map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;return . snd&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; createSample
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Instead of hand-crafting gradient descent, let us use the library function as it performs better and is easier to implement.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; estimates &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Floating a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Ord a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Show a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt;
&amp;gt;              V.Vector Int &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;              V.Vector &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;              BackpropNet a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;              &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;BackpropNet a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; estimates y x &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; gradientDescent $
&amp;gt;                 &lt;span style=&quot;color: red;&quot;&gt;\&lt;/span&gt;theta &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; totalCostNN &lt;span class=&quot;hs-num&quot;&gt;2&lt;/span&gt; y &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;V.map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;map auto&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; x&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; theta
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can examine the weights of our fitted neural net and apply it to some test data.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; test2 &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; IO ()
&amp;gt; test2 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;do&lt;/span&gt;
&amp;gt; 
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;let&lt;/span&gt; fs &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; estimates labels inputs initNet2
&amp;gt;   mapM_ putStrLn $ map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;take &lt;span class=&quot;hs-num&quot;&gt;60&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; $
&amp;gt;                    map show $ extractWeights $
&amp;gt;                    head $ drop &lt;span class=&quot;hs-num&quot;&gt;1000&lt;/span&gt; fs
&amp;gt;   putStrLn $ show $ evalNeuralNet &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;head $ drop &lt;span class=&quot;hs-num&quot;&gt;1000&lt;/span&gt; fs&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt;   putStrLn $ show $ evalNeuralNet &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;head $ drop &lt;span class=&quot;hs-num&quot;&gt;1000&lt;/span&gt; fs&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.9&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;
&lt;code&gt;&lt;span style=&quot;color: gray;&quot;&gt;ghci&amp;gt; &lt;/span&gt;test2
  [[3.3809809537916933,-6.778365921046131],[-5.157492699008754
  [[1.2771246165025043,5.294090869351353,-8.264801192310706],[
  [0.997782987249909,2.216698813392053e-3]
  [1.4853346509852003e-3,0.9985148392767443]
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;example-iii&quot;&gt;Example III&lt;/h2&gt;
&lt;p&gt;Let’s try a more sophisticated example and create a population of 4 groups which we measure with 2 variables.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; c&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; d &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Double
&amp;gt; c          &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;15&lt;/span&gt;
&amp;gt; d          &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;8&lt;/span&gt;
&amp;gt; sample2&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; sample3 &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Double&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; sample2 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; betas nSamples c d
&amp;gt; sample3 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; betas nSamples d c
&lt;/code&gt;
&lt;code&gt;&amp;gt; mixSamples3 &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Num t &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;t&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; mixSamples3 xss &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; concat $ transpose $
&amp;gt;                   zipWith &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;\&lt;/span&gt;n xs &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;n&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; xs&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;                           &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;map fromIntegral &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;..&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt;                           xss
&amp;gt; sample02&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; sample03&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; sample12&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; sample13 &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Double&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Double&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; sample02 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;x&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; y&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;|&lt;/span&gt; x &lt;span style=&quot;color: red;&quot;&gt;&amp;lt;-&lt;/span&gt; sample0&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; y &lt;span style=&quot;color: red;&quot;&gt;&amp;lt;-&lt;/span&gt; sample2&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; sample03 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;x&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; y&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;|&lt;/span&gt; x &lt;span style=&quot;color: red;&quot;&gt;&amp;lt;-&lt;/span&gt; sample0&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; y &lt;span style=&quot;color: red;&quot;&gt;&amp;lt;-&lt;/span&gt; sample3&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; sample12 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;x&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; y&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;|&lt;/span&gt; x &lt;span style=&quot;color: red;&quot;&gt;&amp;lt;-&lt;/span&gt; sample1&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; y &lt;span style=&quot;color: red;&quot;&gt;&amp;lt;-&lt;/span&gt; sample2&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; sample13 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;x&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; y&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;|&lt;/span&gt; x &lt;span style=&quot;color: red;&quot;&gt;&amp;lt;-&lt;/span&gt; sample1&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; y &lt;span style=&quot;color: red;&quot;&gt;&amp;lt;-&lt;/span&gt; sample3&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;
&lt;code&gt;&amp;gt; createSample3 &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;forall&lt;/span&gt; t. Num t &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; V.Vector &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;t&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Double&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Double&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
&amp;gt; createSample3 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; V.fromList $ take &lt;span class=&quot;hs-num&quot;&gt;512&lt;/span&gt; $ mixSamples3 &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt; sample02
&amp;gt;                                                     &lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; sample03
&amp;gt;                                                     &lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; sample12
&amp;gt;                                                     &lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; sample13
&amp;gt;                                                     &lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Rather annoyingly picking random weights seemed to give a local but not global minimum. This may be a feature of having more nodes in the hidden layer than in the input layer. By fitting a neural net with no hidden layers to the data and using the outputs as inputs to fit another neural net with no hidden layers, we can get a starting point from which we can converge to the global minimum.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; w31&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; w32 &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Double&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; w31 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1.795626449637491&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1.0687662199549477&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.6780994566671094&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;        &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.8953174631646047&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1.536931540024011&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1.7631220370122578&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;        &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.4762453998497917&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;2.005243268058972&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1.2945899127545906&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;        &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.43019763097582875&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1.5711869072989957&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1.187180183656747&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; w32 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.65116209142284&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.4837310591797774&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.17870333721054968&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;         &lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.6692619856605464&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1.062292154441557&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;        &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.7521274440366631&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1.2071835415415136e-2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1.0078929981538551&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;         &lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1.3144243587577473&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.5102027925579049&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;        &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.7545728756863981&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.4830112128458844&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1.2901624541811962&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;         &lt;span class=&quot;hs-num&quot;&gt;1.0487049495446408&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;9.746209726152217e-3&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;        &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.8576212271328413&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.9035219951783956&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.4034500456652809&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;
&amp;gt;         &lt;span class=&quot;hs-num&quot;&gt;0.10091187689838758&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.781835908789879&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; 
&amp;gt; testNet3 &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; BackpropNet Double
&amp;gt; testNet3 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; buildBackpropNet lRate &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;w31&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; w32&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; logitAF
&lt;/code&gt;
&lt;code&gt;&amp;gt; labels3 &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; V.Vector Int
&amp;gt; labels3 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; V.map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;round . fst&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; createSample3
&amp;gt; inputs3 &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; V.Vector &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Double&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; inputs3 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; V.map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;\&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;x&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; y&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;x&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; y&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; . snd&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; createSample3
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we use the library &lt;em&gt;gradientDescent&lt;/em&gt; function to generate neural nets which ever better fit the data.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; estimates3 &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Floating a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Ord a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Show a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt;
&amp;gt;               V.Vector Int &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;               V.Vector &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;               BackpropNet a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt;
&amp;gt;               &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;BackpropNet a&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt; estimates3 y x &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; gradientDescent $
&amp;gt;                  &lt;span style=&quot;color: red;&quot;&gt;\&lt;/span&gt;theta &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; totalCostNN &lt;span class=&quot;hs-num&quot;&gt;4&lt;/span&gt; y &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;V.map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;map auto&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; x&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; theta
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally we can fit a neural net and check that it correctly classifies some data.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt; test3 &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; IO ()
&amp;gt; test3 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;do&lt;/span&gt;
&amp;gt;   &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;let&lt;/span&gt; fs &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; drop &lt;span class=&quot;hs-num&quot;&gt;100&lt;/span&gt; $ estimates3 labels3 inputs3 testNet3
&amp;gt;   mapM_ putStrLn $ map &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;take &lt;span class=&quot;hs-num&quot;&gt;60&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; $ map show $ extractWeights $ head fs
&amp;gt;   putStrLn $ take &lt;span class=&quot;hs-num&quot;&gt;60&lt;/span&gt; $ show $ evalNeuralNet &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;head fs&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;0.1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt;   putStrLn $ take &lt;span class=&quot;hs-num&quot;&gt;60&lt;/span&gt; $ show $ evalNeuralNet &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;head fs&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;0.9&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt;   putStrLn $ take &lt;span class=&quot;hs-num&quot;&gt;60&lt;/span&gt; $ show $ evalNeuralNet &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;head fs&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.9&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;0.1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&amp;gt;   putStrLn $ take &lt;span class=&quot;hs-num&quot;&gt;60&lt;/span&gt; $ show $ evalNeuralNet &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;head fs&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.9&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;0.9&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;
&lt;code&gt;&lt;span style=&quot;color: gray;&quot;&gt;ghci&amp;gt; &lt;/span&gt;test3
  [[-2.295896239599931,2.705409060274802,2.1377566388724047],[
  [[-0.6169787627963551,2.5369568963968256,-0.3515306366626614
  [2.638026636449198e-2,9.091308688841797e-2,0.373349222824566
  [0.13674565454319784,1.128123133092104e-2,0.8525700090804755
  [0.30731134024095474,0.8197492648500939,1.3704140162804749e-
  [0.6773814649389487,0.22533958204471505,0.1957913744022863,4
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;
&lt;p&gt;Bishop, Christopher M. 2006. &lt;em&gt;Pattern Recognition and Machine Learning (Information Science and Statistics)&lt;/em&gt;. Secaucus, NJ, USA: Springer-Verlag New York, Inc.&lt;/p&gt;
&lt;p&gt;Rojas, R. 1996. &lt;em&gt;Neural networks: a systematic introduction&lt;/em&gt;. Springer-Verlag New York Incorporated. &lt;a href=&quot;http://books.google.co.uk/books?id=txsjjYzFJS4C&quot; title=&quot;http://books.google.co.uk/books?id=txsjjYzFJS4C&quot;&gt;http://books.google.co.uk/books?id=txsjjYzFJS4C&lt;/a&gt;.&lt;/p&gt;
&lt;br /&gt;  &lt;a href=&quot;http://feeds.wordpress.com/1.0/gocomments/idontgetoutmuch.wordpress.com/460/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/comments/idontgetoutmuch.wordpress.com/460/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;img src=&quot;http://stats.wordpress.com/b.gif?host=idontgetoutmuch.wordpress.com&amp;amp;blog=2944309&amp;amp;post=460&amp;amp;subd=idontgetoutmuch&amp;amp;ref=&amp;amp;feed=1&quot; alt=&quot;&quot; height=&quot;1&quot; border=&quot;0&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Fri, 31 May 2013 14:30:22 +0000</pubDate>
</item>
<item>
	<title>Tom Schrijvers: Ghent FP meeting on June 26</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-5844006452378085451.post-6517670102111175037</guid>
	<link>http://tomschrijvers.blogspot.com/2013/05/ghent-fp-meeting-on-june-26.html</link>
	<description>&lt;div style=&quot;text-align: left;&quot; dir=&quot;ltr&quot;&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;The &quot;Functional Programming Group Ghent&quot; (GhentFPG) [1] is a friendly group for&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;all people interested in functional programming, with a tendency towards Haskell.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;It is organised as part of Zeus WPI [2].&lt;/span&gt;&lt;br /&gt;&lt;br style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot; /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;We are pleased to announce that we will hold a next meeting on Wednesday, 26th&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;of June, starting at 19h00! There will be three talks.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;The main presentation, by Adam Bergmark from Silk [3] is about &lt;b&gt;Fay&lt;/b&gt; [4]:&lt;/span&gt;&lt;br /&gt;&lt;blockquote class=&quot;tr_bq&quot;&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;Fay is a proper subset of Haskell that compiles to JavaScript. There is a&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;  compiler with the same name written in Haskell. Web browsers only speak&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;  JavaScript but more and more people find that they want to compile to&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;  JavaScript instead.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;Why do we want to compile Haskell to JavaScript, and what advantages does&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;  Fay have compared to other compilers?&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;What are the challenges in compiling Haskell and supporting a language&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;  ecosystem, and how do we do it?&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;What can Fay currently do, and what is planned for the future?&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;This will be a broad overview about Fay for prospective users, followed by&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;  an in-depth look at interesting parts of the compiler internals.&lt;/span&gt;&lt;/blockquote&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;Additionally, there will be two short talks by two students who did an Msc. Thesis&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;about functional programming languages:&lt;/span&gt;&lt;br /&gt;&lt;br style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot; /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;&lt;b&gt;Genetic Algorithms in Haskell&lt;/b&gt;&lt;br /&gt;Matthias Delbar&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;&lt;b&gt;Automatic Detection of Recursion Patterns&lt;/b&gt;Jasper Van der Jeugt&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;The meeting will take place in the Jozef Plateauzaal at the following address,&lt;/span&gt;&lt;br /&gt;&lt;blockquote class=&quot;tr_bq&quot;&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;Faculteit Ingenieurswetenschappen&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;Universiteit Gent&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;Plateaustraat 22&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;9000 Gent&lt;/span&gt;&lt;/blockquote&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;As mentioned above, we aim to start at &lt;/span&gt;&lt;span style=&quot;&quot; class=&quot;aBn&quot; tabindex=&quot;0&quot;&gt;&lt;span style=&quot;&quot; class=&quot;aQJ&quot;&gt;19:00&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;. After the meeting we can go&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;for drinks in a nearby pub (this latter part is, of course, completely optional)&lt;/span&gt;&lt;br /&gt;&lt;br style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot; /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;We hope to see you all there!&lt;/span&gt;&lt;br /&gt;&lt;br style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot; /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;Regards,&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;On behalf of the GhentFPG organising committee.&lt;/span&gt;&lt;br /&gt;&lt;br style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot; /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;[1]: &lt;/span&gt;&lt;a style=&quot;background-color: white; color: #1155cc; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot; href=&quot;http://groups.google.com/group/ghent-fpg&quot; target=&quot;_blank&quot;&gt;http://groups.google.com/&amp;lt;wbr&amp;gt;&amp;lt;/wbr&amp;gt;group/ghent-fpg&lt;/a&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;[2]: &lt;/span&gt;&lt;a style=&quot;background-color: white; color: #1155cc; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot; href=&quot;http://zeus.ugent.be/&quot; target=&quot;_blank&quot;&gt;http://zeus.ugent.be/&lt;/a&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;[3]: &lt;/span&gt;&lt;a style=&quot;background-color: white; color: #1155cc; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot; href=&quot;http://www.silkapp.com/&quot; target=&quot;_blank&quot;&gt;http://www.silkapp.com/&lt;/a&gt;&lt;br /&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot;&gt;[4]: &lt;/span&gt;&lt;a style=&quot;background-color: white; color: #1155cc; font-family: arial, sans-serif; font-size: 12.800000190734863px;&quot; href=&quot;http://www.fay-lang.org/&quot; target=&quot;_blank&quot;&gt;http://www.fay-lang.org/&lt;/a&gt;&lt;/div&gt;</description>
	<pubDate>Fri, 31 May 2013 09:36:41 +0000</pubDate>
	<author>noreply@blogger.com (Tom Schrijvers)</author>
</item>
<item>
	<title>wren ng thornton: ANN: prelude-safeenum</title>
	<guid isPermaLink="false">tag:dreamwidth.org,2010-05-25:518115:83994</guid>
	<link>http://winterkoninkje.dreamwidth.org/83994.html</link>
	<description>&lt;h3&gt;prelude-safeenum 0.1.0&lt;/h3&gt;

&lt;p&gt;The prelude-safeenum package offers a safe alternative to the Prelude's &lt;code&gt;Enum&lt;/code&gt; class in order to render it safe. While we're at it, we also generalize the notion of enumeration to support types which can only be enumerated in one direction.&lt;/p&gt;


&lt;h3&gt;Description&lt;/h3&gt;

&lt;p&gt;The prelude-safeenum package offers an alternative to the notion of enumeration provided by the Prelude. For now it is just a package, but the eventual goal is to be incorporated into haskell prime. Some salient characteristics of the new type-class hierarchy are:&lt;/p&gt;

&lt;dl&gt;
&lt;dt&gt;Removes partial functions&lt;/dt&gt;
&lt;dd&gt;The Haskell Language Report &lt;a href=&quot;http://www.haskell.org/onlinereport/haskell2010/haskellch6.html#x13-1310006.3.4&quot;&gt;section 6.3.4&lt;/a&gt; defines &lt;code&gt;pred&lt;/code&gt;, &lt;code&gt;succ&lt;/code&gt;, &lt;code&gt;fromEnum&lt;/code&gt;, and &lt;code&gt;toEnum&lt;/code&gt; to be partial functions when the type is &lt;code&gt;Bounded&lt;/code&gt;, but this is unacceptable. The new classes remove this problem by correcting the type signatures for these functions.&lt;/dd&gt;

&lt;dt&gt;Generalizes the notion of enumeration&lt;/dt&gt;
&lt;dd&gt;Rather than requiring that the type is linearly enumerable, we distinguish between forward enumeration (which allows for multiple predecessors) and backward enumeration (which allows for multiple successors).&lt;/dd&gt;

&lt;dt&gt;Adds new functions: &lt;code&gt;enumDownFrom&lt;/code&gt;, &lt;code&gt;enumDownFromTo&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;One of the big problems with the partiality of &lt;code&gt;pred&lt;/code&gt; is that there is no safe way to enumerate downwards since in the border case &lt;code&gt;enumFromThen x (pred x)&lt;/code&gt; will throw an error rather than evaluating to &lt;code&gt;[x]&lt;/code&gt; as desired. These new functions remove this problem.&lt;/dd&gt;

&lt;dt&gt;Removes the requirement...&lt;/dt&gt;
&lt;dd&gt;...that the enumeration order coincides with the
&lt;code&gt;Ord&lt;/code&gt; ordering (if one exists). Though, of course, it's advisable to keep
them in sync if possible, for your sanity.&lt;/dd&gt;

&lt;dt&gt;Ensures that the notion of enumeration is well-defined&lt;/dt&gt;
&lt;dd&gt;This much-needed rigor clarifies the meaning of enumeration. In addition, it rules out instances for &lt;code&gt;Float&lt;/code&gt; and &lt;code&gt;Double&lt;/code&gt; which are highly problematic and often confuse newcomers to Haskell. Unfortunately, this rigor does render the instance for &lt;code&gt;Ratio&lt;/code&gt; problematic. However, &lt;code&gt;Ratio&lt;/code&gt; instances &lt;i&gt;can&lt;/i&gt; be provided so long as the base type is enumerable (and &lt;code&gt;Integral&lt;/code&gt;, naturally); but they must be done in an obscure order that does not coincide with &lt;code&gt;Ord&lt;/code&gt;.

&lt;/dd&gt;&lt;dt&gt;The obscure order required for well-defined enumeration of &lt;code&gt;Ratio&lt;/code&gt; is provided.&lt;/dt&gt;
&lt;/dl&gt;


&lt;h3&gt;Links&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Homepage: &lt;a href=&quot;http://code.haskell.org/~wren/&quot;&gt;http://code.haskell.org/~wren/&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;Hackage: &lt;a href=&quot;http://hackage.haskell.org/package/prelude-safeenum&quot;&gt;http://hackage.haskell.org/package/prelude-safeenum&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;Darcs: &lt;a href=&quot;http://community.haskell.org/~wren/prelude-safeenum&quot;&gt;http://community.haskell.org/~wren/prelude-safeenum&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;Haddock: &lt;a href=&quot;http://community.haskell.org/~wren/prelude-safeenum/dist/doc/html/prelude-safeenum/&quot;&gt;Darcs version&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src=&quot;http://www.dreamwidth.org/tools/commentcount?user=winterkoninkje&amp;amp;ditemid=83994&quot; alt=&quot;comment count unavailable&quot; height=&quot;12&quot; style=&quot;vertical-align: middle;&quot; width=&quot;30&quot; /&gt; comments</description>
	<pubDate>Thu, 30 May 2013 00:05:57 +0000</pubDate>
</item>
<item>
	<title>Edward Z. Yang: The AST Typing Problem</title>
	<guid isPermaLink="false">http://blog.ezyang.com/?p=8439</guid>
	<link>http://feedproxy.google.com/~r/ezyang/~3/EpcxawDNuhI/</link>
	<description>&lt;div class=&quot;document&quot;&gt;



&lt;p&gt;This &lt;a href=&quot;http://lambda-the-ultimate.org/node/4170&quot; class=&quot;reference external&quot;&gt;Lambda the Ultimate post (dated 2010)&lt;/a&gt; describes a rather universal problem faced by compiler writers: how does one go about adding “extra information” (e.g. types) to an AST? (The post itself divides the problem into three components: adding the information to the data types, using the information to inform the construction of the node, and using the information to inform the destruction of a node—but I’m really only interested in the question of how you define your data type, not do things to it.) In this post, I want to sum up ways of solving the problem which were described in this post, and also take a look at what some real world compilers do.  The running example lambda calculus looks like the following:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;data Exp = Num Int
         | Bool Bool
         | Var Var
         | If Exp Exp Exp
         | Lambda Var Exp
         | App Exp Exp
data Type = TyInt | TyBool | TyArrow Type Type
&lt;/pre&gt;
&lt;div id=&quot;separate-ir-where-nodes-are-decorated-with-types&quot; class=&quot;section&quot;&gt;
&lt;h3&gt;Separate IR where nodes are decorated with types&lt;/h3&gt;
&lt;p&gt;The low-tech solution: if you need a new version of the IR with more information, just define a new IR type where each node can also carry the information. A trick to make these definitions more concise is to make a mutually recursive data structure. &lt;a href=&quot;http://lambda-the-ultimate.org/node/4170#comment-63834&quot; class=&quot;reference external&quot;&gt;[1]&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;type TExp = (TExp', Type)
data TExp' = TNum Int
           | TBool Bool
           | TVar Var
           | TIf TExp TExp TExp
           | TLambda Var TExp
           | TApp TExp TExp
&lt;/pre&gt;
&lt;p&gt;Despite (or perhaps because of) it’s simplicity, this approach is extremely popular among many compilers, especially in the ML community.  A few examples include OCaml (parsetree/typedtree), MLton (AST/CoreML) and Ikarus Scheme. Part of the reason for this is that the transition from frontend language to typed language also comes with some other changes, and when a new AST is defined those changes can be combined in too.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&quot;nullable-field&quot; class=&quot;section&quot;&gt;
&lt;h3&gt;Nullable field&lt;/h3&gt;
&lt;p&gt;The unprincipled solution: use one AST, but have an optional field in which you can slot in the information. &lt;a href=&quot;http://lambda-the-ultimate.org/node/4170#comment-63832&quot; class=&quot;reference external&quot;&gt;[2]&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;type TExp = (TExp', Maybe Type)
data TExp' = TNum Int
           | TBool Bool
           | TVar Var
           | TIf TExp TExp TExp
           | TLambda Var TExp
           | TApp TExp TExp
&lt;/pre&gt;
&lt;p&gt;Presented without further comment.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&quot;explicit-typing&quot; class=&quot;section&quot;&gt;
&lt;h3&gt;Explicit typing&lt;/h3&gt;
&lt;p&gt;While closely related to the separate IR solution, an explicitly typed IR takes the approach of not decorating each node with a type, but arranging that the type of any given node can be quickly computed using only local information. &lt;a href=&quot;http://lambda-the-ultimate.org/node/4170#comment-63884&quot; class=&quot;reference external&quot;&gt;[3]&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;data TExp = TNum Int
          | TBool Bool
          | TVar Var
          | TIf TExp TExp TExp
          | TLambda Var Type TExp
          | TApp TExp TExp
&lt;/pre&gt;
&lt;p&gt;Here, the difference between &lt;tt class=&quot;docutils literal&quot;&gt;TExp&lt;/tt&gt; and &lt;tt class=&quot;docutils literal&quot;&gt;Exp&lt;/tt&gt; is very slight; the &lt;tt class=&quot;docutils literal&quot;&gt;TLambda&lt;/tt&gt; is annotated with an explicit type for the binder. As far as type-checking is concerned, this makes a world of difference: we no longer need to look outside a lambda to figure out what the binder could be.&lt;/p&gt;
&lt;p&gt;Forcing your IR to be explicitly typed is often a good idea for metatheoretic reasons, as complicated type systems often don’t have decidable inference algorithms.  Both GHC’s core IR, Ur/Web's core and Coq are explicitly typed in this way.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&quot;two-level-types&quot; class=&quot;section&quot;&gt;
&lt;h3&gt;Two-level types&lt;/h3&gt;
&lt;p&gt;By deferring when you tie the knot of a recursive data-structure, you can arrange for the base functor to do double-duty for the untyped and typed representations. &lt;a href=&quot;http://lambda-the-ultimate.org/node/4170#comment-63836&quot; class=&quot;reference external&quot;&gt;[4]&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;data ExpF a = Num Int
            | Bool Bool
            | Var Var
            | If a a a
            | Lambda Var a
            | App a a
newtype Exp = Exp (ExpF Exp)
newtype TExp = TExp (ExpF TExp, Type)
&lt;/pre&gt;
&lt;p&gt;The Coq kernel uses this to define its expression type, although it doesn’t use it to define an untyped variant.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&quot;lazy-attribute-grammars&quot; class=&quot;section&quot;&gt;
&lt;h3&gt;(Lazy) Attribute grammars&lt;/h3&gt;
&lt;p&gt;I don’t claim to understand this approach too well, but essentially it is a programming model distinct from usual algebraic data types which associates attributes over nodes of a tree. In some sense, it can be thought as a memoized function from AST nodes to the attributes. Many compilers do utlize maps, but only for top-level declarations. &lt;a href=&quot;http://lambda-the-ultimate.org/node/4170#comment-63903&quot; class=&quot;reference external&quot;&gt;[5]&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&quot;closing-remarks&quot; class=&quot;section&quot;&gt;
&lt;h3&gt;Closing remarks&lt;/h3&gt;
&lt;p&gt;There were a few things that I did not mention here which came up in the discussion. One participant suggested using &lt;a href=&quot;http://lambda-the-ultimate.org/node/4170#comment-63842(polymorphicvariants)&quot; class=&quot;reference external&quot;&gt;polymorphic variants&lt;/a&gt; to define the data type; this doesn’t help much with adding extra information but allows for different ways of writing traversal functions. Indeed, traversal is one of the big concerns, and the mention of &lt;a href=&quot;http://lambda-the-ultimate.org/node/4170#comment-63846&quot; class=&quot;reference external&quot;&gt;generic programming&lt;/a&gt; also is targeted at this problem.&lt;/p&gt;
&lt;p&gt;As for my preference? It’s hard to say. I’ve worked with compilers mostly written in the “define a new IR style”, and while the initial outlay of defining two data structures is quite annoying, it is mostly a fixed cost. What’s yours?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Also, a question.&lt;/strong&gt; Is there a presentation of the conventional set of annotations needed to get explicitly typed System F?&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;img src=&quot;http://feeds.feedburner.com/~r/ezyang/~4/EpcxawDNuhI&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Tue, 28 May 2013 11:25:03 +0000</pubDate>
</item>
<item>
	<title>Lee Pike: Book Review: Automate This</title>
	<guid isPermaLink="false">http://leepike.wordpress.com/?p=556</guid>
	<link>http://leepike.wordpress.com/2013/05/27/book-review-automate-this/</link>
	<description>&lt;p&gt;A family member gave me a copy of &lt;em&gt;&lt;a href=&quot;http://www.amazon.com/dp/1591844924&quot;&gt;Automate This&lt;/a&gt;&lt;/em&gt; by Christopher Steiner as a gift a few months ago.  The subtitle of the book is “How algorithms came to rule our world.”  The book is a non-technical, fast, and easy read.  I did read a few interesting stories, such as NASA’s use of personality categorization algorithms in the 70s to predict which potential astronauts would work well together, or a math professor’s algorithmic dissection of Beatles songs.  The book particularly emphasizes algorithmic trading on Wall Street.  The somewhat non sequitur conclusion is that we need more science, engineering, and math graduates.&lt;/p&gt;
&lt;p&gt;The main point of the book is that algorithms are pervasive and will become more so.  Toward this point, I think the author could have cast an even wider net, mentioning that algorithms are implemented in everything from elevator controllers to autopilots.  There is a cursory pre-computing history of algorithms at the beginning of the book that is (tenuously) tied to a Wall Street trading.&lt;/p&gt;
&lt;p&gt;Rather than focus on algorithms broadly, the focus is patchy, hitting on machine learning, high-speed trading, and game theory.  Some mention of algorithms as might be taught in a “Analysis of Algorithms” course, covering basic topics like time-space complexity and decidability, would help prevent the general public from having a narrow-minded interpretation of algorithms.  Computer scientists invent, analyze, and implement algorithms every day, but much of that work is perhaps not sexy enough for a popular science book.&lt;/p&gt;
&lt;p&gt;Incidentally, for the topics Steiner does cover, an excellent companion treating similar topics, but with a different focus, is Nate Silver’s (of &lt;a href=&quot;http://fivethirtyeight.blogs.nytimes.com/&quot;&gt;538&lt;/a&gt; fame) book, &lt;em&gt;&lt;a href=&quot;http://www.amazon.com/dp/159420411X&quot;&gt;The Signal and the Noise&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;
&lt;br /&gt;  &lt;a href=&quot;http://feeds.wordpress.com/1.0/gocomments/leepike.wordpress.com/556/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/comments/leepike.wordpress.com/556/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;img src=&quot;http://stats.wordpress.com/b.gif?host=leepike.wordpress.com&amp;amp;blog=7358124&amp;amp;post=556&amp;amp;subd=leepike&amp;amp;ref=&amp;amp;feed=1&quot; alt=&quot;&quot; height=&quot;1&quot; border=&quot;0&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Tue, 28 May 2013 01:00:03 +0000</pubDate>
</item>
<item>
	<title>Philip Wadler: How to reject a paper</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-9757377.post-3271912874475557511</guid>
	<link>http://wadler.blogspot.com/2013/05/how-to-reject-paper.html</link>
	<description>&lt;div style=&quot;clear: both; text-align: center;&quot; class=&quot;separator&quot;&gt;&lt;a style=&quot;margin-left: 1em; margin-right: 1em;&quot; href=&quot;http://4.bp.blogspot.com/-Nr_jG4UMaAA/UaMkctFqUcI/AAAAAAAACCQ/wYhoeiOP6T0/s1600/reject.jpg&quot;&gt;&lt;img src=&quot;http://4.bp.blogspot.com/-Nr_jG4UMaAA/UaMkctFqUcI/AAAAAAAACCQ/wYhoeiOP6T0/s320/reject.jpg&quot; height=&quot;240&quot; border=&quot;0&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style=&quot;font-family: inherit; margin-bottom: 0px; margin-top: 0px;&quot; class=&quot;mediumb-text&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;a href=&quot;http://dl.acm.org/citation.cfm?doid=1519103.1519122&quot;&gt;How NOT to review a paper: the tools and techniques of the adversarial reviewer&lt;/a&gt; and &lt;/span&gt;&lt;a href=&quot;http://vixra.org/pdf/0907.0020v1.pdf&quot;&gt;How to reject any scientific manuscript&lt;/a&gt;.  Photo from &lt;a href=&quot;http://academicnegativity.tumblr.com/&quot;&gt;Academic Negativity&lt;/a&gt;.&lt;/div&gt;</description>
	<pubDate>Mon, 27 May 2013 09:24:19 +0000</pubDate>
	<author>noreply@blogger.com (Philip Wadler)</author>
</item>
<item>
	<title>Ken T Takusagawa: [ryvpijhs] Code reflection at compile time</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-6757805.post-6350310239130139689</guid>
	<link>http://kenta.blogspot.com/2013/05/ryvpijhs-code-reflection-at-compile-time.html</link>
	<description>&lt;p dir=&quot;ltr&quot;&gt;Consider a Haskell extension providing this function:&lt;/p&gt;&lt;p dir=&quot;ltr&quot;&gt;compileTimeAnalyze :: (Code -&amp;gt; IO ()) -&amp;gt; a -&amp;gt; a&lt;/p&gt;&lt;p dir=&quot;ltr&quot;&gt;&quot;compileTypeAnalyze f e&quot; passes to user-defined function &quot;f&quot; the parse tree (and any other useful information such as types and symbol table) of the expression &quot;e&quot; and evaluates &quot;f&quot; at compile time.  At run time, it simply evaluates &quot;e&quot;.&lt;/p&gt;&lt;p dir=&quot;ltr&quot;&gt;The likely use is to emit warnings or errors if there is something domain-specifically wrong with the code that the compiler might not detect.&lt;/p&gt;&lt;p dir=&quot;ltr&quot;&gt;Inspired by debugging 2D graphics code.  We often want the number of references to some X coordinate be the same as the number to the corresponding Y coordinate, or else there is likely a typo where we typed X instead of Y or vice versa.&lt;/p&gt;&lt;p dir=&quot;ltr&quot;&gt;We probably also want the user to be able to define&lt;/p&gt;&lt;p dir=&quot;ltr&quot;&gt;compileTimeAnalyzeModule :: Code -&amp;gt; IO()&lt;/p&gt;&lt;p dir=&quot;ltr&quot;&gt;which gets the entire contents of the module for static analysis.  What about static analysis across many modules?&lt;/p&gt;&lt;p dir=&quot;ltr&quot;&gt;Template Haskell can probably do most of this.&lt;/p&gt;</description>
	<pubDate>Mon, 27 May 2013 08:29:01 +0000</pubDate>
	<author>noreply@blogger.com (Ken)</author>
</item>
<item>
	<title>Neil Mitchell: Three types of build-system dependency</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-7094652.post-6220804110186757637</guid>
	<link>http://neilmitchell.blogspot.com/2013/05/three-types-of-build-system-dependency.html</link>
	<description>&lt;i&gt;Summary: There are three types of dependencies you might want to express in a build system, all of which are supported by Shake.&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;A build system, at its heart, is a system which runs commands in an order satisfying user-specified dependencies. But what kind of dependencies can be expressed? This post describes three different types of dependency, only one of which is available in Make, but all of which are available in both &lt;a href=&quot;https://github.com/ndmitchell/shake#readme&quot;&gt;Shake&lt;/a&gt; and &lt;a href=&quot;http://martine.github.io/ninja/&quot;&gt;Ninja&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Feature 1: Static dependencies (available in every build system)&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The most basic form of dependency is a static dependency, where a rule produces an output from some inputs:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;-- In Make --&lt;br /&gt;result.tar : file1 file2&lt;br /&gt;    tar -cf result.tar file1 file2&lt;br /&gt;&lt;br /&gt;-- In Shake --&lt;br /&gt;&quot;result.tar&quot; *&amp;gt; \out -&amp;gt; do&lt;br /&gt;    let deps = [&quot;file1&quot;,&quot;file2&quot;]&lt;br /&gt;    need deps&lt;br /&gt;    cmd &quot;tar -cf&quot; [out] deps&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This rule says that the file &lt;tt&gt;result.tar&lt;/tt&gt; depends on the inputs &lt;tt&gt;file1&lt;/tt&gt; and &lt;tt&gt;file2&lt;/tt&gt;, and provides a command to build &lt;tt&gt;result.tar&lt;/tt&gt;. Whenever &lt;tt&gt;file1&lt;/tt&gt; or &lt;tt&gt;file2&lt;/tt&gt; change, the command will be run, and &lt;tt&gt;result.tar&lt;/tt&gt; will be built.&lt;br /&gt;&lt;br /&gt;Static dependencies occur in almost every build rule, and are supported by all build tools, including Make and Shake.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Feature 2: Dynamic dependencies (available in Shake, Ninja and Redo)&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;A more advanced dependency is where the list of dependencies itself depends on the results of previous dependencies. Imagine we want to build &lt;tt&gt;result.tar&lt;/tt&gt; from the list of files stored in &lt;tt&gt;list.txt&lt;/tt&gt;. The dependencies of &lt;tt&gt;result.tar&lt;/tt&gt; cannot be specified statically, but depend on &lt;i&gt;the contents&lt;/i&gt; of &lt;tt&gt;list.txt&lt;/tt&gt;. In Shake we can write:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&quot;result.tar&quot; *&amp;gt; \out -&amp;gt; do&lt;br /&gt;    need [&quot;list.txt&quot;]&lt;br /&gt;    contents &amp;lt;- readFileLines &quot;list.txt&quot;&lt;br /&gt;    need contents&lt;br /&gt;    cmd &quot;tar -cf&quot; [out] contents&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This rule describes how to build &lt;tt&gt;result.tar&lt;/tt&gt;. We depend on (&lt;tt&gt;need&lt;/tt&gt;) the file &lt;tt&gt;list.txt&lt;/tt&gt;. We read each line from &lt;tt&gt;list.txt&lt;/tt&gt; into the variable &lt;tt&gt;contents&lt;/tt&gt; - being a list of the files that should go into &lt;tt&gt;result.tar&lt;/tt&gt;. Next, we depend on all the files in &lt;tt&gt;contents&lt;/tt&gt;, and finally call the &lt;tt&gt;tar&lt;/tt&gt; program. If either &lt;tt&gt;list.txt&lt;/tt&gt; changes, or any of the files listed by &lt;tt&gt;list.txt&lt;/tt&gt; change, then &lt;tt&gt;result.tar&lt;/tt&gt; will be rebuilt.&lt;br /&gt;&lt;br /&gt;This feature is necessary in almost every build system, yet is shockingly lacking from most build tools - I am only aware of it being available in &lt;a href=&quot;https://github.com/ndmitchell/shake#readme&quot;&gt;Shake&lt;/a&gt;, &lt;a href=&quot;http://martine.github.io/ninja/&quot;&gt;Ninja&lt;/a&gt; and &lt;a href=&quot;https://github.com/apenwarr/redo#readme&quot;&gt;Redo&lt;/a&gt;. As a common example, in Make you might write:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;result.o : result.c result_header1.h result_header2.h&lt;br /&gt;    gcc ...&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The file &lt;tt&gt;result.o&lt;/tt&gt; depends on both the C source file &lt;tt&gt;result.c&lt;/tt&gt; and all headers that file includes. But listing the headers both in &lt;tt&gt;result.c&lt;/tt&gt; with &lt;tt&gt;#include&lt;/tt&gt; directives, and in the Makefile, is a brittle form of duplication. A better approach is for the build system to run &lt;tt&gt;gcc -M result.c&lt;/tt&gt; and extract the includes from there. In Shake we can write:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&quot;result.o&quot; *&amp;gt; \out -&amp;gt; do&lt;br /&gt;    let src = &quot;result.c&quot;&lt;br /&gt;    Stdout stdout &amp;lt;- cmd &quot;gcc -MM&quot; [src]&lt;br /&gt;    need $ src : drop 2 (words stdout)&lt;br /&gt;    cmd &quot;gcc -o&quot; [out] &quot;-c&quot; [src]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;My experience is that about a quarter of rules require some kind of additional dependency based on previous dependencies. While you can hack round some of the issues in Make, and people have become disturbingly adept at doing so, the result often only approximates the dependencies - building either too much or too little.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Feature 3: Multiple outputs from one rule (available in Shake and Ninja)&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The final feature is producing multiple outputs from one command, and is used far more rarely (perhaps one or two rules in a complex build system) - but when needed, is essential. Some programs, such as GHC, can produce two outputs with one command - compiling &lt;tt&gt;Foo.hs&lt;/tt&gt; produces both &lt;tt&gt;Foo.o&lt;/tt&gt; and &lt;tt&gt;Foo.hi&lt;/tt&gt;. As a first approximation, the &lt;tt&gt;.o&lt;/tt&gt; file depends on the entire contents of the source file, while the &lt;tt&gt;.hi&lt;/tt&gt; file depends only on the type signatures. A single &lt;tt&gt;ghc&lt;/tt&gt; invocation needs to do all the work to produce both, but often the &lt;tt&gt;.hi&lt;/tt&gt; file will be left unchanged. In Shake we can write:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;[&quot;Foo.hi&quot;,&quot;Foo.o&quot;] *&amp;gt;&amp;gt; \_ -&amp;gt; do&lt;br /&gt;    need [&quot;Foo.hs&quot;]&lt;br /&gt;    cmd &quot;gcc -c Foo.hs&quot;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;While it is often possible to construct a series of dependencies to approximate a single rule producing multiple outputs, it only works in some cases, and is brittle. The only build systems I am aware of which support multiple outputs are &lt;a href=&quot;https://github.com/ndmitchell/shake#readme&quot;&gt;Shake&lt;/a&gt; and &lt;a href=&quot;http://martine.github.io/ninja/&quot;&gt;Ninja&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Essential features&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;My standard advice when people ask about writing a build system is &quot;don't&quot;. If some existing build system (e.g. ghc --make or Cabal) is capable of building your project, use that instead. Custom build systems are necessary for many complex projects, but many projects are not complex. If you have decided your project is complex, you should use a build tool that can express complex dependencies, both for writing the initial system and to provide the flexibility to make the inevitable changes required.&lt;br /&gt;&lt;br /&gt;Looking only at dependency features, I would consider it unwise to start a complex build system using a tool other than Shake or Ninja, or perhaps Redo (if you accept the absence of multiple outputs from one rule).&lt;br /&gt;&lt;br /&gt;Weak dependency specification in build tools, particularly Make, has left its mark on many programs. I recently talked to some OCaml hackers complaining that their tools were not &quot;Make friendly&quot; because they produced multiple output files. I wonder what lengths other tools have gone to in order to cope with weak dependency specification...&lt;br /&gt;</description>
	<pubDate>Sun, 26 May 2013 19:58:13 +0000</pubDate>
	<author>noreply@blogger.com (Neil Mitchell)</author>
</item>
<item>
	<title>Tom Moertel: Lazy merging in Python using streams</title>
	<guid isPermaLink="true">http://blog.moertel.com/posts/2013-05-26-python-lazy-merge.html</guid>
	<link>http://blog.moertel.com/posts/2013-05-26-python-lazy-merge.html</link>
	<description>&lt;div class=&quot;info&quot;&gt;Posted on May 26, 2013&lt;/div&gt;
&lt;div class=&quot;tags&quot;&gt;Tags: &lt;a href=&quot;http://blog.moertel.com/tags/programming.html&quot;&gt;programming&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/python.html&quot;&gt;python&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/iterators.html&quot;&gt;iterators&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/streams.html&quot;&gt;streams&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/SICP.html&quot;&gt;SICP&lt;/a&gt;, &lt;a href=&quot;http://blog.moertel.com/tags/functional programming.html&quot;&gt;functional programming&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;Recently while solving a programming puzzle in Python, I needed to merge a series of &lt;em&gt;N&lt;/em&gt; iterators, each yielding values in sorted order, into a single iterator over the sorted values. The trick is that, when asked for a value from the merged series, to determine which iterator should contribute that value, you must extract all &lt;em&gt;N&lt;/em&gt; iterators’ next values. And then, of course, you can emit only one. So what do you do with the remaining &lt;em&gt;N&lt;/em&gt; – 1 values you’ve extracted?&lt;/p&gt;
&lt;p&gt;Rather than think about that question too hard, I just converted the iterators into an equivalent form in which the next value was always exposed and hence available for making decisions &lt;em&gt;before&lt;/em&gt; extraction. This form is basically the &lt;a href=&quot;http://mitpress.mit.edu/sicp/full-text/sicp/book/node69.html&quot;&gt;&lt;em&gt;stream&lt;/em&gt; of SICP fame&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The idea is to convert each &lt;a href=&quot;http://www.python.org/dev/peps/pep-0234/&quot;&gt;Python iterator&lt;/a&gt; into either &lt;code&gt;None&lt;/code&gt; (representing an empty stream) or a pair containing the iterator’s next value and the iterator itself:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; iterator_to_stream(iterator):
    &lt;span class=&quot;co&quot;&gt;&quot;&quot;&quot;Convert an iterator into a stream (None if the iterator is empty).&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;kw&quot;&gt;try&lt;/span&gt;:
        &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; iterator.&lt;span class=&quot;dt&quot;&gt;next&lt;/span&gt;(), iterator
    &lt;span class=&quot;kw&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;StopIteration&lt;/span&gt;:
        &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then to extract values from the stream, you just apply &lt;code&gt;stream_next&lt;/code&gt; to it, and it will hand you back the next value and the updated state of the stream:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; stream_next(stream):
    &lt;span class=&quot;co&quot;&gt;&quot;&quot;&quot;Get (next_value, next_stream) from a stream.&quot;&quot;&quot;&lt;/span&gt;
    val, iterator = stream
    &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; val, iterator_to_stream(iterator)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Since streams expose their next value, they can be ordered by that value. And for my task that was the property that made all the difference:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&lt;span class=&quot;ch&quot;&gt;import&lt;/span&gt; heapq

&lt;span class=&quot;kw&quot;&gt;def&lt;/span&gt; merge(iterators):
    &lt;span class=&quot;co&quot;&gt;&quot;&quot;&quot;Make a lazy sorted iterator that merges lazy sorted iterators.&quot;&quot;&quot;&lt;/span&gt;
    streams = &lt;span class=&quot;dt&quot;&gt;map&lt;/span&gt;(iterator_to_stream, &lt;span class=&quot;dt&quot;&gt;map&lt;/span&gt;(&lt;span class=&quot;dt&quot;&gt;iter&lt;/span&gt;, iterators))
    heapq.heapify(streams)
    &lt;span class=&quot;kw&quot;&gt;while&lt;/span&gt; streams:
        stream = heapq.heappop(streams)
        &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; stream is not &lt;span class=&quot;ot&quot;&gt;None&lt;/span&gt;:
            val, stream = stream_next(stream)
            heapq.heappush(streams, stream)
            &lt;span class=&quot;kw&quot;&gt;yield&lt;/span&gt; val&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;An example use:&lt;/p&gt;
&lt;pre class=&quot;sourceCode python&quot;&gt;&lt;code class=&quot;sourceCode python&quot;&gt;&amp;gt;&amp;gt;&amp;gt; xs = merge([&lt;span class=&quot;dt&quot;&gt;xrange&lt;/span&gt;(&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;), &lt;span class=&quot;dt&quot;&gt;xrange&lt;/span&gt;(&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;), &lt;span class=&quot;dt&quot;&gt;xrange&lt;/span&gt;(&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;)])
&amp;gt;&amp;gt;&amp;gt; xs
&amp;lt;generator &lt;span class=&quot;dt&quot;&gt;object&lt;/span&gt; merge at &lt;span class=&quot;bn&quot;&gt;0x7fea07c9d320&lt;/span&gt;&amp;gt;

&amp;gt;&amp;gt;&amp;gt; &lt;span class=&quot;dt&quot;&gt;list&lt;/span&gt;(xs)
[&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;6&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;8&lt;/span&gt;]&lt;/code&gt;&lt;/pre&gt;</description>
	<pubDate>Sun, 26 May 2013 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Joachim Breitner: My first CTAN package: Typesetting Continued Equalities</title>
	<guid isPermaLink="false">http://www.joachim-breitner.de/blog/archives/597-guid.html</guid>
	<link>http://www.joachim-breitner.de/blog/archives/597-My-first-CTAN-package-Typesetting-Continued-Equalities.html</link>
	<description>&lt;p&gt;I recently had a &lt;a href=&quot;http://tex.stackexchange.com/questions/108277/automatic-arrangement-of-equations-with-comments&quot;&gt;TeX itch to scratch&lt;/a&gt;: I am working on a paper that has several multi-line continued equalities¹ where, depending on the size of the expressions and the explanations of each step, I chose among a few layouts. But implementing the layout together with the actual code was inefficient, as switching the layout involved changing every line.&lt;/p&gt; 
&lt;p&gt;So I came up with the package &lt;tt&gt;conteq&lt;/tt&gt; which allows you to typeset continued equations in a simple declarative manner, e.g.&lt;/p&gt; 
&lt;pre&gt;\begin{conteq}
  e^{\pi\cdot i} \\
= -1               &amp;amp; Euler's formula \\
&amp;lt; 0                &amp;amp; this is an inequality \\ 
&amp;lt; \sqrt 3 \\
= \int e^{-x^2} dx &amp;amp; this is due to Gauss.
\end{conteq}&lt;/pre&gt; 
&lt;p&gt;and allows you to select the layout via an parameter to the environment, or globally, or either. Also, the styling of the explanations (italics? wrapped in &lt;tt&gt;{&lt;/tt&gt;...&lt;tt&gt;}&lt;/tt&gt;?) can be configured simply by redefining a macro. For more details and an overview of the various styles, check out the &lt;a href=&quot;http://mirrors.ctan.org/macros/latex/contrib/conteq/conteq.pdf&quot;&gt;package documentation&lt;/a&gt;.&lt;br /&gt;&lt;/p&gt; 
&lt;p&gt;If this sounds useful to you, fetch the &lt;a href=&quot;http://ctan.org/pkg/conteq&quot;&gt;&lt;tt&gt;conteq&lt;/tt&gt; package from CTAN&lt;/a&gt;. But beware: It uses quite current features of the &lt;a href=&quot;http://ctan.org/pkg/expl3&quot;&gt;&lt;tt&gt;expl3&lt;/tt&gt;&lt;/a&gt; package, so you need at least the version from 2012/07/02 (TeXLive 2013 is good). You can file bug reports at the &lt;a href=&quot;https://github.com/nomeata/conteq&quot;&gt;GitHub mirror&lt;/a&gt; of &lt;a href=&quot;http://git.nomeata.de/?p=conteq.git&quot;&gt;my git repository&lt;/a&gt;.&lt;br /&gt;&lt;/p&gt; 
&lt;p&gt;I’d like to thank &lt;a href=&quot;http://tex.stackexchange.com/users/2707/bruno-le-floch&quot;&gt;Bruno Le Floch&lt;/a&gt; and &lt;a href=&quot;http://tex.stackexchange.com/users/73/joseph-wright&quot;&gt;Joseph Wright&lt;/a&gt;, who made me aware of expl3 on &lt;a href=&quot;http://tex.stackexchange.com/q/115700/15107&quot;&gt;various&lt;/a&gt; &lt;a href=&quot;http://tex.stackexchange.com/a/115816/15107&quot;&gt;TeX&lt;/a&gt; &lt;a href=&quot;http://tex.stackexchange.com/a/115909/15107&quot;&gt;Exchange&lt;/a&gt; &lt;a href=&quot;http://tex.stackexchange.com/a/115575/15107&quot;&gt;questions&lt;/a&gt;.&lt;/p&gt; 
&lt;p&gt;¹ I haven’t heard of this term before, but &lt;a href=&quot;http://english.stackexchange.com/questions/114746/correct-phrase-for-chain-of-equations&quot;&gt;supposedly it is the right translation&lt;/a&gt; for the German word „Gleichungskette“.&lt;br /&gt;&lt;/p&gt;</description>
	<pubDate>Sat, 25 May 2013 14:05:10 +0000</pubDate>
	<author>mail@joachim-breitner.de (nomeata)</author>
</item>
<item>
	<title>language-puppet: Embedded ruby interpreter and performance increase</title>
	<guid isPermaLink="false">http://lpuppet.banquise.net/blog/2013/05/24/embedded-ruby-interpreter-and-performance-increase</guid>
	<link>http://lpuppet.banquise.net/blog/2013/05/24/embedded-ruby-interpreter-and-performance-increase/</link>
	<description>&lt;p&gt;Despite hitting a nasty (but obvious) bug involving Ruby’s GC, it seems that the feature is now stable.&lt;/p&gt;

&lt;p&gt;I have been using a script for a while that computes catalogs for 30 nodes, taking into account exported resources, and runs some tests on the results. This script used to run in around 50 seconds. On my puppet master, the combined catalog generation time for those hosts is around 10 minutes¹.&lt;/p&gt;

&lt;p&gt;Language-puppet was about ten times faster than the original implementation, but was wasting a significant amount of time spawning Ruby processes, rendering gobs of data (the list of all known variables and their values), and feeding them to said process, for each template evaluation. On the Ruby side, the data was interpreted (with eval), the templates were loaded and interpolated, and the response spit back to the Haskell executable. For this reason I wrote a minimalist template parser that is capable of interpolating the simplest ones while staring in Haskell land.&lt;/p&gt;

&lt;p&gt;Now the Ruby process is embedded, and variable resolution happens only when needed, by providing a callback Haskell function to the Ruby runtime.&lt;/p&gt;

&lt;p&gt;The whole script now runs in less than 10 seconds (six if you omit the tunnelled accesses to PuppetDB). This is now acceptable to run it before almost all commits, which was the goal. It will help making sure nothing got (too) broken, especially with regards to exported resources.&lt;/p&gt;

&lt;p&gt;The software is now stable enough, and I will probably prepare a new binary release soon, along with a Debian-style repository.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;¹ This is not a fair comparison however. My script queries the PuppetDB for facts using a SSH tunnel, whereas the puppet master is local. On the other hand the puppet master does stuff my script doesn’t, such as updating facts and reporting data into PuppetDB (in all fairness my script updates a local PuppetDB-like database). I do not believe this accounts for an important fraction of those ten minutes, but might be wrong. Also, the puppet master has a faster CPU, and does not run unit tests on the catalogs.&lt;/p&gt;</description>
	<pubDate>Fri, 24 May 2013 17:38:00 +0000</pubDate>
</item>
<item>
	<title>Paul Johnson: Elevator pitch for Haskell short enough for an elevator ride</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-5975524006824862804.post-6916588378407081659</guid>
	<link>http://paulspontifications.blogspot.com/2013/05/elevator-pitch-for-haskell-short-enough.html</link>
	<description>Greg Hale has written an &quot;&lt;a href=&quot;https://www.fpcomplete.com/user/imalsogreg/functional-programming-elevator-pitch&quot;&gt;elevator pitch&lt;/a&gt;&quot; for Haskell. While it is certainly a good piece of advocacy, it is quite long, and therefore not an elevator pitch. The idea of an elevator pitch is something you can deliver in the 30 seconds or so that you find yourself sharing an elevator with a potential investor.&lt;br /&gt;&lt;br /&gt;I've been looking for an effective Haskell elevator pitch for some years now, but the only thing I was able to come up with was just that you can deliver software better, faster and cheaper because you need fewer lines of code. This just sounds like hype.&lt;br /&gt;&lt;br /&gt;However I think I've now got something better. Here it is:&lt;br /&gt;&lt;br /&gt;&lt;blockquote class=&quot;tr_bq&quot;&gt;Conventional languages make the programmer construct both a control flow and a data flow for the program. There is no way to check they are consistent, and anytime they are inconsistent you get a bug. In Haskell the programmer just specifies the data flow: the control flow is up to the compiler. That simplifies the program, cutting down the work and completely preventing a big class of errors.&lt;/blockquote&gt;</description>
	<pubDate>Fri, 24 May 2013 15:08:27 +0000</pubDate>
	<author>noreply@blogger.com (Paul Johnson)</author>
</item>
<item>
	<title>Twan van Laarhoven: The complete correctness of sorting</title>
	<guid isPermaLink="true">http://twanvl.nl/blog/agda/sorting</guid>
	<link>http://twanvl.nl/blog/agda/sorting</link>
	<description>&lt;p&gt;A while ago I set out to prove the correctness of &lt;a href=&quot;http://en.wikipedia.org/wiki/Merge_sort&quot;&gt;merge sort&lt;/a&gt; in Agda.
Of course this has been done before.
But &lt;a href=&quot;http://mazzo.li/posts/AgdaSort.html&quot;&gt;most&lt;/a&gt; &lt;a href=&quot;http://www.iis.sinica.edu.tw/~scm/2007/agda-exercise-proving-that-mergesort-returns-ordered-list/&quot;&gt;proofs&lt;/a&gt; you find are far from complete.
All they prove is a lemma such as
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;is-sorted&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt;) &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;IsSortedList&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;sort&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;)
&lt;/pre&gt;&lt;p&gt;Maybe even restricted to lists of natural numbers.
While it is nice that a sort function indeed produces a sorted output, that is only half of the story.
Consider this function:
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;cheat-sort&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt;
&lt;span class=&quot;varid&quot;&gt;cheat-sort&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;listcon&quot;&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;Clearly the empty list is sorted. So we are done.
What is missing is the second half of correctness of sorting: that the output is a permutation of the input.
You want something like:
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;sort&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt;) &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Sorted'&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;record&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Sorted'&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt;) &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;where&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;field&lt;/span&gt;
    &lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt;       &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt;
    &lt;span class=&quot;varid&quot;&gt;isSorted&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;IsSorted&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt;
    &lt;span class=&quot;varid&quot;&gt;isPerm&lt;/span&gt;   &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;IsPermutation&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;While I was at it, I decided to add the third half of correctness: a bound on the runtime or computational complexity.
In the end I was able to define:
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;insertion-sort&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;conid&quot;&gt;Sorted&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;) &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;)
&lt;span class=&quot;varid&quot;&gt;selection-sort&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;conid&quot;&gt;Sorted&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;) &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;)
&lt;span class=&quot;varid&quot;&gt;merge-sort&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;conid&quot;&gt;Sorted&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;) &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;⌈log₂&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;⌉&lt;/span&gt;)
&lt;/pre&gt;&lt;p&gt;This was not as easy as I would have hoped.
In this post I will not bore you with all the details, I'll just go over some of the highlights. The &lt;a href=&quot;https://gist.github.com/twanvl/5635740&quot;&gt;full code is on github&lt;/a&gt;.
&lt;/p&gt;&lt;h2&gt;&lt;a name=&quot;what-it-means-to-be-sorted&quot;&gt;&lt;/a&gt;What it means to be sorted &lt;/h2&gt;
&lt;p&gt;There are roughly two ways to define sorted lists that I know of:
&lt;/p&gt;&lt;ol&gt;&lt;li&gt; Parametrize the sorted list by a lower bound on the values it contains. For a cons cell the head should be smaller than the lower bound, and the tail should be larger than the head. This requires the type to have a smallest element, but you can adjoin -∞ with a new datatype.&lt;/li&gt;
&lt;li&gt; Parametrize the sorted list by a list of all values in it. For a cons cell require that the head is smaller than all the values in the tail.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Since I already need to parametrize by all values in the list to show that the sorted list contains a permutation of them, I went with the second approach:
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;comment&quot;&gt;-- A proof that x is less than all values in xs&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;≤*&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt;) &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;where&lt;/span&gt;
  &lt;span class=&quot;listcon&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;listcon&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤*&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;listcon&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;∷&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; {&lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt;} &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt;) &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤*&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤*&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; ∷ &lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt;)
&lt;div class=&quot;empty-line&quot;&gt;&lt;/div&gt;
&lt;span class=&quot;comment&quot;&gt;-- Proof that a list is sorted&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;IsSorted&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;where&lt;/span&gt;
  &lt;span class=&quot;listcon&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;listcon&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;IsSorted&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;listcon&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;∷&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; {&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;} &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤*&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;IsSorted&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;IsSorted&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; ∷ &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;)
&lt;/pre&gt;&lt;h2&gt;&lt;a name=&quot;what-it-means-to-be-a-permutation&quot;&gt;&lt;/a&gt;What it means to be a permutation &lt;/h2&gt;
&lt;p&gt;To show that one list is a permutation of another I again used two data types.
Suppose that we know that &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;&lt;/tt&gt; is a permutation of &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt;&lt;/tt&gt;. Then when is &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; ∷ &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;&lt;/tt&gt; a permutation of some list &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;xys&lt;/span&gt;&lt;/tt&gt;? Well, we can permute &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;&lt;/tt&gt; to &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt;&lt;/tt&gt;, and insert &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt;&lt;/tt&gt; anywhere. I used &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varop&quot;&gt;◂&lt;/span&gt;&lt;/tt&gt; to denote this insertion,
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;comment&quot;&gt;-- x ◂ xs ≡ xys means that xys is equal to xs with x inserted somewhere&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;◂&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt;) &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;where&lt;/span&gt;
  &lt;span class=&quot;varid&quot;&gt;here&lt;/span&gt;  &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; {&lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;}           &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;◂&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; ∷ &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;)
  &lt;span class=&quot;varid&quot;&gt;there&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; {&lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt;} {&lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;} {&lt;span class=&quot;varid&quot;&gt;xys&lt;/span&gt;} &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;◂&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xys&lt;/span&gt;) &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;◂&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; ∷ &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;) &lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; ∷ &lt;span class=&quot;varid&quot;&gt;xys&lt;/span&gt;)
&lt;/pre&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;comment&quot;&gt;-- Proof that a list is a permutation of another one&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;IsPermutation&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;where&lt;/span&gt;
  &lt;span class=&quot;listcon&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;listcon&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;IsPermutation&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;listcon&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;listcon&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;∷&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; {&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xys&lt;/span&gt;}
      &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;◂&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xys&lt;/span&gt;)
      &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;ps&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;IsPermutation&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt;)
      &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;IsPermutation&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; ∷ &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;) &lt;span class=&quot;varid&quot;&gt;xys&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;Now the &lt;tt&gt;&lt;span class=&quot;conid&quot;&gt;Sorted&lt;/span&gt;&lt;/tt&gt; data type has three components: the sorted list, a proof that it is sorted, and a proof that it is a permutation of the input. These parts are either all &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;listcon&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;listcon&quot;&gt;]&lt;/span&gt;&lt;/tt&gt;, or they are all &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;∷&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;/tt&gt;.
It turns out to be much nicer to combine the parts together,
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;comment&quot;&gt;-- Sorted permutations of a list&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Sorted&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Set&lt;/span&gt;  &lt;span class=&quot;keyword&quot;&gt;where&lt;/span&gt;
  &lt;span class=&quot;listcon&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;listcon&quot;&gt;]&lt;/span&gt;   &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Sorted&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;listcon&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;varid&quot;&gt;cons&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; {&lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xxs&lt;/span&gt;}
       &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;◂&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xxs&lt;/span&gt;) &lt;span class=&quot;comment&quot;&gt;-- inserting x somewhere into xs gives xxs&lt;/span&gt;
       &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;least&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤*&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;)  &lt;span class=&quot;comment&quot;&gt;-- x is the smallest element of the list&lt;/span&gt;
       &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Sorted&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;) &lt;span class=&quot;comment&quot;&gt;-- and we have also sorted xs&lt;/span&gt;
       &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Sorted&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xxs&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;Of course &lt;tt&gt;&lt;span class=&quot;conid&quot;&gt;Sorted&lt;/span&gt;&lt;/tt&gt; and &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;conid&quot;&gt;Sorted'&lt;/span&gt;&lt;/tt&gt; are equivalent.
&lt;/p&gt;&lt;p&gt;As an aside, these are all the ingredients necessary for proving
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;sorted-unique&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; {&lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;} &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;zs&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Sorted&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;)
              &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;sorted-to-List&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;sorted-to-List&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;zs&lt;/span&gt;
&lt;/pre&gt;&lt;h2&gt;&lt;a name=&quot;a-monad-for-keeping-track-of-the-runtime&quot;&gt;&lt;/a&gt;A monad for keeping track of the runtime &lt;/h2&gt;
&lt;p&gt;To be able to reason about the runtime, as measured in the number of comparisons performed, I decided to use a monad. The type is simply
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; (&lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Set&lt;/span&gt;) (&lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conop&quot;&gt;ℕ&lt;/span&gt;) &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;where&lt;/span&gt;
  &lt;span class=&quot;varid&quot;&gt;box&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;the constructor &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;box&lt;/span&gt;&lt;/tt&gt; is private, and it can only be accessed through the standard monad operations, 
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; {&lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;} &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;
&lt;div class=&quot;empty-line&quot;&gt;&lt;/div&gt;
&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; {&lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;B&lt;/span&gt;} {&lt;span class=&quot;varid&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;} &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;m&lt;/span&gt;) &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;m&lt;/span&gt;)
&lt;/pre&gt;&lt;p&gt;Then the sorting functions will be parametrized by a function that for some partial order decides between &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt;&lt;/tt&gt; and &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt;&lt;/tt&gt; in one step, using the monad we defined above:
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Sorting&lt;/span&gt;
    {&lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Set&lt;/span&gt;} {&lt;span class=&quot;varid&quot;&gt;l&lt;/span&gt;} {&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;≤&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Rel&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;l&lt;/span&gt;}
    (&lt;span class=&quot;varid&quot;&gt;isPartialOrder&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;IsPartialOrder&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;≤&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;) 
    (&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;≤?&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt;) &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;conop&quot;&gt;⊎&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt;) &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; &lt;span class=&quot;num&quot;&gt;1&lt;/span&gt;)
  &lt;span class=&quot;keyword&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;...&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;Note that I specify that &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;≤&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;/tt&gt; is a &lt;em&gt;partial&lt;/em&gt; order,
because the Agda standard library definition of a total order actually comes with a function
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt;) &lt;span class=&quot;conop&quot;&gt;⊎&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt;)
&lt;/pre&gt;&lt;p&gt;which would defeat the whole prupose of &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;≤?&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;/tt&gt;.
In fact, the standard &lt;tt&gt;&lt;span class=&quot;conid&quot;&gt;TotalOrder&lt;/span&gt;&lt;/tt&gt;s are decidable up to base equality, and if the base equality is propositional equality, then they are decidable. I.e.
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;total-decidable&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; {&lt;span class=&quot;varid&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;r&lt;/span&gt;} {&lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;a&lt;/span&gt;} &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;≤&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Rel&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;r&lt;/span&gt;)
                &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;IsTotalOrder&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;≤&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;
                &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;IsDecTotalOrder&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;≤&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;See the source for the proof of this side theorem. It relies on a trick to show that &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varid&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt;&lt;/tt&gt; can only be different from &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varid&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt;&lt;/tt&gt; if &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≢&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt;&lt;/tt&gt;. Which holds for propositional equality, but not in general.
&lt;/p&gt;&lt;h2&gt;&lt;a name=&quot;logarithms&quot;&gt;&lt;/a&gt;Logarithms &lt;/h2&gt;
&lt;p&gt;To be able to complete the specification of merge sort, we still need to add some missing functions on natural numbers. In particular, we need a logarithm.
This logarithm turns out to be surprisingly tricky to define in Agda.
Why? Because the usual definition uses non-structural recursion. In haskell you would write
&lt;/p&gt;&lt;pre class=&quot;haskell&quot;&gt;&lt;span class=&quot;comment&quot;&gt;-- @log n@ calculates ⌊log₂ (n+1)⌋&lt;/span&gt;
&lt;span class=&quot;varid&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;num&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;num&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;varid&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;num&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;log&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; `&lt;span class=&quot;varid&quot;&gt;div`&lt;/span&gt; &lt;span class=&quot;num&quot;&gt;2&lt;/span&gt;)
&lt;/pre&gt;&lt;p&gt;But Agda is not able to see that &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; `&lt;span class=&quot;varid&quot;&gt;div`&lt;/span&gt; &lt;span class=&quot;num&quot;&gt;2&lt;/span&gt;&lt;/tt&gt; (or in agda notation, &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varop&quot;&gt;⌊&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;/2⌋&lt;/span&gt;&lt;/tt&gt;) is smaller than &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;&lt;/tt&gt;.
There are two approaches to circumvent this problem:
&lt;/p&gt;&lt;ol&gt;&lt;li&gt; Use a different algorithm: Convert &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;&lt;/tt&gt; to a binary representation, and count the number of digits.&lt;/li&gt;
&lt;li&gt; Use well-founded recursion, manually supplying a proof that &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varop&quot;&gt;⌊&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;/2⌋&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;&lt;/tt&gt;.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;I went with the second option, because I will also be using the same shape of recursion inside merge sort itself.
The standard way to use well-founded recursion is through the function &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;keyglyph&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;varid&quot;&gt;rec&lt;/span&gt;&lt;/tt&gt;, which works a bit like &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;fix&lt;/span&gt;&lt;/tt&gt; in haskell, except that you need to pass in a proof that the argument is smaller. The code would look like this:
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;varid&quot;&gt;rec&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;log'&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;where&lt;/span&gt;
  &lt;span class=&quot;varid&quot;&gt;log′&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;self&lt;/span&gt; &lt;span class=&quot;num&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;num&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;varid&quot;&gt;log′&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;self&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;suc&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;) &lt;span class=&quot;keyglyph&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;num&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;self&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;⌊&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;suc&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;/2⌋&lt;/span&gt; (&lt;span class=&quot;comment&quot;&gt;{-proof ommitted-}&lt;/span&gt;)
&lt;/pre&gt;&lt;p&gt;But this leads to a problem as soon as you want to prove a property of logarithms. For example, you would think that &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varid&quot;&gt;log&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;suc&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;) &lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt; &lt;span class=&quot;num&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;+&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;⌊&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;suc&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;/2⌋&lt;/span&gt;)&lt;/tt&gt;. But that is not definitionally true, since one &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;keyglyph&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;varid&quot;&gt;rec&lt;/span&gt;&lt;/tt&gt; is not like another. I found that the well-founded recursion library was in general a pain to work with, especially because it uses so many type synonyms. My solution was to use the slightly lower level accessibility relation. A value of type &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;conid&quot;&gt;Acc&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;&amp;lt;′&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;&lt;/tt&gt; allows you to do recursion with any &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varid&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;&amp;lt;′&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;&lt;/tt&gt;. Now I can use actual recursion:
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;log-acc&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Acc&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;&amp;lt;′&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conop&quot;&gt;ℕ&lt;/span&gt;
&lt;span class=&quot;varid&quot;&gt;log-acc&lt;/span&gt; &lt;span class=&quot;num&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;num&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;varid&quot;&gt;log-acc&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;suc&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;) (&lt;span class=&quot;varid&quot;&gt;acc&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;more&lt;/span&gt;) &lt;span class=&quot;keyglyph&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;num&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;log-acc&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;⌊&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;suc&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;/2⌋&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;more&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;comment&quot;&gt;{-proof ommitted-}&lt;/span&gt;)
&lt;/pre&gt;&lt;p&gt;And use the well-foundedness of ℕ to get an &lt;tt&gt;&lt;span class=&quot;conid&quot;&gt;Acc&lt;/span&gt;&lt;/tt&gt; for any number:
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conop&quot;&gt;ℕ&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conop&quot;&gt;ℕ&lt;/span&gt;
&lt;span class=&quot;varid&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;log-acc&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; (&lt;span class=&quot;keyglyph&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;varid&quot;&gt;well-founded&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;)
&lt;div class=&quot;empty-line&quot;&gt;&lt;/div&gt;
&lt;span class=&quot;varop&quot;&gt;⌈log₂&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;⌉&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conop&quot;&gt;ℕ&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conop&quot;&gt;ℕ&lt;/span&gt;
&lt;span class=&quot;varop&quot;&gt;⌈log₂&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;⌉&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;log&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;pred&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;)
&lt;/pre&gt;&lt;p&gt;There is still a snag when proving properties of &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;log&lt;/span&gt;&lt;/tt&gt; or &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varid&quot;&gt;log-acc&lt;/span&gt;&lt;/tt&gt;, namely that you need to prove that &lt;tt class=&quot;complex&quot;&gt;(&lt;span class=&quot;varid&quot;&gt;more&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;...&lt;/span&gt;) &lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;varid&quot;&gt;well-founded&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;&lt;/tt&gt;. But the accessibility relation doesn't actually matter for the computation, so I decided to just postulate
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;postulate&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;acc-irrelevance&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; {&lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conop&quot;&gt;ℕ&lt;/span&gt;} &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; {&lt;span class=&quot;varid&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Acc&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;&amp;lt;′&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;} &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;b&lt;/span&gt;
 &lt;span class=&quot;comment&quot;&gt;-- this also follows from function extensionality&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;If anyone knows a better way to prove properties of functions defined with well-founded recursion, I am open to suggestions.
&lt;/p&gt;&lt;h2&gt;&lt;a name=&quot;vectors-versus-lists&quot;&gt;&lt;/a&gt;Vectors versus lists &lt;/h2&gt;
&lt;p&gt;While working on the proofs I had to choose: Do I use fixed length &lt;tt&gt;&lt;span class=&quot;conid&quot;&gt;Vec&lt;/span&gt;&lt;/tt&gt;s or variable length &lt;tt&gt;&lt;span class=&quot;conid&quot;&gt;List&lt;/span&gt;&lt;/tt&gt;s? Both have their pros and cons.
&lt;/p&gt;&lt;p&gt;On the one hand, the sorting functions with vectors look a bit nicer, because we can use &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;&lt;/tt&gt; instead of &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varid&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;&lt;/tt&gt;:
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;merge-sort&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; {&lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;} (&lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Vec&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;) &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Sorted&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;⌈log₂&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;⌉&lt;/span&gt;)
&lt;/pre&gt;&lt;p&gt;Additionally, with lists we can only do recursion on the input list, with vectors we can do recursion on the length of the list. The former works fine for insertion sort, where in each step you do something with the head element of the list; but it fails for selection and merge sort.
&lt;/p&gt;&lt;p&gt;On the other hand, with vectors you sometimes can't even &lt;em&gt;state&lt;/em&gt; the property that one vector is equal to another.
For the term &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;zs&lt;/span&gt;&lt;/tt&gt; to be well-typed, &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;&lt;/tt&gt; must have the type &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;conid&quot;&gt;Vec&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;)&lt;/tt&gt;.
&lt;/p&gt;&lt;p&gt;I went back and forth a couple of times between vectors and lists.
In the end I settled for using vectors only when needed, and specifying properties in terms of lists.
For example the split function for merge sort has the type
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;splitHalf&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; {&lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;} &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Vec&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;)
          &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; ∃₂ &lt;span class=&quot;keyglyph&quot;&gt;\&lt;/span&gt;(&lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Vec&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;⌈&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;/2⌉&lt;/span&gt;) (&lt;span class=&quot;varid&quot;&gt;zs&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Vec&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;⌊&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;/2⌋&lt;/span&gt;)
               &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;toList&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;toList&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;zs&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;toList&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;So instead of using &lt;tt class=&quot;complex&quot;&gt;Vec&lt;span class=&quot;varop&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;/tt&gt;, I use &lt;tt class=&quot;complex&quot;&gt;List&lt;span class=&quot;varop&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;/tt&gt;.
In this style 'select' from selection sort looks like
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;select&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; {&lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;} (&lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Vec&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;suc&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;))
       &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (∃₂ &lt;span class=&quot;keyglyph&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;◂&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;toList&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;toList&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;) × (&lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤*&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;toList&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt;)) &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;I.e. given a &lt;em&gt;vector&lt;/em&gt; &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;&lt;/tt&gt; with &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varid&quot;&gt;n+1&lt;/span&gt;&lt;/tt&gt; elements, return a vector &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;ys&lt;/span&gt;&lt;/tt&gt; with &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;&lt;/tt&gt; elements, such that inserting &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt;&lt;/tt&gt; into it gives us back &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;&lt;/tt&gt;. And this item &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt;&lt;/tt&gt; should be the smallest one.
&lt;/p&gt;&lt;h2&gt;&lt;a name=&quot;extension-expected-runtime&quot;&gt;&lt;/a&gt;Extension: expected runtime &lt;/h2&gt;
&lt;p&gt;An extension of this post would be to look at randomized sorting algorithms. In particular, quick sort with a randomly chosen pivot has expected runtime &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;conid&quot;&gt;O&lt;/span&gt;(&lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;)&lt;/tt&gt;. At first I thought that all that would be needed is a function
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;expected&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; {&lt;span class=&quot;conid&quot;&gt;P&lt;/span&gt;}
         &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;ns&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;conop&quot;&gt;ℕ&lt;/span&gt;)             &lt;span class=&quot;comment&quot;&gt;-- A list of numbers&lt;/span&gt;
         &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;All&lt;/span&gt; (&lt;span class=&quot;keyglyph&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;P&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;) &lt;span class=&quot;varid&quot;&gt;ns&lt;/span&gt; &lt;span class=&quot;comment&quot;&gt;-- for each n we have P in-time n&lt;/span&gt;
         &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;P&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;⌈mean&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;ns&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;⌉&lt;/span&gt;      &lt;span class=&quot;comment&quot;&gt;-- then expect time is mean of ns&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;But that is not quite right, since if we actually knew the runtimes &lt;tt&gt;&lt;span class=&quot;varid&quot;&gt;ns&lt;/span&gt;&lt;/tt&gt; we could just pick the fastest one.
With the randomized quicksort you will end up in a situation where you have two or more computations to choose from, and you know that some are faster than the others, but you don't yet know which one. That sounds a bit classical. A second idea is to return the runtimes at a later time, something like
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;expected&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; {&lt;span class=&quot;conid&quot;&gt;P&lt;/span&gt;} {&lt;span class=&quot;varid&quot;&gt;long-time&lt;/span&gt;}
         &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;List&lt;/span&gt; (&lt;span class=&quot;keyglyph&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;varid&quot;&gt;ex&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;P&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;) &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;long-time&lt;/span&gt;)
         &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;P&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;⌈mean&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;proj1&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;⌉&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;But this is not quite right either, since after &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varid&quot;&gt;long-time&lt;/span&gt;&lt;/tt&gt; computing &lt;tt&gt;&lt;span class=&quot;conid&quot;&gt;P&lt;/span&gt;&lt;/tt&gt; (i.e. a sorting) can be done in 0 time.
Rather, we need to decouple the proof about the runtime from the computation.
This is not possible with the &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;/tt&gt; monad. We would need to get rid of the runtime from the type, and store it as a value instead.
&lt;/p&gt;&lt;p&gt;I have tried redoing the proofs in this post with the monad
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Timed&lt;/span&gt; (&lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Set&lt;/span&gt;) &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;where&lt;/span&gt;
  &lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt;&lt;span class=&quot;keyglyph&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conop&quot;&gt;ℕ&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Timed&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt;
&lt;span class=&quot;varid&quot;&gt;runtime&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Timed&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conop&quot;&gt;ℕ&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;But I didn't succeed; I ended up with the baffling error message 
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;runtime&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;big-lambda-term&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;unbox&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤?&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;u&lt;/span&gt;)))
&lt;span class=&quot;varop&quot;&gt;!=&lt;/span&gt;
&lt;span class=&quot;varid&quot;&gt;runtime&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;big-lambda-term&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;unbox&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤?&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;u&lt;/span&gt;)))
&lt;/pre&gt;&lt;h2&gt;&lt;a name=&quot;another-extension-lower-bound-on-runtime&quot;&gt;&lt;/a&gt;Another extension: lower bound on runtime &lt;/h2&gt;
&lt;p&gt;So far I have proved that you can sort a list in time &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;n&lt;/span&gt;&lt;/tt&gt;. 
It would also be interesting to look at the well known &lt;a href=&quot;http://planetmath.org/LowerBoundForSorting&quot;&gt;lower bound on the runtime of sorting&lt;/a&gt;, and prove a theorem such as
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;can't-sort-in-linear-time&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; ¬ ∃ &lt;span class=&quot;keyglyph&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;varid&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Sorted&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;unfortunately this statement is not actually true for all types. For finite sets you actually &lt;em&gt;can&lt;/em&gt; sort in linear time with counting sort.
It also fails if we happen to have some decidable total order for that type lying around. But it might be possible to prove
&lt;/p&gt;&lt;pre class=&quot;agda&quot;&gt;&lt;span class=&quot;varid&quot;&gt;can't-sort-in-linear-time&lt;/span&gt;
  &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;no-fast-compare&lt;/span&gt; &lt;span class=&quot;listcon&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; (&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;conop&quot;&gt;⊎&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt;) &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; &lt;span class=&quot;num&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt;)
  &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; ¬ ∃ &lt;span class=&quot;keyglyph&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;varid&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;conid&quot;&gt;Sorted&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;xs&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;But you have to be really careful with a term like &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;varid&quot;&gt;no-fast-compare&lt;/span&gt;&lt;/tt&gt;, because inside the runtime monad we do have values of type &lt;tt class=&quot;complex&quot;&gt;(&lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;conop&quot;&gt;⊎&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≤&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt;)&lt;/tt&gt;. And so you can derive &lt;tt class=&quot;complex&quot;&gt;&lt;span class=&quot;keyglyph&quot;&gt;∀&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;keyglyph&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;≡&lt;/span&gt; &lt;span class=&quot;varid&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; &lt;span class=&quot;num&quot;&gt;1&lt;/span&gt;&lt;/tt&gt;, and therefore also &lt;tt class=&quot;complex&quot;&gt;⊥ &lt;span class=&quot;varop&quot;&gt;in-time&lt;/span&gt; &lt;span class=&quot;num&quot;&gt;1&lt;/span&gt;&lt;/tt&gt; for non trivial types. Which certainly looks wrong to me.
&lt;/p&gt;&lt;p&gt;I don't know a way around this problem, but it might be related to the same issue as expected runtime.
I.e. the problem is that all information about the runtime is bundled together with the return value.
The lower bound proof essentially asks to sort a 'random' list, and by a counting argument shows that at least a certain number of comparisons are needed to be able to produce all outputs.
&lt;/p&gt;</description>
	<pubDate>Thu, 23 May 2013 12:43:33 +0000</pubDate>
</item>
<item>
	<title>wren ng thornton: Upcoming talk</title>
	<guid isPermaLink="false">tag:dreamwidth.org,2010-05-25:518115:83774</guid>
	<link>http://winterkoninkje.dreamwidth.org/83774.html</link>
	<description>&lt;p&gt;Next month I'll be giving a talk at the &lt;a href=&quot;http://www.indiana.edu/~iulg/nlcs.html&quot;&gt;NLCS&lt;/a&gt; workshop, on the chiastic lambda-calculi I first presented at NASSLLI 2010 (&lt;a href=&quot;http://llama.freegeek.org/~wren/pubs/ccgjp_nasslli2010.pdf&quot;&gt;slides&lt;/a&gt;[1]). After working out some of the metatheory for one of my quals, I gave more recent talks at our local PL Wonks and CLingDing seminars (&lt;a href=&quot;http://llama.freegeek.org/~wren/pubs/chiastic_plwonks2013.pdf&quot;&gt;slides&lt;/a&gt;). The NASSLLI talk was more about the linguistic motivations and the general idea, whereas the PLWonks/CLingDing talks were more about the formal properties of the calculus itself. For NLCS I hope to combine these threads a bit better— which has always been the challenge with this work.&lt;/p&gt;

&lt;p&gt;NLCS is collocated with this year's &lt;a href=&quot;http://lii.rwth-aachen.de/lics/lics13/&quot;&gt;LICS&lt;/a&gt; (and MFPS and CSF). I'll also be around for LICS itself, and in town for MFPS though probably not attending. So if you're around, feel free to stop by and chat.&lt;/p&gt;

&lt;p&gt;[1] N.B., the NASSLLI syntax is a bit different than the newer version: square brackets were used instead of angle brackets (the latter were chosen because they typeset better in general); juxtaposition was just juxtaposition rather than being made explicit; and the left- vs right-chiastic distinction was called chi vs ksi (however, it turns out that ksi already has an important meaning in type theory).&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src=&quot;http://www.dreamwidth.org/tools/commentcount?user=winterkoninkje&amp;amp;ditemid=83774&quot; alt=&quot;comment count unavailable&quot; height=&quot;12&quot; style=&quot;vertical-align: middle;&quot; width=&quot;30&quot; /&gt; comments</description>
	<pubDate>Mon, 20 May 2013 22:11:45 +0000</pubDate>
</item>
<item>
	<title>language-puppet: Incoming: Ruby bridge</title>
	<guid isPermaLink="false">http://lpuppet.banquise.net/blog/2013/05/20/incoming-ruby-bridge</guid>
	<link>http://lpuppet.banquise.net/blog/2013/05/20/incoming-ruby-bridge/</link>
	<description>&lt;p&gt;I am working on a quick and dirty Ruby bridge &lt;a href=&quot;https://github.com/bartavelle/hruby&quot;&gt;library&lt;/a&gt;, that I hope will yield a huge performance gain with template interpolation in the language-puppet library. Right now, it is capable of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initializing a Ruby interpreter from libruby&lt;/li&gt;
&lt;li&gt;Calling Ruby methods and functions&lt;/li&gt;
&lt;li&gt;Registering methods or functions that will be called from Ruby code&lt;/li&gt;
&lt;li&gt;Converting data between the two Worlds (right now the most complex instance is the JSON one, which means that many complex Ruby types can’t be converted, but it is more than enough for passing data)&lt;/li&gt;
&lt;li&gt;Embedding native Haskell values that can be passed around in Ruby to the Haskell-provided external functions (I will use this for passing the Puppet catalog state around)&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;There are still a few things to do before releasing it :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Making compilation a bit less dependant on the system. This will probably require quite a few flags in the cabal definition …&lt;/li&gt;
&lt;li&gt;Hunting for memory leaks. I am not sure how to do this with the GHC Runtime in the middle, and I do hope that &lt;em&gt;ruby_finalize&lt;/em&gt; frees everything that is managed by the Ruby runtime. After all, restarting processes seems to be the only working garbage collection method for Ruby daemons …&lt;/li&gt;
&lt;li&gt;Writing stubs for the Puppet library methods that might be needed by templates. I would like to be able to support custom types and functions directly written in Ruby instead of Lua, but this will probably turn into a nightmare …&lt;/li&gt;
&lt;li&gt;Cleaning things up !&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Here is a quick code preview :&lt;/p&gt;

&amp;lt;figure class=&quot;code&quot;&amp;gt;&amp;lt;figcaption&amp;gt;&lt;span&gt;test.hs &lt;/span&gt;&amp;lt;/figcaption&amp;gt;
 &lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;14&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;15&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;16&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;17&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;18&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;19&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;21&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;22&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;23&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;24&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;25&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;26&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;27&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;28&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;29&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;30&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;31&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;32&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;33&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;34&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;35&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;36&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;37&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;38&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;39&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;40&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;41&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;42&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;43&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;44&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;45&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;46&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;47&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;48&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;49&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;50&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;51&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;52&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;53&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;hs&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;cm&quot;&gt;{-# LANGUAGE OverloadedStrings, OverloadedStrings #-}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;kr&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Main&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Foreign.Ruby.Bindings&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Data.Aeson&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Data.Attoparsec.Number&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;c1&quot;&gt;-- this is an external function that will be executed from the Ruby interpreter&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;c1&quot;&gt;-- the first parameter to the function is probably some reference to some top object&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;c1&quot;&gt;-- my knowledge of ruby is close to nonexistent, so I can't say for sure ...&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;nf&quot;&gt;extfunc&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;RValue&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;RValue&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;RValue&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;nf&quot;&gt;extfunc&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;-- deserialize the Ruby value into some JSON Value&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;onv&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fromRuby&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;-- and display it&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;onv&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;-- now let's create a JSON object containing all kind of data types&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;kr&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nv&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;bigint&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Number&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;I&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16518656116889898998656112323135664684684&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                    &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;int&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Number&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;I&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                    &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;double&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Number&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;D&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.123&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                    &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;null&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Null&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                    &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;string&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;string&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                    &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Bool&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                    &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Bool&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                    &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;array&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;toJSON&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                    &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;object&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;k&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;v&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                    &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;-- turn it into Ruby values, and return this&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;toRuby&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nv&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;c1&quot;&gt;-- this is the function that is called if everything was loaded properly&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;nf&quot;&gt;nextThings&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;nf&quot;&gt;nextThings&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;-- turn the extfunc function into something that can be called by the Ruby interpreter&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;myfunc&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mkRegistered2&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;extfunc&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;-- and bind it to the global 'hsfunction' function&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;rb_define_global_function&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;hsfunction&quot;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;myfunc&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;-- now call a method in the Ruby interpreter&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;safeMethodCall&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;MyClass&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;testfunc&quot;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;[]&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;kr&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;of&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;kt&quot;&gt;Right&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fromRuby&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;kt&quot;&gt;Left&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;-- initialize stuff&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;ruby_init&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;ruby_init_loadpath&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;-- and load &quot;test.rb&quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rb_load_protect&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;test.rb&quot;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;kr&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;kr&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nextThings&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;kr&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;showError&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;putStrLn&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&amp;lt;/figure&amp;gt;


&lt;p&gt;And here is the ruby program, that calls our external function :&lt;/p&gt;

&amp;lt;figure class=&quot;code&quot;&amp;gt;&amp;lt;figcaption&amp;gt;&lt;span&gt;test.rb &lt;/span&gt;&amp;lt;/figcaption&amp;gt;
 &lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;14&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;15&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;rb&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyClass&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;testfunc&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;n&quot;&gt;hsfunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16588&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                    &lt;span class=&quot;s2&quot;&gt;&quot;qsqsd&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                    &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'a'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'b'&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                    &lt;span class=&quot;ss&quot;&gt;:symbol&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                    &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;432&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                    &lt;span class=&quot;mi&quot;&gt;5611561561186918918918618789115616591891198189123165165889&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;            &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; =&amp;gt; &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;]&quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&amp;lt;/figure&amp;gt;


&lt;p&gt;And the output, showing that data is properly converted from either sides :&lt;/p&gt;

&amp;lt;figure class=&quot;code&quot;&amp;gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;&quot;&gt;&lt;span class=&quot;line&quot;&gt;Just (Array (fromList [Number 16588,String &quot;qsqsd&quot;,Bool True,Object fromList [(&quot;a&quot;,String &quot;b&quot;)],String &quot;symbol&quot;,Number 0.432,Number 5611561561186918918918618789115616591891198189123165165889]))
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;bigint =&amp;gt; 16518656116889898998656112323135664684684 [Bignum]
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;int =&amp;gt; 12 [Fixnum]
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;double =&amp;gt; 0.123 [Float]
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;array =&amp;gt; 12345 [Array]
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;true =&amp;gt; true [TrueClass]
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;null =&amp;gt; Null [String]
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;string =&amp;gt; string [String]
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;object =&amp;gt; kv [Hash]
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;false =&amp;gt; false [FalseClass]
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Just (Number 12)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&amp;lt;/figure&amp;gt;


&lt;p&gt;EDIT: added link to the code.&lt;/p&gt;</description>
	<pubDate>Mon, 20 May 2013 09:35:00 +0000</pubDate>
</item>
<item>
	<title>Kevin Reid (kpreid): Game idea: reverse bullet hell</title>
	<guid isPermaLink="true">http://kpreid.livejournal.com/50670.html</guid>
	<link>http://kpreid.livejournal.com/50670.html</link>
	<description>&lt;p&gt;I have come to realize that I have more ideas for programs than I'll ever have time to write. (This means they're not actually all that significant, on average — see all that's been said on ‘ideas vs. execution’.) But maybe I have the time to scribble a blog post about them, and that's stuff to blog about, if nothing else. 

&lt;/p&gt;&lt;p&gt;So, a video game idea I had today: reverse bullet-hell shooter.

&lt;/p&gt;&lt;p&gt;A regular bullet-hell shooter is a game where you move in a 2D space dodging an immense number of mostly dumb instant-death projectiles launched in mostly predefined patterns, and trying to shoot back with dinkier, but better aimed, weapons. Instead, here you design the bullet pattern so as to trap and kill AI enemies doing the dodging.

&lt;/p&gt;&lt;p&gt;The roles seem a bit similar to tower defense, but the space of strategies would be considerably more, ah, bumpy, since you're not doing a little bit of damage at a time and how it plays out depends strongly on the AI's choices.

&lt;/p&gt;&lt;p&gt;That's probably the downfall of this idea: either the outcome is basically &lt;a href=&quot;http://en.wikipedia.org/wiki/Butterfly_effect&quot; rel=&quot;nofollow&quot;&gt;butterfly effect&lt;/a&gt; random due to enemy AI decisions and you mostly lose, or there are trivial ways to design undodgeable bullet patterns and you mostly win. I don't immediately see how to make the space of inputs and outcomes “smooth” enough.&lt;/p&gt;</description>
	<pubDate>Mon, 20 May 2013 04:56:02 +0000</pubDate>
	<author>kpreid@switchb.org (Kevin Reid (kpreid))</author>
</item>
<item>
	<title>Edward Z. Yang: Anatomy of an MVar operation</title>
	<guid isPermaLink="false">http://blog.ezyang.com/?p=8282</guid>
	<link>http://feedproxy.google.com/~r/ezyang/~3/nmmQFXNcDMg/</link>
	<description>&lt;div class=&quot;document&quot;&gt;



&lt;p&gt;Adam Belay (of &lt;a href=&quot;http://dune.scs.stanford.edu/&quot; class=&quot;reference external&quot;&gt;Dune&lt;/a&gt; fame) was recently wondering why Haskell’s MVars are so slow. “Slow?” I thought, “aren’t Haskell’s MVars supposed to be really fast?” So I did some digging around how MVars worked, to see if I could explain.&lt;/p&gt;
&lt;p&gt;Let’s consider the operation of the function &lt;tt class=&quot;docutils literal&quot;&gt;takeMVar&lt;/tt&gt; in &lt;a href=&quot;http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Concurrent-MVar.html#v:takeMVar&quot; class=&quot;reference external&quot;&gt;Control.Concurrent.MVar&lt;/a&gt;.  This function is very simple, it unpacks &lt;tt class=&quot;docutils literal&quot;&gt;MVar&lt;/tt&gt; to get the underlying &lt;tt class=&quot;docutils literal&quot;&gt;MVar#&lt;/tt&gt; primitive value, and then calls the primop &lt;tt class=&quot;docutils literal&quot;&gt;takeMVar#&lt;/tt&gt;:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;takeMVar :: MVar a -&amp;gt; IO a
takeMVar (MVar mvar#) = IO $ \ s# -&amp;gt; takeMVar# mvar# s#
&lt;/pre&gt;
&lt;p&gt;&lt;a href=&quot;http://hackage.haskell.org/trac/ghc/wiki/Commentary/PrimOps&quot; class=&quot;reference external&quot;&gt;Primops&lt;/a&gt; result in the invocation of &lt;tt class=&quot;docutils literal&quot;&gt;stg_takeMVarzh&lt;/tt&gt; in &lt;tt class=&quot;docutils literal&quot;&gt;PrimOps.cmm&lt;/tt&gt;, which is where the magic happens. For simplicity, we consider only the &lt;em&gt;multithreaded&lt;/em&gt; case.&lt;/p&gt;
&lt;p&gt;The first step is to &lt;strong&gt;lock the closure&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;(&quot;ptr&quot; info) = ccall lockClosure(mvar &quot;ptr&quot;);
&lt;/pre&gt;
&lt;p&gt;Objects on the GHC heap have an &lt;em&gt;info table header&lt;/em&gt; which indicates what kind of object they are, by pointing to the relevant info table for the object.  These headers are &lt;em&gt;also&lt;/em&gt; used for synchronization: since they are word-sized, they can be atomically swapped for other values. &lt;tt class=&quot;docutils literal&quot;&gt;lockClosure&lt;/tt&gt; is in fact a spin-lock on the info table header:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;EXTERN_INLINE StgInfoTable *lockClosure(StgClosure *p)
{
    StgWord info;
    do {
        nat i = 0;
        do {
            info = xchg((P_)(void *)&amp;amp;p-&amp;gt;header.info, (W_)&amp;amp;stg_WHITEHOLE_info);
            if (info != (W_)&amp;amp;stg_WHITEHOLE_info) return (StgInfoTable *)info;
        } while (++i &amp;lt; SPIN_COUNT);
        yieldThread();
    } while (1);
}
&lt;/pre&gt;
&lt;p&gt;&lt;tt class=&quot;docutils literal&quot;&gt;lockClosure&lt;/tt&gt; is used for some other objects, namely thread state objects (&lt;tt class=&quot;docutils literal&quot;&gt;stg_TSO_info&lt;/tt&gt;, via &lt;tt class=&quot;docutils literal&quot;&gt;lockTSO&lt;/tt&gt;) and thread messages i.e. exceptions (&lt;tt class=&quot;docutils literal&quot;&gt;stg_MSG_THROWTO_info&lt;/tt&gt;, &lt;tt class=&quot;docutils literal&quot;&gt;stg_MSG_NULL_info&lt;/tt&gt;).&lt;/p&gt;
&lt;p&gt;The next step is to &lt;strong&gt;apply a GC write barrier on the MVar&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;if (info == stg_MVAR_CLEAN_info) {
    ccall dirty_MVAR(BaseReg &quot;ptr&quot;, mvar &quot;ptr&quot;);
}
&lt;/pre&gt;
&lt;p&gt;As I’ve &lt;a href=&quot;http://blog.ezyang.com/2013/01/the-ghc-scheduler/&quot; class=&quot;reference external&quot;&gt;written before&lt;/a&gt;, as the MVar is a mutable object, it can be mutated to point to objects in generation 0; thus, when a mutation happens, it has to be added to the root set via the mutable list. Since mutable is per capability, this boils down into a bunch of pointer modifications, and does not require any synchronizations. Note that we will need to add the MVar to the mutable list, &lt;em&gt;even&lt;/em&gt; if we end up blocking on it, because the MVar is a retainer of the &lt;em&gt;thread&lt;/em&gt; (TSO) which is blocked on it! (However, I suspect in some cases we can get away with not doing this.)&lt;/p&gt;
&lt;p&gt;Next, we case split depending on whether or not the MVar is full or empty.  If the MVar is empty, we need to &lt;strong&gt;block the thread until the MVar is full&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;/* If the MVar is empty, put ourselves on its blocking queue,
 * and wait until we're woken up.
 */
if (StgMVar_value(mvar) == stg_END_TSO_QUEUE_closure) {

    // We want to put the heap check down here in the slow path,
    // but be careful to unlock the closure before returning to
    // the RTS if the check fails.
    ALLOC_PRIM_WITH_CUSTOM_FAILURE
        (SIZEOF_StgMVarTSOQueue,
         unlockClosure(mvar, stg_MVAR_DIRTY_info);
         GC_PRIM_P(stg_takeMVarzh, mvar));

    q = Hp - SIZEOF_StgMVarTSOQueue + WDS(1);

    SET_HDR(q, stg_MVAR_TSO_QUEUE_info, CCS_SYSTEM);
    StgMVarTSOQueue_link(q) = END_TSO_QUEUE;
    StgMVarTSOQueue_tso(q)  = CurrentTSO;

    if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
        StgMVar_head(mvar) = q;
    } else {
        StgMVarTSOQueue_link(StgMVar_tail(mvar)) = q;
        ccall recordClosureMutated(MyCapability() &quot;ptr&quot;,
                                         StgMVar_tail(mvar));
    }
    StgTSO__link(CurrentTSO)       = q;
    StgTSO_block_info(CurrentTSO)  = mvar;
    StgTSO_why_blocked(CurrentTSO) = BlockedOnMVar::I16;
    StgMVar_tail(mvar)             = q;

    jump stg_block_takemvar(mvar);
}
&lt;/pre&gt;
&lt;p&gt;A useful thing to know when decoding C-- primop code is that &lt;tt class=&quot;docutils literal&quot;&gt;&lt;span class=&quot;pre&quot;&gt;StgTSO_block_info(...)&lt;/span&gt;&lt;/tt&gt; and its kin are how we spell field access on objects. C-- doesn’t know anything about C struct layout, and so these “functions” are actually macros generated by &lt;tt class=&quot;docutils literal&quot;&gt;utils/deriveConstants&lt;/tt&gt;. Blocking a thread consists of three steps:&lt;/p&gt;
&lt;ol class=&quot;arabic simple&quot;&gt;
&lt;li&gt;We have to add the thread to the blocked queue attached to the MVar (that’s why blocking on an MVar mutates the MVar!) This involves performing a heap allocation for the linked list node as well as mutating the tail of the old linked list.&lt;/li&gt;
&lt;li&gt;We have to mark the thread as blocked (the &lt;tt class=&quot;docutils literal&quot;&gt;StgTSO&lt;/tt&gt; modifications).&lt;/li&gt;
&lt;li&gt;We need to setup a stack frame for the thread so that when the thread wakes up, it performs the correct action (the invocation to &lt;tt class=&quot;docutils literal&quot;&gt;stg_block_takemvar&lt;/tt&gt;). This invocation is also responsible for unlocking the closure. While the machinery here is pretty intricate, it’s not really in scope for this blog post.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If the MVar is full, then we can go ahead and &lt;strong&gt;take the value from the MVar.&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;/* we got the value... */
val = StgMVar_value(mvar);
&lt;/pre&gt;
&lt;p&gt;But that’s not all. If there are other blocked &lt;tt class=&quot;docutils literal&quot;&gt;putMVars&lt;/tt&gt; on the MVar (remember, when a thread attempts to put an MVar that is already full, it blocks until the MVar empties out), then we should immediately unblock one of these threads so that the MVar can always be left in a full state:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;    q = StgMVar_head(mvar);
loop:
    if (q == stg_END_TSO_QUEUE_closure) {
        /* No further putMVars, MVar is now empty */
        StgMVar_value(mvar) = stg_END_TSO_QUEUE_closure;
        unlockClosure(mvar, stg_MVAR_DIRTY_info);
        return (val);
    }
    if (StgHeader_info(q) == stg_IND_info ||
        StgHeader_info(q) == stg_MSG_NULL_info) {
        q = StgInd_indirectee(q);
        goto loop;
    }
&lt;/pre&gt;
&lt;p&gt;There is one interesting thing about the code that checks for blocked threads, and that is the check for &lt;em&gt;indirectees&lt;/em&gt; (&lt;tt class=&quot;docutils literal&quot;&gt;stg_IND_info&lt;/tt&gt;). Under what circumstances would a queue object be stubbed out with an indirection? As it turns out, this occurs when we &lt;em&gt;delete&lt;/em&gt; an item from the linked list. This is quite nice, because on a singly-linked list, we don't have an easy way to delete items unless we also have a pointer to the previous item. With this scheme, we just overwrite out the current item with an indirection, to be cleaned up next GC. (This, by the way, is why we can't just chain up the TSOs directly, without the extra linked list nodes. [1])&lt;/p&gt;
&lt;p&gt;When we find some other threads, we immediately run them, so that the MVar never becomes empty:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;// There are putMVar(s) waiting... wake up the first thread on the queue

tso = StgMVarTSOQueue_tso(q);
StgMVar_head(mvar) = StgMVarTSOQueue_link(q);
if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
    StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
}

ASSERT(StgTSO_why_blocked(tso) == BlockedOnMVar::I16); // note: I16 means this is a 16-bit integer
ASSERT(StgTSO_block_info(tso) == mvar);

// actually perform the putMVar for the thread that we just woke up
W_ stack;
stack = StgTSO_stackobj(tso);
PerformPut(stack, StgMVar_value(mvar));
&lt;/pre&gt;
&lt;p&gt;There is one detail here: &lt;tt class=&quot;docutils literal&quot;&gt;PerformPut&lt;/tt&gt; doesn’t actually run the thread, it just looks at the thread’s stack to figure out what it was &lt;em&gt;going&lt;/em&gt; to put. Once the MVar is put, we then wake up the thread, so it can go on its merry way:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;// indicate that the MVar operation has now completed.
StgTSO__link(tso) = stg_END_TSO_QUEUE_closure;

// no need to mark the TSO dirty, we have only written END_TSO_QUEUE.

ccall tryWakeupThread(MyCapability() &quot;ptr&quot;, tso);

unlockClosure(mvar, stg_MVAR_DIRTY_info);
return (val);
&lt;/pre&gt;
&lt;p&gt;To sum up, when you &lt;tt class=&quot;docutils literal&quot;&gt;takeMVar&lt;/tt&gt;, you pay the costs of:&lt;/p&gt;
&lt;ul class=&quot;simple&quot;&gt;
&lt;li&gt;one spinlock,&lt;/li&gt;
&lt;li&gt;on order of several dozen memory operations (write barriers, queue twiddling), and&lt;/li&gt;
&lt;li&gt;when the MVar is empty, a (small) heap allocation and stack write.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Adam and I puzzled about this a bit, and then realized the reason why the number of cycles was so large: our numbers are for &lt;em&gt;roundtrips&lt;/em&gt;, and even with such lightweight synchronization (and lack of syscalls), you still have to go through the scheduler when all is said and done, and that blows up the number of cycles.&lt;/p&gt;
&lt;hr class=&quot;docutils&quot; /&gt;
&lt;p&gt;[1] It wasn’t always this way, see:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;commit f4692220c7cbdadaa633f50eb2b30b59edb30183
Author: Simon Marlow &amp;lt;marlowsd@gmail.com&amp;gt;
Date:   Thu Apr 1 09:16:05 2010 +0000

    Change the representation of the MVar blocked queue

    The list of threads blocked on an MVar is now represented as a list of
    separately allocated objects rather than being linked through the TSOs
    themselves.  This lets us remove a TSO from the list in O(1) time
    rather than O(n) time, by marking the list object.  Removing this
    linear component fixes some pathalogical performance cases where many
    threads were blocked on an MVar and became unreachable simultaneously
    (nofib/smp/threads007), or when sending an asynchronous exception to a
    TSO in a long list of thread blocked on an MVar.

    MVar performance has actually improved by a few percent as a result of
    this change, slightly to my surprise.

    This is the final cleanup in the sequence, which let me remove the old
    way of waking up threads (unblockOne(), MSG_WAKEUP) in favour of the
    new way (tryWakeupThread and MSG_TRY_WAKEUP, which is idempotent).  It
    is now the case that only the Capability that owns a TSO may modify
    its state (well, almost), and this simplifies various things.  More of
    the RTS is based on message-passing between Capabilities now.
&lt;/pre&gt;
&lt;/div&gt;
&lt;img src=&quot;http://feeds.feedburner.com/~r/ezyang/~4/nmmQFXNcDMg&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Mon, 20 May 2013 00:00:37 +0000</pubDate>
</item>
<item>
	<title>Alessandro Vermeulen: Why you should switch to declarative programming</title>
	<guid isPermaLink="false">http://alessandrovermeulen.me/2013/05/19/why-you-should-switch-to-declarative-programming</guid>
	<link>http://alessandrovermeulen.me/2013/05/19/why-you-should-switch-to-declarative-programming/</link>
	<description>&lt;p&gt;We are reaching limits of what is feasible with imperative languages and we should move to declarative languages. &lt;/p&gt;

&lt;p&gt;When applications written in imperative languages grow, the code becomes convoluted. Why? Imperatively programmed applications contain statements such as &lt;code&gt;if X do Y else do Z&lt;/code&gt;. As &lt;code&gt;Y&lt;/code&gt; and &lt;code&gt;Z&lt;/code&gt; contain &lt;em&gt;invisible&lt;/em&gt; side-effects the correctness of the program relies on some implicit invariant. This invariant has to be maintained by the programmer or else the code will break. Thus each time a new feature is added to an application or a bug is fixed the code for the application gets more complex as keeping the invariant intact becomes harder. After a while the code becomes spaghetti-code and bugs are introduced as the programmer fails to maintain the invariant. This is going to happen despite the best intentions of the programmer to keep things clean. Why is this? &lt;/p&gt;



&lt;p&gt;An imperative language is a type of language that tells the computer what to do and in which order. However, most, if not all, applications are nothing but a translation of some business domain into a computer program. In order to get the imperative code the programmer has to translate the business model to a set of imperative instructions, the business logic. The imperative instructions bear little resemblance to the original description of the business model. When the business model changes the imperative counterpart could change entirely but what happens is that programmers make incremental updates to the code. This is done because either they do not see that a more drastic change is necessary or because they are under pressure to deliver results. Over time this leads to bugs and unmaintainable code. Summarized, bugs are introduced because there is a manual translation step between the business model and the program code.&lt;/p&gt;

&lt;p&gt;Imagine a system for calculating whether a person should receive a certain allowance. To receive the allowance a person has to meet several criteria such as &lt;code&gt;age &amp;gt; 18 and income &amp;lt; 2400&lt;/code&gt;. We can denote this in the following way:&lt;/p&gt;

&lt;div class=&quot;bogus-wrapper&quot;&gt;&amp;lt;notextile&amp;gt;&amp;lt;figure class=&quot;code&quot;&amp;gt;&amp;lt;figcaption&amp;gt;&lt;span&gt;&lt;/span&gt;&amp;lt;/figcaption&amp;gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;receives_allowance?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;income&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;income&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2400&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&amp;lt;/figure&amp;gt;&amp;lt;/notextile&amp;gt;&lt;/div&gt;

&lt;p&gt;There are several remarks to be made for this code. Adding a criteria such as marital status would involve adding a parameter to the function and changing the boolean expression. We could already avoid having to change parameters when we had chosen a parameter of type Person that contains the information about a person. But what if we would introduce time as an aspect in our criteria? We need to change the function again. And what if the age criteria changed? If the programmer erroneously codes something like &lt;code&gt;age &amp;gt; 18 &amp;amp;&amp;amp; age &amp;lt; 18&lt;/code&gt; into the condition we would only find the bug during testing, if we are lucky. Additionally when our criteria become more complex we would like to extract criteria to their own functions. In short, it is easy to make mistakes this way.&lt;/p&gt;

&lt;p&gt;A solution to this is to avoid the translation process by using a declarative language. A declarative language is a language that describes &lt;em&gt;what&lt;/em&gt; is to be computed but not &lt;em&gt;how&lt;/em&gt; it should be computed.&lt;sup id=&quot;fnref:declarativereference&quot;&gt;&lt;a href=&quot;http://alessandrovermeulen.me/atom.xml#fn:declarativereference&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; In essence: it omits control-flow. By encoding and thereby recording the business model into a set of declarative statements it becomes easier to spot irregularities in the business logic as the business logic reads more like the description of the business model and all invariants are &lt;em&gt;visible&lt;/em&gt;. In this manner the programmer no longer tells the computer how to perform a computation but rather what the computation should be. This makes it easier to maintain &lt;/p&gt;

&lt;p&gt;However, we take it even to an higher level entirely. By just using the declarative language, such as Haskell or Prolog, you are still using a general-purpose language and are thus lacking domain specific checks. It would be advantageous to devise a Domain Specific Language (DSL) instead. This would be a language specifically geared towards your business domain and can be done easily in a language such as Haskell. Creating a DSL has a great benefit: Because the business domain is written down in code the responsibility of translating the business domain into a program shifts from the programmer to the interpreter of the DSL. This has two benefits: the translation is consolidated in one single point (the interpreter) and can be verified or even proven to be correct. It could be checked that no contradicting statements are present. In this sense we can compare the validation to a spell-checker or JSLint but for our specific domain. Secondly the programmer cannot make mistakes in the translation of the business logic to imperative code.&lt;/p&gt;

&lt;p&gt;A simple DSL embedding the idea of the allowance could look like the text in the examples below. The interpreter / compiler is able to inspect the separate rules and check whether they would cause a tautology or contradiction.&lt;/p&gt;

&lt;p&gt;We will illustrate this with some examples. Consider the following text below, it is easy to read and it is clear what it means. Each line is a condition that has to hold, so we could read an “AND” at eacht line end. &lt;small&gt;(Whether Income is monthly or annually or weekly is not taken into account here but you get the picture.)&lt;/small&gt;&lt;/p&gt;

&lt;div class=&quot;bogus-wrapper&quot;&gt;&amp;lt;notextile&amp;gt;&amp;lt;figure class=&quot;code&quot;&amp;gt;&amp;lt;figcaption&amp;gt;&lt;span&gt;&lt;/span&gt;&amp;lt;/figcaption&amp;gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;person&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;adult&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;when&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;his&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Age&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;GREATER&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;THAN&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;OR&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;EQUAL&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;TO&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;person&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allegeable&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allowance&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;IF&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;he&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;adult&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;person&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allegeable&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allowance&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;IF&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;his&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Income&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;GREATER&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;THAN&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;€&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2400&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&amp;lt;/figure&amp;gt;&amp;lt;/notextile&amp;gt;&lt;/div&gt;

&lt;p&gt;When we would add a rule as shown below, we could get a warning or error from the interpreter telling us that we have two different conditions on a person’s age.&lt;/p&gt;

&lt;div class=&quot;bogus-wrapper&quot;&gt;&amp;lt;notextile&amp;gt;&amp;lt;figure class=&quot;code&quot;&amp;gt;&amp;lt;figcaption&amp;gt;&lt;span&gt;&lt;/span&gt;&amp;lt;/figcaption&amp;gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;person&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;adult&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;when&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;his&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Age&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;GREATER&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;THAN&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;OR&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;EQUAL&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;TO&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;person&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allegeable&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allowance&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;IF&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;he&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;adult&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;person&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allegeable&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allowance&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;IF&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;his&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Income&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;GREATER&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;THAN&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;€&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2400&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;person&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allegeable&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allowance&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;IF&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;his&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Age&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;GREATER&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;THAN&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;21&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&amp;lt;/figure&amp;gt;&amp;lt;/notextile&amp;gt;&lt;/div&gt;

&lt;p&gt;And when we would add a rule as this it could warn us that no-one will ever get an allowance.&lt;/p&gt;

&lt;div class=&quot;bogus-wrapper&quot;&gt;&amp;lt;notextile&amp;gt;&amp;lt;figure class=&quot;code&quot;&amp;gt;&amp;lt;figcaption&amp;gt;&lt;span&gt;&lt;/span&gt;&amp;lt;/figcaption&amp;gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;person&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;adult&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;when&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;his&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Age&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;GREATER&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;THAN&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;OR&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;EQUAL&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;TO&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;person&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allegeable&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allowance&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;IF&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;he&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;adult&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;person&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allegeable&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allowance&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;IF&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;his&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Income&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;GREATER&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;THAN&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;€&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2400&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;person&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allegeable&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allowance&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;IF&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;his&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Age&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;LESS&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;THAN&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&amp;lt;/figure&amp;gt;&amp;lt;/notextile&amp;gt;&lt;/div&gt;

&lt;p&gt;Not only would the interpreter be able to spot these kinds of errors but it would also be easier for the writer of these rules to spot whether there is a mistake.
The astute reader will have noticed than we have not included any control flow into our language making it a declarative language.&lt;/p&gt;

&lt;p&gt;Because the business-logic is now represented by the DSL it can be written by domain experts instead of the programmers of the application itself. The compiler can provide feedback when something is wrong in the DSL and there is less chance for errors in the implementation of the business logic. Additionally this frees the programmes for implementing better translations of the DSL or other projects saving time and other resources. &lt;/p&gt;

&lt;p&gt;To summarise 4 reasons why you should switch to declarative languages:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Direct translation of the business model into business logic &lt;/li&gt;
  &lt;li&gt;Better readability of the business logic&lt;/li&gt;
  &lt;li&gt;Better scalability for the program in terms of functionality&lt;/li&gt;
  &lt;li&gt;Less bugs&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And 3 reasons why you should use DSLs to boot:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Free up programmers to do important stuff&lt;/li&gt;
  &lt;li&gt;Let the domain-experts handle the business logic and have it machine-checked!&lt;/li&gt;
  &lt;li&gt;It is just awesome&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;footnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:declarativereference&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;ftp://clip.dia.fi.upm.es/pub/papers/PARFORCE/second_review/D.WP3.1.M2.3.ps.Z&quot;&gt;Lloyd, J.W., Practical Advantages of Declarative Programming&lt;/a&gt;&lt;a href=&quot;http://alessandrovermeulen.me/atom.xml#fnref:declarativereference&quot; rev=&quot;footnote&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;</description>
	<pubDate>Sun, 19 May 2013 10:52:00 +0000</pubDate>
</item>
<item>
	<title>language-puppet: language-puppet v0.4.0</title>
	<guid isPermaLink="false">http://lpuppet.banquise.net/blog/2013/05/16/language-puppet-v0-dot-4-0</guid>
	<link>http://lpuppet.banquise.net/blog/2013/05/16/language-puppet-v0-dot-4-0/</link>
	<description>&lt;p&gt;I just released the latest language-puppet version. For the full list of changes, please take a look at the
&lt;a href=&quot;https://github.com/bartavelle/language-puppet/blob/master/Changelog&quot;&gt;changelog&lt;/a&gt;. Here are the highlights.&lt;/p&gt;

&lt;h3&gt;PuppetDB code reworked&lt;/h3&gt;

&lt;p&gt;The PuppetDB code and API has been completely overhauled. It is now more generic : the resource collection and puppet
query functions now work the same. Additionally, a PuppetDB stub has been created for testing use.&lt;/p&gt;

&lt;h3&gt;Better diagnostic facilities&lt;/h3&gt;

&lt;p&gt;As the main use of this library is to test stuff, the following features were added:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Several error messages have been reworked so that they are more informative.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;dumpvariables&lt;/code&gt; built-in function has been added. It just prints all known variables (and facts) to stdout, and can
be quite handy.&lt;/li&gt;
&lt;li&gt;The “scope stack” description is stored with the resources. This turned out to be extremely useful when debugging
resource names colisions or to find out where some resource is defined.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Here is an example, let’s say you do not remember which package installs the &lt;code&gt;collectd&lt;/code&gt; package. Just run this :&lt;/p&gt;

&amp;lt;figure class=&quot;code&quot;&amp;gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;&quot;&gt;&lt;span class=&quot;line&quot;&gt;» puppetresources . default.domain 'Package[collectd]'
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;package {
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &quot;collectd&quot;: #&quot;./modules/collectd/manifests/base.pp&quot; (line 4, column 9) [&quot;::&quot;,&quot;site::baseconfig&quot;,&quot;collectd&quot;,&quot;collectd::client&quot;,&quot;collectd::base&quot;]
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        ensure          =&amp;gt; &quot;installed&quot;,
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        require         =&amp;gt; [Class[&quot;collectd&quot;], Class[&quot;collectd::base&quot;], Class[&quot;collectd::client&quot;], Class[&quot;site::baseconfig&quot;]];
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&amp;lt;/figure&amp;gt;


&lt;p&gt;You now know exactly where the &lt;code&gt;package&lt;/code&gt; resource is declared, and the list of “scopes” that have been traversed in
order to do so. Note that this information is displayed when resources names collide.&lt;/p&gt;

&lt;h3&gt;Easier to setup&lt;/h3&gt;

&lt;p&gt;This library doesn’t depend from a newish &lt;code&gt;bytestring&lt;/code&gt; anymore, and should build with the package provided with a GHC
compiler of the 7.6.x serie.&lt;/p&gt;

&lt;p&gt;This is not yet done, but I will certainly soon publish a debian-style repository of the compiled &lt;code&gt;puppetresources&lt;/code&gt; binary. I am interested in suggestions for an automated building system.&lt;/p&gt;

&lt;h3&gt;Better testing&lt;/h3&gt;

&lt;p&gt;The testing API seems sufficient to write pretty strong tests, but would still benefit from a few more helper functions.
The testing “daemon” has been reworked to use the new PuppetDB stub. It makes it possible to test complex interactions
between hosts using the exported resource or PuppetDB query features.&lt;/p&gt;

&lt;h3&gt;Work in progress&lt;/h3&gt;

&lt;p&gt;I will probably &lt;a href=&quot;http://hackage.haskell.org/package/lens&quot;&gt;lensify&lt;/a&gt; the code until I get a descent understanding of it.&lt;/p&gt;

&lt;p&gt;I do not intend to work on Hiera emulation just yet, as I am probably the only user of this library for now and I do not
use this feature.&lt;/p&gt;

&lt;p&gt;One area of improvement would be to embed the ruby interpreter in the library. I am not sure how to do this, but as
there are quite a few projects of lightweight interpreters sprouting from the earth, it might be possible in the near
future. The only problem would be figuring out how to build a large C project with cabal.&lt;/p&gt;

&lt;h3&gt;Some other considerations&lt;/h3&gt;

&lt;p&gt;I recently ported the code from &lt;code&gt;random.c&lt;/code&gt; to Haskell
(&lt;a href=&quot;https://github.com/bartavelle/language-puppet/blob/master/Puppet/Interpreter/RubyRandom.hs&quot;&gt;here&lt;/a&gt;). This has been
quite tedious, and is quite hard to read. This is an almost naive port of the code found in the Ruby interpreter,
without the useless loop variables. For some reason, there are many loops like this :&lt;/p&gt;

&amp;lt;figure class=&quot;code&quot;&amp;gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;&quot;&gt;&lt;span class=&quot;line&quot;&gt;i=1; j=0;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;k = (N&amp;gt;key_length ? N : key_length);
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;for (; k; k--) {
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    mt-&amp;gt;state[i] = (mt-&amp;gt;state[i] ^ ((mt-&amp;gt;state[i-1] ^ (mt-&amp;gt;state[i-1] &amp;gt;&amp;gt; 30)) * 1664525U))
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        + init_key[j] + j; /* non linear */
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    mt-&amp;gt;state[i] &amp;amp;= 0xffffffffU; /* for WORDSIZE &amp;gt; 32 machines */
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    i++; j++;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    if (i&amp;gt;=N) { mt-&amp;gt;state[0] = mt-&amp;gt;state[N-1]; i=1; }
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    if (j&amp;gt;=key_length) j=0;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&amp;lt;/figure&amp;gt;


&lt;p&gt;As you can see, the value of k is never used in the loop. I am not sure why the author didn’t go for something like :&lt;/p&gt;

&amp;lt;figure class=&quot;code&quot;&amp;gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;&quot;&gt;&lt;span class=&quot;line&quot;&gt;for(i=1;i&amp;lt;k;i++) {&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&amp;lt;/figure&amp;gt;


&lt;p&gt;Anyway, the Haskell code is pretty bad, and will certainly only work for 64-bit builds. I am not sure how I should have
written it. I suppose staying in the ST monad would have lead to nicer code, and I am open to suggestions.&lt;/p&gt;</description>
	<pubDate>Thu, 16 May 2013 18:32:00 +0000</pubDate>
</item>
<item>
	<title>Philip Wadler: From Session Types to Data Types: RA posts and PhD studentships</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-9757377.post-3416321216685234394</guid>
	<link>http://wadler.blogspot.com/2013/05/from-session-types-to-data-types-ra.html</link>
	<description>&lt;div style=&quot;clear: both; text-align: center;&quot; class=&quot;separator&quot;&gt;&lt;a style=&quot;margin-left: 1em; margin-right: 1em;&quot; href=&quot;http://2.bp.blogspot.com/-nZkF32umhTc/UZTeDERPduI/AAAAAAAABi4/Ybi1BefzLhw/s1600/Screenshot-1.png&quot;&gt;&lt;img src=&quot;http://2.bp.blogspot.com/-nZkF32umhTc/UZTeDERPduI/AAAAAAAABi4/Ybi1BefzLhw/s320/Screenshot-1.png&quot; height=&quot;160&quot; border=&quot;0&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;I've posted this elsewhere, but neglected to blog before now. &lt;br /&gt;&lt;br /&gt;We are recruiting for research associate positions in design and implementation of  programming languages, and also may have PhD studentships available this year and next.  The posts are on the project &quot;From &lt;span class=&quot;il&quot;&gt;Data&lt;/span&gt; &lt;span class=&quot;il&quot;&gt;Types&lt;/span&gt; to  &lt;span class=&quot;il&quot;&gt;Session&lt;/span&gt; &lt;span class=&quot;il&quot;&gt;Types&lt;/span&gt;: A Basis for Concurrency and Distribution&quot; which is a  programme grant funded by EPSRC for five years from 20 May 2013, joint with Simon Gay at the University of Glasgow and Nobuko Yoshida at Imperial College London.&lt;br /&gt;&lt;br /&gt;The RA post at Edinburgh for an initial period of 24 months, with  possibility of extension, and is on the UE07 scale (£30,424 - £36,298). Deadline for applications for the RA post is Monday 20 May 2013, anyone interested in a PhD studentship should apply as soon as possible.  Glasgow and Imperial are also recruiting.&lt;br /&gt;&lt;br /&gt;Please contact me if you are interested in either the RA post or a PhD studentship.  Further description of the Edinburgh RA post is below. &lt;br /&gt;&lt;div class=&quot;yj6qo ajU&quot;&gt;&lt;div tabindex=&quot;0&quot; id=&quot;:4y&quot; class=&quot;ajR&quot;&gt;&lt;img src=&quot;https://mail.google.com/mail/u/0/images/cleardot.gif&quot; class=&quot;ajT&quot; /&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   &lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Just as &lt;span class=&quot;il&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;il&quot;&gt;types&lt;/span&gt; describe the structure of &lt;span class=&quot;il&quot;&gt;data&lt;/span&gt;, &lt;span class=&quot;il&quot;&gt;session&lt;/span&gt; &lt;span class=&quot;il&quot;&gt;types&lt;/span&gt; describe the structure of communication between concurrent and  distributed processes. Our project has particular emphasis on putting  theory into practice, by embedding &lt;span class=&quot;il&quot;&gt;session&lt;/span&gt; &lt;span class=&quot;il&quot;&gt;types&lt;/span&gt; in a range of programming languages and applying them to realistic case  studies. The research programme is joint between the University of  Edinburgh, University of Glasgow, and Imperial College London, and  includes collaboration with Amazon, Cognizant, Red Hat, VMware, and the  Ocean Observatories Initiative.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Principal Duties&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The successful candidate will join a team responsible for extending  the functional web programming language Links with &lt;span class=&quot;il&quot;&gt;session&lt;/span&gt; &lt;span class=&quot;il&quot;&gt;types&lt;/span&gt; to  support concurrency and distribution.  We will test our techniques by  providing a library to access Amazon Web Services (AWS) cloud computing  infrastructure, and perform empirical experiments to assess how our  language design impacts the performance of programmers. &lt;br /&gt;&lt;br /&gt;You should possess a PhD in a relevant area, or be nearing  completion of same, or have comparable experience.  You should have a  track-record of publication, or other evidence of ability to undertake  research and communicate well.  You should have a strong background in  programming languages, including &lt;span class=&quot;il&quot;&gt;type&lt;/span&gt; systems, and strong programming  and software engineering skills. &lt;br /&gt;&lt;br /&gt;It is desirable for candidates to also have one or more of the  following: a combination of theoretical and practical skills; experience  of web programming or cloud programming; knowledge of the theory or  practice of concurrent and distributed systems; knowledge of linear  logic; or training in empirical measurement of programming tasks. &lt;br /&gt;&lt;br /&gt;We seek applicants at an international level of excellence.  The  Laboratory for Foundations of Computer Science is internationally  renowned, the School of Informatics at Edinburgh is among the strongest  in the world, and Edinburgh is known as a cultural centre providing a  high quality of life. &lt;br /&gt;&lt;br /&gt;Further details of the RA post, including how to apply, are &lt;a href=&quot;https://www.vacancies.ed.ac.uk/pls/corehrrecruit/erq_jobspec_version_4.jobspec?p_id=013243&quot;&gt;here&lt;/a&gt;.</description>
	<pubDate>Thu, 16 May 2013 13:39:54 +0000</pubDate>
	<author>noreply@blogger.com (Philip Wadler)</author>
</item>
<item>
	<title>Bryn Keller: Hot Towel SPA Is a Great Starter</title>
	<guid isPermaLink="true">http://www.xoltar.org/?p=127</guid>
	<link>http://www.xoltar.org/?p=127</link>
	<description>&lt;p&gt;A few months ago, &lt;a href=&quot;http://www.johnpapa.net/&quot;&gt;John Papa&lt;/a&gt; released a Visual Studio template called &lt;a href=&quot;http://www.johnpapa.net/hottowel/&quot;&gt;Hot Towel SPA&lt;/a&gt;, which &lt;a href=&quot;http://www.hanselman.com/&quot;&gt;Scott Hanselman&lt;/a&gt; kindly pointed out to me. SPA, as all the hip kids will tell you, stands for Single Page Application. That is, the kind of application that you start by visiting a web page, and you stay on that same page for as long as you use the application. As opposed to most web applications, where you skip from page to page as you interact with the site.&lt;/p&gt;
&lt;p&gt;People have been doing this for a long time, of course,  but the Hot Towel SPA starter really is a nice introduction to the style. In a SPA, you really  need to think of the browser as “the client,” a standalone entity that communicates with your server via (web) API calls. Once you get used to it, it’s really rather refreshing, and it allows you to take advantage of all the computing power on the client machine in a way that can be quite liberating.&lt;/p&gt;
&lt;p&gt;Hot Towel uses a JavaScript application framework called &lt;a href=&quot;http://durandaljs.com/&quot;&gt;Durandal&lt;/a&gt; to structure the client side code. It divides the world up into services (JavaScript modules, basically), views, and view models. All of this is just for the JavaScript side of things, remember – you may also have views and view models and so on on the server side, but that’s a different thing – you’ll interact with those via AJAX calls, usually using JSON to encode the data.&lt;/p&gt;
&lt;p&gt;Hot Towel uses HTML for the views and lets &lt;a href=&quot;http://knockoutjs.com/&quot;&gt;Knockout&lt;/a&gt; do the view composition and data binding, which makes it a good source of examples for learning Knockout as well.&lt;/p&gt;
&lt;p&gt;The JavaScript code is nicely modular, in the style of &lt;a href=&quot;http://requirejs.org/&quot;&gt;require.js&lt;/a&gt;. If you’ve not seen this style, it’s worth checking out. Basically, you declare all your dependencies for your JavaScript module, and the framework asynchronously loads them as necessary, as passes them to your module. It’s great documentation, great for structuring the code so you don’t get circular dependencies, and makes for easier unit testing too, since can easily supply alternative (mock) implementations of your module’s dependencies.&lt;/p&gt;
&lt;p&gt;On the server, the MVC code is well organized as well, and it’s straightforward to plug in your new Web API controllers and start coding.&lt;/p&gt;
&lt;p&gt;I’ve been playing with this starter for a while, working on a proof of concept for a new series of articles on my blog. I found Hot Towel to be a great starting point, and it opened my eyes to some interesting new techniques on the client side. Give Hot Towel a try for your next project, it’ll be fun.&lt;/p&gt;</description>
	<pubDate>Thu, 16 May 2013 10:30:00 +0000</pubDate>
</item>
<item>
	<title>Philip Wadler: Aleph Cloud</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-9757377.post-7007610545535304157</guid>
	<link>http://wadler.blogspot.com/2013/05/aleph-cloud.html</link>
	<description>&lt;div style=&quot;clear: both; text-align: center;&quot; class=&quot;separator&quot;&gt;&lt;a style=&quot;margin-left: 1em; margin-right: 1em;&quot; href=&quot;http://1.bp.blogspot.com/-5PEq8CrNw1I/UZSm9ZuKzXI/AAAAAAAABhw/F82qIaaKv0g/s1600/aleph-cloud-logo.png&quot;&gt;&lt;img src=&quot;http://1.bp.blogspot.com/-5PEq8CrNw1I/UZSm9ZuKzXI/AAAAAAAABhw/F82qIaaKv0g/s320/aleph-cloud-logo.png&quot; height=&quot;78&quot; border=&quot;0&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style=&quot;clear: both; text-align: center;&quot; class=&quot;separator&quot;&gt;&lt;/div&gt;&lt;a href=&quot;http://www.alephcloud.com/&quot;&gt;Aleph Cloud&lt;/a&gt; is looking to &lt;a href=&quot;http://homepages.inf.ed.ac.uk/wadler/documents/AlephCloud_Haskell_Engineer_JD_final.pdf&quot;&gt;hire Haskell programmers&lt;/a&gt;.</description>
	<pubDate>Thu, 16 May 2013 09:36:14 +0000</pubDate>
	<author>noreply@blogger.com (Philip Wadler)</author>
</item>

</channel>
</rss>
