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

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

<item>
	<title>John Whitington: Compiling Code Under OCaml and F# (Part Two)</title>
	<guid isPermaLink="true">http://coherentpdf.com/blog/?p=12</guid>
	<link>http://coherentpdf.com/blog/?p=12</link>
	<description>&lt;p&gt;&lt;a href=&quot;http://coherentpdf.com/blog/?p=10&quot;&gt;[Part One]&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Twenty thousand lines of &lt;a href=&quot;http://www.coherentpdf.com/ocaml-libraries.html&quot;&gt;CamlPDF&lt;/a&gt; and &lt;a href=&quot;http://www.coherentpdf.com/&quot;&gt;cpdf&lt;/a&gt; later, here are some numbers:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt; Occasions on which conditional compilation is required: 22&lt;/li&gt;
&lt;li&gt; Compilation warnings with fsc –no-warn 62: 15&lt;/li&gt;
&lt;li&gt; Time taken: 22 hours&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The current executable appears to be about 8 times slower than OCaml native compilation, but I haven’t examined this enough to know how much we might be able to improve upon it.&lt;/p&gt;
&lt;p&gt;I’m planning to clean up the code to see how much of the conditional compilation we can get into a single Utility module.&lt;/p&gt;
&lt;p&gt;What’s next is to repackage the &lt;a href=&quot;http://www.coherentpdf.com/&quot;&gt;command line tools&lt;/a&gt; as an API for .NET users. I know very little about this topic, so it’s going to require quite a bit of effort before I’m willing to put it on sale and support it.&lt;/p&gt;
&lt;p&gt;If you’re familiar with packaging up libraries for .NET and would like to beta test, drop me a line in the comments, or via our &lt;a href=&quot;http://www.coherentpdf.com/&quot;&gt;website&lt;/a&gt;.&lt;/p&gt;</description>
	<pubDate>Fri, 04 Jul 2008 14:02:53 +0000</pubDate>
</item>
<item>
	<title>David Teller: Extrapol update</title>
	<guid isPermaLink="false">http://dutherenverseauborddelatable.wordpress.com/?p=107</guid>
	<link>http://dutherenverseauborddelatable.wordpress.com/2008/07/04/extrapol-update/</link>
	<description>&lt;div class=&quot;snap_preview&quot;&gt;&lt;br /&gt;&lt;p&gt;A quick work regarding the current status of Extrapol and its release.&lt;/p&gt;
&lt;p style=&quot;text-align: justify;&quot;&gt;Development of Extrapol progresses. With our current set of sample, Extrapol works flawlessly. We’re now adding features, improving error reporting and de-hard-wiring the model of the C standard library from the tool and moving it towards an external configuration file as well as progressively moving towards larger and more realistic samples. Development will come to an abrupt (and temporary) halt at the end of this week, though, due to personal matters (i.e. I’m getting married).&lt;/p&gt;
&lt;p style=&quot;text-align: justify;&quot;&gt;The release planned for next week, on the other hand, is canceled. As the research field of applied security is very competitive, and after careful discussion with the rest of my research team, we have decided to only release a version of Extrapol after the scientific content has been accepted for publication in a conference or journal. At the request of one of the institutes which founds this research, I will also refrain from posting detailed information on the theory and algorithms behind Extrapol, until these are cleared by the institute and accepted for publication. Without entering the details, Extrapol is expected to serve in critical infrastructures, which explains the need for clearance.&lt;/p&gt;
&lt;p style=&quot;text-align: justify;&quot;&gt;However, rest assured that there &lt;em&gt;will&lt;/em&gt; be a release and it &lt;em&gt;will&lt;/em&gt; be open-source (presumably licenced under a combination of MIT and LGPL). The only question is when — and this probably won’t happen before November.&lt;/p&gt;
&lt;img src=&quot;http://feeds.wordpress.com/1.0/categories/dutherenverseauborddelatable.wordpress.com/107/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt; &lt;img src=&quot;http://feeds.wordpress.com/1.0/tags/dutherenverseauborddelatable.wordpress.com/107/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt; &lt;a href=&quot;http://feeds.wordpress.com/1.0/gocomments/dutherenverseauborddelatable.wordpress.com/107/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/comments/dutherenverseauborddelatable.wordpress.com/107/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.wordpress.com/1.0/godelicious/dutherenverseauborddelatable.wordpress.com/107/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/delicious/dutherenverseauborddelatable.wordpress.com/107/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.wordpress.com/1.0/gostumble/dutherenverseauborddelatable.wordpress.com/107/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/stumble/dutherenverseauborddelatable.wordpress.com/107/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.wordpress.com/1.0/godigg/dutherenverseauborddelatable.wordpress.com/107/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/digg/dutherenverseauborddelatable.wordpress.com/107/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.wordpress.com/1.0/goreddit/dutherenverseauborddelatable.wordpress.com/107/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/reddit/dutherenverseauborddelatable.wordpress.com/107/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;img src=&quot;http://stats.wordpress.com/b.gif?host=dutherenverseauborddelatable.wordpress.com&amp;amp;blog=1202429&amp;amp;post=107&amp;amp;subd=dutherenverseauborddelatable&amp;amp;ref=&amp;amp;feed=1&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/div&gt;</description>
	<pubDate>Fri, 04 Jul 2008 11:09:01 +0000</pubDate>
