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

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

<item>
	<title>Jamie Brandon: Flowing faster: External memory</title>
	<guid isPermaLink="false">http://scattered-thoughts.net/blog/2013/05/21/flowing-faster-external-memory</guid>
	<link>http://scattered-thoughts.net/blog/2013/05/21/flowing-faster-external-memory/</link>
	<description>&lt;p&gt;I always want to be a better developer than I am. What work I do that is worthwhile happens in the few hours of flow I manage to achieve every week. A million different things break that flow every day. I suspect that a large part of achieving flow is keeping the current problem in working memory. To improve my chances I can improve my working memory, offload parts of the problem to the computer or prevent context switches. I’m on my own with the first option, but a better development environment can help with the latter two.&lt;/p&gt;




&lt;p&gt;The first thing that I want to fix in this series is offloading memory. There are basically two kinds of questions I regularly deal with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;How did I solve this problem / build this software / configure this program X months ago?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What was I trying to remember to change X seconds ago?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;I’ve started using &lt;a href=&quot;http://jblevins.org/projects/deft/&quot;&gt;deft&lt;/a&gt; to answer both of these. Deft stores notes in a folder full of flat files and adds an incremental search buffer to emacs (searching &amp;gt; organising). This means that my notes are simple plain text which I can easily edit, backup, grep or serve on the web.&lt;/p&gt;

&lt;p&gt;For long-term memory I create a new note every time I solve a problem or learn something useful. Within emacs M-’ brings up the deft window, typing triggers the incremental search and hiting Enter opens the first matching note.&lt;/p&gt;

&lt;p&gt;For short-term memory I have a single note called stack. Hitting C-’ opens the stack note with the cursor on a new blank line for adding items to the stack. Hitting C-DEL deletes the previous line and C-q closes the stack. Hopefully this is sufficiently low-friction that the extra memory makes up for the context switch.&lt;/p&gt;

&lt;p&gt;My config is &lt;a href=&quot;https://github.com/jamii/emacs-live-packs/blob/master/deft-pack/init.el&quot;&gt;here&lt;/a&gt;. I’m considering writing a gnome-shell extension which displays the last line of the stack in the status bar to remind me what I’m supposed to be doing when my mental stack gets rudely dumped. I also want to add the global key bindings to gnome-shell so I don’t have to navigate to emacs first.&lt;/p&gt;

&lt;p&gt;This is a very simple tool, which is kind of the point. The more stucture and options added to a note-taking tool the more effort it takes to actually use it and the more likely it is that I lose my entire mental stack whilst doing so.&lt;/p&gt;</description>
	<pubDate>Tue, 21 May 2013 19:43:00 +0000</pubDate>
</item>
<item>
	<title>Caml Weekly News: Caml Weekly News, 21 May 2013</title>
	<guid isPermaLink="true">http://alan.petitepomme.net/cwn/2013.05.21.html</guid>
	<link>http://alan.petitepomme.net/cwn/2013.05.21.html</link>
	<description>ocaml-bitstring 2.0.4 / standard 3d vector library in OCaml / concurrent caml-light? / 2D vector graphics / Core Suite 109.23.00 + async_parallel / Other Caml News</description>
	<pubDate>Tue, 21 May 2013 12:00:00 +0000</pubDate>
</item>
<item>
	<title>Open Mirage: The road to a developer preview at OSCON 2013</title>
	<guid isPermaLink="false">http://www.openmirage.org/blog/the-road-to-a-dev-release</guid>
	<link></link>
	<description>&lt;p&gt;There's been a crazy stream of activity since the start of the year, but the most important news is that we have a release target for an integrated developer preview of the Mirage stack: a talk at &lt;a href=&quot;http://www.oscon.com/oscon2013/public/schedule/detail/28956&quot;&gt;O'Reilly OSCon&lt;/a&gt; in July!  Do turn up there and find &lt;a href=&quot;http://dave.recoil.org&quot;&gt;Dave Scott&lt;/a&gt; and &lt;a href=&quot;http://anil.recoil.org&quot;&gt;Anil Madhavapeddy&lt;/a&gt; showing off interactive demonstrations.&lt;/p&gt;&lt;p&gt;Meanwhile, another significant announcement has been that Xen is &lt;a href=&quot;http://www.linuxfoundation.org/news-media/announcements/2013/04/xen-become-linux-foundation-collaborative-project&quot;&gt;joining the Linux Foundation&lt;/a&gt; as a collaborative project.  This is great news for Mirage: as a library operating system, we can operate just as easily under other hypervisors, and even on bare-metal devices such as the &lt;a href=&quot;http://raspberrypi.org&quot;&gt;Raspberry Pi&lt;/a&gt;.  We're very much looking forward to getting the Xen-based developer release done, and interacting with the wider Linux community (and FreeBSD, for that matter, thanks to Gabor Pali's &lt;a href=&quot;https://github.com/pgj/mirage-kfreebsd&quot;&gt;kFreeBSD&lt;/a&gt; backend).&lt;/p&gt;&lt;p&gt;Here's some other significant news from the past few months:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ul&gt; &lt;li&gt; &lt;p&gt;&lt;a href=&quot;http://www.ocamlpro.com/blog/2013/03/14/opam-1.0.0.html&quot;&gt;OPAM 1.0 was released&lt;/a&gt;, giving Mirage a solid package manager for handling the many libraries required to glue an application together.  &lt;a href=&quot;https://github.com/vbmithr&quot;&gt;Vincent Bernardoff&lt;/a&gt; joined the team at Citrix and has been building a Mirage build-frontend called &lt;a href=&quot;https://github.com/mirage/mirari&quot;&gt;Mirari&lt;/a&gt; to hide much of the system complexity from a user who isn't too familiar with either Xen or OCaml.&lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;A new group called the &lt;a href=&quot;http://ocaml.io&quot;&gt;OCaml Labs&lt;/a&gt; has started up in the &lt;a href=&quot;http://www.cl.cam.ac.uk&quot;&gt;Cambridge Computer Laboratory&lt;/a&gt;, and is working on improving the OCaml toolchain and platform.  This gives Mirage a big boost, as we can re-use several of the documentation, build and test improvements in our own releases.  You can read up on the group's activities via the &lt;a href=&quot;http://ocaml.io/news&quot;&gt;monthly updates&lt;/a&gt;, or browse through the various &lt;a href=&quot;http://ocaml.io/tasks&quot;&gt;projects&lt;/a&gt;.  One of the more important projects is the &lt;a href=&quot;http://www.cl.cam.ac.uk/projects/ocamllabs/tasks/platform.html#OCamlot&quot;&gt;OCamlot&lt;/a&gt; continuous build infrastructure, which will also be testing Mirage kernels as one of the supported backends.&lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;As we head into release mode, we've started &lt;a href=&quot;http://www.openmirage.org/wiki/tag/overview/meetings&quot;&gt;weekly meetings&lt;/a&gt; to coordinate all the activities.  We're keeping notes as we go along, so you should be able to skim the notes and &lt;a href=&quot;https://lists.cam.ac.uk/pipermail/cl-mirage/&quot;&gt;mailing list archives&lt;/a&gt; to get a feel for the overall activities.  Anil is maintaining a &lt;a href=&quot;http://www.openmirage.org/wiki/dev-preview-checklist&quot;&gt;release checklist&lt;/a&gt; for the summer developer preview.&lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;Anil (along with Yaron Minsky and Jason Hickey) is finishing up an O'Reilly book on &lt;a href=&quot;http://realworldocaml.org&quot;&gt;Real World OCaml&lt;/a&gt;, which will be a useful guide to using OCaml for systems and network programming. If you'd like to review an early copy, please get in touch.  The final book is anticipated to be released towards the end of the year, with a &lt;a href=&quot;http://shop.oreilly.com/category/roughcuts.do&quot;&gt;Rough Cut&lt;/a&gt; at the end of the summer.&lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;The core system was described in an &lt;a href=&quot;http://anil.recoil.org/papers/2013-asplos-mirage.pdf&quot;&gt;ASPLOS 2013&lt;/a&gt; paper, which should help you understand the background behind library operating systems. Some of the Mirage libraries are also currently being integrated into the next-generation &lt;a href=&quot;http://blogs.citrix.com/2012/05/17/introducing-windsor-a-new-xen-based-virtualization-architecture/&quot;&gt;Windsor&lt;/a&gt; release of the Xen Cloud Platform, which means that several of the libraries will be used in production and hence move beyond research-quality code.&lt;/p&gt; &lt;/li&gt; &lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;In the next few months, the installation notes and getting started guides will all be revamped to match the reality of the new tooling, so expect some flux there.   If you want to take an early try of Mirage beforehand, don't forget to hop on the &lt;code&gt;#mirage&lt;/code&gt; IRC channel on Freenode and ping us with questions directly.  We will also be migrating some of the project infrastructure to be fully self-hosted on Mirage and Xen, and placing some of the services onto the new &lt;a href=&quot;http://xenproject.org&quot;&gt;xenproject.org&lt;/a&gt; infrastructure.&lt;/p&gt;</description>
	<pubDate>Mon, 20 May 2013 16:20:00 +0000</pubDate>
	<author>anil@recoil.org (Anil Madhavapeddy)</author>
