title: Mixing OCaml and C: is it worth the pain?
body: <p>I am faced with the task of building a new component to be integrated into a large existing C codebase.  The component is essentially a kind of compiler, and will be complicated enough that I would like to write it in OCaml (for reasons along the lines of those given <a href="https://stackoverflow.com/questions/2906064/why-is-writing-a-compiler-in-a-functional-language-easier">here</a>).  I know that OCaml-C interaction is possible (as per the <a href="http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html" rel="nofollow noreferrer">manual</a> and this <a href="http://www.linux-nantes.org/~fmonnier/OCaml/ocaml-wrapping-c.php" rel="nofollow noreferrer">tutorial</a>), but it looks somewhat painful.</p>
  
  <p>What I'd like to know is whether others here have attempted large-scale integration of OCaml and C code, what were some of the unexpected gotchas they found, and whether at the end of the day they concluded that they would have been better off just writing the new code in C.</p>
  
  <p>Note, I'm <em>not</em> trying to start a debate about the merits of functional versus imperative programming: let's just say we assume that OCaml happens to be the right tool for the job I have in mind, and the potential difficulty in integration is the only issue.  I also don't have the option of rewriting the rest of the codebase.</p>
  
  <p>To give a little more detail about the task: the component I need to implement is a certain kind of query optimizer that incorporates some research ideas my group at UC Davis is working on, and will be integrated into PostgreSQL so that we can run experiments.  (A query optimizer is, essentially, a compiler.)  The component would be invoked from C code, would function mostly independently but would make a certain number of calls to other PostgreSQL components to retrieve things like system catalog information, and would construct a complex C data structure (representing a physical query plan) as output.</p>
  
  <p>Apologies for the somewhat open-ended question, but I'm hoping the community might be able to save me a little trouble :)</p>
  
  <p>Thanks,</p>
  
  <p>TJ</p>