This is just some notes about my experiences with the various FFI tools, and my opinions. Maybe I am missing something but...... Let me just say that I like the 'new' FFI. The approach is much better then the usual approach to 'native' interfaces that you see with Tcl, java, python, whatever. In those cases you have to do the marshalling by hand, except where you can use SWIG, which is a pretty cool tool. Overall, I think there is not a perfect solution. Someone correct me if I am wrong. The ideal solution should take advantage of the fact that we are using Haskell: 1) Use the new FFI, for hugs and ghc. 2) Do Marshalling on the haskell side only. 3) Be a high level marshaling combinator library (written in haskell). 4) OPTIONALLY support some sort of IDL for automatically finding invoking the correct marshalling combinators. How do the existing tools stack up? I am not sure, because they are all in various states of support/etc. Here are my impressions... they may be wrong! Please take this with a grain of salt, I have not spent a lot of time with the tools below, just played with them a bit. Most of time was spent trying to build them L:) Greencard: Seems to work pretty well overall.. I dont beleive it supports hugs/ffi, but it does support hugs. It does the marshalling in haskell, but with a lot of _casm_ so I am not sure what that means. I got some simple examples to work, but not the Gdbm. The makefile didnt seem to make sense.. HC not defined, etc. Probably could with abit of work. I cant tell if callbacks are supported. HDirect: This one is still pretty beta, and so I had some trouble getting it to compile on my solaris machine. Probably not intended for solaris. I had to by hand prune some of the makefiles, ( in lib, there was some mysterious error about 'test' haveing no arguments when deciding whether to create a 'hugs' subdirectory, and In the end I commented out the all :: dlls line. but I did finally get it to go, at least the ghc parts. This one does its marshalling in haskell. It uses an IDL that is fairly common, although I dont know how much longer COM is going to be important. Anyway, when ready for prime time it might work pretty well, although like I said I would rather have a nice library. As for examples, I got the string example to work (you have to set FOR_WIN32 to no, even if you set FOR_HUGS to no. The makefile is a bit misleading). The gd example doesnt work, it assumes an old libgd that used to support GIFs. They arent suppored anymore, for legal reasons. KDirect: Looked at it abit. Pretty sparsely documented. I could not figure out how it fits in with the latest tools. How does QForeign relate to Foreign? What is an hsc file? Also it is pretty limitted in the marshalling. No callbacks etc. Others: I know there are a couple of others, but I lost interest. I Just wanted to report my findings for others, and then I am going into wait and see mode :) I dont have a dire need for this stuff at the moment, I can get my tasks done a different way. (Dont ask, it aint pretty).... Cheers to all! __________________________________________________ Do You Yahoo!? Get email at your own domain with Yahoo! Mail. http://personal.mail.yahoo.com/
On Thu, 5 Apr 2001, Ronald Legere wrote:
KDirect: Looked at it abit. Pretty sparsely documented. I could not figure out how it fits in with the latest tools. How does
KDirect is still in a development phase. That, and lack of time, is a cause for sparse documentation.
QForeign relate to Foreign? What is an hsc file? Also it is pretty
Thanks for the criticism. I'll add these informations to the documentation.
limitted in the marshalling.
It is, indeed. What more would you want?
No callbacks etc.
Could you elaborate on that? Wojciech Moczydlowski, Jr
Thu, 5 Apr 2001 07:45:44 -0700 (PDT), Ronald Legere <rjljr2@yahoo.com> pisze:
KDirect: Looked at it abit. Pretty sparsely documented. I could not figure out how it fits in with the latest tools. How does QForeign relate to Foreign? What is an hsc file? Also it is pretty limitted in the marshalling. No callbacks etc.
Here is the story of QForeign, hsc and KDirect. One year ago the core 'foreign' declarations were already stable and implemented in ghc (for sure) and nhc (probably already at that time). The FFI documentation said that support for more complex marshalling is delegated to separate tools. Available tools, namely HDirect and Manuel's C2HS, were too heavy for my taste. But Sven's modules provided by ghc: FFI (later renamed to Foreign) and Marshal, were insufficient. I realized that a few more functions would make the basic FFI usable directly, without sophisticated preprocessors. I also wanted to experiment with typed 'Ptr a' instead of amorphic 'Addr'. So I merged ideas from ghc's module FFI and C2HS with my own FFI vision, and QForeign was born. QForeign evolved. The basic style of calling a C function while marshalling arguments and results changed a few times. It got examples. It suggested how to handle structs and enums. It became my Unicode laboratory. Since writing lots of small C functions for getting struct fields and constants was painful, I wrote glue-hsc: a simple preprocessor which translates a Haskell module with embedded C bits into a C program which outputs a real Haskell module. It allows embedding snippets of C code which are extracted into .c and .h files to be compiled separately. The source file extension .hsc means: .hs + bits of C included. QForeign was following changes in ghc and it also influenced ghc. ghc got typed pointers, alloca, withForeignObj, C array support, hsc2hs (renamed from glue-hsc), errno handling, C string support interface. As soon as some QForeign's feature got into ghc, QForeign switched to use it from ghc instead of providing it itself. If a feature mutated during its path to ghc, QForeign reflected the change. ghc's FFI support and my FFI vision were slowly converging. I had to write a networking program at the University, so of course I decided to use Haskell. That's why QForeign has pcap in examples. I asked my admin to install ghc in students' lab. The latest stable version was 4.08.1, so I #ifdefed parts of QForeign to support ghc-4.08* too by providing its own typed pointers and Storable again (no Unicode, no --add-package). Supporting several ghc versions at once happened to be easy (hsc2hs delegates #ifdefs to cpp). So I tried to port QForeign to nhc too and it worked, after some bugs that I reported were fixed. So I also thought about supporting hbc, which would require writing a preprocessor which translates 'foreign' declarations to appropriate hbc's magic, but hbc is dead, so I forgot it. Since Hugs recently got 'foreign' declarations, it might be possible to port QForeign to Hugs in future, but its foreign module management looks hard. QForeign and FFI modules provided by ghc-5.00 (to be released really soon) are now very close, after Manuel put some marshalling utilities into ghc and I backported them to QForeign as usual. The largest difference is that QForeign makes real use of Unicode (when the compiler has wide Chars) by converting most strings exchanged with the world between the specified encoding (default: locale dependent) and Haskell's Unicode, even though Unicode support modules are still experimental and transparent conversion on Handle IO is temporarily hacked on top of original Handle functions. In essence QForeign provides the interface of ghc-5.00's FFI on ghc >= 4.08 and nhc98. My friend Wojciech Moczydlowski <khaliff@astercity.net> asked me what is the currently recommended way of interfacing between Haskell and C. I showed him how do I do it. He said that Greencard and HDirect were easier: you didn't have to write marshalling code around each function, but all arguments were magically coverted basing on a single type declaration of the function. I replied that my solution is more general, especially if the Haskell's interface of a C module doesn't want to exactly mirror the original C interface, and that it's impossible to match everything automatically, and that it's not that painful to write function wrappers by hand. He said that a tool could handle simple cases automatically, leaving only hard types to the programmer. I said that I don't know how express a C interface to archieve this, and that C libraries are too complex to be wrapped in Haskell automatically, and thus I can't provide anything more sophisticated than hsc2hs. So he created KDirect. KDirect currently works on top of QForeign and hsc2hs. I assume that in future it will be able to use FFI modules provided by ghc >= 5.00, and that eventually all Haskell implementations will provide common FFI modules. -- __("< Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/ \__/ ^^ SYGNATURA ZASTÊPCZA QRCZAK
--- Marcin 'Qrczak' Kowalczyk <qrczak@knm.org.pl> wrote:
Here is the story of QForeign, hsc and KDirect.
Wow, that really answered my questions about QForeign! My impression was completely wrong. Maybe Marcin can append his story to the README :) I will in my copy:) Marcin's philosophy is pretty close to what I was suggesting in my email: Have a powerfull library to do all marshalling in, and optionally use some tools to automate some of it. I had no idea that these things were planned for ghc 5.00. This is good news to me. I wish I could be on the bleeding edge :) Cheers! __________________________________________________ Do You Yahoo!? Get email at your own domain with Yahoo! Mail. http://personal.mail.yahoo.com/
Fri, 6 Apr 2001 04:28:28 -0700 (PDT), Ronald Legere <rjljr2@yahoo.com> pisze:
Maybe Marcin can append his story to the README :)
Done. QForeign-0.65 will be released in a few days. -- __("< Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/ \__/ ^^ SYGNATURA ZASTÊPCZA QRCZAK
Greencard: Seems to work pretty well overall.. I dont beleive it supports hugs/ffi, but it does support hugs.
That's correct. The FFI support recently added to Hugs is not yet in a state to support GreenCard. The main thing lacking is an up to date StdDIS and updating Hugs FFI-related types and libraries (Foreign objects, Int, Addr, Word, Ptr a, etc.) match GreenCard's assumptions (i.e., GHC and, I think, NHC). In the meantime, GreenCard still works great with Hugs when you use --target=hugs (which doesn't use the new ffi support). -- Alastair Reid
participants (4)
-
Alastair Reid -
qrczak@knm.org.pl -
Ronald Legere -
Wojciech Moczydlowski, Jr