Skip to content
refractalize edited this page Apr 28, 2012 · 1 revision

I've been wondering for a little while wether to have PogoScript generate fully CPS code for situations that would benefit from it. The situations I was thinking of were highly IO bound code, or code that would benefit from a distributed parallelism.

For example, we can make IO calls without having to worry about callbacks or funny async operators. Our code is back as it was in regular languages like C# or Ruby.

locations = read file "locations.xml"
http: post (locations) to "http://example.com/locations"

But this would break one of Node's best features for concurrent programming: cooperative multi-threading. With cooperative multi-threading, the thread chooses when to yield, meaning that it has a chance to get its state consistent before another thread is scheduled. In preemptive multi-threading, when the scheduler chooses to yield a thread, the thread has to use locks to protect inconsistent state.

By allowing implicit CPS (like Scheme) for the purposes of running IO and distributed parallelism, we destroy this property: effectively any call could invoke a yield that may lead to another "thread" accessing inconsistent state.

So we're back to the original proposal for CPS calls in PogoScript: use an operator to mark those calls that should be called CPS.

locations = ~ read file "locations.xml"
http: ~ post (locations) to "http://example.com/locations"

At the time that operator was ~, which is the JavaScript bit-wise not operator, we should find a different one.

Perhaps a leading <- would do?

locations = <- read file "locations.xml"
http: <- post (locations) to "http://example.com/locations"
Clone this wiki locally