Next Previous Contents

2. Foreign function interfaces and Haskell

Most of the world's software is not written in Haskell. Not earth shattering news this, so how can we `improve' on this situation? There's (at least) two alternative routes that can be followed to make a language like Haskell a viable alternative when picking the language(s) to use for a project:

The first route is a very long one, and it often turns out that reverse engineering a library in Haskell doesn't radically improve on the original programming interface. The Haskell variant of the "not-invented-here" syndrome.

The second option makes much more sense, re-using external libraries from Haskell, customising their programming interfaces to provide a veneer that is more appealing to the Haskell Programmer In The Street.

Support for the Wise Choice is provided by most Haskell systems through their foreign function interfaces. One early example of such a beast was (and still is) GHC's support for embedding calls to C functions inside IO code with the _ccall_ construct. Together with built-in compiler support for converting from a primitive set of Haskell types to their expected external representation (and back), this gives the Haskell programmer the ability to interface to external libraries without too much bother. Examples of libraries/applications that have been built using _ccall_ include interfaces to windowing systems/toolkits (e.g., Haggis, Budgets, etc.), system libraries (e.g., POSIX lib), etc. Other Haskell systems provide similar foreign function interface support (e.g., Hugs and Gofer's primitive declarations.)

This is all good stuff and a move in generally the right direction, but current foreign function interfaces fall short in couple of departments:

Based on the experiences with GHC's _ccall_ and Hugs' primitive, Green Card was implemented, a foreign function interface preprocessor for Haskell. It addresses the problem of portability by providing a little language for describing the type signatures of external functions, and how to convert from the Haskell data representation of the external function to the expected representations (and back again.) Given such a specification, the implementation of Green Card takes care of generating the Haskell system specific code for actually calling out.

Green Card has proven to be a very useful addition to the Haskell programmer's toolset, but it too has its problems and limitations:

Instead of extending Green Card to address the above points, we've decided to opt for an off-the-shelf solution to the problem: using an interface definition language(IDL) to describe foreign functionality. In the cases where there is a difference between the type signature of the external function, and the type signature that the programmer wants to present to the Haskell user, Haskell is used to make up the difference.


Next Previous Contents