Dustin Getz on 22 Feb 2012 19:05:30 -0800


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: implementing call/cc with only closures, no special forms ?


i think i may have reconciled the problem - he implements callcc and continuation-monad without generators, but the actual usage of callcc as defined -- `return callcc(self.react)` -- self.react is itself a generator. this generator could (i think) be re-implemented with lambdas, but now the block passed to call/cc must conform to our interface. which means we can't use call/cc with arbitrary code. if i chase down the implications of this, i imagine we will end up concluding that, to turn blocking business logic into cooperative via low level monkey patches in the style of gevent, would also require altering the structure of the business logic itself.


On Wed, Feb 22, 2012 at 9:02 PM, Dustin Getz <dustin.getz@gmail.com> wrote:
this came up after redsnake last night - I mentioned to someone that I had seen a "lambda" implementation of callcc in python. Which, if that's actually possible, is pretty mind-blowing - because it doesn't require patching the runtime/interpreter !! I'm not quite sure if its possible or how close we can get, but here's the link I was talking about[1]:

[1] http://www.valuedlessons.com/2008/01/monads-in-python-with-nice-syntax.html

He implements a few monads in python, including continuation monad. It will take me significant study to understand this code - maybe you guys would be interested in picking this apart at a future meetup - but here's the author's conclusion:

So, you can use monads with elegant syntax in any language that has closures and any of the following:
    • do syntax (Haskell, C#)
    • call/cc (Scheme, Ruby)
    • bidirectional generators (Python 2.5, a future _javascript_?)
    • coroutines (Lua, Io)
my question is, can you implement continuation-monad with "un-elegant" syntax with *only* closures, skipping the generators? I think maybe you can, because 1) haskell's do-notation is just syntactic sugar over the closures, and 2) you can at least implement one common generator pattern - lazy seqs - in python without generators[2], which got me wondering if you can implement a generalized generator using just lambdas, and now my head is spinning and i'm saying stupid things like "i think you might be able to implement call/cc with only closures" but that implies that we can do things like turn third party blocking code into cooperative code per gevent[3] which is absurd, that's a ridiculous statement. I'm not even making sense anymore, so I'm going to stop writing and click "send" now :)

Dustin Getz

[2] my implementation of lazy seqs with only closures: https://github.com/dustingetz/sandbox/blob/master/etc/lazy.py
[3] lightning talk slides where we use callcc to make third party code cooperative: https://docs.google.com/present/view?id=dv9r8sv_82cn7dngcq