And the winner is … PCRE!
May 12th, 2008 by Bryan O'Sullivan
We received a number of great submissions within hours of posting our request for suggestions for a library to develop bindings for in the FFI chapter. Thanks to everyone who contributed.
After looking over the alternatives, we’ve decided to use PCRE as the example. Even though there already exist useful bindings to PCRE, it’s still a good choice for teaching about the FFI, for a number of solid reasons.
- It’s small: its header file is only 300 lines long, and contains plenty of comments. We can thus cover its API almost completely, giving a thorough picture of how to build useful bindings.
- There’s no doubt that it’s both useful and portable.
- Its C API requires that users allocate and free objects manually. We can thus demonstrate how to eliminate the need to manually track objects in the Haskell bindings, through the use of finalisers.
- We can start off simply, with bindings to Haskell’s String type, and then show how to improve performance by using ByteStrings.
- It provides a callback facility, so we demonstrate inversion of control: not only how to call C from Haskell, but how to call Haskell code from C.
For teaching purposes, that’s a pretty compelling set of features. Thanks once more to those who submitted their ideas!

Real World Haskell