home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / lisp / 2869 < prev    next >
Encoding:
Internet Message Format  |  1992-11-13  |  12.1 KB

  1. Xref: sparky comp.lang.lisp:2869 comp.lang.clos:998 news.answers:3956
  2. Path: sparky!uunet!spool.mu.edu!uwm.edu!ogicse!das-news.harvard.edu!cantaloupe.srv.cs.cmu.edu!crabapple.srv.cs.cmu.edu!mkant
  3. From: mkant+@cs.cmu.edu (Mark Kantrowitz)
  4. Newsgroups: comp.lang.lisp,comp.lang.clos,news.answers
  5. Subject: FAQ: CLOS and PCL Questions 5/6 [Monthly posting]
  6. Summary: Questions about CLOS, PCL and object-oriented programming in Lisp
  7. Message-ID: <lisp-faq-5.text_721645367@cs.cmu.edu>
  8. Date: 13 Nov 92 09:02:51 GMT
  9. Article-I.D.: cs.lisp-faq-5.text_721645367
  10. Expires: Sun, 27 Dec 1992 09:02:47 GMT
  11. Sender: news@cs.cmu.edu (Usenet News System)
  12. Reply-To: lisp-faq@think.com
  13. Followup-To: poster
  14. Organization: School of Computer Science, Carnegie Mellon
  15. Lines: 233
  16. Approved: news-answers-request@MIT.Edu
  17. Supersedes: <lisp-faq-5.text_718963479@cs.cmu.edu>
  18. Nntp-Posting-Host: a.gp.cs.cmu.edu
  19.  
  20. Archive-name: lisp-faq/part5
  21. Last-Modified: Thu Nov  5 19:30:40 1992 by Mark Kantrowitz
  22. Version: 1.27
  23.  
  24. ;;; ****************************************************************
  25. ;;; Answers to Frequently Asked Questions about Lisp ***************
  26. ;;; ****************************************************************
  27. ;;; Written by Mark Kantrowitz and Barry Margolin
  28. ;;; lisp-faq-5.text -- 11959 bytes
  29.  
  30. This post contains Part 5 of the Lisp FAQ. It is cross-posted to the
  31. newsgroup comp.lang.clos because it contains material of interest to
  32. people concerned with CLOS, PCL and object-oriented programming in
  33. Lisp. The other parts of the Lisp FAQ are posted only to the
  34. newsgroups comp.lang.lisp and news.answers.
  35.  
  36. If you think of questions that are appropriate for this FAQ, or would
  37. like to improve an answer, please send email to us at lisp-faq@think.com.
  38.  
  39. CLOS/PCL Questions (Part 5):
  40.  
  41.   [5-0]   What is CLOS (PCL) and where can I get it?
  42.           How do you pronounce CLOS?
  43.   [5-1]   What documentation is available about object-oriented
  44.           programming in Lisp?  
  45.   [5-2]   How I write a function that can access defstruct slots by
  46.           name?  I would like to write something like 
  47.           (STRUCTURE-SLOT <object> '<slot-name>).   
  48.   [5-3]   How can I list all the CLOS instances in a class?
  49.   [5-4]   How can I store data and CLOS instances (with possibly circular
  50.           references) on disk so that they may be retrieved at some later
  51.           time?
  52.   [5-5]   Given the name of a class, how can I get the names of its slots?
  53.  
  54. Search for [#] to get to question number # quickly.
  55.  
  56. ----------------------------------------------------------------
  57. [5-0]  What is CLOS (PCL) and where can I get it?
  58.        How do you pronounce CLOS?
  59.  
  60. CLOS (Common Lisp Object System) is the object-oriented programming
  61. standard for Common Lisp. It is the successor to Symbolics FLAVORS and
  62. Xerox LOOPS (Lisp Object Oriented Programming System). The acronym
  63. CLOS is pronouned either as "See-Loss" or "Closs", depending on taste.
  64. PCL (Portable Common Loops) is a portable CLOS implementation, and is
  65. available by anonymous ftp from parcftp.xerox.com (13.1.64.94) in the
  66. /pub/pcl/ directory. Also in the same directory are sources for CLX R5
  67. and an inspecter.
  68.  
  69. The sources for the CLOS Metaobject Protocol specification are
  70. also available from parcftp as /pub/pcl/mop/spec.tar.Z. See also the book
  71. ``The Art of the Metaobject Protocol'' below. The Closette files
  72. related to this book are available from parcftp as /pub/pcl/mop/closette.lisp.
  73.  
  74. ----------------------------------------------------------------
  75. [5-1] What documentation is available about object-oriented
  76.       programming in Lisp? 
  77.  
  78. Books about object-oriented programming in Lisp include:
  79.  
  80.    1. dpANS CL describes the entire Common Lisp language, which includes the
  81.       CLOS standard.  Informally, CLtL2 can also be used to learn about CLOS, 
  82.       but please remember that CLtL2 is not an official X3J13 committee
  83.       document. (The presentation of CLtL2 differs from that of the draft
  84.       proposed standard, and some matters of fact have changed in the proposed
  85.       standard since the publication of CLtL2.)
  86.  
  87.    2. Sonya E. Keene
  88.       "Object-Oriented Programming in Common Lisp: 
  89.        A Programmer's Guide to CLOS"
  90.       Addison-Wesley (Reading, MA), 1989. 266 pages. ISBN 0-201-17589-4.
  91.            Tutorial introduction to CLOS with many examples and
  92.            a lot of good advice for designing large programs using CLOS.
  93.  
  94.    3. Jo A. Lawless and Molly M. Miller.
  95.       "Understanding CLOS: the Common Lisp Object System"
  96.       Digital Press, 1991. 192 pages.
  97.  
  98.    4. Gregor Kiczales, Jim des Rivieres, and Daniel G. Bobrow.
  99.       "The Art of the Metaobject Protocol"
  100.       MIT Press, 1991. 335 pages. ISBN 0-262-61074-4
  101.            The first part of the book presents a model CLOS implementation,
  102.            introduces the basic principles of metaobject protocols, and 
  103.            works through the key elements of the CLOS Metaobject Protocol.
  104.            The second half is the detailed specification of the CLOS
  105.            Metaobject Protocol. A simple working interpreter suitable
  106.            for experimentation is contained in an appendix.
  107.  
  108.    5. Robert R. Kessler and Amy R. Petajan.
  109.       "LISP, Objects, and Symbolic Programming"
  110.       Scott, Foresman and Company (Glenview, IL), 1988. 644 pages.
  111.            Includes a small Lisp compiler.
  112. ----------------------------------------------------------------
  113. [5-2] How can I write a function that can access defstruct slots by name?  I
  114.       would like to write something like 
  115.         (STRUCTURE-SLOT <object> '<slot-name>).
  116.  
  117. There is currently no portable, built-in way to access structure slots
  118. given only the name.  If your Common Lisp includes an implementation
  119. of CLOS that supports the meta-object protocol specified in the
  120. original X3J13 draft spec (document X3J13/88-003), then it probably will
  121. allow (SLOT-VALUE <object> '<slot-name>); however, not all
  122. implementations of CLOS currently provide this.  Lacking this, some
  123. implementations may provide implementation-dependent functions that
  124. allow access to structure slots by name; note that this may cause
  125. saved images to be larger, as some implementations normally open-code
  126. structure accessors and discard slot name information.
  127.  
  128. While it is not possible to write a fully general STRUCTURE-SLOT function,
  129. it is not very difficult to write version that handles specific structure
  130. types.  For instance, after defining:
  131.  
  132.    (defstruct spaceship name captain position velocity)
  133.  
  134. one may then define:
  135.  
  136.    (defun spaceship-slot (spaceship slot-name)
  137.      (ecase slot-name
  138.        (name (spaceship-name spaceship))
  139.        (captain (spaceship-captain spaceship))
  140.        (position (spaceship-position spaceship))
  141.        (velocity (spaceship-velocity spaceship))))
  142.  
  143. or using CLOS (generic functions):
  144.  
  145. (defgeneric spaceship-slot (spaceship slot-name)
  146.   (:method ((x spaceship) (slot (eql :name)))
  147.     (spaceship-name x))
  148.   (:method ((x spaceship) (slot (eql :captain)))
  149.     (spaceship-captain x))
  150.   (:method ((x spaceship) (slot (eql :position)))
  151.     (spaceship-position x))
  152.   (:method ((x spaceship) (slot (eql :velocity)))
  153.     (spaceship-velocity x)))
  154.  
  155. Another popular way to define this is:
  156.  
  157.    (defun spaceship-slot (spaceship slot-name)
  158.      (funcall (symbol-function
  159.                 (find-symbol (format nil "SPACESHIP-~A" slot-name)
  160.                              #.(package-name *package*)))
  161.               spaceship))
  162.  
  163. I personally recommend the first version.  It is likely to be much faster
  164. and more memory efficient than the second version.  It's also easy to get
  165. the second one wrong; many people forget to specify the package argument to
  166. FIND-SYMBOL, which can cause incorrect results when the package at run time
  167. is different from the one at compile time.  Even my version assumes that
  168. SPACESHIP-SLOT is being defined in a file that is in the same package as
  169. the one containing the structure definition; if this isn't the case,
  170. #.(PACKAGE-NAME *PACKAGE*) should be replaced by a string naming the
  171. correct package.
  172.  
  173. Another workaround is to define a MY-DEFSTRUCT macro that parses the
  174. defstruct arguments and expands into a call to DEFSTRUCT along with a
  175. definition of the runtime slot-accessor function.
  176.  
  177. Some non-portable techniques include the use of LCL:STRUCTURE-REF
  178. in Lucid and EXCL:STRUCTURE-REF in Allegro.
  179. ----------------------------------------------------------------
  180. [5-3] How can I list all the CLOS instances in a class?
  181.  
  182. There is no built-in way to enumerate the instances of a class.  If you are
  183. only interested in listing the instances of classes that you have defined,
  184. it is not very difficult to implement it as part of your class definition.
  185. Add a shared slot, e.g. ALL-INSTANCES, with an initial value of NIL, to the
  186. class definition.  Then write an after-method on INITIALIZE-INSTANCE for
  187. this class, which pushes the instance being initialized onto ALL-INSTANCES.
  188. Note that this must be done separately for each class that wants to maintain
  189. such a list; it can't be encapsulated in a mixin class, because all its
  190. dependent classes would share the same ALL-INSTANCES slot.  A compromise
  191. would be to use a mixin to define the INITIALIZE-INSTANCE after-method (and
  192. any other general-purpose methods that use the slot), but not the shared
  193. slot; it would be up to the descendant classes to define the slot at the
  194. level of the class hierarchy that is appropriate. You could also try
  195. defining the classes that need instance-recording as instances of a
  196. metaclass that holds the instance registry on the class object. The
  197. recording behavior could then be built-in to an after method on
  198. initialize-instance for the root class of the metaclass, or even
  199. allocate-instance. To allow for garbage collection of old instances,
  200. you will also need to define a generic function to remove the recorded
  201. instances from the list of instances.
  202. ----------------------------------------------------------------
  203. [5-4]  How can I store data and CLOS instances (with possibly circular
  204.        references) on disk so that they may be retrieved at some later
  205.        time?
  206.  
  207. There are two main techniques of doing this kind of persistent object
  208. storage. The first involves using #. to compile the data into a file.
  209. The second produces an ASCII representation which, when evaluated,
  210. will reproduce an equivalent set of data.
  211.  
  212. If the data you wish to save is stored in the variable *hash-table*,
  213. create a file containing just the lines
  214.         (in-package "YOUR-PACKAGE")
  215.         (setq *hash-table* '#.*hash-table*)
  216. and compile it. The #. macro performs read-time evaluation of the
  217. expression following the dot, and so this compiles the data into the
  218. file. You may then load the file to restore the data. However, the
  219. resulting binary file is not portable between Lisp implementations,
  220. and sometimes not even for the same Lisp on different platforms. Also,
  221. some Lisps will treat the data as constant, and place it on pages in
  222. memory that are marked read-only (after it is loaded). If one tries to
  223. later modify the data, these Lisps will signal an error. Lucid CL only
  224. puts such constants in a read-only area when they appear inside
  225. functions, so this should be safe. Allegro CL doesn't seem to complain
  226. about modification if the data is a cons. DEC's VAXLisp, however, has
  227. problems with #. circular structures in .fas files.
  228.  
  229. The other technique is to produce an ASCII representation of the Lisp
  230. objects which may then be saved to a file. To reproduce the data, one
  231. can load (or compile and load) the file. This technique is portable
  232. between different Lisps and platforms. Unfortunately, the resulting
  233. data is not necessarily EQ to the original. Kerry Koitzsch's
  234. save-object.lisp package is included in the Lisp Utilities Repository,
  235. ftp.cs.cmu.edu:/afs/cs.cmu.edu/user/mkant/Public/Lisp-Utilities/.
  236. (Note: You must cd to this directory in one atomic operation, as some
  237. of the superior directories on the path are protected from access by
  238. an anonymous ftp.) The Lisp Utilities Repository is described in
  239. detail in the answer to question [6-1].
  240.  
  241. See also the discussion of MAKE-LOAD-FORM and MAKE-LOAD-FORM-SAVING-SLOTS 
  242. in CLtL2.
  243.  
  244. ----------------------------------------------------------------
  245. [5-5]   Given the name of a class, how can I get the names of its slots?     
  246.  
  247. (defun class-slot-names (class-name)
  248.   (mapcar #'clos:slot-definition-name
  249.           (clos:class-slots (find-class class-name))))
  250.  
  251. ----------------------------------------------------------------
  252. ;;; *EOF*
  253.