Thu, 22 May 2014 11:00:00 +0000 Paolo Donadeo — LifeLOG/Objective Caml Paolo Donadeo's personal Internet site and blog (Paolo Donadeo) Installing OPAM 1.1.1 on a CentOS 6.5 (Paolo Donadeo) Wed, 21 May 2014 23:00:00 +0000 2014-05-22T11:00:00.000Z English 4Sigma Computer programming Objective Caml Article <div> <p class="noindent">I'm in this situation: I need to compile an OCaml program on a CentOS 6.5 server. This is actually quite problematic, because CentOS 6.5 provide out of the box a very old OCaml 3.11.2, released by INRIA in January 2010, more than four years ago.</p> <p class="noindent">No problem, <a href="" title="OPAM - Home">OPAM</a> come to the rescue! No. The OPAM team doesn't provide a binary executable compatible with the (actually very old) system libraries present in CentOS. </p> <p class="noindent">Ok, step back: OPAM can be compiled from sources (<a href="" title="OPAM - Advanced Install">instructions here</a>) so it's just a matter of minutes. Again: nope. To compile OPAM you need at least OCaml 3.12.1, while in CentOS we have only 3.11.2.</p> <p class="noindent">Step back: first compile and install OCaml from sources. This time I say no, because the whole point of OPAM is to get an up and running OCaml environment in a few minutes, which is <strong>actually true</strong> in most cases.</p> <p class="noindent">I decide to install OCaml using <a href="" title="Get GODI">GODI</a>, the “old” OCaml source distribution, the one everybody used before OPAM was born. So the fastest steps to have an OPAM and OCaml environment on a CentOS server are:</p> <ul> <li>download and install GODI;</li> <li>clone the <a href="" title="GITHUB - ocaml/opam">OPAM repository</a>, compile and install it; it will find a “system” compiler provided by GODI;</li> <li><code>$ opam switch 4.01.0</code> will recompile a new environment;</li> <li><code>$ opam switch remove system</code> to delete the “system” environment;</li> <li><code>$ rm -Rf ~/godi</code>.</li> </ul> <p class="noindent">No root access is required in this process, I usually install OPAM in <code>~/opam</code>, and GODI in <code>~/godi</code>.</p> <p class="noindent">There is an irony in all this story: GODI has been <strong>the</strong> OCaml source distribution for years, and the coming of OPAM, which is actually newer and has some important features missing in GODI, has produced many frictions in the OCaml community, that in the end caused the <a href="" title="GODI is shutting down">shut down of GODI</a>.</p> <p class="noindent">The moral part here is: embrace the new things, but don't be too impatient in throwing out the window the precious work that, in the end, still works.</p> <p class="noindent">In any case, I want to thank both Gerd Stolpmann (author of GODI) and the OPAM team: they gave to the OCaml community a mature, industrial grade, set of tools to use OCaml.</p> </div> 5 First release of OCaml binding of Lua library (Paolo Donadeo) Sat, 01 Sep 2012 14:06:00 +0000 2012-09-01T14:06:00.000Z English Spare time Computer programming Objective Caml Lua News <div> <p class="noindent">I'm happy to announce the first release of ocaml-lua, the OCaml binding of the Lua library. With ocaml-lua you can embed a Lua interpreter in an OCaml program in a few lines of code, and use Lua for configuration or customization purposes.</p> <p class="noindent">Here are some references:</p> <ul> <li>The homepage of the project is hosted on <a href="">OCaml Forge</a>;</li> <li>The complete library reference (ocamldoc generated) is <a href="">here</a>;</li> <li>Source tarballs are on the download page on <a href="">OCaml Forge</a>;</li> <li>The official GIT repository is <a href="">here</a>;</li> <li>Bug reports and feature requests are on my page on <a href="">GitHub</a>.</li> </ul> <p class="noindent">I hope it could be useful.</p> </div> Debugging memory in OCaml: any advice? (Paolo Donadeo) Mon, 27 Feb 2012 23:37:00 +0000 2012-02-27T23:37:00.000Z English 4Sigma Computer programming Objective Caml Article <div> <a title="Memory consumption chart" class="zoom-box-image" href=""><img class="little left" src="" alt="Memory consumption chart" /></a> <p class="noindent">A server I just deployed (written in OCaml, of course) seems to eat RAM at breakfast. This is the chart of the RSS field of "ps" in the past 24h (click to enlarge). The program starts with almost 6 Mb and is now reaching 40 Mb, in a linear trend that has nothing good to say.</p> <p class="noindent">Any advice on how to debug the memory consumption?</p> </div> 1 Installing OCaml Batteries (Paolo Donadeo) Mon, 13 Dec 2010 22:55:00 +0000 2010-12-13T22:55:00.000Z English Digital life Computer programming Objective Caml Article <div> <p class="noindent">In this post I want to help OCaml newcomers to install Batteries. The task is trivial under Linux, while it's a bit tricky under Windows, because OCaml still lacks a self-contained Windows installer.</p> <p>My assumption is that the reader is a coder, so I will not explain <em>everything</em>… Let's start with the easy part: Linux.</p> <h4>Linux</h4> <p class="noindent">The installation of OCaml + Batteries under a Debian/Ubuntu system couldn't be easier, thanks the the hard work of the <a href="">Debian OCaml Task Force</a>. So open a terminal and type:</p> <pre class="brush: bash"> $ sudo aptitude install ocaml-batteries-included </pre> <p>That's all for Debian/Ubuntu. I don't know how Fedora works, but I think it's easy to install Batteries using YUM, something like:</p> <pre class="brush: bash"> $ yum install ocaml-batteries-included </pre> <p>If a RPM package for Batteries wasn't available, you could still install OCaml, Camomile (the Unicode library), and compile Batteries from sources, as described below for the Windows OS.</p> <h4>Windows</h4> <p class="noindent">As said, this OS still lacks a self contained installer which is in progress, at least for installing OCaml. Since <a href="">many OCaml versions</a> are available for Windows, with different <a href="">pros and cons</a>, I had to decide which one to use, and I decided to follow the simplest path to reach the goal of installing all the stuff we need. The Cygwin port is by far the simplest way.</p> <ol> <li>Download <a href="">Cygwin setup</a> and double click the executable. In Windows Vista/7 (I made my test on a Windows 7 64bit box) you will be required to allow the program to be run a couple of times, as usual ;-) …</li> <li>when the list of available packages appears, select: each and every package containing "caml" (see the screenshot below), and also <kbd>make</kbd>, <kbd>m4</kbd>, <kbd>libncurses-devel</kbd>, <kbd>git</kbd>, <kbd>wget</kbd> and <kbd>rlwrap</kbd>; <br /> <a title="Necessary Cygwin packages" class="zoom-box-image" href=""><img class="little left" src="" alt="Necessary Cygwin packages" /></a> <br style="clear:both;" /> </li> <li>open the Cygwin shell;</li> <li>download the Findlib library, version 1.2.6: <pre class="brush: bash"> $ wget </pre> </li> <li>unpack, compile and install Findlib: <pre class="brush: bash"> $ tar -xpzf findlib-1.2.6.tar.gz $ cd findlib-1.2.6/ $ ./configure $ make $ make install </pre> </li> <li>download, unpack, compile and install Camomile 0.8.1: <pre class="brush: bash"> $ wget $ tar -xpjf camomile-0.8.1.tar.bz2 $ cd camomile-0.8.1/ $ ./configure $ make $ make install </pre> </li> <li>the last step is to download compile and install Batteries itself. I wasn't able to compile the latest stable release (1.2.2), for an obscure preprocessor error, but using the latest GIT branch everything went smoothly. So here are the steps: <pre class="brush: bash"> $ git clone git:// $ cd batteries-included/ $ make camomile82 $ make all doc $ make install install-doc </pre> </li> </ol> <h4>Testing the installation</h4> <p class="noindent">Before starting to play with the library and the <em>toplevel</em> (the OCaml <a href="">REPL</a> is called toplevel) let's put into action a couple of helpers.</p> <ol> <li>The OCaml toplevel doesn't support readline. To get this feature back we add an alias to <kbd>.bashrc</kbd>. This works in both Linux and Windows: <pre class="brush: bash"> alias ocaml='rlwrap -H /home/paolo/.ocaml_history -D 2 -i -s 10000 ocaml' </pre> restart the terminal or load another bash; </li> <li>we need to load Batteries in the toplevel. This is not strictly necessary, but it helps a lot and the Batteries ASCII logo is wonderful <kbd>:-)</kbd>. All we need is to create a file named <kbd>.ocamlinit</kbd> in the home directory. Open your favorite editor and put this <em>phrases</em> in <kbd>~/.ocamlinit</kbd>: <pre class="brush: ocaml"> let interactive = !Sys.interactive;; Sys.interactive := false;; (*Pretend to be in non-interactive mode*) #use "topfind";; Sys.interactive := interactive;; (*Return to regular interactive mode*) Toploop.use_silently Format.err_formatter (Filename.concat (Findlib.package_directory "batteries") "");; </pre> </li> </ol> <p>If everything went well you can now type <kbd>ocaml</kbd> and something like this should appear:</p> <pre style="background-color: #fffdbf; font-weight: bold;"> $ ocaml Objective Caml version 3.11.2 _________________________ [| + | | Batteries - | |_____|_|_________________| _________________________ | - Type '#help;;' | | + |] |___________________|_|___| Loading syntax extensions... Camlp4 Parsing version 3.11.2 </pre> <h4>Conclusions</h4> <p class="noindent">This (rather boring) post has been devoted to the installation details of Batteries under Windows, where it presents some difficulties for newbies. Next time we will start on exploring the library with simple examples to exploit its strength.</p> </div> 5 Pearls of OCaml Batteries (1) (Paolo Donadeo) Sun, 05 Dec 2010 15:15:00 +0000 2010-12-05T15:15:00.000Z English Digital life Computer programming Objective Caml Article <div> <img class="little left" src="" alt="OCaml Batteries logo" /> <p class="noindent"><a href="">OCaml</a> is known to be a powerful functional programming language, but one of its presumed weakness is a relatively poor standard library.</p> <p class="noindent">By accident, I'm one of the few people on the planet considering this very clean and virtually bug free library a feature and not a bug, but this is only an opinion.</p> <p><a href="">The standard library</a> contains everything you need to build applications and other libraries, but it's <em>essential</em>, forget something like the Python standard library and things like “sending an email in one line of code”. Instead, think of the C standard library (plus some important data structures missing in the libc).</p> <p>More than two years ago the OCaml community decided to start the development of a library containing all the conveniences that are missing in the standard library. The project is <a href="">OCaml Batteries Included</a> and I'd like to introduce the reader with a series of posts, aimed to cast a light on various aspects of the library, without pretending to be an exhaustive tutorial.</p> <p>The posts will be targeted at novice OCaml programmers because I think that an experienced OCaml hacker already uses "Batteries" or, in any case, he understand the library API and doesn't need help from this blog.</p> <p>Before starting with the (boring) installation details, I want to give you a taste of Batteries, to show how a simple task could be written in a more natural way using Batteries modules, in comparison with a <em>vanilla</em> implementation. Let's take this simple task: we want to read a file by lines and print on the terminal only those lines containing a particular substring.</p> <p>A simple and actually working solution is proposed by the <a href="">PLEAC-Objective CAML</a> project, it's the very first example of the <a href="">file access</a> section. Here is the proposed code:</p> <pre class="brush: ocaml;"> let () = let in_channel = open_in "/usr/local/widgets/data" in try while true do let line = input_line in_channel in try ignore (Str.search_forward (Str.regexp_string "blue") line 0); print_endline line with Not_found -> () done with End_of_file -> close_in in_channel </pre> <p class="noindent">Now let's rephrase using Batteries:</p> <pre class="brush: ocaml;"> Enum.iter (fun l -> if BatString.exists l "blue" then print_endline l) (open_in "/usr/local/widgets/data" |> BatIO.lines_of) </pre> <p class="noindent">The result is the same, but the code is much cleaner and far more idiomatic for a functional language.</p> <p>Next time we will see how to install OCaml and Batteries, under Linux of course, but hopefully even under Windows.</p></div> 6 OCaml per programmatori Python (Paolo Donadeo) Sun, 22 Aug 2010 22:08:00 +0000 2010-08-22T22:08:00.000Z Italian Personal consultancy Computer programming Objective Caml Python Article <div><p class="noindent">Sorry for English speaking people, but this post makes sense only for Italians, so it's in Italian :-)</p> <p>Qualche mese fa, precisamente a fine Febbraio, mi è stato chiesto di presentare in un breve workshop il linguaggio <a href="">Objective Caml</a> che, manco a dirlo, è il mio linguaggio di programmazione preferito. Mi ero riproposto di scrivere qualcosa in merito, ma una serie di problemi me lo ha impedito, così colmo con qualche mese di ritardo la lacuna.</p> <p>L'invito mi è arrivato da <a href="">Develer</a>, un'azienda con sede <a href="">vicino Firenze</a> molto attenta alla formazione ed alle novità nel mondo delle tecnologie informatiche. In questo caso la novità non sta tanto nel linguaggio OCaml, che ha ormai 14 anni, quanto nel volersi avvicinare alla programmazione funzionale con uno sguardo pragmatico.</p> <p>Develer è poi l'azienda che contribuisce in maniera sostanziale alla realizzazione del <a href="">PyCon Italia</a>, una splendida manifestazione che è un unicum nel panorama culturale italiano, e non solo per i programmatori appassionati di Python, ma per tutti gli sviluppatori e smanettoni sufficientemente <em>smart</em> da non ignorare Python.</p> <p>Il background culturale legato a Python e l'assoluto digiuno di programmazione funzionale hanno portato ad un workshop in cui ho cercato di mostrare alcuni dei concetti della <a href="">FP</a>, utilizzando OCaml, e spiegandone nel contempo la sintassi in estrema sintesi, mostrando qualche parallelo con Python.</p> <p>Non so se il risultato sia stato positivo per Develer, perché da un lato non sono un vero esperto OCaml, e dall'altro ho dovuto (voluto?) concentrare in poco tempo una serie di concetti non banali, per cui forse non è stato facile per i presenti seguire il discorso. Comunque sia, nessuno mi ha tirato pomodori :-)</p> <p>Per il workshop ho ovviamente preparato una piccola presentazione che potete <a href="">scaricare in formato PDF</a>, sperando possa essere utile a qualcuno, non fosse altro per l'elenco di link a risorse relative ad OCaml. La presentazione è distribuita con licenza <a href="">Creative Commons (Attribuzione, condividi allo stesso modo)</a>, il che vuol dire che se siete così matti da copiarne dei pezzi, almeno ricordatevi di citarmi tra gli autori.</p></div> 3 Source repository of this blog (Paolo Donadeo) Sun, 08 Aug 2010 16:55:00 +0000 2010-08-08T16:55:00.000Z English Digital life Spare time Computer programming Objective Caml News <div><p class="noindent"> Following my previous post, some people <a href="">asked me</a> to show the source code of this blog, so here it is:<br /> <a href=""></a>.</p> <p>A general warning: the project wasn't thought to be published and so the code is a pile of scripts kept together in some way. Many parameters are present in the SQLite DB, but if you want to use the blog, expect to find my name, surname or email in the HTML templates, or even in some .ml files.</p> <p>Moreover, the source code is not homogeneous and the style changes from module to module, because I changed my way (and skill) of programming in OCaml in time.</p> <p>In any case, I hope you will find it interesting.</p></div> My new blog… in Objective Caml (Paolo Donadeo) Tue, 03 Aug 2010 08:26:00 +0000 2010-08-03T08:26:00.000Z English Digital life Computer programming Objective Caml Article <div><p class="noindent">More than a year ago I decided to dismiss my Wordpress blog, and to write a new one from scratch in OCaml. Unfortunately my spare time for programming projects is getting less and less, so this simple work took a lot of time to reach an usable state.</p> <p>I put online a preliminary release a month ago and immediately several bugs and little problems have arose, so I had to pass through the usual karma of recurring corrections. Now it seems stable.</p> <h4>Software design (strong words, actually!)</h4> <p class="noindent">To write this blog I decided to use <a href="">Ocamlnet</a> to produce a FastCGI server, providing dynamic contents through Apache, leaving the HTTP server alone to serve static resources, like Javascript, CSS and images.</p> <p>Ocamlnet is an excellent network framework, but it offers a low level interface, if compared with <a href="">Ocsigen</a>, which is web specific. Ocsigen (<a href="">Eliom</a>, actually) is a complete, modern and mature web framework, and I was afraid I couldn't use it, but AFAIK it lacks the possibility to create a FastCGI executable. I need this kind of setup because the blog runs on a production server of mine, where Apache is already installed and many virtual hosts are configured to run several sites and web apps, written in Django (Python) or even PHP. </p> <p>To write the blog <del>I stole many ideas</del> I was inspired by many ideas taken here and there. It's fair to list them, and to thank the authors:</p> <ul> <li>from Eliom I took the idea of a way to create the parameters of the web page in a type safe way, using <a href="">some combinators</a> that describe the expected type of the parameter and rebuild it from the CGI string from the browser. The implementation present in Eliom is of course outstanding, while my piece of code just solves a small subset of the problem of parameters reconstruction;</li> <li>the blog of <a href="">Mauricio Fernández</a> is written with Ocsigen (<a href="">source here</a>) and contains a simple <a href="">markdown-like language processor</a> used to format comments. I wrote something like that, but I'm learning to write parsers combinators so my implementation only processes a subset of the language recognized by Mauricio's parser;</li> <li><a href="">Jake Donham</a> wrote lwt-equeue as part of the <a href="">orpc</a> project. It's a module providing the same interface of <a href="">Lwt</a>, but runs on <a href="">equeue</a>, the low level event loop engine of Ocamlnet, and I used it to write a simple SMTP client in a monadic way without having to drop Ocamlnet and FastCGI;</li> <li><a href="">Batteries</a> is of course used almost everywhere;</li> <li>as a template engine to render HTML I used <a href="">CamlTemplate</a> by Benjamin Geer (maintained by Dmitry Grebeniuk).</li> </ul> <p>The resulting software is far from being well structured and designed, but I wrote yet another blog exactly to have the opportunity to test many libraries and programming paradigms in OCaml and, with respect to this goal, the result is a success :-)</p> <h4>Blog features</h4> <p class="noindent">While the code is a mess, the blog itself has many useful features: a very simple administration to edit posts, RSS feeds, comments, <a href="">antispam</a>, and a completely useless URL shortening service available at <a href=""></a>.</p> <h4>Performance</h4> <p class="noindent">What about the performance of the whole object? I don't know how to realistically test the blog, because using the classical <code>ab</code> is rather pointless, since it is generally used with static resources that, in my case, are served by Apache. If someone has some good ideas about 1) how to make a stress test and 2) have some good reference in term of expected hit per second, please don't hesitate and contact me, of course to blame me about how the blog is slow.</p> <h4>Conclusions</h4> <p class="noindent">The blog is up and running and I don't see any particular bug. In the next months I'll probably add some features, like the possibility to follow comments via email, or a decent XML site map.</p></div> 6