Chapter 10. Foreign Function Interface
In the previous chapter, we learned how the GHC code generator works and how to dig deeper into the compiler pipeline when necessary. In this chapter we will learn to interface with C from Haskell, and with Haskell functions from C. The foreign function interface (FFI) is part of the Haskell language report. The tooling for binding into shared libraries and also for compiling Haskell into shared libraries is quite mature. In this chapter we will cover the fundamentals of the FFI: importing and exporting functions, marshalling data, invoking the Haskell runtime from C, and building shared libraries with Haskell.
One of Haskell's strongest points compared to many languages is in fact the FFI: it's relatively easy to integrate parts written in Haskell to programs written in C or the other way around. By extension, integrating Haskell with other languages by going through the C level is also not too hard. Building a shared library (a shared object or a DLL...