<?xml version="1.0" encoding="UTF-8"?>


  <feed xmlns="http://www.w3.org/2005/Atom">
     
     <id>http://www.ocamlpro.com/atom/</id><title>OCamlPro's atom feed</title><rights>All rights reserved by the author</rights><updated>2013-04-22T00:00:00Z</updated><link rel="self" href="http://www.ocamlpro.com/feed/atom.xml"/><link rel="alternate" href="http://www.ocamlpro.com/" type="text/html"/>
     <contributor><name>Louis Gesbert</name><email>louis.gesbert@ocamlpro.com</email></contributor><contributor><name>Fabrice Le Fessant</name><uri>http://fabrice.lefessant.net/</uri><email>fabricel@ocamlpro.com</email></contributor><contributor><name>Thomas Gazagnaire</name><uri>http://gazagnaire.org</uri><email>thomas@ocamlpro.com</email></contributor>
     
  <entry>
    <id>http://www.ocamlpro.com/blog/2013/04/22/monthly-04.html</id><title>April Monthly Report</title><author><name>Thomas Gazagnaire</name><uri>http://gazagnaire.org</uri><email>thomas@ocamlpro.com</email></author><rights>All rights reserved by the author</rights><updated>2013-04-22T00:00:00Z</updated><link rel="alternate" href="http://www.ocamlpro.com/blog/2013/04/22/monthly-04.html"/>
    
    
  <content type="xhtml">
   <div xmlns="http://www.w3.org/1999/xhtml">
      <p>This post aims at summarizing the activities of OCamlPro for the past month. As usual, we worked in three main areas: the OCaml toolchain, development tools for OCaml and R&amp;D projects.</p><h2>The toolchain</h2><p>Our multi-runtime implementation of OCaml had gained stability. <a href="http://ageinghacker.net/">Luca</a> fixed a lot of low-level bugs in the &quot;master&quot; branch of <a href="http://www.github.com/lucasaiu/ocaml">his OCaml repository</a>, which were mainly related to the handling of signals. There are still some issues, which seem to be related to thread-switching (ie. when using OS level mutli-threading).</p><p>We made great progress on improved inlining strategy. In the current OCaml compiler, inlining, closure conversion and constant propagation are done in a single pass interleaved with analysis. It has served well until now, but to improve it in a way which is easily extensible in the future, it needs a complete rewrite. After a few prototypes, <a href="http://www.lsv.ens-cachan.fr/~chambart/">Pierre</a> is now coming up with a suitable intermediate language (IR) more suited for the job, using a dedicated value analysis to guide the simplification and inlining passes. This IR will stand between the lambda code and the C-lambda and is designed such that future specialized optimization can be easily be added. There are two good reasons for this IR: First, it is not as intrusive and reduces the extent of the modifications to the compiler, as it can be plugged between two existing passes and turned on or off using a command-line flag. Second, it can be tweaked to make the abstract interpretation more precise and efficient. For instance, we want the inlining to work with higher-order functions as well as modules and functors, performing basic defunctorization. It is still in an experimentation phase, but we are quickly converging on the API and hope to have something we can demo in the next months.</p><p>Our <a href="http://www.ocamlpro.com/blog/2012/08/08/profile-native-code.html">frame-pointer patch</a> has also been accepted. Note that, as this patch changes the calling sconvention of OCaml programs, you cannot link together libraries compiled with and without the patch. Hence, this option will be provided as a configuration switch (<code>./configure --with-frame-pointer</code>).</p><p>Regarding memory profiling, we released a preliminary prototype of the memory profiler for native code. It is available in <a href="https://github.com/cago/ocaml">Çagdas</a> repository. We are still in the process of testing and evaluating the prototype before making it widely available through OPAM. As the previous bytecode prototype, you need to compile the libraries and the program you want to profile as usual in order to build a table associating unique identifier to program locations (.prof file). Then, for each allocated block, we have then patched the runtime of OCaml to encode in the header the identifier of the line which allocated it. To be able to dump the heap, you can either instrument your program, or send a signal, or set the appropriate environment variable (<code>OCAMLRUNPARAM=m</code>). Finally, you can use the profiler which will read the .prof and .cmt files in order to generate a pdf file which is the amount of memory use by type. More details on this will come soon, you can already read the <a href="https://github.com/cago/ocaml/blob/4.00.1%2Bmemprof/README">README</a> file available on github.</p><p>Finally, we organized a new meeting with the core-team to discuss some of the bugs in the <a href="http://caml.inria.fr/mantis">OCaml bug tracker</a>. It was the first of the year, but we are now going to have one every month, as it has a very positive impact on the involvement of everybody in fixing bugs and helps focus work on the most important issues.</p><h2>Development Tools for OCaml</h2><p>Since the latest release of <a href="http://github.com/OCamlPro/ocp-indent">ocp-indent</a>, <a href="http://louis.gesbert.fr/cv.en.html">Louis</a> continued to improve the tool. We plan to release version 1.2.0 in the next couple of days, with some bug fixes (esp. related to the handling of records) and the following new features: operators are now aligned by default (as well as opened parentheses not finishing a line) and indentation can be bounded using the <code>max_indent</code> parameter. We are also using the great <a href="http://erratique.ch/software/cmdliner">cmdliner</a> which means <code>ocp-indent</code> now has nice manual pages.</p><p>We are also preparing a new minor release of <a href="http://opam.ocamlpro.com/">OPAM</a>, with a few bug fixes, an improved solver heuristic and improved performance. OPAM statistics seem to converge towards round numbers, as <a href="http://github.com/OCamlPro/opam">OcamlPro/opam</a> repository has recently reached 100 &quot;stars&quot; on Github, <a href="http://github.com/OCamlPro/opam-repository">OCamlPro/opam-repository</a> is not very far from being forked 100 times, while the number of unique packages on <a href="http://opam.ocamlpro.com">opam.ocamlpro.com</a> is almost 400. We are also preparing the platform release, with a cleaner and simpler client API to be used by the upcoming &quot;Ocamlot&quot;, the automated infrastructure which will test and improve the quality and consistency of OPAM packages.</p><p>Last, we released a very small - but already incredibly useful tool: <a href="http://github.com/OCamlPro/ocp-index">ocp-index</a>. This new tool provides completion based on what is installed on your system, with type and documentation when available. Similarly to <code>ocp-indent</code>, the main goal of this tool is to make it easy to integrate in your editor of choice. As a proof of concept, we also distribute a small curses-based tool, called <code>ocp-browser</code>, which lets you browse interactively the libraries installed on your system, as well as an emacs binding for <code>auto-complete.el</code>. Interestingly enough, behind the scene <code>ocp-index</code> uses a <a href="https://github.com/OCamlPro/ocp-index/blob/master/src/trie.mli">lazy immutable prefix tree</a> with merge operations to efficiently store and load cmis and cmt files.</p><h2>Other R&amp;D Projects</h2><p>We continued to work on the <a href="http://www.richelieu.pro/">Richelieu</a> project. We are currently adding basic type-inference for Scilab programs to our tool <a href="https://github.com/OCamlPro/richelieu/tree/jit-fabrice/scilab/modules/jit_ocaml/src/scilint">scilint</a>, to be able to print warnings on possible programers mistakes. A first part of the work was to understand how to automatically get rid of some of the <code>eval</code> constructs, especially <code>deff</code> and <code>evalstr</code> primitives that are often used. After this, <a href="https://github.com/Michaaell">Michael</a> manually analyzed some real-world Scilab programs to understand how typing should be done, and he is now implementing the type checker and a table of types for primitive functions.</p><p>We are also submitting a new project, called SOCaml, for funding by the French government. In 2010, <a href="http://www.ssi.gouv.fr/">ANSSI</a>, the French agency for the security of computer systems, commanded a study, called LAFOSEC, to understand the advantages of using functional languages in the domain of security. Early results of the study were presented in <a href="http://jfla.inria.fr/2013/programme">JFLA'2013</a>, with in particular recommandations on how to improve OCaml to use it for security applications. The goal of the SOCaml project would be to implement these recommandations, to improve OCaml, to provide additional tools to detect potential errors and to implement libraries to verify marshaled values and bytecode. We hope the project will be accepted, as it opens a new application domain for OCaml, and would allow us to work on this topic with our partners in the project, such as <a href="http://www.lexifi.com">LexiFi</a> and <a href="http://michel.mauny.net/">Michel Mauny</a>'s team at ENSTA Paristech (the project would also contribute to their <a href="http://github.com/ocaml-bytes/ocamlcc">ocamlcc</a> bytecode-to-c compiler).</p> 
   </div>
  </content>

  </entry>

  <entry>
    <id>http://www.ocamlpro.com/blog/2013/04/02/wxocaml-reloaded.html</id><title>wxOCaml, camlidl and Class Modules</title><author><name>Fabrice Le Fessant</name><uri>http://fabrice.lefessant.net/</uri><email>fabricel@ocamlpro.com</email></author><rights>All rights reserved by the author</rights><updated>2013-04-02T00:00:00Z</updated><link rel="alternate" href="http://www.ocamlpro.com/blog/2013/04/02/wxocaml-reloaded.html"/>
    
    
  <content type="xhtml">
   <div xmlns="http://www.w3.org/1999/xhtml">
      <p>Last week, I was bored doing some paperwork, so I decided to hack a little to relieve my mind...</p><h2>Looking for a GUI Framework for OCaml Beginners</h2><p>Some time ago, at OCamlPro, we had discussed the fact that OCaml was lacking more GUI frameworks. Lablgtk is powerful, but I don't like it (and I expect that some other people in the OCaml community share my opinion) for several reasons:</p><ul> <li> <p>LablGTK makes an extensive use of objects, labels and polymorphic variants. Although using these advanced features of OCaml can help expert OCaml developers, it makes LablGTK hard to use for beginners... and a good reason to have better GUIs is actually to attract beginners!</p> </li><li> <p>GTK does not look native under Windows and Mac OS X, giving an outdated feeling about interfaces written with it.</p> </li> </ul><p>Now, the question was, which GUI framework to support for OCaml ?  A long time ago, I had heard that <a href="http://www.wxwidgets.org/">wxWidgets</a> (formerly wxWindows) had contributed to the popularity of Python at some point, and I remembered that there was a binding called <a href="http://plus.kaist.ac.kr/~shoh/ocaml/wxcaml/doc/">wxCaml</a> that had been started by SooHyoung Oh a few years ago. I had managed to compile it a two years ago, but not to make the examples work, so I decided it was worth another try.</p><h2>From wxEiffel to wxCaml, through wxHaskell</h2><p>wxCaml is based on <a href="http://www.haskell.org/haskellwiki/WxHaskell">wxHaskell</a>, itself based on <a href="http://elj.sourceforge.net/projects/gui/ewxw/">wxEiffel</a>, a binding for wxWidgets done for the Eiffel programming language. Since wxWidgets is written in C++, and most high-level programming languages only support bindings to C functions, the wxEiffel developers wrote a first binding from C++ to C, called the <a href="https://github.com/OCamlPro/wxOCaml/tree/master/elj">ELJ library</a>: for each class wxCLASS of wxWidgets, and for each method Method of that class, they wrote a function wxCLASS_Method, that takes the object as first argument, the other arguments of the method, and then call the method on the first argument, with the other arguments. For example, the code for the <a href="https://github.com/OCamlPro/wxOCaml/blob/master/elj/eljwindow.cpp">wxWindow</a> looks a lot like that:</p><pre><code>EWXWEXPORT(bool,wxWindow_Close)(wxWindow* self,bool _force)
{
        return self-&gt;Close(_force);
}
</code></pre><p>From what I understood, they stopped maintaining this library, so the wxHaskell developers took the code and maintained it for wxHaskell. In wxHaskell, a few include files describe all these C functions. Then, they use a program 'wxc' that generates Haskell stubs for all these functions, in a class hierarchy.</p><p>In the first version of wxCaml, <a href="http://forge.ocamlcore.org/projects/camlidl/">camlidl</a> was used to generate OCaml stubs from these header files. The header files had to be modified a little, for two reasons:</p><ul> <li> <p>They are actually not correct: some parts of these header files have not been updated to match the evolution of wxWidgets API. Some of the classes for which they describe stubs does not exist anymore. The tool used by wxHaskell filters out these classes, because their names are hardcoded in its code, but camlidl cannot.</p> </li><li> <p>camlidl needs to know more information than just what is written in C header files. It needs some attributes on types and arguments, like the fact  that a char pointer is actually a string, or that a pointer argument to a function is used to return a value. See <a href="https://github.com/OCamlPro/wxOCaml/blob/master/idl/wxc_types.idl">wxc_types.idl</a> for macros to automate parts of this step.</p> </li><li> <p>camlidl was not used a lot, and not maintained for a long time, so there are some bugs in it. For example, the names of the arguments given in IDL header files can conflict with variables generated in C by camlidl (such as &quot;_res&quot;) or with types of the caml C API (such as &quot;value&quot;).</p> </li> </ul><p>Since the version of wxCaml I downloaded used outdated versions of wxWidgets (wxWindows 2.4.2 when current version is wxWidgets 2.9) and wxHaskell (0.7 when current version is 0.11), I decided to upgrade wxCaml to the current versions. I copied the ELJ library and the header files from the GitHub repository of wxHaskell. Unfortunately, the corresponding wxWidgets version is 2.9.4, which is not yet officially distributed by mainstream Linux distributions, so I had to compile it also.</p><p>After the painful work of fixing the new header files for camlidl, I was able to run a few examples of wxCaml. But I was not completely satisfied with it:</p><ul> <li> <p>To translate the relation of inheritance between classes for camlidl, wxCaml makes them equivalent, so that the child can be used where the ancestor can be used. Unfortunately, it means also that the ancestor can be used wherever the child would, and since most classes are descendant of wxObject, they can all be used in place of each other in the OCaml code !</p> </li><li> <p>A typed version of the interface had been started, but it was already making heavy use of objects, which I had decided to ban from the new version, as other advanced features of OCaml.</p> </li> </ul><h2>wxCamlidl, modifying camlidl for wxOCaml</h2><p>So, I decided to write a new typed interface, where each class would be translated into an abstract type, a module containing its methods as functions, and a few cast functions, from children to ancestors.</p><p>I wrote just what was needed to make two simple examples work (<a href="https://github.com/OCamlPro/wxOCaml/blob/master/examples/hello_world/hello.ml">hello_world</a> and <a href="https://github.com/OCamlPro/wxOCaml/blob/master/examples/two_panels/two_panels.ml">two_panels</a>, from wxWidgets tutorials), I was happy with the result:</p><p><a href="https://github.com/OCamlPro/wxOCaml/blob/master/examples/hello_world/hello.ml"><img src="http://www.ocamlpro.com//files/wxOCaml-screenshot-hello.png" alt="wxOCaml-screenshot-hello.png"/></a></p><p><a href="https://github.com/OCamlPro/wxOCaml/blob/master/examples/two_panels/two_panels.ml"><img src="http://www.ocamlpro.com//files/wxOCaml-screenshot-panels.png" alt="wxOCaml-screenshot-panels.png"/></a></p><p>But writting by hand the complete interface for all classes and methods would not be possible, so I decided it was time to write a tool for that task.</p><p>My first attempt at automating the generation of the typed interface failed because the basic tool I wrote didn't have enough information to correctly do the task: sometimes, methods would be inherited by a class from an ancestor, without noticing that the descendant had its own implementation of the method. Moreover, I didn't like the fact that camlidl would write all the stubs into a single file, and my tool into another file, making any small wxOCaml application links itself with these two huge modules and the complete ELJ library, even if it would use only a few of its classes.</p><p>As a consequence, I decided that the best spot to generate a modular and typed interface would be camlidl itself. I got a copy of its sources, and created a <a href="https://github.com/OCamlPro/wxOCaml/blob/master/wxCamlidl/wxmore.ml">new module in it</a>, using the symbolic IDL representation to generate the typed version, instead of the untyped version. The module would compute the hierarchy of classes, to be able to propagate statically methods from ancestors to children, and to generate cast functions from children to ancestors.</p><p>A first generated module, called <a href="https://github.com/OCamlPro/wxOCaml/blob/master/wxWidgets/wxClasses.mli">WxClasses</a> defines all the wxWidgets classes as abstract types:</p><pre><code>type eLJDragDataObject
and eLJMessageParameters
...
and wxDocument
and wxFrameLayout
and wxMenu
and wxMenuBar
and wxProcess
and ...
</code></pre><p>Types started by &quot;eLJ...&quot; are classes defined in the ELJ library for wxWidgets classes where methods have to be defined to override basic behaviors.</p><h2>Classes as modules</h2><p>For each wxWidget class, a specific module is created with:</p><ul> <li> <p>the constructor function, usually called &quot;wxnew&quot;</p> </li><li> <p>the methods of the class, and the methods of the ancestors</p> </li><li> <p>the cast functions to ancestors</p> </li> </ul><p>For example, for the <a href="https://github.com/OCamlPro/wxOCaml/blob/master/wxWidgets/wxFrame.ml">WxFrame</a> module, the tool generates <a href="https://github.com/OCamlPro/wxOCaml/blob/master/wxWidgets/wxFrame.mli">this signature</a>:</p><pre><code>open WxClasses

