How would shallow generators compose with lambda?

Dave Herman dherman at ccs.neu.edu
Thu May 28 13:38:38 PDT 2009


[Sorry for my absence lately.]

> If yield does work, then we're capturing deep stacks.  A more  
> plausible example:
>
> function j(x) {
>  (lambda (y) {
>     (lambda (z) {
>        yield z;
>      }
>      (y + " and a dyne"));
>   }
>   (x + ", a poundal"));
> }
> print(j("I love you").next());
>
> This is just a desugaring of some nested lets, but we still have  
> yield capturing many frames.  If this doesn't work, lambda is really  
> useless.

This issue really comes down to whether `lambda' is properly tail  
calling. If so, then the `yield' in this example occurs as if it were  
directly within the body of `j', because there's no stack beyond the  
activation frame of `j'.

To be a little more precise: when people refer to "Tennent," the main  
property they're talking about is that (modulo syntax) an expression E  
is equivalent to the expression (lambda () E)(). Namely, in any  
expression context (roughly, a position in a program where you can  
place an expression), either expression will result in the same  
program behavior.

If we don't want generators to capture deep stacks, then we can make  
it a dynamic error for `yield' to occur in a non-empty sub-stack. Then  
in order to preserve the property that (lambda () E)() for all E, we'd  
have to mandate that (lambda () E)() is really a tail call and  
guaranteed not to push any stack.

Alternatively, we could allow generators to capture deep lambda- 
stacks, but not deep function-stacks.  However...

> For what it's worth, speaking as a long-time Scheme fan, I wouldn't  
> add lambda to ES.  It seems too similar to function; there will be  
> endless blog posts explaining the differences and motivation, mostly  
> slightly wrong.  The best ones will say, "Don't use it; just use  
> function."  And lambda introduces an awful lot of subtlety for  
> something whose main claim to utility would be in allowing precise  
> and clear definitions of new control constructs through desugaring.   
> Natural language is bad, but not this bad.

I hear you. I love lambda, I love the syntactic simplicity of  
Smalltalk blocks (and we could probably approximate its syntactic  
simplicity with a literal syntax), and I love the idea of adding a  
properly tail-calling function form. Nevertheless, I recognize that  
the similarity to `function' is a usability hazard.

Dave



More information about the es-discuss mailing list