4.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 4.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 4.18), Word (Section 4.38), Ptr (Section 4.29), ForeignPtr (Section 4.15), StablePtr (Section 4.34), Storable (Section 4.35), MarshalAlloc (Section 4.22), MarshalArray (Section 4.23), MarshalError (Section 4.24), and MarshalUtils (Section 4.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 4.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.