I was trying to move forward with the new installer for the xll8 library. Again. WiX works, but I’m trying to keep up with new technology. I had high hopes for NuGet for native code, but that project seems to be going nowhere. So much for trying to help out on that. No e-mail address, just a twitter feed. I started signing up for that. First they made me select 5 people twitter thought I might know that had active twitter feeds. Then 5 more people they thought I might like to follow. Took a pass on Justin Egger, but I was good with Jimmy Fallon, Louis CK, and Seth Meyers. Then they wanted me to cough up my address book. Buh, bye. But that didn’t stop them from sending robo-emails.

Category theory has been an ongoing hobby for me, much to the chagrin of my Operator Theory and Functional Analysis colleagues. They think it is generalized abstract nonsense. Pot, kettle.

Actually knowing what a monad is seems to be a hindrance to understanding how computer scientists use them. “Monads reify composition of functions.” At least that is my Dao De Jing definition – if you know everything there is to know about monads, that should make perfect sense to you.

I like examples, but not the examples I’ve been seeing in most monad expositions. I make a living solving practical problems and never came across anything that looked immediately useful to me.

Here is an example that is almost useful: suppose you have an old-school C library that returns error codes that you have to manually check and functions that return results using pointers. Since int is shorter to type than double, lets assume the functions in the library have signature int (*)(int, int*). Or in modern C++, std::function<int(int,int*)>, a function that returns an int error code, takes an integer argument, and stores the integer result where the pointer tells it to.

I’m thinking GSL. They have a lot of std::function<int(double,double*)> functions. I have been plugging some of this stuff into Excel: xllgsl.

Excel is purely functional language, except for macros used to produce side effects. So is Haskell. Brilliant stuff, but I wish they would stop telling people how easy it is. They make fun of C++ in the same breath they use to claim how well metatemplate programming maps to Haskell. Thank the smart C++ people that put many more man-hours into solving really important issues in modern computing. Like r-value references that obviate the need for meta-template programming and let you return big objects by value without a performance hit.

Off my soap box. Back to monads. They have limited usefulness. They are just a programming pattern that involves wrapping up an object and calling a function on the wrapped up object.

They make standard conventions slightly more convenient. E.g., instead of

int err;
int x, y, z;
x = 0;
err = f1(x, &y);
if (err != 0) {
   // deal with it
err = f2(y, &z);
if (err != 0) {
    // deal with it

you can wrap up the error code and argument

struct wrap {
    int err;
    int val;
    wrap(int x) : err(0), val(x)

and lift a GSL style function:

std::function<wrap(wrap)> lift(std::function<int(int,int*)> f) {
    return [f](wrap x) { 
        wrap y;
        if (x.err)
            y.err = x.err;
            y.err = f(x.val, &y.val);

        return y;

Now you can compose functions that return error codes:

    lift F1(f1);
    lift F2(f2);
    auto y = F2(F1(wrap(x));

Those big, bad monads don’t look so tough anymore.

The big boys call wrap unit and write unit: a -> M a. The signature for lift is lift: (a -> b) -> (M a -> M b). The Haskell guys are so smart that they call unit return and use bind: M a -> (a -> M b) -> M b instead of lift (a.k.a., fmap). The language is so easy you have to do something to make it complicated.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s