Ionrock Dot Org

by Eric Larson

My Weblog

Monads... Finally

The Programming Sub-Reddit had about a million articles on monads for a time. The flow of monadic articles has definitely slowed down to a more reasonable pace. I saw an article on using monads in python that struck my fancy. There is a theme in Python and Ruby regarding concurrency due to the previous functional programming flurry. The string of consciousness flowed from functional programming towards concurrency because there was essentially a claim that when you don't have side effects in a function you can easily let other machines and processes deal with the work.

Once you have programmed functionally you begin to see the world in a different place. I'm relatively familiar with it because of XSLT's functional nature. The problem is that when you start getting the functional bug, when you do want to reuse the more traditional procedural style, it is a challenge. Monads then simply make an effort to functionally control the order of operations, or more specifically, the chain of function calls within a certain situation. I'm sure smarter folks might disagree with my definition, but more or less this seems to be a recurring theme so I'm sticking with it.

I'm not going to make this a tutorial or something because I'm not the right guy for the job. I will say that most programmers to need monads because they are programming in a language where order is easy to come by. The place where it could come in handy is in concurrent situations where you want to mix up the order a bit yet still keep control. The obvious problem with threading is that there can be side effects that impact other operations. If I'm using some object or variable and somebody else deletes it before I use it, there is a problem. This is why you see things like using generators and the like to let the flow of things traverse between different objects and threads.

In a way, things like using generators and the like in Python for threading and concurrency, is similar to using monads in something like haskell. In each case you are using the languages inherit features to control flow in concurrent procedures. I'm not very good at this stuff and honestly don't have much use for it in my day to day work, but it is something I'm getting more interested in. Either way, the problems of concurrency are really tough and seeing all the different languages specific solutions makes for a great learning experience on how these sorts of things get worked out.

Posted Tue Jan 8 17:08:49 2008 by Eric Larson

Twitter

Links

Reading

Created using Python, jQuery and Emacs