Dynamic Closure

In a language with DynamicScoping, a DynamicClosure is a function which will be evaluated in the dynamic environment it was created in rather than the one it is called from.

Here is an example of creating an adder in LispOnePointFive:

  define ((
    (make-adder   (lambda (x) (make-closure)))
    (make-closure (lambda () (function (lambda (y) (addx y)))))
    (addx         (lambda (z) (plus x z)))
    (do-test      (lambda () (prog (a)
                               (setq a (make-adder 4))
                               (print (a 2))
                               (print (a 3))
                               (setq a (make-adder 5))
                               (print (a 2))
                               (print (a 3)))))
  ))
  => (make-adder make-closure addx do-test)

do-test () 6 7 7 8 => NIL

In make-closure, the "function" special form binds the "(lambda (y) (addx y))" to the current dynamic environment, in which x is bound to the argument to make-adder.

In the old literature this was called the FunargDevice?. Passing a function as an argument to another function (such as maplist) was known as a DownwardFunarg?. Returning a function as the value of another function was known as an UpwardFunarg? or a FunctionalValue?. Nowadays, functions that take functions as arguments or return functions as values are normally known as HigherOrderFunctions. See ftp://publications.ai.mit.edu/ai-publications/pdf/AIM-199.pdf for a discussion of how the "Funarg Problem" was dealt with in the days of dynamic scope.

Henry Baker published a method for implementing DynamicClosure and ShallowBinding. http://www.pipeline.com/~hbaker1/ShallowBinding.html


See also: LispOnePointFive LexicalClosure DynamicScoping LexicalScoping


CategoryLisp


EditText of this page (last edited February 15, 2010) or FindPage with title or text search