external wxnew :  (* constructor *)
   wxWindow -&gt; int -&gt; wxString -&gt; int -&gt; int -&gt; int -&gt; int -&gt; int
   -&gt; wxFrame
        = &quot;camlidl_wxc_idl_wxFrame_Create_bytecode&quot;
...               (* direct methods *)
external setToolBar : wxFrame -&gt; wxToolBar -&gt; unit
        = &quot;camlidl_wxc_idl_wxFrame_SetToolBar&quot;
...               (* inherited methods *)
external setToolTip : wxFrame -&gt; wxString -&gt; unit
        = &quot;camlidl_wxc_idl_wxWindow_SetToolTip&quot;
...
  (* string wrappers *)
val wxnew : wxWindow -&gt; int -&gt; string -&gt; int -&gt; int -&gt; int -&gt; int -&gt; int -&gt; wxFr
ame
val setToolTip : wxFrame -&gt; string -&gt; unit
...
val ptrNULL : wxFrame (* a NULL pointer *)
...
external wxWindow : wxFrame -&gt; wxWindow = &quot;%identity&quot; (* cast function *)
...
</code></pre><p>In this example, we can see that:</p><ul> <li> <p>WxFrame first defines the constructor for wxFrame objects. The constructor is later refined, because the stub makes use of wxString arguments, for which the tool creates a wrapper to use OCaml strings instead (using WxString.createUTF8 before the stub and WxString.delete after the stub).</p> </li><li> <p>Stubs are then created for direct methods, i.e. functions corresponding to new methods of the class wxFrame. String wrappers are also produced if necessary.</p> </li><li> <p>Stubs are also created for inherited methods. Here, &quot;setToolTip&quot; is a method of the class wxWindow (thus, its stub name wxWindow_SetToolTip). Normally, this function is in the WxWindow module, and takes a wxWindow as first argument. But to avoid the need for a cast from wxFrame to wxWindow to use it, we define it again here, allowing a wxFrame directly as first argument.</p> </li><li> <p>The module also defines a ptrNULL value that can be used wherever a NULL pointer is expected instead of an object of the class.</p> </li><li> <p>Finally, functions like &quot;wxWindow&quot; are cast functions from children to ancestor, allowing to use a value of type wxFrame wherever a value of type wxWindow is expected.</p> </li> </ul><p>All functions that could not be put in such files are gathered in a module <a href="https://github.com/OCamlPro/wxOCaml/blob/master/wxWidgets/wxMisc.mli">WxMisc</a>. Finally, the tool also generates a module <a href="https://github.com/OCamlPro/wxOCaml/blob/master/wxWidgets/wxWidgets.mli">WxWidgets</a> containing a copy of all constructors with simpler names:</p><pre><code>...
val wxFrame : wxWindow -&gt; int -&gt; string -&gt; int -&gt; int -&gt; int -&gt; int -&gt; int -&gt; wxFrame
val wxFontMapper : unit -&gt; wxFontMapper
...
</code></pre><p>and functions to ignore the results of functions:</p><pre><code>...
external ignore_wxFontMapper : wxFontMapper -&gt; unit = &quot;%ignore&quot;
external ignore_wxFrame : wxFrame -&gt; unit = &quot;%ignore&quot;
...
</code></pre><p>We expect wxOCaml applications to just start with &quot;open WxWidgets&quot; to get access to these constructors, to use functions prefixed by the class module names, and to use constants from the <a href="https://github.com/OCamlPro/wxOCaml/blob/master/wxWidgets/wxdefs.ml">Wxdefs module</a>.</p><p>Here is how the minimal application looks like:</p><pre><code>open WxWidgets
let _ =
  let onInit event =
    let frame_id = wxID () in
    let quit_id = wxID() in
    let about_id = wxID() in

    (* Create toplevel frame *)
    let frame = wxFrame WxWindow.ptrNULL frame_id &quot;Hello World&quot;
                        50 50  450 350 Wxdefs.wxDEFAULT_FRAME_STYLE in
    WxFrame.setStatusText frame  &quot;Welcome to wxWidgets!&quot; 0;
    
    (* Create a menu *)
    let menuFile = wxMenu &quot;&quot; 0 in
    WxMenu.append menuFile about_id &quot;&amp;About&quot; &quot;About the application&quot; false;
    WxMenu.appendSeparator menuFile;
    WxMenu.append  menuFile quit_id &quot;E&amp;xit&quot; &quot;Exit from the application&quot; false;

    (* Add the menu to the frame menubar *)
    let menuBar = wxMenuBar 0 in
    ignore_int (WxMenuBar.append menuBar menuFile &quot;&amp;File&quot;);
    WxFrame.setMenuBar frame menuBar;
    ignore_wxStatusBar (WxFrame.createStatusBar frame 1 0);

    (* Handler for QUIT menu *)
    WxFrame.connect frame quit_id Wxdefs.wxEVT_COMMAND_MENU_SELECTED
      (fun _ -&gt; exit 0);

    (* Handler for ABOUT menu *)
    WxFrame.connect frame about_id Wxdefs.wxEVT_COMMAND_MENU_SELECTED
      (fun _ -&gt;
        ignore_int (
          WxMisc.wxcMessageBox &quot;wxWidgets Hello World example.&quot;
            &quot;About Hello World&quot;
            (Wxdefs.wxOK lor Wxdefs.wxICON_INFORMATION)
            (WxFrame.wxWindow frame)
            Wxdefs.wxDefaultCoord
            Wxdefs.wxDefaultCoord
        )
      );

    (* Display the frame *)
    ignore_bool ( WxFrame.show frame );
    ELJApp.setTopWindow (WxFrame.wxWindow frame)
  in
  WxMain.main onInit   (* Main WxWidget loop starting the app *)