</item>
<item>
	<title>OCamlCore Forge News: Zarith 1.2 released</title>
	<guid isPermaLink="true">https://forge.ocamlcore.org/forum/forum.php?forum_id=877</guid>
	<link>https://forge.ocamlcore.org/forum/forum.php?forum_id=877</link>
	<description>Zarith is a fast, space-efficient, GMP-based library for arbitrary-precision integer and rational arithmetic. This minor release fixes a couple of bugs, improves Windows/Mingw support, and adds a fast path coded in assembly for ARM processors.</description>
	<pubDate>Mon, 20 May 2013 09:27:23 +0000</pubDate>
</item>
<item>
	<title>Caml Weekly News: Caml Weekly News, 14 May 2013</title>
	<guid isPermaLink="true">http://alan.petitepomme.net/cwn/2013.05.14.html</guid>
	<link>http://alan.petitepomme.net/cwn/2013.05.14.html</link>
	<description>extlib 1.5.4 / standard 3d vector library in OCaml / Linux epoll bindings / an issue with coercing private types / Interfacing with QtQuick 2.0 from Qt5, RFC / String, Array, Bigarray.char / smarter #load directive / New OCaml-Paris meetup on May 21, 19h30, IRILL / oasis help: support for qtest in oasis / Other Caml News</description>
	<pubDate>Tue, 14 May 2013 12:00:00 +0000</pubDate>
</item>
<item>
	<title>OCamlCore Forge News: OCaml MySQL Protocol 0.7 available</title>
	<guid isPermaLink="true">https://forge.ocamlcore.org/forum/forum.php?forum_id=876</guid>
	<link>https://forge.ocamlcore.org/forum/forum.php?forum_id=876</link>
	<description>This version drops tests for MySQL 5.0 and add tests for MySQL 5.6.</description>
	<pubDate>Sun, 12 May 2013 08:10:04 +0000</pubDate>
