[Haskell-cafe] Intuition to understand poor man's concurrency

martin martin.drautzburg at web.de
Sun Jul 27 10:44:05 UTC 2014


Hello all,

I am trying to understand the ideas of Koen Klaessen, published in Functional Pearls: "A poor man's concurrency" (1993).

The code in the paper doesn't compile. E.g. uses "lambda dot" instead of "labmda arrow", i.e. the way the labmda
calculus guys write things. Was that ever legal haskell or is this the result of some lhs2lex pretty printer?

Anyways, I believe I was able to convert that into modern haskell syntax - at least it compiles. But I have trouble to
understand the Monad instance presented there. Could anyobody walk me through the bind function?

But even more important: how do you guys develop an intuition about what the bind operator does in a specific monad. I
saw a Google tech talk where Douglas Crockford explains mondads in terms of Objects in javascript
(https://www.youtube.com/watch?v=b0EF0VTs9Dc), which was certainly enlightening, but I couldn't apply it to this
particular modad.

I also tried using something like Data Flow Diagrams. This kinda works for simple Mondads such as the state mondad, but
I couldn't apply it to the concurrency mondad. In general DFDs are not very good when it comes to higher order functions.

In any case here is the code. I made it more verbose than necessary in order to understand it (bind was originally just
one line), but to no avail.


newtype C m a = C ((a -> Action m) -> Action m)

instance Monad m => Monad (C m) where
        (C m) >>= k = C cont
                where
                    cont c = m (\a ->
                                        let C h = k a
                                        in h c)
        return x = C $ \c -> c x


data Action m =
        Atom (m (Action m))
        | Fork (Action m) (Action m)
        | Stop


More information about the Haskell-Cafe mailing list