wiki:ForeignFunctionInterface

Version 5 (modified by ross@…, 8 years ago) (diff)

--

Foreign Function Interface

See ExtensionDescriptionHowto for information on how to write these extension descriptions. Please add any new extensions to the list of HaskellExtensions.

Brief Explanation

Adds support for invoking code written in other programming languages from Haskell and vice versa. The FFI is designed as a non-intrusive extension to the Haskell 98 standard.

References

Tickets

#35
ForeignFunctionInterface

Pros

  • Widely accepted and used addendum.
  • Provides an essential facility.

Cons

  • Inaccurate foreign imports can invalidate all guarantees given by Haskell.

Issues

  • Some feel that the inclusion of unsafePerformIO sends the wrong signal.
    • It is true that by giving inaccurate foreign imports one can define unsafePerformIO, but that doesn't mean it should be standard.
    • The stated purpose of unsafePerformIO in the FFI (hiding marshalling and unmarshalling for otherwise pure functions) can be achieved with a runST-like device if the language has Rank2Types or RankNTypes.
  • Foreign imports may specify a single include file. This is sufficient in theory, but most GHC users prefer to use -#include options instead, and the standard form is poorly supported by GHC. The hierarchical libraries use an INCLUDE pragma as a portable replacement for OPTIONS_GHC -#include. jhc lazily collects the imports needed and only includes the ones needed by the FFI functions used after optimizations. this means you can have FFI calls refering to libraries and includes that are not on a system and still compile the program if none of the routines are used.
  • Cross-platform libraries (e.g. HOpenGL) often want to import the same foreign functions using the ccall convention on Unix and the stdcall convention on Windows. The usual method is to use CPP hackery.
  • The specified CString conversions are not yet supported in the hierarchical libraries.
  • jhc allows libraries to be specified as well as include files. this is handy for its lazy linking, the syntax is "-lfoo include.h foo_func"