Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent Haskell type for C99 bool when using FFI?

Tags:

haskell

c99

ffi

I have a library which uses C99 bool data type and I would like to call it via FFI.

What is the corresponding type for C99 bool in Haskell? In Foreign.C.types there are CInt, CShort etc, but no CBool.

If there is no "correct" type for bool, what is a safe alternative type to be passed in a function expecting a bool?

An alternative approach would be to modify the C library but I would like to keep it intact.

like image 457
Zouppen Avatar asked Mar 12 '13 17:03

Zouppen


1 Answers

Since sizeof(_Bool) is implementation-defined in C99 (see ISO/IEC 9899:1999 6.2.5, 6.2.6 and 6.5.3.4/4), the obvious portable solution is to call such functions via wrappers that use int instead of bool. Alternatively, if you don't care about portability, you can check the documentation of your compiler to find out what is sizeof(_Bool) on your platform and use the corresponding FFI type.

My guess for the reason of CBool being absent from Foreign.C.Types is that the underlying C implementation is not expected to support all features of C99. One very widely-used compiler (MSVC) does not support C99 at all.

like image 140
Mikhail Glushenkov Avatar answered Sep 21 '22 06:09

Mikhail Glushenkov