</code></pre><h2>Testers welcome</h2><p>The current code can be downloaded from our <a href="http://github.com/OCamlPro/wxOCaml">repository on GitHub</a>. It should work with wxWidgets 2.9.4, and the latest version of ocp-build (1.99-beta5).</p><p>Of course, as I never wrote an application with wxWidgets before, I could only write a few examples, so I would really appreciate any feedback given by beta testers, especially as there might be errors in the translation to IDL, that make important functions impossible to use, that I cannot detect by myself.</p><p>I am also particularly interested by feedback on the use of modules for classes, to see if the corresponding style is usable. Our current feeling is that it is more verbose than a purely object-oriented style, but it is simpler for beginners, and improves the readability of code.</p><p>Finally, it was a short two-day hack, so it is far from finished. Especially, after hacking wxCamlidl, and looking at the code of the ELJ library, I had the feeling that we could go directly from the C++ header files, or something equivalent, to produce not only the OCaml stubs and the typed interface, but also the C++ to C bindings, and get rid completely of the ELJ library.</p> 
   </div>
  </content>

  </entry>

  <entry>
    <id>http://www.ocamlpro.com/blog/2013/03/18/monthly-03.html</id><title>An Indentation Engine for OCaml</title><author><name>Thomas Gazagnaire</name><uri>http://gazagnaire.org</uri><email>thomas@ocamlpro.com</email></author><author><name>Louis Gesbert</name><email>louis.gesbert@ocamlpro.com</email></author><rights>All rights reserved by the author</rights><updated>2013-03-18T00:00:00Z</updated><link rel="alternate" href="http://www.ocamlpro.com/blog/2013/03/18/monthly-03.html"/>
    
    
  <content type="xhtml">
   <div xmlns="http://www.w3.org/1999/xhtml">
      <p>Since our last <a href="http://www.ocamlpro.com/blog/2013/02/18/monthly-01-02.html">activity report</a> we have released the first stable versions of two projects: <a href="http://opam.ocamlpro.com">OPAM</a>, an installation manager for OCaml source packages, and <a href="https://github.com/OCamlPro/ocp-indent">ocp-indent</a>, an indentation tool.</p><p>We have already described the basics of OPAM in two precedent <a href="http://www.ocamlpro.com/blog/2013/01/17/opam-beta.html">blog</a> <a href="http://www.ocamlpro.com/blog/2013/01/17/opam-1.0.0.html">posts</a>, so today we will focus on the release of <code>ocp-indent</code>.</p><h2>Indentation should be consistent across editors</h2><p>When you work on a very large code-base, it is crucial to keep a consistent indentation scheme. This is not only good for code review purposes (when the indentation carries semantic properties) but also when your code is starting to evolve and when the one who makes the change is not the one who wrote the initial piece of code. In the latter case, the variety of editors and local configurations usually leads to lot of small changes carrying no semantic value at all (such as changing tabs to spaces, adding few spaces at the beginning or end of lines, and so on). This semantic noise considerably decreases the efficiency of any code-review and change process and is usually very good at hiding hard-to-track bugs in your code-base.</p><p>A few months ago, the solutions for OCaml to this indentation problem were limited. For instance, you could write coding guidelines and hope that all the developers in your project would follow them. If you wanted to be more systematic, you could create and share a common configuration file for some popular editors (most OCaml developers use the emacs' <code>tuareg-mode</code> or vim) but it is very hard to get consistent indentation result across multiple tools. Moreover, having to rely on a specific editor mode means that it is harder to fully automatize the indentation process, for instance when setting-up a VCS hook.</p><p>In order to overcome these pitfalls, <a href="http://www.janestreet.com/">Jane Street</a> asked us to design a new external tool with the following high-level specification:</p><ul> <li> <p>it should be easy to use inside and outside any editor;</p> </li><li> <p>it should understand the OCaml semantics and reflect it in the indentation;</p> </li><li> <p>it should be easy to maintain and to extend;</p> </li> </ul><p>So we started to look at the OCaml tools' ecosystem and we found an early prototype of Jun Furuse's <a href="https://bitbucket.org/camlspotter">ocaml-indent</a>. The foundation looked great but the result on real-world code sources was not as nice as it could be, so we decided to start from this base to build our new tool, that we called <code>ocp-indent</code>. Today, <code>ocp-indent</code> and <code>ocaml-indent</code> do not have much code in common anymore, but the global architecture of the system remains the same.</p><h2>Writing an indentation engine for OCaml</h2><p>An indentation engine may seem like a rather simple problem: given any line in the program, we want to compute its indentation level, based on the code structure.</p><p>It turns out to be much more difficult than that, mainly because indentation is only marginally semantic, and, worse, is a matter of taste and &quot;proper layout&quot;. In short, it's not a problem that can be expressed concisely, because one really does want lots of specific cases handled &quot;nicely&quot;, depending on the on-screen layout -- position of line breaks -- rather than the semantic structure. <code>Ocp-indent</code> does contain lots of ad-hoc logic for such cases. To make things harder, the OCaml syntax is known to be difficult to handle, with a few ambiguities.</p><h3>Indent process</h3><p><code>Ocp-indent</code> processes code in a simple and efficient way:</p><ul> <li> <p>We lex the input with a <a href="https://github.com/OCamlPro/ocp-indent/blob/master/src/approx_lexer.mll">modified version of the OCaml lexer</a>, to guarantee complete consistency with OCaml itself. The parser had to be modified to be more robust (ocaml fails on errors, the indentation tool should not) and to keep tokens like comments, quotations, and, in the latest version, some ocamldoc block delimiters.</p> </li><li> <p>Taking the token stream as input, we maintain a <a href="https://github.com/OCamlPro/ocp-indent/blob/master/src/indentBlock.ml">&quot;block&quot; stack</a> that keeps informations like the kinds of blocks we have been through to get to the cursor position, the column and the indentation parameters. For instance, the &quot;block&quot; stack  <code>[KBody KFfun; KLet; KBody KModule]</code> corresponds to the position of <code>X</code> in the following piece of (pseudo-) code:</p> </li> </ul><pre><code>...
module Foo = struct
  ...
  let f = fun a -&gt; X
</code></pre><ul> <li> <p>Each token may look up the stack to find its starting counterpart (<code>in</code> will look for <code>KLet</code>, etc.), or disambiguate (<code>=</code> will look for <code>KLet</code>, stopping on opening tokens like <code>KBracket</code>, and will be inserted as an operator if none is found). This is flexible enough to allow for &quot;breaking&quot; the stack when incorrect grammar is found. For example, the unclosed paren in <code>module let x = ( end</code> should not break indent after the <code>end</code>. Great care was taken in deciding what tokens should be able to remove from the stack in which conditions.</p> </li><li> <p>The stack can also be used to find a token that we want to align on, typically bars <code>|</code> in a pattern-matching.</p> </li><li> <p>On every line break, the stack can be used to compute the indentation of the next line.</p> </li><li> <p>In the case of partial file indentation (typically, reindenting one line or a single block), on lines that shouldn't be reindented the stack is reversely updated to adapt to the current indentation.</p> </li> </ul><h3>Priorities</h3><p>The part where some abstraction can be put into the engine is the knowledge of the semantics, and more precisely of the scope of the operations. It's also in that case that the indenter can help you write, and not only read, your code. On that matter, <code>ocp-indent</code> has a knowledge of the precedence of operators and constructs that is used to know how far to unwind the stack, and what to align on. For example, a <code>;</code> will flush function applications and most operators.</p><p>It is that part that gives it the most edge over <code>tuareg</code>, and avoids semantically incorrect indents. All infix operators are defined with a priority, a kind of indentation (indentation increment or alignment over the above concerned expression), and an indentation value (number of spaces to add). So for example most operators have a priority lower than function application, but not <code>.</code>, which yields correct results for:</p><pre><code>let f =
  somefun
    record.
      field
    y
  + z
</code></pre><p>Boolean operators like <code>&amp;&amp;</code> and <code>||</code> are setup for alignment instead of indentation:</p><pre><code>let r = a
        || b
           &amp;&amp; c
        || d
</code></pre><p>Additionally, some special operators are wanted with a <i>negative</i> alignment in some cases. This is also handled in a generic way by the engine. In particular, this is the case for <code>;</code> or <code>|</code>:</p><pre><code>type t = A
       | B

let r = { f1 = x
        ; f2 = y
        }
</code></pre><h3>A note on the integration in editors</h3><p><code>ocp-indent</code> can be used on the command-line to reindent whole files (or part of them with <code>--lines</code>), but the most common use of an indenter is from an editor. If you are lucky enough to be able to call OCaml code from your editor, you can use it directly as a library, but otherwise, the preferred way is to use the option <code>--numeric</code>: instead of reprinting the file reindented, it will only output indentation levels, which you can then process from your editor (for instance, using <code>indent-line-to</code> with emacs). That should be cheaper and will help preserve cursor position, etc.</p><p>Currently, a simple emacs binding working on either the ocaml or the tuareg mode is provided, together with a vim mode contributed by Raphaël Proust and David Powers.</p><h2>Results</h2><p>We've built <code>ocp-indent</code> based on a growing collection of <a href="https://github.com/OCamlPro/ocp-indent/tree/master/tests/passing">unit-tests</a>. If you find an indentation bug, feel free to <a href="https://github.com/OCamlPro/ocp-indent/issues">send us</a> a code snippet that we will incorporate into our test suite.</p><p>Our tests clearly show that the deep understanding that <code>ocp-indent</code> has of the OCaml syntax makes it shines on specific cases. We are still discussing and evaluating the implementation of few corner-cases related, see for instance the <a href="http://htmlpreview.github.com/?https://github.com/OCamlPro/ocp-indent/blob/master/tests/failing.html">currently failing tests</a>.</p><p>We have also run some <a href="http://htmlpreview.github.com/?https://github.com/AltGr/ocp-indent-tests/blob/master/status.html">benchmarks</a> on real code-bases and the result is quite conclusive: <code>ocp-indent</code> is always better than tuareg! This is a very nice result as most of the existing source files are either indented manually or are following tuareg standards. But <code>ocp-indent</code> is also orders of magnitude faster, which means you can integrate it seamlessly into any automatic process.</p> 
   </div>
  </content>

  </entry>

  <entry>
    <id>http://www.ocamlpro.com/blog/2013/03/14/opam-1.0.0.html</id><title>OPAM 1.0.0 Released</title><author><name>Thomas Gazagnaire</name><uri>http://gazagnaire.org</uri><email>thomas@ocamlpro.com</email></author><rights>All rights reserved by the author</rights><updated>2013-03-14T00:00:00Z</updated><link rel="alternate" href="http://www.ocamlpro.com/blog/2013/03/14/opam-1.0.0.html"/>
    
    
  <content type="xhtml">
   <div xmlns="http://www.w3.org/1999/xhtml">
      <p>We have previously <a href="http://www.ocamlpro.com/blog/2013/01/17/opam-beta.html">announced</a> the beta-release of <a href="http://opam.ocamlpro.com/">OPAM</a>. After two intense months of work, we are now very proud to announce the <b>first official</b> release of OPAM!</p><p>This release fixes many bugs and adds few new features to the previously announced beta-release.</p><p>The most visible new feature, which should be useful for beginners with OCaml and OPAM, is an auto-configuration tool. This tool easily enables all the features of OPAM (auto-completion, fix the loading of scripts for the toplevel, opam-switch-eval alias, etc). This tool runs interactively on each <code>opam init</code> invocation. If you don't like OPAM to change your configuration files, use <code>opam init --no-setup</code>. If you trust the tool blindly, use <code>opam init --auto-setup</code>.</p><pre><code>opam init                 # Interactive setup
opam init --no-setup      # No configuration change
opam init --auto-setup    # Automatic setup
</code></pre><p>You can later change the setup by doing:</p><pre><code>opam config setup --list  # show your current setup
opam config setup --all   # Enable all the features of OPAM
</code></pre><p>You can of course manually review and modifye the changes made b by editing your <code>~/.profile</code> (or <code>~/.zshrc</code> for zsh users), <code>~/.ocamlinit</code> and the <code>~/.opam/opam-init/*</code> init scripts.</p><h2>Contribute</h2><p>Please report:</p><ul> <li> <p>Bug reports and feature requests for the OPAM tool <a href="http://github.com/OCamlPro/opam/issues">here</a>.</p> </li><li> <p>Packaging issues or requests for a new package <a href="http://github.com/OCamlPro/opam-repository/issues">here</a>.</p> </li><li> <p>General queries to the <a href="http://lists.ocaml.org/listinfo/platform">platform list</a>.</p> </li><li> <p>More specific queries about the internals of OPAM to the <a href="http://lists.ocaml.org/listinfo/opam-devel">OPAM devel list</a>.</p> </li> </ul><h2>Install</h2><p>Packages for Debian and OSX (at least homebrew) should follow shortly and I'm looking for volunteers to create and maintain rpm packages. The binary installer is up-to-date for Linux and Darwin 64-bit architectures, the 32-bit version for Linux should arrive shortly.</p><p>If you want to build from sources, the full archive (including dependencies) is available at:</p><p><a href="http://www.ocamlpro.com/pub/opam-full-latest.tar.gz">http://www.ocamlpro.com/pub/opam-full-latest.tar.gz</a></p><h2>Upgrade</h2><p>If you are upgrading from 0.9.* (eg. if you have at least installed the beta release announced in January) you won't have anything special to do apart installing the new binary. You can then update your package metadata by running <code>opam update</code>. If you want to use the auto-setup feature, remove the &quot;eval <code>opam config env</code> line you have previously added in your <code>~/.profile</code> and run <code>opam config setup --all</code>.</p><p>So everything should be fine. But you never know ... so if something goes horribly wrong in the upgrade process (of if your are upgrading from an old version of OPAM) you can still trash your <code>~/.opam</code>, manually remove what OPAM added in your <code>~/.profile</code> (<code>~/.zshrc</code> for zsh users) and <code>~/.ocamlinit</code>, and start again from scratch.</p><h2>Random Statistics</h2><p>The OPAM project on Github is very pleasantly active. We have got lot of contribution from a lot of different people, and I am very pleased to regularly discover new and interesting OCaml projects pushed to the OPAM repository. So thanks everybody for the great contributions!</p><p>We have worked hard to deliver OPAM and we are very delighted to get a warm welcome from the community. This give us a lot of motivation to continue improving OPAM!</p><h3>Repositories</h3><ul> <li> <p><a href="https://github.com/OCamlPro/opam">https://github.com/OCamlPro/opam</a>: +2000 commits, 26 contributors</p> </li><li> <p><a href="https://github.com/OCamlPro/opam-repository">https://github.com/OCamlPro/opam-repository</a>: +1700 commits, 75 contributors, 370+ packages</p> </li> </ul><h3>Usage</h3><p>On <a href="http://opam.ocamlpro.com/">http://opam.ocamlpro.com/</a></p><ul> <li> <p>+400 unique visitor per week, 15k <code>opam update</code> per week</p> </li><li> <p>+1300 unique visitor per month, 55k <code>opam update</code> per month</p> </li><li> <p>3815 unique visitor since the alpha release</p> </li> </ul><h2>Changelog</h2><p>The full change-log is available <a href="https://github.com/OCamlPro/opam/blob/master/CHANGES">here</a></p> 
   </div>
  </content>

  </entry>

  <entry>
    <id>http://www.ocamlpro.com/blog/2013/02/18/monthly-01-02.html</id><title>An Overview of our Current Activities</title><author><name>Thomas Gazagnaire</name><uri>http://gazagnaire.org</uri><email>thomas@ocamlpro.com</email></author><rights>All rights reserved by the author</rights><updated>2013-02-18T00:00:00Z</updated><link rel="alternate" href="http://www.ocamlpro.com/blog/2013/02/18/monthly-01-02.html"/>
    
    
  <content type="xhtml">
   <div xmlns="http://www.w3.org/1999/xhtml">
      <p>From the early days of OCamlPro, people have been curious about our plans; they were asking how we worked at OCamlPro and what we were doing exactly. Now that we have started releasing projects more regularly, these questions come again. They are very reasonable questions, and have resolved to be more public and communicate more regularly. This post covers our activities from the beginning of 2013 and updates are scheduled on a monthly basis.</p><h2>OCamlPro ?</h2><p>OCamlPro has been created to promote the use of OCaml in the industry. In order to do so, we provide a wide range of services targeted at all stages of typical software projects: we train engineers and we improve the efficiency and usability of the OCaml compiler and tools, we help design new projects, advise on which open-source software components to use and how, we help deliver OCaml software projects and we do custom software development. One extra focus is the increase of the accessibility of OCaml for beginners and students.</p><p>Our customers are well-known industrial users such as <a href="http://www.janestreet.com/">Jane-Street</a>, <a href="http://www.citrix.com/">Citrix</a> and <a href="http://www.lexifi.com/">Lexifi</a> but we also help individual developers lost in the wild of non-OCaml environments inter-operate OCaml with other components. We also believe that collaborative R&amp;D projects are a great opportunity to make existing companies discover OCaml and its benefits to their products and we are involved in several of them (see below).</p><p>Our engineering team is steadily growing (currently 9 full-time engineers in a joint lab between OCamlPro and INRIA) located in Paris and Nice. We gather a wide range of technical skills and industrial world expertise as we are all coming from major academic and industrial actors such as <a href="http://www.inria.fr/">INRIA</a>, <a href="http://www.3ds.com/">Dassaut Systèmes</a>, <a href="http://www.mlstate.com/">MLstate</a> and <a href="http://www.citrix.com/">Citrix</a>. We also love the OCaml open-source ecosystem: we have been participating to the development of <a href="http://ocsigen.org/">ocsigen</a>, <a href="http://www.openmirage.org/">mirage</a>, <a href="http://www.xen.org/products/cloudxen.html">XCP</a>, <a href="http://mldonkey.sourceforge.net/">mldonkey</a>, <a href="http://www.marionnet.org/EN/">marionet</a> and so on. By the way, OCamlPro has some open <a href="http://www.ocamlpro.com/company/jobs.html">positions</a> and we are still looking to hire excellent software engineers!</p><h2>OCaml Distribution</h2><p>The first of our technical activities is related to work on the OCaml distribution itself. We are part of the OCaml compiler development team -- our INRIA members are part of the <a href="http://gallium.inria.fr/">Gallium</a> project which develops OCaml at INRIA -- and we regularly contribute patches to improve the usability and performance of the compiler itself.</p><p>We have recently proposed <a href="http://caml.inria.fr/mantis/view.php?id=5894">a series of patches</a> to improve the performance of functions with float arguments and we have started developing a <a href="https://github.com/chambart/ocp-bench">framework</a> to benchmark the efficiency of compiler optimizations.</p><p>We are also actively exploring the design-space for concurrency and distribution in OCaml, with an implementation of (i) a reentrant runtime; (ii) a way to instantiate different runtimes in separate system threads in the same process, and (iii) an efficient multi-scale communication library, between threads and between processes. We call this <b>multi-runtime OCaml</b> and a prototype is available on <a href="https://github.com/lucasaiu/ocaml/tree/master">github</a>.</p><p>Last, we are also making progress with the memory profiling tools. We work on a modified OCaml runtime which can store the location of each allocated block in the heap, with hooks to dump that heap on demand. External tools can then use that dump to produce useful statistics about memory usage of the program. The good news is that we now have a working and usable bytecode runtime and an external tool that produces basic memory information. We are preparing an alpha release in the next month, so stay tuned!</p><h2>Development Tools</h2><p>Our efforts to make OCaml more usable go further than looking at the compiler. We are improving the development tools already existing in the community, such as the recently released <a href="https://github.com/OCamlPro/ocp-indent">indentation tool</a> which was initially coming from an experiment from <a href="https://bitbucket.org/camlspotter/ocaml-indent">Jun Furuse</a>, and creating new ones when the lack is blatant.</p><p>Most recent news on that front concern <a href="http://opam.ocamlpro.com/">OPAM</a>, the package manager for OCaml that we are developing since mid-2012. For people not familiar with it yet, OPAM is a source-based package manager for OCaml. It supports resolution of complex dependency constraints between packages, multiple simultaneous compiler installations, flexible package constraints, and a Git-friendly development workflow. The beta release was announced in January, and we expect the first official release to happen in the next weeks. The OCaml community has gratefully welcomed OPAM, and the <a href="https://github.com/OCamlPro/opam-repository">repository of its package metadata</a> has already become the most forked OCaml project on github! Interestingly, two meetups have gathered more than fifty OPAM users in <a href="http://meetup.ocaml-lang.fr/">Paris</a> and Cambridge in January. We really hope this kind of meetup can be generalized: if you want to help us organize one in your area, feel free to contact us!</p><p>The other major part of our work around development tools for OCaml is TypeRex. TypeRex is a collection of tools which focus on improving developer efficiency, and lowering the entry barrier for experienced developers who are used to shiny IDEs in other languages. The first version of <a href="http://www.typerex.org/">TypeRex</a>, that was released last year, was a first step in this direction: it provided an enhanced emacs mode for OCaml code, with colorization, indentation, refactoring facilities, completion, code-navigation, documentation tooltips, etc. The next version of TypeRex (simply dubbed <b>typerex2</b>) is underway, with more independent tools (like ocp-indent), less tightly coupled to Emacs, and focused on better integration with various IDEs. If you are interested in following the progress of these tools, you can check the <b>typerex2</b> OPAM packages with 1.99.X+beta version numbers, which we release on a regular basis.</p><h2>R&amp;D projects</h2><p>The idea that OCaml is the right choice to create new innovative products is at the core of OCamlPro. We are very involved in the research community, especially on Functional Languages, with participation into the Program Committees of various conferences such as <a href="http://oud.ocaml.org/">the OCaml User and Developer (OUD)</a> workshop and <a href="http://cufp.org/">the Commercial User of Functional Programming (CUFP)</a> conference. We also joined two collaborative R&amp;D projects in 2012, the <a href="http://www.richelieu.pro/">Richelieu FUI</a> and <a href="http://bware.lri.fr/index.php/BWare_project">BWare ANR</a>. As part of the Richelieu project, we are developing a JIT compiler for the <a href="http://www.scilab.org/">Scilab language</a>. As part of the Bware project, we improve the efficiency of automatic theorem provers, with a specific focus on <a href="http://alt-ergo.lri.fr/">Alt-Ergo</a>, an SMT solver particularly suited to program verification. We are always interested in bringing our expertise in compiler technologies and knowledge of complex and distributed systems to new R&amp;D projects: contact us if you are interested!</p><p>In the Richelieu project, our combined technical and theoretical expertise proved particularly effective. The research consortium is led by <a href="http://www.scilab-enterprises.com/">Scilab Entreprises</a> which needed a safer and more efficient execution engine for Scilab in order to compete with Matlab. We joined the consortium to implement the early analysis required by the JIT compiler. The project started last December, and we have since specified the semantics of the language and implemented a working prototype of an interpreter that is already as fast as the current C++ engine of Scilab 6.</p><h2>Growing the Community</h2><p>Our last important domain of activity is geared towards the OCaml community. It is important to us that the community grows bigger, and to achieve this goal there are some basic blocks that we need to help build, together with the other main actors of the community.</p><p>The first missing block is a good reference documentation. This year will end with (at least) one new important book for the language: <a href="http://www.realworldocaml.org/">Real-World OCaml</a> which targets experienced software engineers who do not know OCaml yet. We collaborate with <a href="http://www.cl.cam.ac.uk/projects/ocamllabs/">OCamlLabs</a> to make the technical experience of this book a success. We also work to improve the general experience of using OCaml for complete beginners by creating a stable <a href="https://github.com/OCamlPro/ocp-edit-simple">replacement</a> to the broken <b>ocamlwin</b>, the simple editor distributed with OCaml on Windows.</p><p>It is also important to us that OCaml uses the web as a platform to attract new users, as is becoming the norm for modern programming languages. We are members of the <a href="http://www.ocaml.org/">ocaml.org</a> building effort and have created <a href="http://try.ocamlpro.com/">tryocaml</a> to let newcomers easily discover the language directly from their browser. TryOcaml has been welcomed as a great tool, already adopted and adapted: see for instance <a href="http://rtt.forge.ocamlcore.org/tryrtt.html">tryrtt</a> or <a href="http://rml.lri.fr/tryrml/">try ReactiveML</a>. We are in the process of simplifying the integration with other compiler variants.</p><p>Last, but not least, we collaborate very closely with OCamlLabs to create the OCaml Plateform: a consistent set of libraries, thoroughly tested and integrated, with a rolling release schedule of 6 months. The platform will be based on <a href="http://opam.ocamlpro.com/">OPAM</a> and we are currently designing and prototyping a testing infrastructure to improve and guarantee the quality of packages.</p> 
   </div>
  </content>

  </entry>

  <entry>
    <id>http://www.ocamlpro.com/blog/2013/01/17/opam-beta.html</id><title>Beta-release of OPAM</title><author><name>Thomas Gazagnaire</name><uri>http://gazagnaire.org</uri><email>thomas@ocamlpro.com</email></author><rights>All rights reserved by the author</rights><updated>2013-01-17T00:00:00Z</updated><link rel="alternate" href="http://www.ocamlpro.com/blog/2013/01/17/opam-beta.html"/>
    
    
  <content type="xhtml">
   <div xmlns="http://www.w3.org/1999/xhtml">
      <p>OPAM is a source-based package manager for OCaml. It supports multiple simultaneous compiler installations, flexible package constraints, and a Git-friendly development workflow. I have recently announced the beta-release of OPAM on the <a href="https://sympa.inria.fr/sympa/arc/caml-list/2013-01/msg00073.html">caml-list</a>, and this blog post introduces the basics to new OPAM users.</p><h2>Why OPAM</h2><p>We have decided to start writing a brand new package manager for OCaml in the beginning of 2012, after looking at the state of affairs in the OCaml community and not being completely satisfied with the existing solutions, especially regarding the management of dependency constraints between packages. Existing technologies such as GODI, oasis, odb and ocamlbrew did contain lots of good ideas that we shamelessly stole but the final user-experience was not so great -- and we disagreed with some of the architectural choices, so it wasn't so easy to contribute to fix the existing flaws. Thus we started to discuss the specification of a new package manager with folks from <a href="http://www.janestreet.com/">Jane Street</a> who decided to fund the project and from the <a href="http://www.mancoosi.org/">Mancoosi project</a> to integrate state-of-the-art dependency management technologies. We then hired an engineer to do the initial prototyping work -- and this effort finally gave birth to OPAM!</p><h2>Installing OPAM</h2><p>OPAM packages are already available for homebrew, macports and arch-linux. Debian and Ubuntu packages should be available quite soon. In any cases, you can either use a <a href="http://www.ocamlpro.com/pub/opam_install.sh">binary installer</a> or simply install it from <a href="https://github.com/OCamlPro/opam/archive/0.9.1.tar.gz">sources</a>. To learn more about the installation process, read the <a href="http://opam.ocamlpro.com/doc/Quick_Install.html">installation instructions</a>.</p><h2>Initializing OPAM</h2><p>Once you've installed OPAM, you have to initialize it. OPAM will store all its state under <code>~/.opam</code>, so if you want to reset your OPAM configuration, simply remove that directory and restart from scratch. OPAM can either use the compiler installed on your system or it can also install a fresh version of the compiler:</p><pre><code>opam init                # Use the system compiler
opam init --comp 4.00.1  # Use OCaml 4.00.1
</code></pre><p>OPAM will prompt you to add a shell script fragment to your <code>.profile</code>. It is highly recommended to follow these instructions, as it let OPAM set-up correctly the environment variables it needs to compile and configure the packages.</p><h2>Getting help</h2><p>OPAM user manual is integrated:</p><pre><code>opam --help       # Get help on OPAM itself
opam init --help  # Get help on the init sub-command
</code></pre><h2>Basic commands</h2><p>Once OPAM is initialized, you can ask it to list the available packages, get package information and search for a given pattern in package descriptions:</p><pre><code>opam list *foo* # list all the package containing 'foo' in their name
opam info foo   # Give more information on the 'foo' package
opam search foo # search for the string 'foo' in all package descriptions
</code></pre><p>Once you've found a package you would like to install, just run the usual <code>install</code> command.</p><pre><code>opam install lwt # install lwt and its dependencies
opam remove lwt  # remove lwt and its dependencies
</code></pre><p>Later on, you can check whether new packages are available and you can upgrade your package installation.</p><pre><code>opam update  # check if new packages are available
opam upgrade # upgrade your packages to the latest version
</code></pre><p>Casual users of OCaml won't need to know more about OPAM. Simply remind to update and upgrade OPAM regularly to keep your system up-to-date.</p><h2>Use-case 1: Managing Multiple Compilers</h2><p>A new release of OCaml is available and you want to be able to use it. How to do this in OPAM ? This is as simple as:</p><pre><code>opam update        # pick-up the latest compiler descriptions
opam switch 4.00.2 # switch to the new 4.00.2 release
opam switch export --switch=system | opam switch import -y
</code></pre><p>The first line will get the latest package and compiler descriptions, and will tell you if new packages or new compilers are available. Supposing that 4.00.2 is now available, you can then <code>switch</code> to that version using the second command. The last command imports all the packages installed by OPAM for the OCaml compiler installed on your system (if any).</p><p>You can also easily use the latest unstable version of OCaml if you want to give it a try:</p><pre><code>opam switch 4.01.0dev+trunk            # install trunk
opam switch reinstall 4.01.0dev+trunk  # reinstall trunk
</code></pre><p>Reinstalling trunk means getting the latest changesets and recompiling the packages already installed for that compiler switch.</p><h2>Use-case 2: Managing Multiple Repositories</h2><p>Sometimes, you want to let people use a new version of your software early. Or you are working in a company and expose internal libraries to your coworkers but you don't want them to be available to anybody using OPAM. How can you do that with OPAM? It's easy! You can set-up your own repository (see for instance <a href="https://github.com/xen-org/opam-repo-dev/">xen-org</a>'s development packages) and add it to your OPAM configuration:</p><pre><code>opam repository list  # list the repositories available in your config
opam repository add xen-org git://github.com/xen-org/opam-repo-dev.git
opam repository list  # new xen-org repository available
</code></pre><p>This will add the repository to your OPAM configuration and it will display the newly available packages. The next time you run <code>opam update</code> OPAM will then scan for any change in the remote git repository.</p><p>Repositories can either be local (e.g. on your filesystem), remote (available through HTTP) and stored in git or darcs.</p><h2>Use-case 3: Using Development Packages</h2><p>You want to try the latest version of a package which have not yet been released, or you have a patched version of a package than you want to try. How could you do it? OPAM has a <code>pin</code> sub-command which let you do that easily:</p><pre><code>opam pin lwt /local/path/
opam install lwt          # install the version of lwt stored in /local/path
</code></pre><p>You can also use a given branch in a given git repository. For instance, if you want the library <code>re</code> to be compiled with the code in the <code>experimental</code> branch of its development repository you can do:</p><pre><code>opam pin re git://github.com/ocaml/ocaml-re.git#experimental
opam install re
</code></pre><p>When building the packages, OPAM will use the path set-up with the pin command instead of using the upstream archives. Also, on the next update, OPAM will automatically check whether some changes happened and if the packages needs to be recompiled:</p><pre><code>opam update lwt     # check for changes in /local/path
opam update re      # check for change in the remote git branch
opam upgrade lwt re # upgrade re and lwt if necessary
</code></pre><h2>Conclusion</h2><p>I've briefly explained some of the main features of OPAM. If you want to go further, I would advise to read the <a href="http://opam.ocamlpro.com/doc/Advanced_Usage.html">user</a> and <a href="http://opam.ocamlpro.com/doc/Packaging.html">packager</a> tutorials. If you really want to understand the internals of OPAM, you can also read the <a href="https://github.com/OCamlPro/opam/blob/master/doc/dev-manual/dev-manual.pdf?raw=true">developer manual</a>.</p> 
   </div>
  </content>

  </entry>

  <entry>
    <id>http://www.ocamlpro.com/blog/2012/08/20/ocamlpro-and-4.00.0.html</id><title>OCamlPro's Contributions to OCaml 4.00.0</title><author><name>Fabrice Le Fessant</name><uri>http://fabrice.lefessant.net/</uri><email>fabricel@ocamlpro.com</email></author><rights>All rights reserved by the author</rights><updated>2012-08-20T00:00:00Z</updated><link rel="alternate" href="http://www.ocamlpro.com/blog/2012/08/20/ocamlpro-and-4.00.0.html"/>
    
    
  <content type="xhtml">
   <div xmlns="http://www.w3.org/1999/xhtml">
      <p>OCaml 4.00.0 has been released on July 27, 2012. For the first time, the new OCaml includes some of the work we have been doing during the last year. In this article, I will present our main contributions, mostly funded by Jane Street and Lexifi.</p><h2>Binary Annotations for Advanced Development Tools</h2><p>OCaml 4.00.0 has a new option <code>-bin-annot</code> (undocumented, for now, as it is still being tested). This option tells the compiler to dump in binary format a compressed version of the typed tree (an abstract syntax tree with type annotations) to a file (with the <code>.cmt</code> extension for implementation files, and <code>.cmti</code> for interface files). This file can then be used by development tools to provide new features, based on the full knowledge of types in the sources.  One of the first tools to use it is the new version of <code>ocamlspotter</code>, by Jun Furuse.</p><p>This new option will probably make the old option <code>-annot</code> obsolete (except, maybe, in specific contextes where you don't want to depend on the internal representation of the typedtree, for example when you are modifying this representation !). Generated files are much smaller than with the <code>-annot</code> option, and much faster to write (during compilation) and to read (for analysis).</p><h2>New Options for <code>ocamldep</code></h2><p>As requested on <a href="http://caml.inria.fr/mantis/view.php?id=5396">the bug tracker</a>, we implemented a set of new options for <code>ocamldep</code>:</p><ul> <li> <p><code>-all</code> will print all the dependencies, i.e. not only on <code>.cmi</code>, <code>.cmo</code> and <code>.cmx</code> files, but also on source files, and for <code>.o</code> files. In this mode also, no proxying is performed: if there is no interface file, a bytecode dependency will still appear against the <code>.cmi</code> file, and not against the <code>.cmo</code> file as it would before;</p> </li> </ul><ul> <li> <p><code>-one-line</code> will not break dependencies on several lines;</p> </li><li> <p><code>-sort</code> will print the arguments of <code>ocamldep</code> (filenames) in the order of dependencies, so that the following command should work when all source files are in the same directory:</p> </li> </ul><pre><code>ocamlopt -o my_program `ocamldep -sort *.ml *.mli`
</code></pre><h2>CFI Directives for Debugging</h2><p>OCaml tries to make the best use of available registers and stack space, and consequently, its layout on the stack is much different from the one of C functions. Also, function names are mangled to make them local to their module. As a consequence, debugging native code OCaml programs has long been a problem with previous versions of OCaml:, since the debugger cannot print correctly the backtrace of the stack, nor put breakpoints on OCaml functions.</p><p>In OCaml 4.00.0, we worked on a patch submitted on the <a href="http://caml.inria.fr/mantis/view.php?id=5314">bug tracker</a> to improve the situation: x86 and amd64 backends now emit more debugging directives, such as the locations in the source corresponding to functions in the assembly (so that you can put breakpoints at function entry), and CFI directives, indicating the correct stack layout, for the debugger to correctly unwind the stack. These directives are part of the DWARF debugging standard.</p><p>Unfortunately, line by line stepping is not yet available, but here is an example of session that was not possible with previous versions:</p><pre><code>let f x y = List.map ( (+) x ) y
let _ = f 3 [1;2;3;4]
</code></pre><pre><code>$ ocamlopt -g toto.ml
$ gdb ./a.out
(gdb) b toto.ml:1
Breakpoint 1 at 0x4044f4: file toto.ml, line 1.
(gdb) run
Starting program: /home/lefessan/ocaml-4.00.0-example/a.out

Breakpoint 1, 0x00000000004044f4 in camlToto__f_1008 () at toto.ml:1
1	let f x y = List.map ( (+) x ) y
(gdb) bt
#0  0x00000000004044f4 in camlToto__f_1008 () at toto.ml:1
#1  0x000000000040456c in camlToto__entry () at toto.ml:2
#2  0x000000000040407d in caml_program ()
#3  0x0000000000415fe6 in caml_start_program ()
#4  0x00000000004164b5 in caml_main (argv=0x7fffffffe3f0) at startup.c:189
#5  0x0000000000408cdc in main (argc=&lt;optimized out&gt;, argv=&lt;optimized out&gt;)
    at main.c:56
(gdb)
</code></pre><h2> Optimisation of Partial Function Applications</h2><p>Few people know that partial applications with multiple arguments are not very efficient. For example, do you know how many closures are dynamically allocated in in the following example ?</p><pre><code>let f x y z = x + y + z
let sum_list_offsets orig list = List.fold_left (f orig) 0 list
let sum = sum_list_offsets 10 [1;2;3]
</code></pre><p>Most programmers would reply one, <code>f orig</code>, but that's not all (indeed, <code>f</code> and <code>sum_list_offsets</code> are allocated statically, not dynamically, as they have no free variables). Actually, three more closures are allocated, when <code>List.fold_left</code> is executed on the list, one closure per element of the list.</p><p>The reason for this is that Ocaml has only two modes to execute functions: either all arguments are present, or just one argument. Prior to 4.00.0, when a function would enter the second mode (as <code>f</code> in the previous example), then it would remain in that mode, meaning that the two other arguments would be passed one by one, creating a partial closure between them.</p><p>In 4.00.0, we implemented a simple optimization, so that whenever all the remaining expected arguments are passed at once, no partial closure is created and the function is immediatly called with all its arguments, leading to only one dynamic closure creation in the example.</p><h2> Optimized Pipe Operators</h2><p>It is sometimes convenient to use the pipe notation in OCaml programs, for example:</p><pre><code>let (|&gt;) x f = f x;;
let (@@) f x = f x;;
[1;2;3] |&gt; List.map (fun x -&gt; x + 2) |&gt; List.map print_int;;
List.map print_int @@ List.map (fun x -&gt; x + 1 ) @@ [1;2;3];;
</code></pre><p>However, such <code>|&gt;</code> and <code>@@</code> operators are currently not optimized: for example, the last line will be compiled as:</p><pre><code>let f1 = List.map print_int;;
let f2 = List.map (fun x -&gt; x + 1);;
let x = f2 [1;2;3;];;
f1 x;;
</code></pre><p>Which means that partial closures are allocated every time a function is executed with multiple arguments.</p><p>In OCaml 4.00.0, we optimized these operators by providing native operators, for which no partial closures are generated:</p><pre><code>external (|&gt;) : 'a -&gt; ('a -&gt; 'b) -&gt; 'b = &quot;%revapply&quot;;;
external ( @@ ) : ('a -&gt; 'b) -&gt; 'a -&gt; 'b = &quot;%apply&quot;
</code></pre><p>Now, the previous example is equivalent to:</p><pre><code>List.map print_int (List.map ( (+) 1 ) [1;2;3])
</code></pre><h2>Bug Fixing</h2><p>Of course, a lot of our contributions are not always as visible as the previous ones. We also spent a lot of time fixing small bugs. Although it doesn't sound very fun, fixing bugs in OCaml is also fun, because bugs are often challenging to understand, and even more challenging to remove without introducing new ones !</p> 
   </div>
  </content>

  </entry>

  <entry>
    <id>http://www.ocamlpro.com/blog/2012/08/08/profile-native-code.html</id><title>Profiling OCaml amd64 code under Linux</title><author><name>Fabrice Le Fessant</name><uri>http://fabrice.lefessant.net/</uri><email>fabricel@ocamlpro.com</email></author><rights>All rights reserved by the author</rights><updated>2012-08-08T00:00:00Z</updated><link rel="alternate" href="http://www.ocamlpro.com/blog/2012/08/08/profile-native-code.html"/>
    
    
  <content type="xhtml">
   <div xmlns="http://www.w3.org/1999/xhtml">
      <p>We have recently worked on modifying the OCaml system to be able to profile OCaml code on Linux amd64 systems, using the processor performance counters now supported by stable kernels. This page presents this work, funded by Jane Street.</p><p>The patch is provided for OCaml version 4.00.0. If you need it for 3.12.1, some more work is required, as we would need to backport some improvements that were already in the 4.00.0 code generator.</p><h2>An example: profiling <code>ocamlopt.opt</code></h2><p>Here is an example of a session of profiling done using both Linux performance tools and a modified OCaml 4.00.0 system (the patch is available at the end of this article).</p><p>Linux performance tools are available as part of the Linux kernel (in the <code>linux-tools</code> package on Debian/Ubuntu). Most of the tools are invoked through the <code>perf</code> command, à la git. For example, we are going to check where the time is spent when calling the <code>ocamlopt.opt</code> command:</p><pre><code>perf record -g ./ocamlopt.opt -c -I utils -I parsing -I typing typing/*.ml
</code></pre><p>This command generates a file <code>perf.data</code> in the current directory, containing all the events that were received during the execution of the command. These events contain the values of the performance counters in the amd64 processor, and the call-chain (backtrace) at the event.</p><p>We can inspect this file using the command:</p><pre><code>perf report -g
</code></pre><p>The command displays:</p><pre><code>Events: 3K cycles
+   9.81%  ocamlopt.opt  ocamlopt.opt           [.] compare_val
+   8.85%  ocamlopt.opt  ocamlopt.opt           [.] mark_slice
+   7.75%  ocamlopt.opt  ocamlopt.opt           [.] caml_page_table_lookup
+   7.40%            as  as                     [.] 0x5812
+   5.60%  ocamlopt.opt  [kernel.kallsyms]      [k] 0xffffffff8103d0ca
+   3.91%  ocamlopt.opt  ocamlopt.opt           [.] sweep_slice
+   3.18%  ocamlopt.opt  ocamlopt.opt           [.] caml_oldify_one
+   3.14%  ocamlopt.opt  ocamlopt.opt           [.] caml_fl_allocate
+   2.84%            as  [kernel.kallsyms]      [k] 0xffffffff81317467
+   1.99%  ocamlopt.opt  ocamlopt.opt           [.] caml_c_call
+   1.99%  ocamlopt.opt  ocamlopt.opt           [.] caml_compare
+   1.75%  ocamlopt.opt  ocamlopt.opt           [.] camlSet__mem_1148
+   1.62%  ocamlopt.opt  ocamlopt.opt           [.] caml_oldify_mopup
+   1.58%  ocamlopt.opt  ocamlopt.opt           [.] camlSet__bal_1053
+   1.46%  ocamlopt.opt  ocamlopt.opt           [.] camlSet__add_1073
+   1.37%  ocamlopt.opt  libc-2.15.so           [.] 0x15cbd0
+   1.37%  ocamlopt.opt  ocamlopt.opt           [.] camlInterf__compare_1009
+   1.33%  ocamlopt.opt  ocamlopt.opt           [.] caml_apply2
+   1.09%  ocamlopt.opt  ocamlopt.opt           [.] caml_modify
+   1.07%            sh  [kernel.kallsyms]      [k] 0xffffffffa07e16fd
+   1.07%            as  libc-2.15.so           [.] 0x97a61
+   0.94%  ocamlopt.opt  ocamlopt.opt           [.] caml_alloc_shr
</code></pre><p>Using the arrow keys and the <code>Enter</code> key to expand an item, we can get a better idea of where most of the time is spent:</p><pre><code>Events: 3K cycles
-   9.81%  ocamlopt.opt  ocamlopt.opt           [.] compare_val                ◆
   - compare_val                                                               ▒
      - 71.68% camlSet__mem_1148                                               ▒
         + 98.01% camlInterf__add_interf_1121                                  ▒
         + 1.99% camlInterf__add_pref_1158                                     ▒
      - 21.48% camlSet__add_1073                                               ▒
         - camlSet__add_1073                                                   ▒
            + 93.41% camlSet__add_1073                                         ▒
            + 6.59% camlInterf__add_interf_1121                                ▒
      + 1.44% camlReloadgen__fun_1386                                          ▒
      + 1.43% camlClosure__close_approx_var_1373                               ▒
      + 1.43% camlSwitch__opt_count_1239                                       ▒
      + 1.34% camlTbl__add_1050                                                ▒
      + 1.20% camlEnv__find_1408                                               ▒
+   8.85%  ocamlopt.opt  ocamlopt.opt           [.] mark_slice                 ▒
-   7.75%  ocamlopt.opt  ocamlopt.opt           [.] caml_page_table_lookup     ▒
   - caml_page_table_lookup                                                    ▒
      + 50.03% camlBtype__set_commu_1704                                       ▒
      + 49.97% camlCtype__expand_head_1923                                     ▒
+   7.40%            as  as                     [.] 0x5812                     ▒
+   5.60%  ocamlopt.opt  [kernel.kallsyms]      [k] 0xffffffff8103d0ca         ▒
+   3.91%  ocamlopt.opt  ocamlopt.opt           [.] sweep_slice                ▒
Press '?' for help on key bindings
</code></pre><p>We notice that a lot of time is spent in the <code>compare_val</code> primitive, called from the <code>Pervasives.compare</code> function, itself called from the <code>Set</code> module in <code>asmcomp/interp.ml</code>. We can locate the corresponding code at the beginning of the file:</p><pre><code>module IntPairSet =
  Set.Make(struct type t = int * int let compare = compare end)
</code></pre><p>Let's replace the polymorphic function <code>compare</code> by a monomorphic function, optimized for pairs of small ints:</p><pre><code>module IntPairSet =
  Set.Make(struct type t = int * int
                  let compare (a1,b1) (a2,b2) =
                    if a1 = a2 then b1 - b2 else a1 - a2
           end)
</code></pre><p>We can now compare the speed of the two versions:</p><pre><code>peerocaml:~/ocaml-4.00.0%  time ./ocamlopt.old -c -I utils -I parsing -I typing typing/*.ml
./ocamlopt.old  7.38s user 0.56s system 97% cpu 8.106 total
peerocaml:~/ocaml-4.00.0%  time ./ocamlopt.new -c -I utils -I parsing -I typing typing/*.ml
./ocamlopt.new  6.16s user 0.50s system 97% cpu 6.827 total
</code></pre><p>And we get an interesting speedup ! Now, we can iterate the process, check where most of the time is spent in the new version, optimize the critical path and so on.</p><h2>Installation of the modified OCaml system</h2><p>A modified OCaml system is required because, for each event, the Linux kernel must attach a backtrace of the stack (call-chain). However, the kernel is not able to use standard DWARF debugging information, and current OCaml stack frames are too complex to be unwinded without this DWARF information. Instead, we had to modify OCaml code generator to follow the same conventions as C for frame pointers, i.e. using saving the frame pointer on function entry and restoring it on function exit. This required to decrease the number of available registers from 13 to 12, using <code>%rbp</code> as the frame pointer, leading to an average 3-5% slowdown in execution time.</p><p>The patch for OCaml 4.00.0 is available here:</p><p><a href="http://www.ocamlpro.com//files/omit-frame-pointer-4.00.0.patch">omit-frame-pointer-4.00.0.patch</a> (20 kB, v2, updated 2012/08/13)</p><p>To use it, you can use the following recipe, that will compile and install the patched version in ~/ocaml-4.00-with-fp.</p><pre><code>~%  wget http://caml.inria.fr/pub/distrib/ocaml-4.00.0/ocaml-4.00.0.tar.gz
~%  tar zxf ~/ocaml-4.00.0.tar.gz
~%  cd ocaml-4.00.0
~/ocaml-4.00.0%  wget www.ocamlpro.com/files/omit-frame-pointer-4.00.0.patch
~/ocaml-4.00.0%  patch -p1 &lt; omit-frame-pointer-4.00.0.patch
~/ocaml-4.00.0%  ./configure --prefix ~/ocaml-4.00-with-fp
~/ocaml-4.00.0%  make world opt opt.opt install
~/ocaml-4.00.0%  cd ~
~% export PATH=$HOME/ocaml-4.00.0-with-fp/bin:$PATH
</code></pre><p>It is important to know that the patch modifies OCaml calling convention, meaning that ALL THE MODULES AND LIBRARIES in your application must be recompiled with this version.</p><p>On our benchmarks, the slowdown induced by the patch is between 3 and 5%.  You can still compile your application without frame pointers, for production, using a new option <code>-fomit-frame-pointer</code> that was added by the patch.</p><p>This patch has been submitted for inclusion in OCaml. You can follow its status and contribute to the discussion here:</p><p><a href="http://caml.inria.fr/mantis/view.php?id=5721">http://caml.inria.fr/mantis/view.php?id=5721</a></p> 
   </div>
  </content>

  </entry>

  <entry>
    <id>http://www.ocamlpro.com/blog/2011/08/10/ocaml-pack-functors.html</id><title>Packing and Functors</title><author><name>Fabrice Le Fessant</name><uri>http://fabrice.lefessant.net/</uri><email>fabricel@ocamlpro.com</email></author><rights>All rights reserved by the author</rights><updated>2011-08-10T00:00:00Z</updated><link rel="alternate" href="http://www.ocamlpro.com/blog/2011/08/10/ocaml-pack-functors.html"/>
    
    
  <content type="xhtml">
   <div xmlns="http://www.w3.org/1999/xhtml">
      <p>We have recently worked on modifying the OCaml system to be able to pack a set of modules within a functor, parameterized on some signatures. This page presents this work, funded by Jane Street.</p><p>All the patches on this page are provided for OCaml version 3.12.1.</p><h1>Packing Functors</h1><h2>Installation of the modified OCaml system</h2><p>The patch for OCaml 3.12.1 is available here:</p><p><a href="http://www.ocamlpro.com//files/ocaml+libfunctor-3.12.1.patch.gz">ocaml+libfunctor-3.12.1.patch.gz</a> (26 kB)</p><p>To use it, you can use the following recipe, that will compile and install the patched version in ~/ocaml+libfunctor-3.12.1/bin/.</p><pre><code>~%  wget http://caml.inria.fr/pub/distrib/ocaml-3.12/ocaml-3.12.1.tar.gz
~%  tar zxf ~/ocaml-3.12.1.tar.gz
~%  cd ocaml-3.12.1
~/ocaml-3.12.1%  wget www.ocamlpro.com/code/ocaml+libfunctor-3.12.1.patch.gz
~/ocaml-3.12.1%  gzip -d ocaml+libfunctor-3.12.1.patch.gz
~/ocaml-3.12.1%  patch -p1 &lt; ocaml+libfunctor-3.12.1.patch
~/ocaml-3.12.1%  ./configure --prefix ~/ocaml+libfunctor-3.12.1
~/ocaml-3.12.1%  make coldstart
~/ocaml-3.12.1%  make ocamlc ocamllex ocamltools
~/ocaml-3.12.1%  make library-cross
~/ocaml-3.12.1%  make bootstrap
~/ocaml-3.12.1%  make all opt opt.opt
~/ocaml-3.12.1%  make install
~/ocaml-3.12.1%  cd ~
~% export PATH=$HOME/ocaml+libfunctor-3.12.1/bin:$PATH
</code></pre><p>Note that it needs to bootstrap the compiler, as the format of object files is not compatible with the one of ocaml-3.12.1.</p><h2>Usage of the lib-functor patch.</h2><p>Now that you are equiped with the new system, you can start using it. The lib-functor patch adds two new options to the compilers <b>ocamlc</b> and <b>ocamlopt</b>:</p><ul> <li> <p><b>-functor &lt;interface_file&gt;</b> : this option is used to specify that the current module is compiled with the interface files specifying the argument of the functor. This option should be used together with <b>-for-pack &lt;module&gt;</b>, where <b>&lt;module&gt;</b> is the name of the module in which the current module will be embedded.</p> </li><li> <p><b>-pack-functor &lt;module&gt;</b> : this option is used to pack the modules. It should be used with the option <b>-o &lt;object_file&gt;</b> to specify in which module it should be embedded. The <b>&lt;module&gt;</b> specified with <b>-pack-functor</b> specifies the name of functor that will be created in the target object file.</p> </li> </ul><p>If the interface <code>x.mli</code> contains :</p><pre><code>type t
val compare : t -&gt; t -&gt; int
</code></pre><p>and the files <code>xset.ml</code> and <code>xmap.ml</code> contain respectively :</p><pre><code>(* xset.ml *)
module T = Set.Make(X)

(* xmap.ml *)
module T = Map.Make(X)
</code></pre><p>Then :</p><pre><code>~/test%  ocamlopt -c -for-pack Xx -functor x.cmi xset.ml
~/test%  ocamlopt -c -for-pack Xx -functor x.cmi xmap.ml
~/test%  ocamlopt -pack-functor MakeSetAndMap  -o xx.cmx xset.cmx xmap.cmx
</code></pre><p>will construct a compiled unit whose signature is (that you can get with <code>ocamlopt -i xx.cmi</code>, see below) :</p><pre><code>module MakeSetAndMap :
  functor (X : sig type t val compare : t -&gt; t -&gt; int end) -&gt;
    sig
      module Xset :
        sig
          module T :
            sig
              type elt = X.t
              type t = Set.Make(X).t
              val empty : t
              val is_empty : t -&gt; bool
              ...
            end
        end
      module Xmap :
        sig
          module T :
            sig
              type key = X.t
              type 'a t = 'a Map.Make(X).t
              val empty : 'a t
              val is_empty : 'a t -&gt; bool
              ...
            end
        end
    end
</code></pre><h2>Other extension: printing interfaces</h2><p>OCaml only allows you to print the interface of a module or interface by compiling its source with the <b>-i</b> option. However, you don't always have the source of an object interface (in particular, if it was generated by packing), and you might still want to do it.</p><p>In such a case, the lib-functor patch allows you to do that, by using the <b>-i</b> option on an interface object file:</p><pre><code>~/test%  cat &gt; a.mli
val x : int
~/test%  ocamlc -c -i a.mli
val x : int
~/test%  ocamlc -c -i a.cmi
val x : int
</code></pre><h2>Other extension: packing interfaces</h2><p>OCaml only allows you to pack object files inside another object file (.cmo or .cmx). When doing so, you can either provide an source interface (.mli) that you need to compile to provide the corresponding object interface (.cmi), or the object interface will be automatically generated by exporting all the sub-modules within the packed module.</p><p>However, sometimes, you would want to be able to specify the interfaces of each module separately, so that:</p><ul> <li> <p>you can reuse most  of the interfaces you already specified</p> </li><li> <p>you can use a different interface for a module, that the one used to compile the other modules. This happens when you want to export more values to the other internal sub-modules than you want to export to the user.</p> </li> </ul><p>In such a case, the lib-functor patch allows you to do that, by using the <b>-pack</b> option on interface object files:</p><pre><code>test%  cat &gt; a.mli
val x : int
test%  cat &gt; b.mli
val y : string
test%  ocamlc -c a.mli b.mli
test%  ocamlc -pack -o c.cmi a.cmi b.cmi
test%  ocamlc -i c.cmi
module A : sig val x : int end
module B : sig val y : string end
</code></pre><h1>Using <b>ocp-pack</b> to pack source files</h1><h2>Installation of ocp-pack</h2><p>Download the source file from:</p><ul> <li> <p><a href="http://www.ocamlpro.com//files/ocp-pack-1.0.1.tar.gz">ocp-pack-1.0.1.tar.gz</a> (20 kB, GPL Licence, Copyright OCamlPro SAS)</p> </li> </ul><p>Then, you just need to compile it with:</p><pre><code>~%  tar zxf ocp-pack-1.0.1.tar.gz
~%  cd ocp-pack-1.0.1
~/ocp-pack-1.0.1%  make
~/ocp-pack-1.0.1%  make install
</code></pre><h2>Usage of ocp-pack</h2><p><b>ocp-pack</b> can be used to pack source files of modules within just one source file. It allows you to avoid the use of the <b>-pack</b> option, that is not always supported by all ocaml tools (for example, ocamldoc). Moreover, <b>ocp-pack</b> tries to provide the correct locations to the compiler, so errors are not reported within the generated source file, but within the original source files.</p><p>It supports the following options:</p><pre><code>%  ocp-pack -help
Usage:

  ocp-pack -o target.ml [options] files.ml*

Options:
  -o &lt;filename.ml&gt;         generate filename filename.ml
  -rec                     use recursive modules
                           all .ml files must have a corresponding .mli file
  -pack-functor &lt;modname&gt;  create functor with name &lt;modname&gt;
  -functor  &lt;filename.mli&gt; use filename as an argument for functor
  -mli                     output the .mli file too
                           .ml files without .mli file will not export any value
  -no-ml                   do not output the .ml file
  -with-ns                 use directory structure to create a hierarchy of modules
  -v                       increment verbosity
  --version                display version information
</code></pre><p><b>ocp-pack</b> automatically detects interface sources and implementation sources. When only the interface source is available, it is assumed that it is a type-only module, i.e. no <b>val</b> items are present inside.</p><p>Here is an example of using <b>ocp-pack</b> to build the ocamlgraph package:</p><pre><code>test%  ocp-pack -o graph.ml \
  lib/bitv.ml  lib/heap.ml  lib/unionfind.ml \
  src/sig.mli src/dot_ast.mli src/sig_pack.mli \
  src/version.ml src/util.ml src/blocks.ml \
  src/persistent.ml src/imperative.ml  src/delaunay.ml \
  src/builder.ml src/classic.ml src/rand.ml src/oper.ml \
  src/path.ml src/traverse.ml src/coloring.ml src/topological.ml \
  src/components.ml src/kruskal.ml src/flow.ml  src/graphviz.ml \
  src/gml.ml src/dot_parser.ml src/dot_lexer.ml src/dot.ml \
  src/pack.ml  src/gmap.ml src/minsep.ml src/cliquetree.ml \
  src/mcs_m.ml src/md.ml src/strat.ml
test%  ocamlc -c graph.ml
test%  ocamlopt -c graph.ml
</code></pre><p>The <b>-with-ns</b> option can be used to automatically build a hierarchy of modules. With that option, sub-directories are seen as sub-modules. For example, packing <b>a/x.ml</b>, <b>a/y.ml</b> and <b>b/z.ml</b> will give a result like:</p><pre><code>module A = struct
  module X = struct ... end
  module Y = struct ... end
end
module B = struct
  module Z = struct ... end
end
</code></pre><h2>Packing modules as functors</h2><p>The <b>-pack-functor</b> and <b>-functor</b> options provide the same behavior as the same options with the lib-functor patch. The only difference is that <b>-functor</b> takes the interface source as argument, not the interface object.</p><h2>Packing recursive modules</h2><p>When trying to pack modules with <b>ocp-pack</b>, you might discover that your toplevel modules have recursive dependencies. This is usually achieved by types declared abstract in the interfaces, but depending on each other in the implementations. Such modules cannot simply packed by <b>ocp-pack</b>.</p><p>To handle them, <b>ocp-pack</b> provides a <b>-rec</b> option. With that option, modules are put within a <b>module rec</b> construct, and are all required to be accompagnied by an interface source file.</p><p>Moreover, in many cases, OCaml is not able to compile such recursive modules:</p><ul> <li> <p>For typing reasons: recursive modules are typed in an environment containing only an approximation of other recursive modules signatures</p> </li><li> <p>For code generation reasons: recursive modules can be reordered depending on their shape, and this reordering can generate an order that is actually not safe, leading to an exception at runtime</p> </li> </ul><p>To solve these two issues in most cases, you can use the following patch (you can apply it using the same recipe as for lib-functor, and even apply both patches on the same sources):</p><ul> <li> <p><a href="http://www.ocamlpro.com//files/ocaml+rec-3.12.1.patch.gz">ocaml+rec-3.12.1.patch.gz</a></p> </li> </ul><p>With this patch, recursive modules are typed in an environment that is enriched progressively with the final types of the modules as soon as they become available. Also, during code generation, a topological order is computed on the recursive modules, and the subset of modules that can be initialized using in that topological order are immediatly generated, leaving only the other modules to be reordered.</p> 
   </div>
  </content>

  </entry>

  <entry>
    <id>http://www.ocamlpro.com/blog/2011/06/23/ocaml-mscv-win64.html</id><title>OCaml and Windows</title><author><name>Thomas Gazagnaire</name><uri>http://gazagnaire.org</uri><email>thomas@ocamlpro.com</email></author><rights>All rights reserved by the author</rights><updated>2011-06-23T00:00:00Z</updated><link rel="alternate" href="http://www.ocamlpro.com/blog/2011/06/23/ocaml-mscv-win64.html"/>
    
    
  <content type="xhtml">
   <div xmlns="http://www.w3.org/1999/xhtml">
      <p>Recently, I have been experimenting wiht OCaml / MSVC running on Windows 7 64bit. I have mainly followed what the <a href="http://caml.inria.fr/pub/distrib/ocaml-3.12/notes/README.win32">OCaml's README.win32</a> was saying and I learned some NSIS tricks. The result of this experiment is the following two (rather big) windows binaries :</p><ul> <li> <p><a href="http://www.ocamlpro.com//files/ocaml-trunk-64-installer.exe">ocaml-trunk-64-installer.exe</a> (92 MB)</p> </li><li> <p><a href="http://www.ocamlpro.com//files/ocaml-3.12-64-installer.exe">ocaml-3.12-64-installer.exe</a> (92 MB)</p> </li> </ul><p>These binaries are auto-installer for :</p><ul> <li> <p>the OCaml distribution (either the 3.12.1+rc1 version or trunk);</p> </li><li> <p>Emacs (version 23.3) + tuareg mode (version 2.0.4);</p> </li><li> <p>OCamlGraph (version 1.7) : this is just a little experiment with packaging external libraries.</p> </li> </ul><p>Hopefully, all of this might be useful to some people, at least to people looking for an alternative to WinOcaml which seems to be broken. You should need no other dependencies if you just want to use the OCaml top-level (ocaml.exe). If you want to compile your project you will need MSVC installed and correctly set-up. If your project is using Makefiles then you should probably install cygwin as well. I can give more details if some people are interested.</p><p>Unfortunately, the current process for creating these binaries involves an awlful lot of manual steps (including switching for Windows Termninal to cygwin shell) and further, many OCaml packages won't install directly on windows (as most of them are using shell tricks to be configured correctly). I hope we will be able to release something cleaner in a later stage.</p> 
   </div>
  </content>

  </entry>

  <entry>
    <id>http://www.ocamlpro.com/blog/2011/06/03/cheatsheets.html</id><title>OCaml Cheat Sheets</title><author><name>Fabrice Le Fessant</name><uri>http://fabrice.lefessant.net/</uri><email>fabricel@ocamlpro.com</email></author><rights>All rights reserved by the author</rights><updated>2011-06-03T00:00:00Z</updated><link rel="alternate" href="http://www.ocamlpro.com/blog/2011/06/03/cheatsheets.html"/>
    
    
  <content type="xhtml">
   <div xmlns="http://www.w3.org/1999/xhtml">
      <p>When you are beginning in a new programming language, it is sometimes helpful to have an overview of the documentation, that you can pin on your wall and easily have a look at it while you are programming. Since we couldn't find such <b>Cheat Sheets</b>, we decided to start writting our own cheat sheets for OCaml.</p><p>Beware, these documents are drafts, that we plan to improve in the next months. In the meantime, feel free to tell us how we could improve them, what is missing, and where the focus should be !</p><ul> <li> <p><a href="http://www.ocamlpro.com//files/ocaml-lang.pdf">The OCaml Language</a> (June 8, 2011)</p> </li><li> <p><a href="http://www.ocamlpro.com//files/ocaml-tools.pdf">OCaml Standard Tools</a> (June 7, 2011)</p> </li><li> <p><a href="http://www.ocamlpro.com//files/ocaml-stdlib.pdf">OCaml Standard Library</a>  (June 7, 2011)</p> </li><li> <p><a href="http://www.ocamlpro.com//files/tuareg-mode.pdf">OCaml Emacs Mode (Tuareg)</a> (June 27, 2011)</p> </li> </ul> 
   </div>
  </content>

  </entry>

  <entry>
    <id>http://www.ocamlpro.com/blog/2011/05/06/longval.html</id><title>OCaml 32bits longval</title><author><name>Thomas Gazagnaire</name><uri>http://gazagnaire.org</uri><email>thomas@ocamlpro.com</email></author><rights>All rights reserved by the author</rights><updated>2011-05-06T00:00:00Z</updated><link rel="alternate" href="http://www.ocamlpro.com/blog/2011/05/06/longval.html"/>
    
    
  <content type="xhtml">
   <div xmlns="http://www.w3.org/1999/xhtml">
      <p>Archive: <a href="http://www.ocamlpro.com//files/ocaml-3.11.2-longval-byte-i686.tar.bz2">ocaml-3.11.2-longval-byte-i686.tar.bz2</a></p><p>You will need OCaml 3.11.2 installed on a i686 linux computer. The archive contains:</p><ul> <li> <p>libcamlrun-linux-i686.a</p> </li><li> <p>ocamlrun-linux-i686</p> </li><li> <p>Makefile</p> </li><li> <p>README</p> </li> </ul><p>The Makefile has two targets:</p><ul> <li> <p><b>sudo make install</b> will save <i>/usr/bin/ocamlrun</i> and <i>/usr/lib/ocaml/libcamlrun.a</i> in the current directory and replace them with the longval binaries.</p> </li><li> <p><b>sudo make restore</b> will restore the saved files.</p> </li> </ul><p>If your install directories are not the default ones, you should modify the Makefile.  After installing, you can test it with the standard OCaml top-level:</p><pre><code># ocaml
        Objective Caml version 3.11.2

# let s = ref &quot;&quot;;;
val s : string ref = {contents = &quot;&quot;}

# s := String.create 20_000_000;;
- : unit = ()
</code></pre><p>Now you can enjoy big values in all your strings and arrays in bytecode. You will need to relink all your custom binaries. If you are interested in the native version of the longval compiler, you can <a href="http://www.ocamlpro.com//contact.html">contact</a> us.</p> 
   </div>
  </content>

  </entry>

  </feed>

