[Haskell-cafe] foreign function types

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Tue Jan 27 04:04:29 EST 2009

On Tue, 2009-01-27 at 06:32 +0200, Eyal Lotem wrote:
> I have encountered quite a bit of bugs in foreign bindings where "Int"
> was used in place of "Cint".
> As far as I know, no foreign function should ever take a Haskell Int,
> only CInts.  
> Would it be possible to create an empty type-class of which various
> C-acceptable types are instances (e.g CInt, CString) but Haskell types
> aren't (Ints)?

The compiler does already know which types are valid in FFI calls. The
problem perhaps is that Int is one of the allowed types, it's just that
on the C side the corresponding type must be 'HsInt' and not 'int'.

Unfortunately the hsc2hs tool provides no support for checking C <->
Haskell type consistency. In contrast, the c2hs tool can prevent these
problems because it will generate the correct ffi import types given the
C headers.


More information about the Haskell-Cafe mailing list