The foreign interface consists of the following components:
The Foreign Function Interface language specification (included in this manual, in Chapter 7).
The Foreign module (see Section 4.9 in Haskell Libraries) collects together several interfaces which are useful in specifying foreign language interfaces, including the following:
The ForeignObj module (see Section 4.10 in Haskell Libraries), for managing pointers from Haskell into the outside world.
The StablePtr module (see Section 4.24 in Haskell Libraries), for managing pointers into Haskell from the outside world.
The CTypes module (see Section 4.5 in Haskell Libraries) gives Haskell equivalents for the standard C datatypes, for use in making Haskell bindings to existing C libraries.
The CTypesISO module (see Section 4.6 in Haskell Libraries) gives Haskell equivalents for C types defined by the ISO C standard.
The Storable library, for primitive marshalling of data types between Haskell and the foreign language.
The following sections also give some hints and tips on the use of the foreign function interface in GHC.
When generating C (using the -fvia-C directive), one can assist the C compiler in detecting type errors by using the -#include directive to provide .h files containing function headers.
For example,
#include "HsFFI.h" void initialiseEFS (HsInt size); HsInt terminateEFS (void); HsForeignObj emptyEFS(void); HsForeignObj updateEFS (HsForeignObj a, HsInt i, HsInt x); HsInt lookupEFS (HsForeignObj a, HsInt i); |
The types HsInt, HsForeignObj etc. are described in Table 7-1.
Note that this approach is only essential for returning floats (or if sizeof(int) != sizeof(int *) on your architecture) but is a Good Thing for anyone who cares about writing solid code. You're crazy not to do it.