4.9. Foreign

The Foreign Function Interface (FFI) consists of three parts:

  1. foreign import and export declarations (defined in an extra document A Haskell Foreign Function Interface),

  2. a low-level marshalling library (see Section 4.13, Section 4.27, Section 4.1, Section 4.10, Section 4.24, Section 4.25, Section 4.5, and Section 4.6), and

  3. a high-level marshalling library (this is still under development and not included in the current distribution).

The module Foreign provides the interface to the language independent portion of the second component, i.e., the modules Int (Section 4.13), Word (Section 4.27), Addr (Section 4.1), ForeignObj (Section 4.10), StablePtr (Section 4.24), and Storable (Section 4.25). The two modules CTypes (Section 4.5) and CTypesISO (Section 4.6) are specific to code interfacing with C - especially, for implementing portable Haskell bindings to C libraries. However, currently there is no dedicated support for languages other than C, so that Foreign will usually be used in conjunction with CTypes and CTypesISO.

The code for marshalling of Haskell structures into a foreign representation and vice versa can generally be implemented in either Haskell or the foreign language. At least if the foreign language is a significantly lower level language, such as C, there are good reasons for doing the marshalling in Haskell:

Consequently, the Haskell FFI emphasises Haskell-side marshalling.