</item>
<item>
	<title>Matthias Puech: malloc() is the new gensym()</title>
	<guid isPermaLink="false">http://syntaxexclamation.wordpress.com/?p=365</guid>
	<link>http://syntaxexclamation.wordpress.com/2013/05/04/malloc-is-the-new-gensym/</link>
	<description>&lt;p&gt;Teaching an introductory course to “compilation” this semester (actually it was called  &lt;a href=&quot;http://www.pps.univ-paris-diderot.fr/~puech/ens/mv6.html&quot;&gt;Virtual Machines&lt;/a&gt;, but it was really about compiling expressions to stack machines), I realized something I hadn’t heard before, and wish I had been told when I first learned OCaml many years ago. Here it is: as soon as you are programming in a functional language with physical equality (i.e. pointer equality, the &lt;code&gt;(==)&lt;/code&gt; operator in OCaml), then you are actually working in a “weakly impure” language, and you can for example implement a limited form of &lt;code&gt;gensym&lt;/code&gt;. What? &lt;code&gt;gensym&lt;/code&gt; is this classic “innocuously effectful” function returning a different &lt;i&gt;symbol&lt;/i&gt;—usually a string—each time it is called. It is used pervasively to generate fresh variable names, in compilers notably. How? well, you actually don’t have much to do, except let the runtime call &lt;code&gt;malloc&lt;/code&gt;: it will return a “fresh” pointer where to store your data. &lt;code&gt;malloc&lt;/code&gt; and the garbage collector together ensures this freshness condition, and you can then compare two pointers with &lt;code&gt;(==)&lt;/code&gt;. As a bonus, you can even store data along your fresh symbol.&lt;/p&gt;
&lt;p&gt;In this post, I’ll exploit that simple idea to develop an assembler for a little stack machine close to that of OCaml.&lt;/p&gt;
&lt;p&gt;&lt;span id=&quot;more-365&quot;&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;The idea&lt;/h3&gt;
&lt;p&gt;In OCaml, something as simple as this is a &lt;code&gt;gensym&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;brush: fsharp; title: ; notranslate&quot;&gt;type 'a sym = C of 'a
let gensym x = C x
&lt;/pre&gt;
&lt;p&gt;Each call to say &lt;code&gt;gensym ()&lt;/code&gt; will allocate one new data block in memory; you can then compare two symbols with the physical equality &lt;code&gt;(==)&lt;/code&gt;.What we care about here is not the content of that memory span, but its &lt;i&gt;address&lt;/i&gt;, which is unique.&lt;/p&gt;
&lt;p&gt;A few warnings first: in OCaml, the constructor must have arguments, otherwise the compiler optimizes the representation to a simple integer and nothing is allocated. Also, don’t replace the argument &lt;code&gt;x&lt;/code&gt; to &lt;code&gt;C&lt;/code&gt; by a constant, say &lt;code&gt;()&lt;/code&gt;, in the function code: if you do so, the compiler will place value &lt;code&gt;C ()&lt;/code&gt; in the data segment of the program, and calling &lt;code&gt;gensym&lt;/code&gt; will not trigger an allocation either. There is an excellent and already classic series of blog post about OCaml’s value representation &lt;a href=&quot;http://rwmj.wordpress.com/2009/08/04/ocaml-internals/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Another way of saying the same thing is that (non-cyclic) values in OCaml are not trees, as they can be thought of considering the purely functional fragment, but DAGs, that is trees with sharing. &lt;/p&gt;
&lt;p&gt;I think that not many beginner/intermediate OCaml programmers realize the power of this, so I’d like to show a cool application of this remark. We will code a small compiler from a arithmetic language to a stack machine. Bear with me, it’s going to be fun!&lt;/p&gt;
&lt;h3&gt;An application: compiling expressions to a stack machine&lt;/h3&gt;
&lt;p&gt;The input language of expressions is:&lt;/p&gt;
&lt;pre class=&quot;brush: fsharp; title: ; notranslate&quot;&gt;type expr =
  | Int of int
  | Plus of expr * expr
  | If of expr * expr * expr
&lt;/pre&gt;
&lt;p&gt;Its semantics should be clear, except for the fact that &lt;code&gt;If&lt;/code&gt; are like in C: if their condition is different than 0, then their first branch is taken; if it is 0, then the second is taken. Because we have these conditionals, the stack machine will need instructions to jump around in the code. The instructions of this stack machine are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Push i&lt;/code&gt; pushes &lt;code&gt;i&lt;/code&gt; on the stack;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Add&lt;/code&gt; pops two values off the stack and pushes their sum;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Halt&lt;/code&gt; stops the machine and returning the (supposedly unique) stack value;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Branch o&lt;/code&gt; skips the next &lt;code&gt;o&lt;/code&gt; instructions in the code;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Branchif o&lt;/code&gt; skips the next &lt;code&gt;o&lt;/code&gt; instructions &lt;i&gt;if&lt;/i&gt; the top of the stack is not &lt;code&gt;0&lt;/code&gt;, and has no effect otherwise
&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;For instance, the expression &lt;i&gt;1 + (if 0 then 2 else (3+3))&lt;/i&gt; is compiled into:&lt;/p&gt;
&lt;pre class=&quot;brush: fsharp; title: ; notranslate&quot;&gt;[Push 1; Push 0; Branchif 3; 
   Push 3; Push 3; Add; Branch 1;
   Push 2;
 Add; Halt]
&lt;/pre&gt;
&lt;p&gt;and evaluates of course to &lt;code&gt;7&lt;/code&gt;. Notice how the two branches of the &lt;code&gt;If&lt;/code&gt; are turned around in the code? First, we’ve got the code of expression &lt;i&gt;2&lt;/i&gt;, then the code of &lt;i&gt;3+3&lt;/i&gt;. In general, expression &lt;i&gt;if e1 then e2 else e3&lt;/i&gt; will be compiled to [&lt;i&gt;c1&lt;/i&gt;; &lt;code&gt;Branchif&lt;/code&gt; (|&lt;i&gt;c3&lt;/i&gt;|+1); &lt;i&gt;c3&lt;/i&gt;; &lt;code&gt;Branch&lt;/code&gt; |&lt;i&gt;c2&lt;/i&gt;|; &lt;i&gt;c2&lt;/i&gt;; ...] where &lt;i&gt;ci&lt;/i&gt; is the compiled code of &lt;i&gt;ei&lt;/i&gt;, and |&lt;i&gt;l&lt;/i&gt;| is the size of code &lt;i&gt;l&lt;/i&gt;. But I’m getting ahead of myself.&lt;/p&gt;
&lt;h3&gt;Compilation&lt;/h3&gt;
&lt;p&gt;Now, compiling an &lt;code&gt;expr&lt;/code&gt; to a list of instructions in one pass would be a little bit messy, because we have to compute these integer offset for jumps. Let’s follow instead the common practice and first compile expressions to an assembly language where some suffixes of the code have &lt;i&gt;labels&lt;/i&gt;, which are the names referred to by instructions &lt;code&gt;Branch&lt;/code&gt; and &lt;code&gt;Branchif&lt;/code&gt;. This assembly language &lt;code&gt;asm&lt;/code&gt; will then be well… assembled into actual &lt;code&gt;code&lt;/code&gt;, where jumps are translated to integer offsets. But instead of generating label names by side-effect as customary, let’s use our trick: we will refer to them by a unique &lt;i&gt;pointer&lt;/i&gt; to the code attached to it. In other words, the arguments to &lt;code&gt;Branch&lt;/code&gt; and &lt;code&gt;Branchif&lt;/code&gt; will actually be pointers to &lt;code&gt;asm&lt;/code&gt; programs, comparable by &lt;code&gt;(==)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To represent the &lt;code&gt;code&lt;/code&gt; and &lt;code&gt;asm&lt;/code&gt; data structures, we generalize over the notion of label:&lt;/p&gt;
&lt;pre class=&quot;brush: fsharp; title: ; notranslate&quot;&gt;type 'label instr =
  | Push of int
  | Add
  | Branchif of 'label
  | Branch of 'label
  | Halt
&lt;/pre&gt;
&lt;p&gt;An assembly program is a list of instruction where labels are themselves assembly programs (the &lt;code&gt;-rectypes&lt;/code&gt; option of OCaml is required here):&lt;/p&gt;
&lt;pre class=&quot;brush: fsharp; title: ; notranslate&quot;&gt;type asm = asm instr list
&lt;/pre&gt;
&lt;p&gt;For instance, taking our previous example,&lt;/p&gt;
&lt;pre class=&quot;brush: fsharp; title: ; notranslate&quot;&gt;Plus (Int 1, If (Int 0, Int 2, Plus (Int 3, Int 3)))
&lt;/pre&gt;
&lt;p&gt;is compiled to the (shared) value:&lt;/p&gt;
&lt;pre class=&quot;brush: fsharp; title: ; notranslate&quot;&gt;Push 1 :: Push 0 :: 
  let k = [Add; Halt] in 
  Branchif (Push 2 :: k) :: 
  Push 3 :: Push 3 :: Add :: k
&lt;/pre&gt;
&lt;p&gt;See how the suffix &lt;code&gt;k&lt;/code&gt; (the continuation of the &lt;code&gt;If&lt;/code&gt;) is shared among the &lt;code&gt;Branchif&lt;/code&gt; and the main branch? In call-by-value, this is a value: if you reduce it any further by inlining &lt;code&gt;k&lt;/code&gt;, you will get a different value, that can be told apart from the first by using &lt;code&gt;(==)&lt;/code&gt;. So don’t let OCaml’s pretty-printing of values fool you: this is not a tree, the sharing of &lt;code&gt;k&lt;/code&gt; &lt;i&gt;is&lt;/i&gt; important! What you get is the DAG of all possible execution traces of your program; they eventually all merge in one point, the code suffix &lt;code&gt;k = [Add; Halt]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The compilation function is relatively straightforward; it’s an accumulator-based function:&lt;/p&gt;
&lt;pre class=&quot;brush: fsharp; title: ; notranslate&quot;&gt;let rec compile e k = match e with
  | Int i -&amp;gt; Push i :: k
  | Plus (e1, e2) -&amp;gt; compile e1 (compile e2 (Add :: k))
  | If (e1, e2, e3) -&amp;gt;
    compile e1 (Branchif (compile e2 k) :: compile e3 k)

let compile e = compile e [Halt]
&lt;/pre&gt;
&lt;p&gt;The sharing discussed above is realized here in the &lt;code&gt;If&lt;/code&gt; case, by compiling its two branches using the accumulator (continuation) &lt;code&gt;k&lt;/code&gt; twice. Again, many people think of this erroneously as &lt;i&gt;duplicating&lt;/i&gt; a piece of value. Actually, this is only mentioning twice a pointer to an already-allocated unique piece of value; and since we can compare pointers, we have a way to know that they are the same. Note also that this compilation function is purely compositional: to each subexpression corresponds a contiguous span of assembly code.&lt;/p&gt;
&lt;h3&gt;Assembly&lt;/h3&gt;
&lt;p&gt;Now, real code for our machine is simply a list of instructions where labels are represented by (positive) integers:&lt;/p&gt;
&lt;pre class=&quot;brush: fsharp; title: ; notranslate&quot;&gt;type code = int instr list
&lt;/pre&gt;
&lt;p&gt;Why positive? Well, since we have no way to make a loop, code can be arranged such that all jumps are made &lt;i&gt;forward&lt;/i&gt; in the code.&lt;/p&gt;
&lt;p&gt;The assembly function took me a while to figure out. It “linearizes” the assembly, a DAG, into a list by traversing it depth-first. The tricky part is that we don’t want to repeat the common suffixes of all branches; that’s where we use the fact that they are at the same memory address, which we can check with &lt;code&gt;(==)&lt;/code&gt;. If a piece of input code has already been compiled &lt;i&gt;n&lt;/i&gt; instructions ahead in the output code, instead of repeating it we just emit a &lt;code&gt;Branch&lt;/code&gt; &lt;i&gt;n&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;So practically, we must keep as an argument an association list &lt;code&gt;k&lt;/code&gt; mapping already-compiled suffixes of the input to the corresponding output instruction; think of it as a kind of “cache” of the function. It also doubles as the &lt;i&gt;result&lt;/i&gt; of the process: it is what’s eventually returned by &lt;code&gt;assemble&lt;/code&gt;. For each input &lt;code&gt;is&lt;/code&gt;, we first traverse that list &lt;code&gt;k&lt;/code&gt; looking for the pointer &lt;code&gt;is&lt;/code&gt;; if we find it, then we have our &lt;code&gt;Branch&lt;/code&gt; instruction; otherwise, we assemble the next instruction. This first part of the job corresponds to the &lt;code&gt;assemble&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&quot;brush: fsharp; title: ; notranslate&quot;&gt;let rec assemble is k =
  try (is, Branch (List.index (fun (is', _) -&amp;gt; is == is') k)) :: k
  with Not_found -&amp;gt; assem is k
&lt;/pre&gt;
&lt;p&gt;(&lt;code&gt;List.index p xs&lt;/code&gt; returns the index of the first element &lt;code&gt;x&lt;/code&gt; of &lt;code&gt;xs&lt;/code&gt; such that &lt;code&gt;p x&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;). &lt;/p&gt;
&lt;p&gt;Now the auxiliary function &lt;code&gt;assem&lt;/code&gt; actually assembles instructions into a list of pairs of source programs and target instruction:&lt;/p&gt;
&lt;pre class=&quot;brush: fsharp; title: ; notranslate&quot;&gt;and assem asm k = match asm with
  | (Push _ | Add | Halt as i) :: is -&amp;gt;
    (asm, i) :: assemble is k
  | Branchif is :: js -&amp;gt;
    let k = assemble is k in
    let k' = assemble js k in
    (asm, Branchif (List.length k' - List.length k)) :: k'
  | Branch _ :: _ -&amp;gt; assert false
  | [] -&amp;gt; k
&lt;/pre&gt;
&lt;p&gt;Think of the arguments &lt;code&gt;asm&lt;/code&gt; and &lt;code&gt;k&lt;/code&gt; as one unique list &lt;code&gt;asm @ k&lt;/code&gt; that is “open” for insertion in two places: at top-level, as usual, and in the middle, between &lt;code&gt;asm&lt;/code&gt; and &lt;code&gt;k&lt;/code&gt;. The &lt;code&gt;k&lt;/code&gt; part is the already-processed suffix, and &lt;code&gt;asm&lt;/code&gt; is what remains to be processed. The first case inserts the non-branching instructions &lt;code&gt;Push, Add, Halt&lt;/code&gt; at top-level in the output (together with their corresponding assembly suffix of course). The second one, &lt;code&gt;Branchif&lt;/code&gt;, begins by inserting the branch &lt;code&gt;is&lt;/code&gt; at top-level, and then inserts the remainder &lt;code&gt;js&lt;/code&gt; in front of it. Note that when assembling this remainder, we can discover sharing that was recorded in &lt;code&gt;k&lt;/code&gt; when compiling the branch. Note also that there can’t be any &lt;code&gt;Branch&lt;/code&gt; in the assembly since it would not make much sense (everything after a &lt;code&gt;Branch&lt;/code&gt; instruction would be dead code), hence the &lt;code&gt;assert false&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Finally, we can strip off the “cached” information in the returned list, keeping only the target instructions:&lt;/p&gt;
&lt;pre class=&quot;brush: fsharp; title: ; notranslate&quot;&gt;let assemble is = snd (List.split (assemble is []))
&lt;/pre&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;That’s it, we have a complete compilation chain for our expression language! We can execute the target code on this machine:&lt;/p&gt;
&lt;pre class=&quot;brush: fsharp; title: ; notranslate&quot;&gt;let rec exec = function
  | s, Push i :: c -&amp;gt; exec (i :: s, c)
  | i :: j :: s, Add :: c -&amp;gt; exec (i + j :: s, c)
  | s, Branch n :: c -&amp;gt; exec (s, List.drop n c)
  | i :: s, Branchif n :: c -&amp;gt; exec (s, List.drop (if i&amp;lt;&amp;gt;0 then n else 0) c)
  | [i], Halt :: _ -&amp;gt; i
  | _ -&amp;gt; failwith &quot;error&quot;

let exec c = exec ([], c)
&lt;/pre&gt;
&lt;p&gt;The idea of using labels that are actual pointers to the code seems quite natural and seems to scale well (I implemented a compiler from a mini-ML to a virtual machine close to OCaml’s bytecode). In terms of performance however, &lt;code&gt;assemble&lt;/code&gt; is quadratic: before assembling each instruction, we look up if we didn’t assemble it already. When we have real (string) labels, we can represent the “cache” as a data structure with faster lookup; unfortunately, if labels are pointers, we can’t really do this because we don’t have a total order on pointers, only equality &lt;code&gt;(==)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This is only one example of how we can exploit pointer equality in OCaml to mimick a name generator. I’m sure there are lots of other applications to be discovered, or that I don’t know of (off the top of my head: to represent variables in the lambda-calculus). The big unknown for me is the nature of the language we’ve been working in, functional OCaml + pointer equality. Can we still consider it a functional language? How to reason on its programs? The comment section is right below!&lt;/p&gt;
&lt;br /&gt;  &lt;a href=&quot;http://feeds.wordpress.com/1.0/gocomments/syntaxexclamation.wordpress.com/365/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/comments/syntaxexclamation.wordpress.com/365/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;img src=&quot;http://stats.wordpress.com/b.gif?host=syntaxexclamation.wordpress.com&amp;amp;blog=14690639&amp;amp;post=365&amp;amp;subd=syntaxexclamation&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>Sat, 04 May 2013 15:15:44 +0000</pubDate>
</item>
<item>
	<title>GaGallium: A new Coq tactic for inversion</title>
	<guid isPermaLink="true">http://gallium.inria.fr/blog/a-new-Coq-tactic-for-inversion</guid>
	<link>http://gallium.inria.fr/blog/a-new-Coq-tactic-for-inversion</link>
	<description>&lt;p&gt;With &lt;a href=&quot;http://www.pps.univ-paris-diderot.fr/~pboutill/&quot;&gt;Pierre Boutillier&lt;/a&gt;, we have been working on a new Coq tactic lately, called &lt;code&gt;invert&lt;/code&gt;. From my point of view, it started as a quest to build a replacement to the &lt;code&gt;inversion&lt;/code&gt; tactic. &lt;code&gt;inversion&lt;/code&gt; is a pain to use, as it generates sub-goals with many (dependent) equalities that must be substituted, which force the use of &lt;code&gt;subst&lt;/code&gt;, which in turns also has its quirks, making the mantra &lt;code&gt;inversion H; clear H; subst&lt;/code&gt; quite fragile. Furthermore, &lt;code&gt;inversion&lt;/code&gt; has efficiency problems, being quite slow and generating big proof terms. From Pierre's point of view, this work was a good way to implement a better &lt;code&gt;destruct&lt;/code&gt; tactic, based on what he did during an internship (&lt;a href=&quot;http://www.pps.univ-paris-diderot.fr/~pboutill/files/Boutillier10.pdf&quot;&gt;report in French (PDF)&lt;/a&gt;).&lt;/p&gt;




&lt;p&gt;In a nutshell, the idea behind a destruction and an inversion is quite similar: it boils down to a case analysis over a given hypothesis. And there are quite a few tactics that follow this scheme: &lt;code&gt;elim&lt;/code&gt;, &lt;code&gt;case&lt;/code&gt;, &lt;code&gt;destruct&lt;/code&gt;, &lt;code&gt;inversion&lt;/code&gt;, &lt;code&gt;dependent destruction&lt;/code&gt;, &lt;code&gt;injection&lt;/code&gt; and &lt;code&gt;discriminate&lt;/code&gt; (it is true that the last two tactics are quite specialized, but fit the bill nevertheless). Why on Earth would we need to add a new element to this list?&lt;/p&gt;
&lt;p&gt;Well, it turns out that building on ideas by &lt;a href=&quot;http://www-verimag.imag.fr/~monin/&quot;&gt;Jean-Francois Monin&lt;/a&gt; to make so called &lt;a href=&quot;http://hal.inria.fr/inria-00489412/en/&quot;&gt;&quot;small inversions&quot;&lt;/a&gt;, one can unify the inner-working of most of the aforementioned list: it suffices to build the right return clause for the case analysis.&lt;/p&gt;
&lt;p&gt;Let's take an example.&lt;/p&gt;
&lt;pre class=&quot;sourceCode ocaml&quot;&gt;&lt;code class=&quot;sourceCode ocaml&quot;&gt;&lt;span class=&quot;dt&quot;&gt;Variable&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;A&lt;/span&gt; : Type&lt;span class=&quot;kw&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;dt&quot;&gt;Inductive&lt;/span&gt; vector: nat -&amp;gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; :=
| nil : vector 0
| cons: forall n (h:A) (v: vector n), vector (&lt;span class=&quot;dt&quot;&gt;S&lt;/span&gt; n).

&lt;span class=&quot;dt&quot;&gt;Inductive&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; : forall n, vector n -&amp;gt; &lt;span class=&quot;dt&quot;&gt;Prop&lt;/span&gt; :=
| &lt;span class=&quot;dt&quot;&gt;Pnil&lt;/span&gt; : &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; 0 nil
| &lt;span class=&quot;dt&quot;&gt;Pcons:&lt;/span&gt; forall n h v, &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; n v -&amp;gt; &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;S&lt;/span&gt; n) (cons n h v).

&lt;span class=&quot;dt&quot;&gt;Lemma&lt;/span&gt; test n h v (&lt;span class=&quot;dt&quot;&gt;H:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;S&lt;/span&gt; n) (cons n h v)) : &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; n v.
Proof&lt;span class=&quot;kw&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At this point, doing &lt;code&gt;inversion H&lt;/code&gt; generates 4 new hypotheses:&lt;/p&gt;
&lt;pre class=&quot;sourceCode ocaml&quot;&gt;&lt;code class=&quot;sourceCode ocaml&quot;&gt;&lt;span class=&quot;dt&quot;&gt;H2&lt;/span&gt; : &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; n v0
&lt;span class=&quot;dt&quot;&gt;H0&lt;/span&gt; : n0 = n
&lt;span class=&quot;dt&quot;&gt;H1&lt;/span&gt; : h0 = h
&lt;span class=&quot;dt&quot;&gt;H3&lt;/span&gt; : existT (&lt;span class=&quot;kw&quot;&gt;fun&lt;/span&gt; n : nat =&amp;gt; vector n) n v0 =
       existT (&lt;span class=&quot;kw&quot;&gt;fun&lt;/span&gt; n : nat =&amp;gt; vector n) n v
 ============================
   &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; n v&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Yuck: first, &lt;code&gt;H0&lt;/code&gt; and &lt;code&gt;H1&lt;/code&gt; are just cruft. Then, the goal isn't very palatable, because the equality &lt;code&gt;H3&lt;/code&gt; between &lt;code&gt;v&lt;/code&gt; and &lt;code&gt;v0&lt;/code&gt; is defined in terms of a dependent equality: in order to go further, one need to assume axioms about dependent equality&lt;sup&gt;&lt;a href=&quot;http://gallium.inria.fr/blog/index.rss#fn1&quot; id=&quot;fnref1&quot; class=&quot;footnoteRef&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;, equivalent to Streicher's axiom K. (Just to keep tabs, note that running the &lt;code&gt;Show Proof&lt;/code&gt; command in Coq outputs a partial proof term that is already 73 lines long at this point.)&lt;/p&gt;
&lt;p&gt;If we use &lt;code&gt;dependent destruction H&lt;/code&gt; instead of inversion, we get the expected hypothesis &lt;code&gt;H: P n v&lt;/code&gt; (which is far better from an usability point of view). Yet, there is no magic here: dependent destruction simply used a dependent equality axiom internally to get rid of the dependent equality, and generates a 64 lines long proof term that is not very pretty.&lt;/p&gt;
&lt;p&gt;At this point, one may wonder: what &lt;em&gt;should&lt;/em&gt; the proof term look like? and, is it necessary to use the K axiom here?&lt;/p&gt;
&lt;p&gt;A black belt Coq user versed in dependent types could write the following one.&lt;/p&gt;
&lt;pre class=&quot;sourceCode ocaml&quot;&gt;&lt;code class=&quot;sourceCode ocaml&quot;&gt;&lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; diag :=
   &lt;span class=&quot;kw&quot;&gt;fun&lt;/span&gt; n0 : nat =&amp;gt;
   &lt;span class=&quot;kw&quot;&gt;match&lt;/span&gt; n0 &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; n' return (forall v0 : vector n', &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; n' v0 -&amp;gt; &lt;span class=&quot;dt&quot;&gt;Prop&lt;/span&gt;) &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt;
   | 0 =&amp;gt; &lt;span class=&quot;kw&quot;&gt;fun&lt;/span&gt; (v0 : vector 0) (_ : &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; 0 v0) =&amp;gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt;
   | &lt;span class=&quot;dt&quot;&gt;S&lt;/span&gt; m =&amp;gt;
       &lt;span class=&quot;kw&quot;&gt;fun&lt;/span&gt; v0 : vector (&lt;span class=&quot;dt&quot;&gt;S&lt;/span&gt; m) =&amp;gt;
       &lt;span class=&quot;kw&quot;&gt;match&lt;/span&gt; v0 &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; v1 &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; (vector m0) return (&lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; m0 v1 -&amp;gt; &lt;span class=&quot;dt&quot;&gt;Prop&lt;/span&gt;) &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt;
       | nil =&amp;gt; &lt;span class=&quot;kw&quot;&gt;fun&lt;/span&gt; _ : &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; 0 nil =&amp;gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt;
       | cons p x v1 =&amp;gt; &lt;span class=&quot;kw&quot;&gt;fun&lt;/span&gt; _ : &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;S&lt;/span&gt; p) (cons p x v1) =&amp;gt; &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; p v1
       end
   end &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt;
 &lt;span class=&quot;kw&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;H&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;H'&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; x y) return (diag x y &lt;span class=&quot;dt&quot;&gt;H'&lt;/span&gt;) &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt;
 | &lt;span class=&quot;dt&quot;&gt;Pnil&lt;/span&gt; =&amp;gt; &lt;span class=&quot;dt&quot;&gt;I&lt;/span&gt;
 | &lt;span class=&quot;dt&quot;&gt;Pcons&lt;/span&gt; n0 h0 v0 &lt;span class=&quot;dt&quot;&gt;Pv&lt;/span&gt; =&amp;gt; &lt;span class=&quot;dt&quot;&gt;Pv&lt;/span&gt;
 end.&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Wow, 15 lines long. Let's demystify it a bit.&lt;/p&gt;
