Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

rocaml – Write Ruby extensions in OCaml

By Peter Cooper / June 29, 2007

Camllogo

OCaml (short for Objective Caml) is an object oriented implementation of Caml, a derivative of ML. Like Ruby, it's an open source language, but it provides extremely high performance (at least 50% that of compiled C code, in general) and features static typing. You can learn more, and look at code examples, at OCaml's Wikipedia entry.

Mauricio Fernandez of Eigenclass has put together rocaml (readme), a bridge between Ruby and OCaml that lets you write Ruby extensions in OCaml. It handles the type conversions for you and creates boilerplate code that registers Ruby methods and wraps the calls to your OCaml code. Currently rocaml is considered to be "pre-release" but it seems to work okay, and it's certainly worth having a play with.

Comments

  1. Per Wigren says:

    Damn cool!!

  2. Lars T. says:

    So, is this re-inventing (or improving) RubyOCaml?

  3. Greg Vanard says:

    For those on Mac OS X there is an OCaml installer that comes with Labltk, LablGL and GraphPS at http://maxao.free.fr. Windows (as well as Mac) users may try http://godi.ocaml-programming.de.

    Some more OCaml example code can be found at
    http://pleac.sourceforge.net/pleac_ocaml/index.html

    A nifty web server written in ocaml, btw, is http://camlserv.sourceforge.net. Looks pretty good, doesn't it?

  4. mfp says:

    Lars: rocaml automates most of the steps described in that page, in particular those described in "Wrapping the OCaml code as a C library", "Compiling the OCaml code and the C wrapper", "The Ruby/C wrapper" and "Create extconf.rb". In other words, there's no need to write a single line of code in C when you use rocaml, as the latter is the "code generator" that page says it'd be nice to have...

    The thing that makes rocaml really convenient is the ability to pass rich structures between Ruby and OCaml. It can handle arrays, tuples, lists, records, (symbolic) variant and recursive types... So if you have some function taking a list of arrays of arrays of tuples with a float and a string and returning an array of floats, this declaration is all you need:


    fun "some_function", LIST(ARRAY(ARRAY(TUPLE(FLOAT, STRING)))) => ARRAY(FLOAT)

    You can declare things like


    tree_t = sym_variant("string_tree") do |t|
    constant :Empty
    non_constant :Node, TUPLE(t, STRING, t)
    end

    which corresponds to the OCaml type 'a tree in


    type 'a tree = Empty | Node of 'a tree * 'a * 'a tree

    and then have Ruby OCaml conversion routines written automagically. This all goes way beyond the sort of conversions something like RubyInline would do for you (it only handles strings and numeric types by default IIRC).

    There are some examples at http://eigenclass.org/repos/rocaml/head/examples/ .
    You can find there, amongst others, a 30-line set implementation built atop RB trees with 3X faster lookup than RBTree (which is written in C), and some 2-line specialized marshallers that operate 4-5 times faster than Ruby's Marshal...

Other Posts to Enjoy

Twitter Mentions