For the past few weeks I have been trying to write a little embedded DSL. The goal is to be able to easily generate bindings between OCaml and C, and maybe Objective-C, too.
So far, I have written a simplified representation of C 1 using normal algebraic datatypes. This will allow me to have a loosely-typed intermediate representation, which I can then pretty print. The pretty printer I have implemented using the Format module from stdlib. It already supports types, expressions and statements, but no global definitions and declarations, yet. To print OCaml code, I will use Format again for a first draft, and switch to CamlP4, should the need arise. I briefly considered using CamlP4 quotations for the whole project, but that would necessitate quotations and parsers for each language to bind. Unfortunately, my parser-fu is not up-to scratch, and the sparse documentation for CamlP4 is not helping either. 2 Maybe it is time to dust of the dragon books?
The EDSL that I am currently working on uses a feature just recently introduced with OCaml 4.0: generalized algebraic datatypes, or GADT for short. This will allow me to use OCaml’s type-inference and -checking for the FFI description language. So basically, I’m writing a strongly-typed code generator in OCaml. When run, this generator will in turn create the necessary OCaml and C code, constituting a particular OCaml foreign function interface.
One thing I’ve come to notice when working with GADTs is that it helps to have as much type annotations as possible in the beginning. Only later on, when everything is working, can some annotations be removed. Otherwise, there will be a lot of strange type errors. Which for a mere software engineer like me are sometimes hard to understand. With type annotations, the error messages are usually much clearer. I’ve found that recursive functions help a lot, as they are probably easiest to annotate and allow me to structure the code a little better. A concrete example is the following bind function:
1 2 3 4 |
|
With mutually recursive functions and additional type informations, the above example would look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
With the following types, interested readers should be able to toy around with the above functions in OCaml (Version 4.0 is required):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
-
I am aware of CIL, but it seems to heavyweight for my purposes, and does not support Objective-C. And I view this whole exercise as a learning experience. ↩
-
There is now at least a wiki for CamlP4. And Andrei Formiga has a nice collection of links on the subject. ↩