Dustin Getz on 22 Feb 2012 18:03:14 -0800


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

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


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:
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