How to find out the C type signature corresponding to a Haskell function type in FFI?

Simon Marlow simonmarhaskell at gmail.com
Thu Mar 9 09:08:09 EST 2006


Sven Panne wrote:
> Am Donnerstag, 9. März 2006 08:46 schrieb Bulat Ziganshin:
> 
>>Thursday, March 9, 2006, 2:20:00 AM, you wrote:
>>
>>>>foreign import ccall duma_init :: Int -> IO Int
>>
>>MQK> HsInt duma_init(HsInt arg);
>>MQK> Or use int on the C side and CInt on the Haskell side.
>>MQK> fromIntegral can be used for converting integers in Haskell.
>>
>>for small "home" projects you can even use "Int" and "int" which
>>would work in most of Haskell implementations
> 
> 
> Uh, oh... :-( This is roughly as "safe" as assuming that short, int, long, and 
> long long are of the same size, so this is highly discouraged. The rationale 
> behind the FFI addendum is as follows:
> 
>  * If you want to use a given C API, use the types from Foreign.C.Types on the 
> Haskell side in the foreign imports.
> 
>  * If the C API is under your control, you have basically two options: Either 
> use Foreign.C.Types as above, or use the types/preprocessor #defines in the 
> HsFFI.h header to bind to basic Haskell types. Which option is better suited 
> depends on what you are trying to achieve.
> 
> This should be easy enough to use and everything else is not guaranteed to 
> work...

Yes, and to give a concrete example:  HsInt and int are different types 
on x86_64 (and most other 64-bit C implementations).

Cheers,
	Simon


More information about the Glasgow-haskell-users mailing list