home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / lisp / 2861 < prev    next >
Encoding:
Text File  |  1992-11-12  |  1.9 KB  |  45 lines

  1. Path: sparky!uunet!mcsun!news.funet.fi!hydra!klaava!cc.helsinki.fi!pirinen
  2. From: pirinen@cc.helsinki.fi
  3. Newsgroups: comp.lang.lisp
  4. Subject: Re: Adding FUNCTION to a shallow-binding interp.
  5. Summary: external value cells
  6. Keywords: closure shallow-binding
  7. Message-ID: <1992Nov12.144242.1@cc.helsinki.fi>
  8. Date: 12 Nov 92 12:42:42 GMT
  9. References: <1992Nov2.182028.22168@uk03.bull.co.uk>
  10. Sender: news@klaava.Helsinki.FI (Uutis Ankka)
  11. Organization: University of Helsinki
  12. Lines: 31
  13.  
  14. In article <1992Nov2.182028.22168@uk03.bull.co.uk>, bbirch@hemel.bull.co.uk (Bill Birch) writes:
  15. > I have a shallow-binding interpreter, which has no lexical closure. 
  16. > My attempts to implement (function ) have been only partially succesful,
  17. > am I right in assuming that this is impossible?
  18. You're right: it is impossible with the tools you're using.  Basically,
  19. since the same name should have different bindings in each different
  20. scope, you need a distinct storage location for each, and SETQ of a
  21. local variable has to know about them.  A good test case is:
  22.  
  23. (defun two-functions(&aux x)
  24.   (list #'(lambda () x) #'(lambda (y) (setq x y))))
  25.  
  26. Each call of TWO-FUNCTIONS has to create a new binding of X, which the
  27. two functions share.
  28.  
  29. The standard solution for shallow-binding systems is external value
  30. cells: a closed-over variable has a pointer to an external value cell,
  31. which is a dynamically allocated object that holds the value of the
  32. variable in that scope; each closure has a list of variables it closes
  33. over and their external value cells.  For example, TWO-FUNCTIONS might
  34. return something like this:
  35.  
  36. (#<closure (lambda () x) ((x . #1=#<external-value-cell nil))>
  37.  #<closure (lambda (y) (setq x y)) ((x . #1#))>)
  38.  
  39. with new closures containing a new external value cell for each
  40. invocation.  There are quite a few pitfalls in getting closures just
  41. right, good luck!
  42. ___
  43. Pekka P. Pirinen           pekka.pirinen@helsinki.fi
  44. Lisp implementor
  45.