&lt;p&gt;First, recall that the return type of a match is dictated by its return clause (the &lt;code&gt;as ... in ... return ...&lt;/code&gt; part). This is basically a function that binds the arguments of the inductive (&lt;code&gt;S n&lt;/code&gt; as x, &lt;code&gt;cons n h v&lt;/code&gt; as y in our case), &lt;code&gt;H'&lt;/code&gt; of type &lt;code&gt;P x y&lt;/code&gt;, and which body is the return part. Usually, the return part is a constant (e.g., nat for the match in the List.length), but it is not mandatory. Here, the &lt;code&gt;diag&lt;/code&gt; term packs some computations, such that &lt;code&gt;diag (S n) (cons n h v) H&lt;/code&gt; reduces to &lt;code&gt;P n v&lt;/code&gt;, the conclusion of the goal. (In general, this kind of return clauses make it possible to eliminate impossible branches in a match, as done here by marking them with the trivial return type &lt;code&gt;True&lt;/code&gt;; we direct the interested readers to the online CPDT book by Adam Chlipala for more informations on this, especially &lt;a href=&quot;http://adam.chlipala.net/cpdt/html/MoreDep.html&quot;&gt;this chapter&lt;/a&gt;.)&lt;/p&gt;
&lt;p&gt;Then, what is &lt;code&gt;diag&lt;/code&gt;? Well, it is a function that follows the structure of the arguments of &lt;code&gt;P&lt;/code&gt; to single out impossible cases, and to refine the context in the other ones using dependent pattern matching, in order to reduce to the right type (the type of the initial conclusion of the goal). The idea behind such &quot;small-scale inversions&quot; was described by Monin in 2010 and is out of the scope of this blog post. What is new here is that we have mechanized the construction of the &lt;code&gt;diag&lt;/code&gt; functions as a Coq tactic, making this whole approach practical.&lt;/p&gt;
&lt;p&gt;All in all, using our new tactic, we can just use the following proof script:&lt;/p&gt;
&lt;pre class=&quot;sourceCode ocaml&quot;&gt;&lt;code class=&quot;sourceCode ocaml&quot;&gt;invert &lt;span class=&quot;dt&quot;&gt;H;&lt;/span&gt; tauto. &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At this point, &lt;code&gt;Show Proof.&lt;/code&gt; outputs the following complete proof term (where &lt;code&gt;invert_subgoal&lt;/code&gt; is the type of the subgoal solved by &lt;code&gt;tauto&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&quot;sourceCode ocaml&quot;&gt;&lt;code class=&quot;sourceCode ocaml&quot;&gt;&lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; diag :=
  &lt;span class=&quot;kw&quot;&gt;fun&lt;/span&gt; n0 : nat =&amp;gt;
  &lt;span class=&quot;kw&quot;&gt;match&lt;/span&gt; n0 &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; n1 return (forall v0 : vector n1, &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; n1 v0 -&amp;gt; &lt;span class=&quot;dt&quot;&gt;Prop&lt;/span&gt;) &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt;
  | 0 =&amp;gt; &lt;span class=&quot;kw&quot;&gt;fun&lt;/span&gt; (v0 : vector 0) (_ : &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; 0 v0) =&amp;gt; &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; -&amp;gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt;
  | &lt;span class=&quot;dt&quot;&gt;S&lt;/span&gt; x =&amp;gt;
      &lt;span class=&quot;kw&quot;&gt;fun&lt;/span&gt; v0 : vector (&lt;span class=&quot;dt&quot;&gt;S&lt;/span&gt; x) =&amp;gt;
      &lt;span class=&quot;kw&quot;&gt;match&lt;/span&gt;
        v0 &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; v1 &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; (vector n1)
        return
          (&lt;span class=&quot;kw&quot;&gt;match&lt;/span&gt; n1 &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; n2 return (vector n2 -&amp;gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt;) &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt;
           | 0 =&amp;gt; &lt;span class=&quot;kw&quot;&gt;fun&lt;/span&gt; _ : vector 0 =&amp;gt; &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; -&amp;gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt;
           | &lt;span class=&quot;dt&quot;&gt;S&lt;/span&gt; x0 =&amp;gt; &lt;span class=&quot;kw&quot;&gt;fun&lt;/span&gt; v2 : vector (&lt;span class=&quot;dt&quot;&gt;S&lt;/span&gt; x0) =&amp;gt; &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;S&lt;/span&gt; x0) v2 -&amp;gt; &lt;span class=&quot;dt&quot;&gt;Prop&lt;/span&gt;
           end v1)
      &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt;
      | nil =&amp;gt; &lt;span class=&quot;kw&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;H0&lt;/span&gt; : &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; =&amp;gt; &lt;span class=&quot;dt&quot;&gt;False_rect&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;H0&lt;/span&gt;
      | cons n1 h0 v1 =&amp;gt; &lt;span class=&quot;kw&quot;&gt;fun&lt;/span&gt; _ : &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;S&lt;/span&gt; n1) (cons n1 h0 v1) =&amp;gt; &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; n1 v1
      end
  end &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt;
