Chapter 8. Foreign function interface

Table of Contents
8.1. Introduction
8.2. Calling foreign functions
8.3. Invoking external functions via a pointer
8.4. Exposing Haskell functions
8.5. Using the FFI with GHC

The foreign function interface consists of the following components:

8.1. Introduction

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.