[Haskell-cafe] how to write an haskell binding

Brian Hulley brianh at metamilk.com
Mon Jun 26 18:07:56 EDT 2006

minh thu wrote:
> about writing an haskell (or is it *a* haskell ?) binding for a c or a

It is defnitely *a* haskell. There is actually no word in English with a 
silent 'h', though this statement is unfortunately controversial and news to 
whoever wrote the spell checker used in many printed publications. Of course 
some particular dialects use different pronunciation like "me 'otel room 'ad 
an 'askell 'mpiler in t' closet as well as tub 'n sink tha knows"

> c++ library.
> i worry about :
> * implementation tools : use tools like c2hs or others, or do that
> bare hands

I'd recommend just using "bare hands" :-)

> * haskell idioms/usage : make good use of monads, type classes and
> other haskell related things

You might like to look at some existing libraries to get ideas ( 
http://www.haskell.org/haskellwiki/Libraries_and_tools )

> * for c++, is it better to first write a c api for the c++ code before
> writing the binding

Personnally, I write everything in C++, then have just one unit of plain C 
functions which are exported as the api, so the entire api is contained in 
one .h, .cpp (with extern "C" around the bindings), and .def file. I find it 
helpful to have a consistent naming convention, so if I have a static C++ 
class such as:

    class TimerFactory {
        static void Construct();
        static void Destruct();

then the C api functions are given names like:


where xxx is the name of the api.

> * maybe advice about the c api knowing there will be haskell binding
> * any thing i forgot

Perhaps to consider using the Haskell types for everything in the C code eg 
HsInt, HsWord16 etc instead of plain int, unsigned short etc, so that your 
Haskell code isn't messed up by having to use CInt instead of plain Int etc.

Regards, Brian.
Logic empowers us and Love gives us purpose.
Yet still phantoms restless for eras long past,
congealed in the present in unthought forms,
strive mightily unseen to destroy us.


More information about the Haskell-Cafe mailing list