Ciml : C in OCaml

Olivier has been busy extending my previous work on inlining C in OCaml, and it is pretty cool. You can get it from the following darcs repository.

darcs get http://chadok.info/darcs/ciml/

Basically, it allows you to inline C in OCaml code, in a very fashionable way. Type conversion between OCaml and C is (almost always) automatically dealt with for base types (int, float, bool, string, etc.) and you can register and unregister custom converters. A few examples are shown in the test.ml.

letext hello (s:string) : unit =  <<
  printf("Hello %s !\n", s);
  Return();
>>
letext add (a:int) (b:int) : int =  <<
  int c;
  c = a + b;
  Return(c);
>>

(* Unregister the converters for int *)
unregister_fromval : int
unregister_toval : int

(* Register converters *)
register_fromval "Int_val" "int" : int
register_toval "Val_int" : int

The idea behind all this syntaxic sugar for inlining C code in OCaml is to provide a natural way of using C and OCaml together to whoever wants to bind C libraries to OCaml. Of course, this is still under developement, and any ideas to improve it are welcome.

5 Responses to "Ciml : C in OCaml"

till says:

This is very cool indeed. I've been thinking something along these lines for opengl shaders would be a killer feature. Never got to implement anything though (and will probably not in the foreseeable future).

David Teller says:

Impressive, quite impressive.

Olivier says:

Thanks to everybody :)

I didn't have time to write an article about Ciml so I thank Adrien for this one.

Reid says:

Indeed great idea and very practical… the biggest obstacle to my happy usage is lack of tuareg support. Do you know anybody who got things setup to allow c-mode inside the <> blocks? I think I've seen this done in other emacs packages, but I can't be 100% sure.

Reid says:

After posting my question… I went to do a little research and found the emacs "two-mode-mode":

http://www.welton.it/freesoftware/files/two-mode-mode.el

I modified the configuration to look something like this and it seems to work!

(defvar default-mode (list "tuareg" 'tuareg-mode))
(defvar second-modes (list
(list "C" "<>" 'c-mode)
))

Its a little clunky as you flip back and forth between modes, hopefully that won't get too ugly for large buffers. Combined with something like folding/hide-show mode, you could then open/close the c-blocks as required.

Leave a Reply