5.13. 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 language-independent marshalling library (whose interface is provided by the present module), and

  3. a language-dependent marshalling library (see Section 5.6).

The language-independent portion of the marshalling library, whose entire interface is provided by the module Foreign, consists of the following modules: Int (Section 5.18), Word (Section 5.39), Ptr (Section 5.29), ForeignPtr (Section 5.15), StablePtr (Section 5.34), Storable (Section 5.35), MarshalAlloc (Section 5.22), MarshalArray (Section 5.23), MarshalError (Section 5.24), and MarshalUtils (Section 5.25). The language-dependent portion of the marshalling library is currently only available for the language C, and is essentially intended to support writing portable Haskell bindings to C libraries. All C-dependent functionality can be obtained from the module CForeign (Section 5.6).

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.

As module Foreign simply re-exports the interface of the other modules listed above, please refer to the documentation of these modules for the details of the provided definitions.