The foreign function interface consists of the following components:
The Foreign Function Interface language specification (which constitutes most of this Chapter, beginning with Section 8.1). You must use the -fglasgow-exts command-line option to make GHC understand the foreign declarations defined by the FFI.
Several library modules which provide access to types used by foreign languages and utilties for marshalling values to and from foreign functions, and for converting errors in the foreign language into Haskell IO errors. See Section 5.13 in Haskell Libraries for more details.
The motivation behind this foreign function interface (FFI) specification is to make it possible to describe in Haskell source code the interface to foreign functionality in a Haskell system independent manner. It builds on experiences made with the previous foreign function interfaces provided by GHC and Hugs. However, the FFI specified in this document is not in the market of trying to completely bridge the gap between the actual type of an external function, and what is a convenient type for that function to the Haskell programmer. That is the domain of tools like HaskellDirect or Green Card, both of which are capable of generating Haskell code that uses this FFI.
In the following, we will discuss the language extensions of the FFI. The extensions can be split up into two complementary halves; one half that provides Haskell constructs for importing foreign functionality into Haskell, the other which lets you expose Haskell functions to the outside world. We start with the former, how to import external functionality into Haskell.