C++ class structure mapping

Abraham Egnor aegnor@antioch-college.edu
Mon, 16 Jun 2003 21:33:40 -0400


I'd like to make a haskell binding for a C++ library.  Most of the tools
out there seem oriented towards c bindings, so it looks like I'll be
writing the FFI code generator myself.  While I'm at it I figure I might
as well just make a general C++ binding tool.  I've been thinking about
this, and I'd like some feedback on the ideas I've had.

First - it seems natural to use template haskell to do the code
generation.  That way, the library could either write the generated code
to a file using the pretty-printers, or just be spliced in directly.

Now comes the question of how to map a C++ class hierarchy into haskell. 
It seems natural to try to map C++ classes into haskell typeclasses;
however, there are a few issues involved with that.  A separate datatype
would have to be made for each C++ class to allow it to actually be
instantiated, which isn't too bad.  However, to allow haskell instances of
the typeclass to call the old behavior it seems that there'd have to be
duplicate functions of the ones in the typeclass, i.e.

class A a where
	foo :: a -> IO ()

foo_cpp :: (A a) => a -> IO ()

That seems to be needed to allow haskell instances to call the old
implementation in their definition, but it rubs me the wrong way.  Can
anyone suggest an alternate method, or suggest a different direction
entirely?

Abe