(&lt;span class=&quot;kw&quot;&gt;fun&lt;/span&gt;
   invert_subgoal : forall (n0 : nat) (h0 : &lt;span class=&quot;dt&quot;&gt;A&lt;/span&gt;) (v0 : vector n0)
                      (&lt;span class=&quot;dt&quot;&gt;H0&lt;/span&gt; : &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; n0 v0),
                    diag (&lt;span class=&quot;dt&quot;&gt;S&lt;/span&gt; n0) (cons n0 h0 v0) (&lt;span class=&quot;dt&quot;&gt;Pcons&lt;/span&gt; n0 h0 v0 &lt;span class=&quot;dt&quot;&gt;H0&lt;/span&gt;) =&amp;gt;
 &lt;span class=&quot;kw&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;H&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; p &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; n0 v0) return (diag n0 v0 p) &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt;
 | &lt;span class=&quot;dt&quot;&gt;Pnil&lt;/span&gt; =&amp;gt; &lt;span class=&quot;kw&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;H0&lt;/span&gt; : &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; =&amp;gt; &lt;span class=&quot;dt&quot;&gt;False_rect&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;H0&lt;/span&gt;
 | &lt;span class=&quot;dt&quot;&gt;Pcons&lt;/span&gt; x x0 x1 x2 =&amp;gt; invert_subgoal x x0 x1 x2
 end) (&lt;span class=&quot;kw&quot;&gt;fun&lt;/span&gt; (n0 : nat) (_ : &lt;span class=&quot;dt&quot;&gt;A&lt;/span&gt;) (v0 : vector n0) (&lt;span class=&quot;dt&quot;&gt;H0&lt;/span&gt; : &lt;span class=&quot;dt&quot;&gt;P&lt;/span&gt; n0 v0) =&amp;gt; &lt;span class=&quot;dt&quot;&gt;H0&lt;/span&gt;))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Some of the differences with the proof term above come from the fact that we generate it interactively, rather than writing it once at all.&lt;/p&gt;
