Example: Binding C++ to Haskell using the ffi

Here is some example code doing just that. It uses ghc. Questions: Can one think of better ways to interface generic c++ libraries to Haskell? What do you think of the ->> member function operator? for the ffi people: Has any though gone into cplusplus? If so, how would it interface? Would stubs on the c++ side be generated or would it use the code directly. Later, David J. Sankel

On Sat, 1 Mar 2003 13:40:42 -0800 (PST), David Sankel
Has any though gone into cplusplus? If so, how would it interface? Would stubs on the c++ side be generated or would it use the code directly.
I think that it is in general infeasable to call c++ member function directly as different c++ compilers use wildly different calling conventions and name-mangling schemes. It makes more sense to call C++ via extern "C" wrappers and use foreign import ccall on the Haskell side. In my opinion, the 'ideal' way of interfacing to C++, or C, would be an adoption of H/Direct to read C or C++ header files directly and translating it to a default IDL specification -- one should than be able to augment this specification with IDL attributes, much like the current 'asf' files of H/Direct but probably more friendly. H/Direct could than generate both exern "C" wrappers and Haskell marshalling code at the same time. On the other hand, even though the above approach would solve all the interface problems once and for all, it certainly involves a significant amount of work to extend H/Direct with a C++ front-end and better asf support. All the best, Daan. ps. I have been experimenting with interfacing to the wxWindows library, a large C++ framework. I have used an existing Eiffel library that wrapped the C++ classes in C functions together with a small hand-written tool to translate their (simple) header files to Haskell marshalling code -- a poor-mans solution until H/Direct gets an update :-) It works rather well and imports 400 classes with 2500 methods with very reasonable type signatures. You may want to look at it to get some inspiration for interfacing to C++: http://wxhaskell.sourceforge.net It also shows how you can use polymorphism to mimic inheritance relationships (as described in "Calling hell from heaven and heaven from hell") pps. This is just a preview, don't expect a wxHaskell too soon, I have a thesis to finish in the next weeks :-)
Later,
David J. Sankel _______________________________________________ GUI mailing list GUI@haskell.org http://www.haskell.org/mailman/listinfo/gui

The C++ compilers are all supposed to use the same name mangling scheme, which is part of the standard. On Saturday 01 March 2003 03:11 pm, Daan Leijen wrote:
On Sat, 1 Mar 2003 13:40:42 -0800 (PST), David Sankel
wrote: Has any though gone into cplusplus? If so, how would it interface? Would stubs on the c++ side be generated or would it use the code directly.
I think that it is in general infeasable to call c++ member function directly as different c++ compilers use wildly different calling conventions and name-mangling schemes. It makes more sense to call C++ via extern "C" wrappers and use foreign import ccall on the Haskell side.
In my opinion, the 'ideal' way of interfacing to C++, or C, would be an adoption of H/Direct to read C or C++ header files directly and translating it to a default IDL specification -- one should than be able to augment this specification with IDL attributes, much like the current 'asf' files of H/Direct but probably more friendly. H/Direct could than generate both exern "C" wrappers and Haskell marshalling code at the same time.
On the other hand, even though the above approach would solve all the interface problems once and for all, it certainly involves a significant amount of work to extend H/Direct with a C++ front-end and better asf support.
All the best, Daan.
ps. I have been experimenting with interfacing to the wxWindows library, a large C++ framework. I have used an existing Eiffel library that wrapped the C++ classes in C functions together with a small hand-written tool to translate their (simple) header files to Haskell marshalling code -- a poor-mans solution until H/Direct gets an update :-) It works rather well and imports 400 classes with 2500 methods with very reasonable type signatures. You may want to look at it to get some inspiration for interfacing to C++: http://wxhaskell.sourceforge.net It also shows how you can use polymorphism to mimic inheritance relationships (as described in "Calling hell from heaven and heaven from hell") pps. This is just a preview, don't expect a wxHaskell too soon, I have a thesis to finish in the next weeks :-)
Later,
David J. Sankel _______________________________________________ GUI mailing list GUI@haskell.org http://www.haskell.org/mailman/listinfo/gui
_______________________________________________ GUI mailing list GUI@haskell.org http://www.haskell.org/mailman/listinfo/gui
-- Seth Kurtzberg M. I. S. Corp. 480-661-1849 seth@cql.com

--- Seth Kurtzberg
The C++ compilers are all supposed to use the same name mangling scheme, which is part of the standard.
Just to clarify on this a little bit; there is no ABI specification in standard C++. However, there is an industry standard emerging that g++ >=3.2 attempts (and succeds?) to adhere too. Here is the link: http://www.codesourcery.com/cxx-abi/ Some gcc information on this: http://gcc.gnu.org/gcc-3.2/c++-abi.html David J. Sankel

David Sankel
for the ffi people:
Has any though gone into cplusplus? If so, how would it interface? Would stubs on the c++ side be generated or would it use the code directly.
Interfacing in a clean and comprehensive way to OO languages is hard. For a discussion of some of the problems wrt to typing, see http://research.microsoft.com/Users/simonpj/Papers/oo-haskell/ AFAIK, nobody is currently actively working on a FFI extension for C++ or Java. Cheers, Manuel
participants (4)
-
Daan Leijen
-
David Sankel
-
Manuel M T Chakravarty
-
Seth Kurtzberg