Monads in C++ “You must be kidding!”
Would be the expected reaction to “Monads in C++.” Hence my surprise when I was invited to Boostcon 11 to give a three-hour presentation on said topic, a presentation which was met with totally unexpected level of interest. I imagine there was a small uptick in the sales of Haskell books afterwards. Before I answer the question: “Why are monads relevant in C++?” , let me first answer the question: “Why is Haskell relevant in C++?”
Now, not everybody falls in love with the Haskell syntax (at least not at first sight) but it fits the functional paradigm much better than anything else. Armed with Haskell I could study and analyze some of the most sophisticated C++ metacode. Boost Proto is a library for implementing domain-specific languages (DSLs). What Is an EDSL? Great things come out of abusing C++. In this post I will construct a simple EDSL in C++. My moment of Zen was when I realized that an EDSL corresponds to a Haskell reader monad. Let’s start with a short: Haskell’s foldr in JavaScript – That's Captain to You! Ride the snake: Calling Python libraries from Haskell. Ffipkg: a neat bindings creation tool. Usage example. « Mostly Code.
The FFI Packaging Utility (ffipkg) is a surprisingly easy tool for creating bindings from C to Haskell.
It works by processing header files and creating a bunch of modules with accessors to structures, bindings for calling functions and all the usual boilerplate you usually need to write when doing FFI. What it doesn’t do is to provide a nice, high level binding to the underlying C library. But with the boring part worked out we can do it ourselves in Haskell. In this post I’ll show how to create a working binding to CURAND library. For those unfamiliar with CUDA: CURAND is a library for creating a high-quality pseudo random numbers directly on the graphic card’s GPU. First let’s install ffipkg itself. . $ cabal install hsffig There shouldn’t be any problems with that. The next step would be installing the CUDA toolkit. I used the one for Ubuntu and it worked very well.
Creating the binding library is dead simple. . $ mkdir hs-curand && cd hs-curand Then call ffipkg: $ cabal install Like this: Developing iPhone applications in Haskell — a tutorial (13 February 2011) 13 February 2011 (programming haskell iphone) (5 comments) I couldn't find a step-by-step tutorial explaining the process of developing iOS applications in Haskell, so after finally getting "Hello World" to run on an iPhone, I decided to write this tutorial.
I should also credit Lőry, who did the iOS side of the work. The basic overview of what we're going to do in this tutorial is the following: Write the backend of our application in Haskell Create FFI bindings for our backend Use Jhc to compile the Haskell code into vanilla C Write the front-end using the official iOS SDK, using Objective-C Create wrappers on the Objective-C side to make resource managment easier Compile and link it all using Objective-C and C compilers targeting the iOS devices Writing the backend in Haskell For this tutorial, we will simulate an intricate Haskell backend with the a simple function.
FFI bindings bool engine (in string s, out string error, out int[] result)