Search code examples
haskellprotocol-buffersswigthriftidl

Options for wrapping a C++ library for Haskell (and other languages)


This question is about design / is fairly open-ended.

I'd like to use OpenCV, a large C++ library, from Haskell. The closest solution at the moment is probably Arjun Comar's attempt to adapt the Python / Java binding generator. See here, here, and here. His approach generates a C interface, which is then wrapped using hsc2hs. Due to OpenCV's lack of referential transparency in its API, as well as its frequent use of call parameters for output, for Arjun's approach to fully succeed he'll need to define a new API for OpenCV, and implement it in terms of the existing one.

So, it seems it might not be too much extra work to go whole-hog and define an API using an interface description languages (IDL), such as SWIG, protobuf-with-RPC, or Apache Thrift. This would provide interfaces to a number of languages besides Haskell.

My questions:

  1. Is there anything better than SWIG for a server-free solution? (I just want to call into C++; I'd rather not go through a local server.)

  2. If there's no good server-free solution, should I use protobuf-with-RPC or Thrift?

    1. Related: How good is Thrift's Haskell support? From the code, it looks like it needs updating (I see references to GHC 6).

    2. Related: What's a good protobuf-with-RPC solution?


Solution

    1. With Apache Thrift, you get Haskell support. You are correct, code is not generally "latest", but you rarely care. You can do complex things on other abstraction levels and keep things as simple as possible at messaging level.

    2. Google Protobuf has no support for Haskell, nor does SWIG. With Protobuf you get C++, Java, JavaScript and Python, to my knowledge the main languages at Google. Have a look at this presentation. Without contest, Thrift and Protobuf are the best in house.

    It seems in your case you have to go with Thrift, as it supports Haskell.