</item>
<item>
	<title>David Baelde: Enigmes</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-17133288.post-5226956284706444364</guid>
	<link>http://misterpingouin.blogspot.com/2007/11/enigme.html</link>
	<description>Quand on pose une énigme mathématique, à moitié formalisée, on finit toujours par avoir une réponse type: &quot;on enlève le bandeau qu'on a sur les yeux&quot;, &quot;on casse la gueule au bourreau&quot;, etc. D'où l'idée de formaliser vraiment, sous forme d'un exercice de programmation... (qui a dit &quot;maniaque&quot; ?)&lt;br /&gt;&lt;br /&gt;Un groupe de &lt;code&gt;n&lt;/code&gt; prisonniers se trouve en mauvaise posture. Ils vont être mis en file indienne, chaque prisonnier voyant seulement les prisonniers devant lui. Puis on leur mettra sur la tête un chapeau bleu, blanc ou rouge, qu'ils ne peuvent voir. Un bourreau procède alors comme suit: en partant du dernier prisonnier il demande &quot;Quelle est la couleur de ton chapeau ?&quot;, et le prisonnier n'a le droit qu'à une réponse, qui doit être une couleur. A la fin, les prisonniers qui ont deviné la couleur de leur chapeau sont épargnés, les autres exécutés. Les prisonniers peuvent se concerter avant l'épreuve sur la stratégie à adopter. Quelle est la meilleure stratégie ?&lt;br /&gt;&lt;br /&gt;Voici maintenant l'énoncé complètement formalisé. Un prisonnier est représenté par une fonction qui prend deux listes de couleurs (celles qu'il voit devant lui, celles qui ont été prononcées comme réponses derrière lui), et renvoie une couleur (sa réponse). Dans la liste des réponses précédentes, la réponse du dernier de la file est la dernière de la liste. Trouver une fonction &lt;code&gt;prisonniers&lt;/code&gt; qui à un entier &lt;code&gt;n&lt;/code&gt; associe une liste de &lt;code&gt;n&lt;/code&gt; fonctions/prisonniers telle que le nombre de morts calculé par &lt;code&gt;(execution (prisonniers n))&lt;/code&gt; soit le plus petit possible dans le pire cas. La fonction d'exécution est la suivante:&lt;br /&gt;&lt;pre&gt;type couleur = Bleu | Blanc | Rouge&lt;br /&gt;type prisonnier = couleur list -&amp;gt; couleur list -&amp;gt; couleur&lt;br /&gt;&lt;br /&gt;let random_couleur () =&lt;br /&gt;  let n = Random.int 3 in&lt;br /&gt;    if n = 0 then Bleu else if n = 1 then Blanc else Rouge&lt;br /&gt;&lt;br /&gt;let execution (prisonniers : prisonnier list) =&lt;br /&gt;  let prisonniers =&lt;br /&gt;    List.map (fun p -&amp;gt; p, random_couleur ()) prisonniers&lt;br /&gt;  in&lt;br /&gt;  let rec morts avant = function&lt;br /&gt;    | (p,c)::l -&amp;gt;&lt;br /&gt;        let reponse = p (List.map snd l) avant in&lt;br /&gt;          (if reponse = c then 0 else 1) +&lt;br /&gt;          (morts (reponse::avant) l)&lt;br /&gt;    | [] -&amp;gt; 0&lt;br /&gt;  in&lt;br /&gt;    morts [] prisonniers&lt;/pre&gt;&lt;br /&gt;Amusez-vous bien, et postez vos réponses dans le langage de votre choix...&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Edit&lt;/b&gt;: une autre énigme beaucoup plus étonnante, que Florent et Julien m'ont racontée. Vous avez devant vous sur une table 7 pièces côté pile et 19 pièces côté face. Mais vos yeux sont bandés (et vous portez des moufles) de sorte que vous ne pouvez savoir l'état d'une pièce. Tout ce que vous pouvez faire c'est en retourner certaines, autant que vous voulez, et les déplacer. Il vous faut les séparer en deux groupes qui contiennent le même nombre de pièces côté pile.&lt;br /&gt;&lt;br /&gt;La solution sera une fonction qui prend le nombre de piles et le nombre de faces (je vous aide bien en généralisant) et qui renvoie deux tableaux de taille &lt;code&gt;piles+faces&lt;/code&gt;: le premier indique les indices des pièces choisies, le second indique les pièces qu'il faut retourner. La fonction sera passée à la fonction de test suivantes:&lt;br /&gt;&lt;pre&gt;(** Perfect Fisher-Yates shuffle&lt;br /&gt;  * (http://www.nist.gov/dads/HTML/fisherYatesShuffle.html). *)&lt;br /&gt;let randomize a =&lt;br /&gt;  let permute i j =&lt;br /&gt;    let tmp = a.(i) in&lt;br /&gt;      a.(i) &amp;lt;- a.(j);&lt;br /&gt;      a.(j) &amp;lt;- tmp&lt;br /&gt;  in&lt;br /&gt;  let l = Array.length a in&lt;br /&gt;    if l&amp;gt;=2 then&lt;br /&gt;      for i=0 to l-2 do&lt;br /&gt;        permute i (i + Random.int (l-i))&lt;br /&gt;      done&lt;br /&gt;&lt;br /&gt;let test nb_piles nb_faces joueur =&lt;br /&gt;  let choix,retourne = joueur nb_piles nb_faces in&lt;br /&gt;  let n = nb_piles + nb_faces in&lt;br /&gt;  let a = Array.init n (fun i -&amp;gt; i&amp;lt;nb_piles) in&lt;br /&gt;  (* Nombre de piles à gauche - nombre de piles à droite *)&lt;br /&gt;  let d = ref 0 in&lt;br /&gt;    randomize a ;&lt;br /&gt;    for i = 0 to n-1 do&lt;br /&gt;      if retourne.(i) then a.(i) &amp;lt;- not a.(i) ;&lt;br /&gt;      d := (if choix.(i) then (+) else (-)) !d (if a.(i) then 1 else 0)&lt;br /&gt;    done ;&lt;br /&gt;    !d = 0&lt;/pre&gt;&lt;br /&gt;A suivre: les solutions commentées...</description>
	<pubDate>Thu, 03 Jul 2008 13:30:18 +0000</pubDate>
	<author>noreply@blogger.com (mrpingouin)</author>
</item>
<item>
	<title>David Teller: Improving exception-management in OCaml</title>
	<guid isPermaLink="false">http://dutherenverseauborddelatable.wordpress.com/?p=105</guid>
	<link>http://dutherenverseauborddelatable.wordpress.com/2008/07/02/improving-exception-management-in-ocaml/</link>
	<description>&lt;div class=&quot;snap_preview&quot;&gt;&lt;br /&gt;&lt;h2 style=&quot;text-align: justify;&quot;&gt;Short version&lt;/h2&gt;
&lt;p style=&quot;text-align: justify;&quot;&gt;Catch me if you can is a small library for OCaml 3.10. The latest release is version 0.2, which you may find &lt;a href=&quot;http://www.univ-orleans.fr/lifo/Members/David.Teller/software/exceptions/catch_0_2.tgz&quot;&gt;here&lt;/a&gt;. This library improves management of errors in OCaml. It is released under the LGPL licence. It has been written by David Teller, Arnaud Spiwack, Till Varoquaux and Gabriel Scherer.&lt;/p&gt;
&lt;p&gt;&lt;span id=&quot;more-105&quot;&gt;&lt;/span&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;Long version&lt;br /&gt;
&lt;/strong&gt;&lt;/h2&gt;
&lt;p align=&quot;justify&quot;&gt;As all languages of the ML family — and most modern languages indeed — OCaml permits the management of exceptional situations using &lt;em&gt;exceptions&lt;/em&gt;. This mechanism lets programmer register protected sections of code, as well as &lt;em&gt;exception handlers&lt;/em&gt; to handle any exception which may be &lt;em&gt;raised&lt;/em&gt; during the execution of a protected section. Whenever an exception is &lt;em&gt;raised&lt;/em&gt;, the protected section of code is immediately stopped and the corresponding exception handler is executed instead. In addition, exceptions may convey some information regarding the nature of the exceptional circumstance.&lt;/p&gt;
&lt;p align=&quot;justify&quot;&gt;In OCaml, the mechanism is fast, it’s convenient and it’s type-safe, much like the rest of the language (barring any type-unsafe interaction with C). However, a few things are missing. If we consider the rest of the language, exceptions are both heavyweight and clumsy: each exception must be declared before being used and there’s no way to introduce a polymorphic type parameter in the exception. In addition, languages such as Java offer to important features missing in OCaml: automatic case coverage and exception hierarchies. While &lt;a href=&quot;http://caml.inria.fr/pub/old_caml_site/ocamlexc/ocamlexc.htm&quot;&gt;a nice tool&lt;/a&gt; exists  to provide case coverage for exceptions in OCaml, this tool is complex and  unfortunately unmaintained.&lt;/p&gt;
&lt;p align=&quot;justify&quot;&gt;&lt;em&gt;Catch me if you can&lt;/em&gt; offers an alternative mechanism, comparable to ML exceptions, to handle errors. In comparison with OCaml’s native exception mechanism, this library adds:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;automatic inference of exceptions (i.e. no need to declare your exceptions, unless you want to)&lt;/li&gt;
&lt;li&gt;more flexible exceptions (i.e. exceptions may have polymorphic type parameters, constraints, etc.)&lt;/li&gt;
&lt;li&gt;hierarchies (i.e. an IOException is a sub-case of Exception and a super-case of NetworkException)&lt;/li&gt;
&lt;li&gt;case coverage (i.e. the compiler can tell you if you forgot a case or sometimes if you wrote useless ones)&lt;/li&gt;
&lt;li&gt;conditional success handlers (i.e. do something with the result in case of success)&lt;/li&gt;
&lt;li&gt;conditional success-and-failure handlers (i.e. “finally”).&lt;/li&gt;
&lt;/ul&gt;
&lt;p style=&quot;text-align: justify;&quot;&gt;To attain this, we replace the mechanism of exceptions by an error monad, we replace exception constructors with polymorphic variants and we introduce a dose of syntactic sugar.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;Examples&lt;/h2&gt;
&lt;h3&gt;Expression evaluator&lt;/h3&gt;
&lt;p&gt;Let’s write a simple expression evaluator for the following set of expressions:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;
type expr =
  | Value of float
  | Div     of expr * expr
  | Add    of expr * expr
  | Mult   of expr * expr
  | Subs  of expr * expr
&lt;/pre&gt;
&lt;p&gt;These may be evaluated using the following function:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;
let rec eval = function
 | Value x    -&amp;gt; x
 | Add (x,y) -&amp;gt; eval x +. eval y
 | Mult(x,y)  -&amp;gt; eval x *. eval y
 | Div(x,y)   -&amp;gt; eval x /. eval y
 | Subs(x,y) -&amp;gt; eval x -. eval y
&lt;/pre&gt;
&lt;p style=&quot;text-align: justify;&quot;&gt;Of course, this function is bound to fail in case of division by zero. While this is expected, there is nothing in the source code — much less in the type of the function — to let us know which exception will be raised in case of division by zero.&lt;/p&gt;
&lt;p&gt;An alternative would be to add manual error checking, as follows:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;
type ('a, 'b) result =
 | Ok of 'a
 | Error of 'b

let rec eval = function
 | Value x    -&amp;gt; OK x
 | Add (x,y) -&amp;gt; (match eval x with
                      | Error e -&amp;gt; Error e
                      | Ok x' -&amp;gt;  match eval y with
                               | Error e -&amp;gt; Error e
                               | Ok y'    -&amp;gt; Ok (x' +. y'))
 | Mult (x,y) -&amp;gt; (match eval x with
                      | Error e -&amp;gt; Error e
                      | Ok x' -&amp;gt;  match eval y with
                               | Error e -&amp;gt; Error e
                               | Ok y'    -&amp;gt; Ok (x' *. y'))
 | Div (x,y) -&amp;gt; (match eval x with
                      | Error e -&amp;gt; Error e
                      | Ok x' -&amp;gt;  match eval y with
                               | Error e -&amp;gt; Error e
                               | Ok y'    -&amp;gt; if y' = 0. then Error &quot;Division by zero&quot;
                                                else              Ok (x' /. y'))
 | Subs (x,y) -&amp;gt; (match eval x with
                      | Error e -&amp;gt; Error e
                      | Ok x' -&amp;gt;  match eval y with
                               | Error e -&amp;gt; Error e
                               | Ok y'    -&amp;gt; Ok (x' -. y'))
(*eval : expr -&amp;gt; (float, string) result*)
&lt;/pre&gt;
&lt;p style=&quot;text-align: justify;&quot;&gt;After this transformation, the type of the exception appears in the type of &lt;code&gt;eval&lt;/code&gt; — here, we used strings, but anything else would have been fine. Of course, the downside is that this is unreadable. Well, what about the following ?&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;
let rec eval = function
 | Value x    -&amp;gt; return x
 | Add (x,y) -&amp;gt; perform with module Error
                        x' &amp;lt;-- eval x;
                        y' &amp;lt;-- eval y;
                        return (x' +. y' &lt;img src=&quot;http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif&quot; alt=&quot;;)&quot; class=&quot;wp-smiley&quot; /&gt;
 | Mult (x,y) -&amp;gt; perform with module Error
                        x' &amp;lt;– eval x;
                        y' &amp;lt;– eval y;
                        return (x' *. y' &lt;img src=&quot;http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif&quot; alt=&quot;;)&quot; class=&quot;wp-smiley&quot; /&gt;
 | Div (x,y) -&amp;gt; perform with module Error
                        x' &amp;lt;– eval x;
                        y' &amp;lt;– eval y;
                        if y'=0. then throw &quot;Division by zero&quot;
                        else            return (x' /. y' &lt;img src=&quot;http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif&quot; alt=&quot;;)&quot; class=&quot;wp-smiley&quot; /&gt;
 | Subs (x,y) -&amp;gt; perform with module Error
                        x' &amp;lt;– eval x;
                        y' &amp;lt;– eval y;
                        return (x' -. y' &lt;img src=&quot;http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif&quot; alt=&quot;;)&quot; class=&quot;wp-smiley&quot; /&gt;
(*eval : expr -&amp;gt; (float, string) result*)
&lt;/pre&gt;
&lt;p style=&quot;text-align: justify;&quot;&gt;This extract uses [our customized version of] Pa_monad (included in the package), which brings syntactic support for monads. While this is more verbose than the original version, it’s also safer, insofar as we can guarantee that exceptions won’t remain uncaught.&lt;/p&gt;
&lt;p&gt;Still too long? Then what about using the appropriate operators?&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;
open Error.Operators
let rec eval = function
 | Value x    -&amp;gt; x
 | Add (x,y) -&amp;gt; eval x +. eval y
 | Mult(x,y) -&amp;gt; eval x *. eval y
 | Div(x,y)   -&amp;gt; eval x /. eval y
 | Subs(x,y) -&amp;gt; eval x -. eval y
&lt;/pre&gt;
&lt;p&gt;Except for the module opening, that’s the same thing as our first listing. Just with the added safety.&lt;/p&gt;
&lt;h3&gt;Throwing, catching and hierarchies&lt;/h3&gt;
&lt;p&gt;By the way, the type of the result is&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;
(*eval : expr -&amp;gt; (float, [&amp;gt; `Arithmetic of (unit, [&amp;gt; `Div_by_zero of (unit, _) exc ]) exc ]) result*)
&lt;/pre&gt;
&lt;p&gt;That is, eval may either succeed and return a float or fail and return an arithmetic exception, which also turns out to be a division by zero. That’s classes of exceptions.&lt;/p&gt;
&lt;p&gt;With our syntactic sugar, raising such an exception is done by&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;
throw (exception Arithmetic (); Div_by_zero ())
&lt;/pre&gt;
&lt;p style=&quot;text-align: justify;&quot;&gt;Note that we could have put some content instead of &lt;code&gt;()&lt;/code&gt;. Note that exceptions are typed as they appear in the code and don’t need to be declared (if you wonder, polymorphic variants are involved in this).&lt;/p&gt;
&lt;p&gt;Of course, various kinds of exceptions may be combined, as in the following extract:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;
match ... with
| 1 -&amp;gt; throw (exception Arithmetic (); Div_by_zero ())
| 2 -&amp;gt; throw (exception Arithmetic (); Overflow &quot;by gosh !&amp;amp;quot &lt;img src=&quot;http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif&quot; alt=&quot;;)&quot; class=&quot;wp-smiley&quot; /&gt;
| 3 -&amp;gt; throw (exception IO file_descr)
| …
(*
('a, [&amp;gt; `Arithmetic of (unit, [&amp;gt; `Div_by_zero of (unit, _) exc
                                     |   `Overflow of (string, _)   exc]) exc
     |   `IO of (int, [`&amp;gt; ]) exc ]) result*)
*)
&lt;/pre&gt;
&lt;p&gt;While the type of the expression is difficult to read, catching is easy&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;
attempt ... with
 | val s -&amp;gt; (*success*)
 | Arithmetic (); Div_by_zero () -&amp;gt; (*Division by zero*)
 | Arithmetic (); _                   -&amp;gt; (*Other arithmetic*)
 | IO _                                  -&amp;gt; (*Some IO stuff *)
 | finally _                             -&amp;gt; (*Don't forget to close the door*)
&lt;/pre&gt;
&lt;p&gt;This extract introduces three keywords:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;attempt&lt;/code&gt; is our replacement for &lt;code&gt;try&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;val&lt;/code&gt; is used to pattern-match against the result of a successful evaluation&lt;/li&gt;
&lt;li&gt;&lt;code&gt;finally&lt;/code&gt; is used to pattern-match against the final result, whether this result was obtained after a successful evaluation or after an exception was raised and handled.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Unbreaking tail-recursion&lt;/h3&gt;
&lt;p&gt;The following extract is wrong:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;
let line_count filename =
  let rec loop file count =
  try
    ignore (input_line file);
    loop file (count + 1)
  with
    End_of_file -&amp;gt; count
  in
    loop (open_file filename) 0
&lt;/pre&gt;
&lt;p align=&quot;justify&quot;&gt;Don’t get me wrong, it will compile and run. The problem is that it’s not tail-recursive. In other words, it will be much slower and much more memory-consuming than if exceptions had been ignored. Why ? Because exception End_of_file may have been raised from the next call to loop, so the recursive call cannot be optimized into a non-recursive call. Of course, exceptions can’t be ignored in this extract, as they are required to determine when to stop reading the file. Now, a simple transformation would make the problem go away :&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;
let line_count filename =
  let rec loop file count =
    let should_continue =
    try
      ignore (input_line file);
      true
    with End_of_file -&amp;gt; false
  in
    if should_continue then loop file (count + 1)
    else                    count
  in
    loop (open_file filename) 0
&lt;/pre&gt;
&lt;p style=&quot;text-align: justify;&quot;&gt;Well, the transformation is simple, but it’s annoying and hard to read. What’s even more annoying is that it’s quite common. With Catch me if you can, we would rather write the following:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;
let input_line2 x = Error.legacy input_line x

let line_count filename =
  let rec loop file count =
    attempt input_line2 file with
      | val _ -&amp;gt; loop file (count + 1)
      | _     -&amp;gt; count
  in
  loop (open_file filename) 0
&lt;/pre&gt;
&lt;p style=&quot;text-align: justify;&quot;&gt;In this extract, legacy is a simple manner of wrapping an existing, one-argument, function and convert it to our new exception style. It’s not quite as good as wrapping the function manually and giving it an actual exception, but it’s better than nothing.&lt;/p&gt;
&lt;p align=&quot;justify&quot;&gt;All in all, the resulting function line_count is shorter, easier to read, takes less memory and is also faster than the original.&lt;/p&gt;
&lt;h2&gt;What about performance?&lt;/h2&gt;
&lt;p style=&quot;text-align: justify;&quot;&gt;Now, that’s a complex question. Short answer: there’s an acceptable performance loss. False short answer: we wrote a &lt;a href=&quot;http://www.univ-orleans.fr/lifo/Members/David.Teller/publications/ml2008.pdf&quot;&gt;paper&lt;/a&gt; on that subject.&lt;/p&gt;
&lt;img src=&quot;http://feeds.wordpress.com/1.0/categories/dutherenverseauborddelatable.wordpress.com/105/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt; &lt;img src=&quot;http://feeds.wordpress.com/1.0/tags/dutherenverseauborddelatable.wordpress.com/105/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt; &lt;a href=&quot;http://feeds.wordpress.com/1.0/gocomments/dutherenverseauborddelatable.wordpress.com/105/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/comments/dutherenverseauborddelatable.wordpress.com/105/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.wordpress.com/1.0/godelicious/dutherenverseauborddelatable.wordpress.com/105/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/delicious/dutherenverseauborddelatable.wordpress.com/105/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.wordpress.com/1.0/gostumble/dutherenverseauborddelatable.wordpress.com/105/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/stumble/dutherenverseauborddelatable.wordpress.com/105/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.wordpress.com/1.0/godigg/dutherenverseauborddelatable.wordpress.com/105/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/digg/dutherenverseauborddelatable.wordpress.com/105/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://feeds.wordpress.com/1.0/goreddit/dutherenverseauborddelatable.wordpress.com/105/&quot; rel=&quot;nofollow&quot;&gt;&lt;img src=&quot;http://feeds.wordpress.com/1.0/reddit/dutherenverseauborddelatable.wordpress.com/105/&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;img src=&quot;http://stats.wordpress.com/b.gif?host=dutherenverseauborddelatable.wordpress.com&amp;amp;blog=1202429&amp;amp;post=105&amp;amp;subd=dutherenverseauborddelatable&amp;amp;ref=&amp;amp;feed=1&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/div&gt;</description>
	<pubDate>Wed, 02 Jul 2008 14:46:28 +0000</pubDate>
</item>
<item>
	<title>Matías Giovannini: Sorting out Sorting</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-5888658295182480819.post-1953465720744409128</guid>
	<link>http://alaska-kamtchatka.blogspot.com/2008/07/sorting-out-sorting_02.html</link>
	<description>I believe that refactoring in the functional milieu is as important as it is in the object-oriented world. There's usually room for further improvement of one's definitions. Often, it is no more than the trivial application of selective inlining of definitions that clarify code that turned out more cluttered and opaque that it could have been, possibly from working at a remove from the problem</description>
	<pubDate>Wed, 02 Jul 2008 12:17:40 +0000</pubDate>
	<author>noreply@blogger.com (Matías Giovannini)</author>
</item>
<item>
	<title>The Caml Humps: Ooauth 0.1</title>
	<guid isPermaLink="true">http://caml.inria.fr/cgi-bin/hump.cgi?contrib=648</guid>
	<link>http://caml.inria.fr/cgi-bin/hump.cgi?contrib=648</link>
	<description>an implementation of the OAuth
1.0 protocol for OCaml, as well as an OCaml binding to Skydeck web API
(providing access to your cell phone call log).</description>
	<pubDate>Wed, 02 Jul 2008 07:52:56 +0000</pubDate>
</item>
<item>
	<title>Matías Giovannini: Bitonic Sorting</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-5888658295182480819.post-3529671655362192218</guid>
	<link>http://alaska-kamtchatka.blogspot.com/2008/06/bitonic-sorting.html</link>
	<description>The riffle shuffle I've written about is not a mere card trick. It is a basic building block in the art of devising parallel networks for efficient use of SIMD computers. A typical application is the so-called bitonic sort. An elementary introduction to the algorithm with animations might make its working clear, but I find Knuth's exposition transparent:


Let us say that a sequence 〈z1, … zp〉 of</description>
	<pubDate>Wed, 02 Jul 2008 00:25:44 +0000</pubDate>
	<author>noreply@blogger.com (Matías Giovannini)</author>
</item>
<item>
	<title>Skydeck: Skydeck API and OAuth for OCaml</title>
	<guid isPermaLink="true">http://skydeck.com/blog/announcements/skydeck-api-and-oauth-for-ocaml/</guid>
	<link>http://skydeck.com/blog/announcements/skydeck-api-and-oauth-for-ocaml/</link>
	<description>&lt;p&gt;The &lt;a href=&quot;http://skydeck.com/blog/announcements/three-more-reasons-to-love-your-phone-bill/&quot;&gt;Skydeck API&lt;/a&gt; had only been out a few days when &lt;a href=&quot;http://github.com/abhay/skydeck/tree/master&quot;&gt;someone began a Ruby binding for it&lt;/a&gt;. We’re thrilled that there’s been so much interest already, and look forward to some great applications built against our API.&lt;/p&gt;
&lt;p&gt;Of course, we like OCaml here, so we are pleased to announce the release of an OCaml binding to the API. You can find it on our &lt;a href=&quot;http://skydeck.com/developer/downloads/#ocaml-skydeck-api&quot;&gt;developer downloads page&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As part of our API design we decided to use &lt;a href=&quot;http://oauth.net/&quot;&gt;OAuth&lt;/a&gt;, so you can allow applications to access your data without giving away your Skydeck username and password. We are also pleased to announce the release of &lt;strong&gt;ooauth&lt;/strong&gt;, an OCaml implementation of OAuth.&lt;/p&gt;
&lt;p&gt;Ooauth implements both the consumer and service provider parts of OAuth (we use it in our server and in the API binding above), so you can use it to implement your own API or to consume the many APIs that use OAuth, such as the &lt;a href=&quot;http://googledataapis.blogspot.com/2008/06/oauth-for-google-data-apis.html&quot;&gt;Google Data APIs&lt;/a&gt;. It is also available on the &lt;a href=&quot;http://skydeck.com/developer/downloads/#ooauth&quot;&gt;developer downloads page&lt;/a&gt;.&lt;/p&gt;</description>
	<pubDate>Tue, 01 Jul 2008 19:37:02 +0000</pubDate>
</item>
<item>
	<title>Caml Weekly News: Caml Weekly News, 01 Jul 2008</title>
	<guid isPermaLink="true">http://alan.petitepomme.net/cwn/2008.07.01.html</guid>
	<link>http://alan.petitepomme.net/cwn/2008.07.01.html</link>
	<description>patterns v0.4 / ocamlhackers.ning.com is open / OCaml PLEAC reaches 70%</description>
	<pubDate>Tue, 01 Jul 2008 12:00:00 +0000</pubDate>
</item>
<item>
	<title>Ocamlcore Forge News: EThreads version 1.0.1 released</title>
	<guid isPermaLink="true">http://forge.ocamlcore.org/forum/forum.php?forum_id=112</guid>
	<link>http://forge.ocamlcore.org/forum/forum.php?forum_id=112</link>
	<description>The first early version of EThreads has been released.

EThreads extends threads library.
 - threadless ivar and mvar
 - several usefull modules such as mailbox, broadcast, timeout, rpc.</description>
	<pubDate>Mon, 30 Jun 2008 08:56:54 +0000</pubDate>
</item>

</channel>
</rss>
