return when desugaring to closures

Mark S. Miller erights at google.com
Mon Aug 25 18:15:10 PDT 2008


On Mon, Aug 25, 2008 at 4:33 PM, Brendan Eich <brendan at mozilla.org> wrote:
> On Aug 25, 2008, at 4:10 PM, Mark S. Miller wrote:
>
>> are shallow generators
>> implementable by a local cps transform?
>
> Yes, the local evaluation (inside the generator function) is the only place
> that needs to be CPS'ed to allow yield in any expression or statement
> context.
>
>
>> This approach would seem relevant to adding generators by adding new
>> semantic state. But would it help us understand how to add generators
>> by a local code transformation?
>
> Why does this matter? Changing any future ES spec to use CPS is a semantic
> state shift, and a very big one. The plan (designed and implemented by Dave)
> for specifying generators in ES4 was to use delimited continuations in the
> SML reference implementation.

I find continuations, delimited or otherwise, very hard to reason
about. Generally, my best route to understanding what's going on is by
CPS transform anyway. YMMV. I didn't know SML has delimited
continuations. But I'd still rather not try to understand generators
by CPS-transforming the relevant bits of the RI.

As for which technique should be used to specify generators, I'll
postpone forming an opinion until I understand them ;).


> It's not pretty, but you can CPS-convert generators. Here's a concrete
> "desugaring" ("souring"? :-P) example based on JS1.7, not ES4 (no iterator
> namespace, ugly __iterator__ name instead):
>
> function simple_range(n) {
>    for (let i = 0; i < n; i++)
>        yield i;
> }
>
> function simple_range(n) {
>    let i = 0;
>    return {
>        __iterator__: function () {
>            return {
>                next: function () {
>                    if (i == n)
>                        throw StopIteration;
>                    return i++;
>                }
>            }
>        }
>    };
> }
>
> Object.freeze on the returned iterator object omitted ;-). Adding send,
> throw, and close also left as an exercise.

Thanks, that helps a lot. But shouldn't that "i == n" be "!(i < n)"
since it's a transform of the original "i < n"?


On Thu, Aug 21, 2008 at 11:31 PM, Dave Herman <dherman at ccs.neu.edu> wrote:
> Can't be done without defining the entire language in CPS. Yes, it's
> shallow, but a captured activation can involve any arbitrary combination of
> expressions other than function call.

Dave, is the violent transform of the for-loop above the kind of
rest-of-the-language transform you were referring to? If so, isn't
this only an issue for control structures (including &&, ||, and ?:)?
What other elements of the language might need to be turned inside out
this way? Or have I misunderstood what you're getting at.

-- 
    Cheers,
    --MarkM


More information about the Es-discuss mailing list