[Haskell-cafe] FFI basics
sven.panne at aedion.de
Sat Feb 10 07:46:14 EST 2007
Am Samstag, 10. Februar 2007 09:21 schrieb Donald Bruce Stewart:
> > Hello Yitzchak,
> > Friday, February 9, 2007, 3:23:53 PM, you wrote:
> > > I would like to use FFI for the first time. Can someone
> > > give me a really, really simple complete example?
> > nothing can be easier
> > main = print (c_mysin 1.0)
> > foreign import ccall "mysin.h mysin"
> > c_mysin :: Double -> Double
> Shouldn't that be CDouble? At least for Int/CInt you can hit troubles on
> 64 bit machines...
Yes, the code above is wrong in the sense that it makes assumptions which are
not guaranteed at all in the FFI spec. The rules to follow are extremely
simple, so there is no reason to ignore them:
* If you want to use a C type "foo" in Haskell, use "CFoo" in the FFI
(imported from Foreign.C.Types).
* If you want to use a Haskell type "Bar" in C, use "HsBar" in C code
(#included from HsFFI.h).
It depends on the application/library in question which alternative is easier,
but never use a mix.
More information about the Haskell-Cafe