&lt;p&gt;A legitimate question: how do we compare to destruct and inversion and dependent destruction? First, we aim at producing a &quot;better&quot; destruct: that is, we might resolve the situation in which &lt;code&gt;destruct&lt;/code&gt; fails, in order to avoid producing ill-typed terms. Then, the situation with respect to inversion and dependent destruction is less clear. Right now, we would rather &lt;em&gt;not&lt;/em&gt; assume the K axiom (the right thing to do if homotopy is the future). In that case, we would fail for inversion problems that require K, and inversion and dependent destruction would be more powerful than our tactic. For problems that do no require to use K, &lt;code&gt;invert&lt;/code&gt; would be equivalent to &lt;code&gt;dependent destruction&lt;/code&gt; with better looking proof terms&lt;sup&gt;&lt;a href=&quot;http://gallium.inria.fr/blog/index.rss#fn2&quot; id=&quot;fnref2&quot; class=&quot;footnoteRef&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;We are still working on our prototype, but we are quite confident that we got the main thing right: mechanizing the construction of the return clause. We will come back to this blog when we need beta-testers!&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&quot;fn1&quot;&gt;&lt;p&gt;See &lt;a href=&quot;http://coq.inria.fr/faq?som=5#htoc47&quot;&gt;the following FAQ question&lt;/a&gt; (Can I prove that the second components of equal dependent pairs are equal?). You may also be interested in &lt;a href=&quot;http://coq.inria.fr/faq?som=5#htoc39&quot;&gt;this other question&lt;/a&gt; (What is Streicher's axiom K?). The &lt;a href=&quot;http://coq.inria.fr/library/Coq.Logic.EqdepFacts.html&quot;&gt;EqdepFacts&lt;/a&gt; standard library module, that has the equivalence proofs between all those subtle notions. Finally, if you want to finish this proof using these axioms, you can use &lt;code&gt;Require Import Eqdep.&lt;/code&gt; then the &lt;code&gt;inj_pair2&lt;/code&gt; lemma. Once you're done, &lt;code&gt;Print Assumptions test.&lt;/code&gt; will let you check that you relied on an additional axiom -- or &lt;code&gt;Print Assumptions inj_pair2&lt;/code&gt;.&lt;a href=&quot;http://gallium.inria.fr/blog/index.rss#fnref1&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn2&quot;&gt;&lt;p&gt;Moreover, since our proof terms are less cluttered, it seems less likely than recursive definitions made in &quot;proof mode&quot; with &lt;code&gt;invert&lt;/code&gt; will fail to pass the termination check once Coq's guard condition deals properly with such commutative cuts, another part of Pierre's thesis work.&lt;a href=&quot;http://gallium.inria.fr/blog/index.rss#fnref2&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</description>
	<pubDate>Sat, 04 May 2013 08:00:00 +0000</pubDate>
