<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>

<channel>
	<title>Goanna Static Analysis by Red Lizard Software &#187; Ocaml</title>
	<atom:link href="http://redlizards.com/blog/feed/?tag=ocaml" rel="self" type="application/rss+xml" />
	<link>http://redlizards.com/blog</link>
	<description>The Blog of the Goanna Team</description>
	<pubDate>Mon, 23 Aug 2010 03:26:42 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Experiments with F#</title>
		<link>http://redlizards.com/blog/uncategorized/experiments-with-f/</link>
		<comments>http://redlizards.com/blog/uncategorized/experiments-with-f/#comments</comments>
		<pubDate>Sun, 08 Aug 2010 06:43:50 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
		
		<category><![CDATA[Development]]></category>

		<category><![CDATA[General]]></category>

		<category><![CDATA[F#]]></category>

		<category><![CDATA[Ocaml]]></category>

		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://redlizards.com/blog/?p=501</guid>
		<description><![CDATA[A couple of customers have asked for a command-line tool to run Goanna over their Visual Studio projects, similar to the way the Linux command-line tool works.  The difficult bit for such a tool is to translate the information in a project file to the appropriate arguments to the core Goanna executable, goannacc.exe on [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of customers have asked for a command-line tool to run Goanna over their Visual Studio projects, similar to the way the Linux command-line tool works.  The difficult bit for such a tool is to translate the information in a project file to the appropriate arguments to the core Goanna executable, goannacc.exe on Windows.  We already have code to do just that in the Visual Studio extension.</p>
<p>The Goanna VS extensions for VS2005/2008/2010 are written in C#, because there&#8217;s a wizard that generates a simple extension in C#.  From that starting point, we (meaning I) built the current extensions.  If there hadn&#8217;t been the wizard, I would have written the extensions in F#, because I prefer the functional style of programming.   So I decided to write the command-line tool, that was an opportunity to try out F# in earnest.  I&#8217;d written a wee bit of F# before; here was a chance to try it out on production code.</p>
<p>When writing the command-line tool, my main concern was, how easy would it be to pull in the C# code that does the project-to-command-line translation.  It was very easy: just add an F# project reference to the .DLL containing the code, open the namespace, and I was good to go.  I had to make some C# classes explicitly public for visibility, but that was the only change I needed to make.</p>
<p>Programming in F# is very much like programming in OCaml, a language I&#8217;ve used off and on for maybe 15 years.  Nice thing: instead of the clumsy &#8220;delegate&#8221; syntax of C#, you can just pass a function argument to another function.   Not so nice: in VS2010, the editor does not seem to auto-format F# code, the way it does with C# code (the Ctrl-E F magic).  And the editor&#8217;s Intellisense feature does not appear to suggest variable names that are in scope.  Also: although there are surely good reasons for it, the F# list type is distinct from the C#/.Net System.Collections list type, which is slightly maddening.  Finally: I have to build the tool for the various VS versions in slightly different ways, and the conditional compilation facility works for that &#8212; but why are there no boolean operations allowed, as you have in C#?</p>
<p>Here&#8217;s an example of code that uses .Net lists instead of F# lists:</p>
<pre>
  let expandedProjs = new System.Collections.Generic.List<EnvDTE.Project>() in
  while projsIter.MoveNext() do
    expandedProjs.AddRange(ProjectUtil.expandProject(projsIter.Current :?> EnvDTE.Project))
  done;
</pre>
<p>Ooof.</p>
<p>When the command-line tool starts, it fires up an instance of Visual Studio, no GUI.  That way it can get information about solutions and projects from VS, like default include paths and configuration information, using code originally written for the extensions.  Sometimes the calls to VS fail with COM retry errors, so those calls are done in a loop containing a try-with block.  When the tool finishes, or the user hits Ctrl-C, it gracefully shuts down VS.  I often run the tool from a Cygwin shell, and I haven&#8217;t yet found a way to trap Cygwin SIGKILL signals, so that VS is still running afterwards.</p>
<p>There&#8217;s still some work to do on the command-line tool, like deciding what kind of output it should produce, but it&#8217;s basically there.  Let me know if you&#8217;d like to try it out before we make it generally available.   The tool is tentatively called &#8220;GoRun&#8221;, and its syntax is:</p>
<pre>
  GoRun sln-file [projName ...] ...
</pre>
<p>That is, you supply one or more solution files, and for each solution file, zero or more project names.  If you don&#8217;t supply project names, GoRun invokes Goanna on all the projects in the solution, otherwise only those specified.</p>
<p>A complaint: Soon after VS2010 was released, the MSDN site was updated with all-new documentation for the .Net libraries. But the only language there&#8217;s documentation for is C# (OK, sometimes J#) .  In the type signatures, there are no hyperlinks for keywords (like public, final, etc.) and types.  You can&#8217;t tell when a type is really a forall-quantified type variable.  It wouldn&#8217;t be much harder to do these pages right.</p>
<p>One last comment: programming in F# doesn&#8217;t feel all that different than programming in C#, though the code is more concise.  You definitely feel the presence of .Net every step of the way, and there&#8217;s statefulness lurking everywhere.  Functional programming for the masses &#8230; sort of!</p>
]]></content:encoded>
			<wfw:commentRss>http://redlizards.com/blog/uncategorized/experiments-with-f/feed/</wfw:commentRss>
		</item>
		<item>
		<title>CamlP4 for unit testing</title>
		<link>http://redlizards.com/blog/uncategorized/camlp4-for-unit-testing/</link>
		<comments>http://redlizards.com/blog/uncategorized/camlp4-for-unit-testing/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 04:00:56 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
		
		<category><![CDATA[General]]></category>

		<category><![CDATA[camlp4]]></category>

		<category><![CDATA[Ocaml]]></category>

		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.redlizards.com/blog/?p=290</guid>
		<description><![CDATA[We&#8217;ve been mulling over how to add new unit tests to our OCaml codebase.  Our first thought was to write a test suite apart from the code itself; we&#8217;ve already some end-to-end tests written in that way.  The problem with that approach is that the tests are separated from the code they&#8217;re testing, [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve been mulling over how to add new unit tests to our OCaml codebase.  Our first thought was to write a test suite apart from the code itself; we&#8217;ve already some end-to-end tests written in that way.  The problem with that approach is that the tests are separated from the code they&#8217;re testing, which discourages writing tests.</p>
<p>A few weeks ago, we wrote some time- and heap-profiling CamlP4 macros, which add entries to a centralized table as our code runs.  When the program finishes, it spits out statistics that we can examine for time and space sinks.  The profiling hints &#8212; macros, that is &#8212; wrap the code to be profiled.  So why not use the same approach for our tests?</p>
<p><span id="more-290"></span></p>
<p>I wrote a CamlP4 macro which is used like this:<br />
<code><br />
LET_TEST some_fun : test1 = TestCase (fun () -&gt; .... some_fun ...)<br />
</code></p>
<p>Such a test can be put in the code right next to the function some_fun.  In fact, there are two versions of the macro.  One version registers the test in a centralized table.  The other version expands to (essentially) nothing, so the test is compiled away for release versions of our code.</p>
<p>Writing the macro was tricky &#8212; mostly because I don&#8217;t have a lot of experience with CamlP4.  The subtlest part was converting identifiers (some_fun and test1 above) to strings which are passed to the registration function.  I could&#8217;ve required the user to use strings directly in the code, but that&#8217;s ugly.</p>
<p>The macro calls the following function at expansion time:<br />
<code><br />
let string_of_patt p errMsg =<br />
match Ast.ident_of_patt p with<br />
| Ast.IdLid (_loc,x) -&gt; &lt;:expr&lt;$str:x$&gt;&gt;<br />
| _                        -&gt; raise (Failure errMsg)<br />
</code></p>
<p>This function extracts an identifier from the pattern, extracts the identifier text, then builds a string, which is syntactically an expression.  The &lt;:expr&lt; &gt;&gt; syntax is sugar for an Ast node representing an expression.  The _loc in the pattern clause is required, because it binds an identifier in the desugared syntax (boo &#8212; variable capture!).</p>
<p>Writing this macro seemed to harder than writing, say, the equivalent Scheme macro.  The CamlP4 syntax is kind of baroque, and some of its conventions aren&#8217;t intuitive.  But it lets us do some very useful things.</p>
]]></content:encoded>
			<wfw:commentRss>http://redlizards.com/blog/uncategorized/camlp4-for-unit-testing/feed/</wfw:commentRss>
		</item>
		<item>
		<title>In Praise of s-Expressions</title>
		<link>http://redlizards.com/blog/development/in-praise-of-s-expressions/</link>
		<comments>http://redlizards.com/blog/development/in-praise-of-s-expressions/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 00:33:58 +0000</pubDate>
		<dc:creator>David Crawshaw</dc:creator>
		
		<category><![CDATA[Development]]></category>

		<category><![CDATA[Ocaml]]></category>

		<guid isPermaLink="false">http://www.redlizards.com/blog/?p=176</guid>
		<description><![CDATA[OCaml is a wonderful language. But as with any language, there are certain features you would like to see.
The obvious one in OCaml is the lack of type-classes, specifically the lack of Haskell&#8217;s Show. There is no more useful debugging tool than printf, and it is a pain trying to debug complex types when you [...]]]></description>
			<content:encoded><![CDATA[<p>OCaml is a wonderful language. But as with any language, there are certain features you would like to see.</p>
<p>The obvious one in OCaml is the lack of type-classes, specifically the lack of Haskell&#8217;s <em>Show</em>. There is no more useful debugging tool than <em>printf</em>, and it is a pain trying to debug complex types when you can&#8217;t see them.</p>
<p>This is where s-expressions come in. Specifically, <a href="http://ocaml.janestreet.com/">Jane Street</a>&#8217;s <a href="http://ocaml.janestreet.com/?q=node/13">s-expression library</a>.</p>
<p><span id="more-176"></span></p>
<p>It provides a camlp4 pre-processor that lets you take a datatype:</p>
<pre>
type nat
  = Zero
  | Succ of nat
</pre>
<p>and tell the pre-processor to generate s-expression support functions:</p>
<pre>
open Sexplib
open Sexp

type nat
  = Zero
  | Succ of nat
  <strong>with sexp</strong>
</pre>
<p>Now in your code you have access to two new functions, with the types:</p>
<pre>
sexp_of_nat : nat -> Sexp.t
nat_of_sexp : Sexp.t -> nat
</pre>
<p>We can throw these together with some useful sexplib library functions:</p>
<pre>
string_of_sexp : Sexp.t -> string
sexp_of_string : string -> Sexp.t
</pre>
<p>And we have a string representation of our data-type. Easy!</p>
<h2>Pretty printing s-expressions</h2>
<p>We can use the built-in human-format pretty printer, that does nice line indenting. Here&#8217;s a full example file, <code>Tree.ml</code>:</p>
<pre>
TYPE_CONV_PATH "Succ"
open Sexplib
open Sexp

type tree = Leaf of int | Branch of tree * tree with sexp

let a = Branch ((Leaf 1), (Leaf 2)) in
let b = Branch (a, a) in
let c = Branch (b, a) in
let d = Branch (c, Leaf 3) in
let sexp = sexp_of_tree d in
Format.fprintf Format.std_formatter "%a@n" Sexp.pp_hum sexp
</pre>
<p>We compile using the sexplib pre-processor and linking against the sexp library:</p>
<pre>
ocamlfind ocamlc -linkpkg -package sexplib
  -pp "camlp4o -I `ocamlfind query type-conv` -I `ocamlfind query sexplib`
    pa_type_conv.cmo pa_sexp_conv.cmo"
  Tree.ml
</pre>
<p>Then we run and the output is:</p>
<pre>
$ ./a.out
(Branch
 (Branch (Branch (Branch (Leaf 1) (Leaf 2)) (Branch (Leaf 1) (Leaf 2)))
  (Branch (Leaf 1) (Leaf 2)))
 (Leaf 3))
</pre>
<h2>Reading in s-expressions</h2>
<p>Now let&#8217;s read in the s-expression for our natural numbers, add one, and then pretty-print the result:</p>
<pre>
TYPE_CONV_PATH "Succ"
open Sexplib
open Sexp

type nat = Zero | Succ of nat with sexp

let () =
  let sexp = input_sexp stdin in
  let number = Succ (nat_of_sexp sexp) in
  let new_sexp = sexp_of_nat number in
  Format.fprintf Format.std_formatter "%a@n" Sexp.pp_hum new_sexp
</pre>
<p>Note the use of a new function, <code>input_sexp : in_channel -> Sexp.t</code>. This reads an s-expression from an input channel. We can compile and run this program as before:</p>
<pre>
$ echo '(Succ Zero)' | ./a.out
(Succ (Succ Zero))
</pre>
<p>So now we not only have an equivalent to Haskell&#8217;s <i>Show</i>, but also to <i>Read</i>. And all we had to do is add <i>with sexp</i> to our data type!</p>
]]></content:encoded>
			<wfw:commentRss>http://redlizards.com/blog/development/in-praise-of-s-expressions/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Strict Beyond Reproach</title>
		<link>http://redlizards.com/blog/development/strict-beyond-reproach/</link>
		<comments>http://redlizards.com/blog/development/strict-beyond-reproach/#comments</comments>
		<pubDate>Sat, 23 May 2009 08:26:29 +0000</pubDate>
		<dc:creator>David Crawshaw</dc:creator>
		
		<category><![CDATA[Development]]></category>

		<category><![CDATA[C/C++]]></category>

		<category><![CDATA[Ocaml]]></category>

		<guid isPermaLink="false">http://www.redlizards.com/blog/?p=131</guid>
		<description><![CDATA[Pascal Cuoq made an interesting comment on my last post about C developers accidentally writing &#8220;==&#8221; in OCaml when they meant to use &#8220;=&#8221;. It reminds me of a similar issue I run into, when I am writing in OCaml but thinking in Haskell, and I am confronted with a value of type:
   [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">Pascal Cuoq made an interesting comment on my last post about C developers accidentally writing &#8220;==&#8221; in OCaml when they meant to use &#8220;=&#8221;. It reminds me of a similar issue I run into, when I am writing in OCaml but thinking in Haskell, and I am confronted with a value of type:</p>
<pre>   'a option list</pre>
<p><span id="more-131"></span><br />
My first thought when processing this is to use the function:</p>
<pre>    cat_options (l: 'a option list): 'a list</pre>
<p>which behaves just like Data.Maybe.catMaybes in Haskell. But I often find myself writing things like:</p>
<pre>   List.hd (cat_options someList)</pre>
<p align="justify">
&#8230;which would be efficient in Haskell, because catMaybes is lazy, and would only process the list until it found a value. But in OCaml, the entire list is processed by cat_options, then the first item is returned. This is wasteful, and could become a noticeable problem for long enough lists.</p>
<p>An efficient function would be:</p>
<pre>let first_some (l : 'a option list) : 'a option =
  let rec aux xs =
    match xs with
    | []           -&gt; None
    | None   :: xs -&gt; aux xs
    | Some x :: xs -&gt; Some x
  in
  aux l</pre>
<p>Thanks for the kind wishes Pascal!</p>
]]></content:encoded>
			<wfw:commentRss>http://redlizards.com/blog/development/strict-beyond-reproach/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Glossing over Bugs</title>
		<link>http://redlizards.com/blog/development/glossing-over-bugs/</link>
		<comments>http://redlizards.com/blog/development/glossing-over-bugs/#comments</comments>
		<pubDate>Fri, 15 May 2009 23:53:49 +0000</pubDate>
		<dc:creator>David Crawshaw</dc:creator>
		
		<category><![CDATA[Development]]></category>

		<category><![CDATA[C/C++]]></category>

		<category><![CDATA[Ocaml]]></category>

		<guid isPermaLink="false">http://www.redlizards.com/blog/?p=116</guid>
		<description><![CDATA[We have a set of end-to-end tests that run on Goanna every night. This ensures that our commits during the day don&#8217;t break our development tree too badly.
Here is one of those tests. It was producing a strange result.
void example(void) {
  int *x;
  x = malloc(sizeof(int));
  free(x);
  if (rand()) {
  [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">We have a set of end-to-end tests that run on Goanna every night. This ensures that our commits during the day don&#8217;t break our development tree too badly.</p>
<p>Here is one of those tests. It was producing a strange result.</p>
<pre>void example(void) {
  int *x;
  x = malloc(sizeof(int));
  free(x);
  if (rand()) {
    x = malloc(sizeof(int));
  }

  *x++;
}</pre>
<p><span id="more-116"></span>For this code, Goanna produced two warnings:
<p style="padding-left: 30px;">
<pre>ex.c:12: warning: Variable `x' may have been freed
ex.c:12: warning: Pointer `x' reassigned, possible memory leak</pre>
<p align="justify">
The first was expected. Depending on the output of rand(), sometimes we de-reference after a free(), a segfault waiting to happen. But what is this second warning about memory being reassigned?</p>
<p align="justify">I assumed it was a bug in Goanna, and immediately Paul and I started generating debugging output and messing with the example code.</p>
<p align="justify">After a full ten minutes of searching, Paul and I suddenly realised what *x++ does: First it de-references the pointer, returning the value pointed to by x, then it post-increments the <em>pointer</em> x. That is, after the statement *x++, x is no longer pointing to the same piece of memory. Calling free(x) after *x++ would lead to disaster.</p>
<p align="justify">So the second warning is no bug in Goanna, <strong>it is a bug in our C example</strong>. Only 13 lines long and I managed to get confused, because when I saw *x++, I read it as (*x)++.</p>
]]></content:encoded>
			<wfw:commentRss>http://redlizards.com/blog/development/glossing-over-bugs/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
