Hi,

"Alessandro Warth" <[EMAIL PROTECTED]> writes:

>     (define f
>       (let ((x 0))
>         (lambda ()
>           (... (set x (+ x 1)) ...))))
>
> which I think is pretty nice - i.e., easy for the programmer to write, and
> confines x in f, just as you'd expect. I wonder if this already works with
> Michael FIG's patch...

No, my dynamic-closure.patch is only for creating BlockClosure
objects, and they become invalid once the closed-over variables go out
of scope (since the variables live on the stack).

I do have an idea for implementing proper closures, though.  I'd
introduce two new syntax elements:

"stack-lambda" would be like lambda except it can capture lexical
variables even if they aren't GlobalVariables.  The name is to remind
you that once any of the captured variables are popped off the stack,
all hell will break loose if you try to use the closure again.  It
would be really nice to have an "alloca" (hint, hint) to implement
this feature without any memory leaks or dependence on a garbage
collector.

"heap-lambda" would be more clever (bletcherous?).  When it is used,
it would discover which lexically captured variables are not allocated
on the heap, then the enclosing "lambda"s and "let"s would detect
these variables and recompile themselves by moving those variables to
the heap (whether as an anonymous global, as "x" in Alessandro's
example above, or an indirection to a GC_alloc'ed storage location).
I'm not sure whether to just fail compilation if the second pass
results in different captured variables, or to keep iterating until
the compilation succeeds.

This is clever because it would give a way for the user to specify
closures with no garbage collection overhead (stack-lambda or
heap-lambda with only anonymous globals).  It's a bletcherous hack
because it uses multiple compiler passes.

I'll work on trying these out over the next few days, as time permits.
I surely don't know if this idea qualifies as a "cool closure
implementation" that Ian was hoping for.  The idea of multiple
compiler passes makes me somewhat nauseous, but I don't see any other
way to implement heap-lambda short of abandoning stack frames entirely
and allocating everything on the garbage-collected heap (a la Scheme).

Michael (Haupt): would Alessandro's construct satisfy your needs?  If
so, then under my proposal it would look like:

(define f
  (let ((x 0)) ;; first stack-allocated, then forced into heap storage
    (heap-lambda ()
       (... (set x (+ x 1)) ...))))

Let me know if this would work for you,

-- 
Michael FIG <[EMAIL PROTECTED]> //\
   http://michael.fig.org/    \//

_______________________________________________
fonc mailing list
[email protected]
http://vpri.org/mailman/listinfo/fonc

Reply via email to