home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / lisp / lispnews / text0028.txt < prev    next >
Encoding:
Text File  |  1985-11-10  |  3.6 KB  |  109 lines

  1.  
  2.   Franz lisp now has a form of closure called an fclosure.  A fclosure is a
  3. compromise between a funarg and the type of functional object that we
  4. currently have in Franz. In this short note, I'll explain through examples
  5. what fclosures are and where you might use them, and finally describe the new
  6. functions.  The fclosure system was designed to be compatible with the Lisp
  7. Machine closures, so you may want to look at a Lisp Machine manual for more
  8. information.  fclosure are related to closures in this way:
  9.    (fclosure '(a b) 'foo) <==> (let ((a a) (b b)) (closure '(a b) 'foo))
  10.  
  11. A example of the use of fclosures:
  12.  
  13. ->(setq counter 0)
  14. 0
  15. ->(setq x (fclosure '(counter) '(lambda (val) (setq counter (+ val counter)))))
  16. fclosure[1]
  17.  
  18. The function 'fclosure' creates a new type of object called a fclosure.
  19. A fclosure object contains a functional object, and a set of symbols and
  20. values for the symbols.  In the above example, the fclosure functional
  21. object is (lambda (val) (setq counter (+ val counter)))
  22. and the set of symbols and values just contains the symbol 'counter' and
  23. zero, the value of counter when the fclosure was created.
  24.  
  25. When  a fclosure is funcall'ed:
  26.   1) the lisp system lambda binds the symbols in the fclosure to their
  27.      values in the fclosure.
  28.   2) it continues the funcall on the functional object of the fclosure
  29.   3) finally it un-lambda binds the symbols in the fclosure and at the
  30.      same time stores the current values of the symbols in the fclosure.
  31.  
  32. To see what that means, let us continue the example:
  33. -> (funcall x 32)
  34. 32
  35. -> (funcall x 21)
  36. 53
  37.  
  38. Notice that the fclosure is saving the value of the symbol 'counter'.
  39. Each time a fclosure is created, new space is allocated for saving
  40. the values of the symbols.
  41. If we executed the same fclosure function:
  42. ->(setq y (fclosure '(counter) '(lambda (val) (setq counter (+ val counter)))))
  43. fclosure[1]
  44.  
  45. We now have two independent counters:
  46. -> (funcall y 2)
  47. 2
  48. -> (funcall y 12)
  49. 14
  50. -> (funcall x 3)
  51. 56
  52.  
  53. To summarize:
  54.  
  55. (fclosure 'l_vars 'g_funcobj)
  56.  l_vars is a list of symbols (not containing nil)
  57.  g_funcobj is lambda expression or a symbol or another fclosure
  58.  
  59.   examples: (fclosure '(foo bar baz) 'plus)
  60.             (fclosure '(a b) #'(lambda (x) (plus x a))) notice the #'
  61.                which will make the compiler compile the
  62.             lambda expression.
  63.  
  64.  
  65. There are time when you want to share variables between fclosures.
  66. This can be done if the fclosures are created at the time times using
  67. fclosure-list:
  68.  
  69. (fclosure-list 'l_vars1 'g_funcobj1 ['l_vars2 'g_funcobj2 ... ...])
  70.   This creates a list of closures such that if a symbol appears in
  71.   l_varsN and l_varsM then its value will be shared between the
  72.   closures associated with g_funcobjN and g_funcobjM.
  73.  
  74.   example: -> (setq x (fclosure-list '(a) '(lambda (y) (setq a y))
  75.                          '(c a) '(lambda () (setq c a))))
  76.         (fclosure[4] fclosure[7])
  77.        -> (funcall (car x) 123)   ; set the value of a in the 1st fclosure
  78.        123
  79.        -> (funcall (cadr x))     ; read the same value in the 2nd fclosure
  80.        123
  81.  
  82.  
  83. Other fclosure functions:
  84.  
  85. (fclosure-alist 'c_fclosure)
  86.   returns an assoc list giving the symbols and values in the fclosure
  87.  
  88. (fclosurep 'g_obj)
  89.   returns t iff g_obj is a fclosure
  90.  
  91. (fclosure-function 'c_fclosure)
  92.   returns the functional object of the fclosure
  93.  
  94.  
  95.  
  96. Note: If a throw (or error) occurs during the evaluation of a fclosure,
  97.  which passes control out of the fclosure, then current values of the
  98.  symbols will not be stored.   This may be a bug.  We could set up
  99.  an unwind-protect, but it would then take longer to funcall
  100.  a fclosure.  If you think an unwind protect is important, let me know.
  101.  
  102. Note 2: you can also 'apply' a fclosure.
  103.  
  104.  
  105.  
  106.   
  107.  
  108.  
  109.