</item>
<item>
	<title>Jane Street: Patch review vs diff review, revisited</title>
	<guid isPermaLink="false">https://ocaml.janestreet.com/114 at https://ocaml.janestreet.com</guid>
	<link>https://ocaml.janestreet.com/?q=node/114</link>
	<description>&lt;p&gt;I've been thinking about code review a lot recently.&lt;/p&gt;

&lt;p&gt;Code review is a key part of our dev process, and has been from the
beginning.  From our perspective, code is more than a way of getting
things done, it's a way of expressing your intent.  Code that's easy
to read and understand is likely to be more robust, more flexible, and
critically, safer.  And we care about safety a lot, for obvious
reasons.&lt;/p&gt;

&lt;p&gt;But the importance of code review doesn't mean that we've always done
a good job of organizing it.  I'll talk a bit more about how we used
to do code review, how we do it now, and the impact that those changes
have had.&lt;/p&gt;

&lt;h2&gt;The bad old world&lt;/h2&gt;

&lt;p&gt;Our old code review process was what you might call &lt;em&gt;batch-oriented&lt;/em&gt;.
We'd prepare a set of changes for a release, and then, after someone
gave it a quick look-over, combine these changes together in a branch.
We'd then read over these changes very carefully, with multiple people
reading each file, making comments, requesting changes, and fixing
those changes, until the code was in a releasable state.&lt;/p&gt;

&lt;p&gt;This was a big and expensive process, involving many people, and quite
a lot of work and coordination.  Given the time it took, we focused
our code review on our so-called &lt;em&gt;critical path systems&lt;/em&gt;, &lt;em&gt;i.e.&lt;/em&gt;, the
ones that are involved in sending orders to the market.&lt;/p&gt;

&lt;p&gt;The management task was complex enough that we wrote a tool called
&lt;code&gt;cr&lt;/code&gt; for managing and tracking the reading of these diffs, parceling
out responsibility for different files to different people.  We've
actually blogged about this before,
&lt;a href=&quot;https://ocaml.janestreet.com/?q=node/66&quot;&gt;here&lt;/a&gt; and
&lt;a href=&quot;https://ocaml.janestreet.com/?q=node/67&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Batch-oriented review worked well when we and our codebase were
smaller, but it did not scale.  By combining multiple changes into a
single branch, you were stuck reading a collection of unrelated
changes, and the breadth of the changes made fitting it all into your
head harder.  Even worse, when you throw a bunch of changes together,
some are going to take longer than others, so the release is blocked
until the slowest change gets beaten into shape.&lt;/p&gt;

&lt;p&gt;The end result is that, while we found code review to be indispensable
in creating high quality code that we felt comfortable connecting to
the markets, the overhead of that review kept on getting higher and
higher as we grew.&lt;/p&gt;

&lt;p&gt;We needed a better solution.&lt;/p&gt;

&lt;h2&gt;Feature-based review&lt;/h2&gt;

&lt;p&gt;Another approach to code review, and a more common one, is patch-based
review.  In patch review, someone proposes a change to the current
release in the form of a patch, and it is the patch itself that is
reviewed.  Once it passes review, it can be applied to the tree.
Patch-based review is great in that it gives you independence: one
patch taking a while doesn't block other patches from getting out.&lt;/p&gt;

&lt;p&gt;We avoided patch-based review initially because we were worried about
the complexities of dealing with conflicting patches.  Indeed, one
issue with patch-based review is that the state of the tree when the
patch is reviewed is likely not the state of the tree when the patch
is applied.  Even when this doesn't lead to textual conflicts, this
should leave you a little nervous, since a patch that is correct
against one version of the tree is not necessarily correct against a
changed tree.&lt;/p&gt;

&lt;p&gt;And then, what do you do when there's a conflict and the patch no
longer applies cleanly?  You can rebase the patch against the new
state of the tree, and then re-review the patch from scratch.  But
humans are really bad at investing mental energy in boring work, and
carefully re-reviewing a patch you've already mostly read is deadly
dull.&lt;/p&gt;

&lt;p&gt;Moreover, when do you decide that there's a conflict?  When dealing
with patches that involve file moves and renames, even deciding what
it means for a patch written previously to still apply cleanly is a
tricky question.&lt;/p&gt;

