Interfacing variadic functions from Haskell

Posted on by nek0 in english, programming

I confess I am a Haskell aficionado. Whenever I program something for pleasure, I usually prefer this language because of its elegance.

Currently I am working on Haskell bindings to the GEGL library. The motivation behind this my desire to dabble in Game development and I have the need for a library to draw on SDL Surfaces. I am obviously not really a fan of the easy solutions and I try to learn new things. Like using the Haskell FFI.

While writing the bindings I encountered the problem, that GEGL exposes variadic functions in its header which I need to interface. This poses a serious Problem for Haskell because the number of function arguments has to be constant. There is simply no way defining a function without knowing how many arguments it has and of what type each argument is. This stays true even for my solution. The only reason my solution works is that I can limit the cases how to interface these variadic functions to a manageable amount.

To build my bindings I do not use the standard FFI of Haskell, but the Haskell library inline-c to call the C functions directly without using rigid bindings. This is achieved in inline-c by wrapping the function call into a QuasiQuoter. As I said earlier, this still requires you to write a QuasiQuoter for every case this function gets called, but you don’t have to clutter your code with foreign import ccall declarations.

For limiting your cases I recommend using a sum type as a function argument. A sum type is a type which has multiple constructors. You can have a constructor for each case you need to interface and distinguish between them using Haskell’s pattern matching. You can see an example on how to make all this in my bindings.