2 ms·
"Upward funargs" is where you return a closure which captures the bindings of your local variables, so it can access arguments from an outer scope which no long
by kragen 2y ago
"Upward funargs" is where you return a closure which captures the bindings of your local variables, so it can access arguments from an outer scope which no longer exists. Like this:
scheme@(guile-user)> (define (make-adder x) (lambda (y) (+ x y)))
scheme@(guile-user)> (define x+3 (make-adder 3))
scheme@(guile-user)> (define x+4 (make-adder 4))
scheme@(guile-user)> (x+3 7)
$1 = 10
scheme@(guile-user)> (x+4 8)
$2 = 12
In Scheme those bindings are even mutable:
scheme@(guile-user)> (define (counter n) (lambda () (set! n (1+ n)) n))
scheme@(guile-user)> (define booga (counter 53))
scheme@(guile-user)> (booga)
$3 = 54
scheme@(guile-user)> (booga)
$4 = 55
This makes Scheme closures a fully general substitute for Smalltalk-like objects.
Upward funargs generally require a non-stack-like allocation discipline for the local-variable bindings.
While implicit variable capture like Scheme is the most common way to do closures, it isn't the only way; C++ and old versions of Python (before nested_scopes) have a form of closures where you instead explicitly list the variables you want to capture. Old dynamically-scoped Lisps like Lisp 1.5 had a different solution which I don't understand; see AI memo AIM-199, "The function of FUNCTION in LISP".
Usually dynamic scoping is incompatible with tail-call elimination (except in the special case of tail recursion) because you can't eliminate a tail call if which you have more work to do after it returns; usually popping some variable bindings off a context stack amounts to "more work to do". Maybe your parameter stack has a way for that to happen implicitly, for example by implicitly restoring a parameter-stack offset on function return.
I hope this helps. Keep being awesome!
- vdupras 2y agoYeah, to be honest I'm too out of this context to speak of deep technicalities, but a quick test seems to indicate that Dusk's comp/lisp can do something similar to your first example, unless I misunderstood it: $ make run stty -icanon -echo min 0; ./dusk ; stty icanon echo Dusk OS 79KB used 31MB free ok needs comp/lisp ok lisp (defun make-adder (x) (lambda (y) (+ x y))) ok lisp (defun x+3 (x) ((make-adder 3) x)) ok lisp (defun x+7 (x) ((make-adder 7) x)) ok lisp. (x+3 7) 10 ok lisp. (x+7 8) 15 ok
- kragen 2y agoThat could work without upward funargs because, in your version, (make-adder 3) and (make-adder 7) aren't live at the same time, so they might be using the same "x" instead of two separate "x" bindings. My hunch is that that "x" is in memory space that would be overwritten if you called another largish function in between calling make-adder and invoking its return value, but I haven't grokked comp/lisp fully enough to be sure that that's true.