&lt;p&gt;Also, squaring patch-based review with a git or hg-based workflow can
be tricky.  There's something quite nice about the github-style
pull-request workflow; but the semantics of merging are pretty tricky,
and you need to be careful that what you read corresponds with the
actual changes that are made by the merge.&lt;/p&gt;

&lt;p&gt;For all the problems, the virtues of patch-based review are clear, and
so about six months ago we started a project to revamp our &lt;code&gt;cr&lt;/code&gt; tool
to make it suitable for doing patch-like review.  The new version of
&lt;code&gt;cr&lt;/code&gt; is now organized around what we call &lt;em&gt;features&lt;/em&gt;, which are
essentially hg bookmarks (similar to git branches) augmented with some
associated metadata.  This metadata includes&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An English description of the change&lt;/li&gt;
&lt;li&gt;A base-revision that the changes should be read against&lt;/li&gt;
&lt;li&gt;An owner&lt;/li&gt;
&lt;li&gt;A collection (usually just one other than the owner) of full-feature
reviewers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The workflow for a developer goes something like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;create a new feature&lt;/strong&gt; by running &lt;code&gt;cr feature create&lt;/code&gt;.  You'll
select a name for the feature and write the initial description.
The base-revision will automatically be chosen as the most recent
release.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Write the code&lt;/strong&gt;, using &lt;code&gt;hg&lt;/code&gt; in the ordinary way, making commits
as you go and pushing the result to a shared, multi-headed repo that
has all of the features people are working on.&lt;/li&gt;
&lt;li&gt;When you think the code is in a good state, &lt;strong&gt;get the feature
enabled&lt;/strong&gt; for review.  At this point, you'll need to get a
full-feature reviewer selected.  It's this person's job to read
every change that goes into this feature.&lt;/li&gt;
&lt;li&gt;The full feature reviewer then &lt;strong&gt;reads the diff&lt;/strong&gt; from the base-revision
to the tip, adding comments, requesting fixes, and reading diffs
forward until they're happy, at which point, it's &lt;strong&gt;seconded&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Once it's seconded, the feature is &lt;strong&gt;enabled for review&lt;/strong&gt; by anyone
who is signed up for review for the specific files you touched.  How
many file reviewers you are depends on the nature of the project.
In our most safety-critical systems, every file has three reviewers.
In some other systems, there are no file reviewers at all.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The remaining work needs to be done by the release manager.  A release
manager can create a new release based on a set of features that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;are fully reviewed, and have no outstanding reviewer complaints to
be resolved.&lt;/li&gt;
&lt;li&gt;compile cleanly on their own and pass their automated tests&lt;/li&gt;
&lt;li&gt;have as their base revision the previous release&lt;/li&gt;
&lt;li&gt;can be merged together cleanly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Checking that things &quot;can be merged together cleanly&quot; is actually
tricky, since you can't just trust hg's notion of a merge.  &lt;code&gt;cr&lt;/code&gt; has
its own merge logic that is more conservative than what &lt;code&gt;hg&lt;/code&gt; and &lt;code&gt;git&lt;/code&gt;
do.  The biggest worry with &lt;code&gt;hg&lt;/code&gt; is that it tries to guess at a
reasonable base-point for the 3-way merge (usually the greatest common
ancestor of the two heads to be merged).  Usually this works well, but
it's easy to construct crazy cases where on one branch you make
changes that are just silently dropped in the merge.  There is also
some rather surprising behavior that can come into play when files are
moved, copied or deleted as part of the mix.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cr&lt;/code&gt;, on the other hand, will always choose the base-point of the
features to be merged as the base-point for the 3-way merge.  This
way, the diffs that are reviewed are also the diffs that are used for
constructing the merged node.  Also, &lt;code&gt;cr&lt;/code&gt; has some extra sanity
conditions on what merges it's willing to try.  This all greatly
reduces the scope for surprising results popping out the other side of
a merge.&lt;/p&gt;

&lt;p&gt;If the base-revision of a given feature is against an older release,
then you need to &lt;em&gt;rebase&lt;/em&gt; the review before it can be released,
&lt;em&gt;i.e.&lt;/em&gt;, update the base-revision to the most recent release.  Among
other things requires you to merge your changes with tip.  If there
are conflicts, you then either need to review the resolution of the
conflicts, or you simply need to reread the diffs from scratch.  The
last bit is pretty rare, but it's an important escape hatch.&lt;/p&gt;

&lt;h2&gt;How'd it go?&lt;/h2&gt;

&lt;p&gt;The new code-review process has had a dramatic effect on our world.
The review process for our main repository used to take anywhere from
a couple of weeks to a three months to complete a cycle.  Today, those
releases go out every week, like clockwork.  Everyone knows that if
they can get their feature cleaned up and ready, they can always get
it out that week.  Indeed, if you're following our open-source
releases on github, you'll see that new packages have shown up once a
week for the last 16 weeks.&lt;/p&gt;

&lt;p&gt;Feature-baaed review has led to a significant increase in the rate of
change of our critical-path systems.  Code review is now considerably
less painful, and most importantly, it's easier than ever to say no to
a feature that isn't ready to go.  In old-style batch review, there
was a lot of pressure to not hold up a release polishing some small
bit, which sometimes lead you to release code that wasn't really
ready.  Now, that problem has largely vanished.&lt;/p&gt;

&lt;p&gt;The barrier to entry for people who want to contribute to critical
path systems has also been lowered.  This has also contributed to us
being able to get projects out the door faster.&lt;/p&gt;

&lt;p&gt;But the most striking result I've seen is from our post-trade group,
which operates outside of the review process used for the
critical-path systems.  The post-trade team is responsible for our
infrastructure that handles everything after a transaction is done,
like tracking the clearing and settlement of our trades or managing
our trading books and records.&lt;/p&gt;

&lt;p&gt;Post-trade has historically had a more relaxed approach to code review
--- they do it, but not on all parts of the system, and not in a
particularly strict way.  In the last few months, however, they
switched over to using the new feature-based workflow, and even though
they're doing a lot more code review (which takes serious time), their
overall process has become faster and more efficient.  We think that's
largely do to having a well-managed workflow for managing and merging
independent features, even without whatever the benefits of review
itself.&lt;/p&gt;

&lt;p&gt;I'm now pushing to get feature-based review adopted throughout the
firm.  Obviously, not all code needs to be scrutinized to the same
level --- having three reviewers for every file is sometimes sensible,
sometimes overkill --- but ensuring that no change can get in unless
one other person reads it and thinks it's reasonable is a good
baseline rule.  Review has a lot of benefits: it improves the quality
of the code, gives you a great opportunity for training, and helps
spread knowledge.  Those benefits make sense everywhere we have people
programming.&lt;/p&gt;

&lt;p&gt;Maybe the biggest lesson in this for me is the importance of thinking
through your processes, focusing on the biggest bottlenecks, and doing
what you can to fix them.&lt;/p&gt;</description>
	<pubDate>Fri, 03 May 2013 17:52:42 +0000</pubDate>
</item>
<item>
	<title>OCamlCore Forge Projects: Merlin</title>
	<guid isPermaLink="true">https://forge.ocamlcore.org/projects/merlin/</guid>
	<link>https://forge.ocamlcore.org/projects/merlin/</link>
	<description>Context sensitive completion and interactive error-reporting for Ocaml in Vim and Emacs.</description>
	<pubDate>Wed, 01 May 2013 22:18:16 +0000</pubDate>
</item>

</channel>
</rss>
