home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / Tool Chest / Development Platforms / Macintosh Common Lisp Related / Lisp FAQ 21Sept93 / 5.Object-oriented Programming < prev    next >
Encoding:
Internet Message Format  |  1993-09-21  |  17.2 KB  |  [TEXT/ttxt]

  1. Subject: FAQ: Object-oriented Programming in Lisp 5/7 [Monthly posting]
  2. Newsgroups: comp.lang.lisp,comp.lang.clos,news.answers,comp.answers
  3. Summary: Questions about CLOS, PCL and object-oriented programming in Lisp
  4. Distribution: world
  5. Followup-To: poster
  6. Reply-To: lisp-faq@think.com
  7. Approved: news-answers-request@MIT.Edu
  8.  
  9. Archive-name: lisp-faq/part5
  10. Last-Modified: Mon Sep 20 21:10:40 1993 by Mark Kantrowitz
  11. Version: 1.38
  12.  
  13. ;;; ****************************************************************
  14. ;;; Answers to Frequently Asked Questions about Lisp ***************
  15. ;;; ****************************************************************
  16. ;;; Written by Mark Kantrowitz and Barry Margolin
  17. ;;; lisp-faq-5.text -- 17617 bytes
  18.  
  19. This post contains Part 5 of the Lisp FAQ. It is cross-posted to the
  20. newsgroup comp.lang.clos because it contains material of interest to
  21. people concerned with CLOS, PCL and object-oriented programming in
  22. Lisp. The other parts of the Lisp FAQ are posted only to the
  23. newsgroups comp.lang.lisp and news.answers.
  24.  
  25. If you think of questions that are appropriate for this FAQ, or would
  26. like to improve an answer, please send email to us at lisp-faq@think.com.
  27.  
  28. CLOS/PCL Questions (Part 5):
  29.  
  30.   [5-0]   What is CLOS (PCL) and where can I get it?
  31.           How do you pronounce CLOS?
  32.   [5-1]   What documentation is available about object-oriented
  33.           programming in Lisp?  
  34.   [5-2]   How do I write a function that can access defstruct slots by
  35.           name?  I would like to write something like 
  36.           (STRUCTURE-SLOT <object> '<slot-name>).   
  37.   [5-3]   How can I list all the CLOS instances in a class?
  38.   [5-4]   How can I store data and CLOS instances (with possibly circular
  39.           references) on disk so that they may be retrieved at some later
  40.           time?
  41.   [5-5]   Given the name of a class, how can I get the names of its slots?
  42.   [5-6]   Free CLOS software.
  43.  
  44. Search for \[#\] to get to question number # quickly.
  45.  
  46. In general, questions about object oriented programming in Lisp,
  47. especially questions about using CLOS or compiling PCL, should be
  48. directed to the newsgroup comp.lang.clos.
  49.  
  50. ----------------------------------------------------------------
  51. Subject: [5-0]  What is CLOS (PCL) and where can I get it?
  52.                 How do you pronounce CLOS?
  53.  
  54. CLOS (Common Lisp Object System) is the object-oriented programming
  55. standard for Common Lisp. It is the successor to Symbolics FLAVORS and
  56. Xerox LOOPS (Lisp Object Oriented Programming System). The acronym
  57. CLOS is pronouned either as "See-Loss" or "Closs", depending on taste.
  58. PCL (Portable Common Loops) is a portable CLOS implementation, and is
  59. available by anonymous ftp from parcftp.xerox.com (13.1.64.94) in the
  60. /pub/pcl/ directory. Also in the same directory are sources for CLX R5
  61. and an inspecter.
  62.  
  63. Most Common Lisp implementations now include their own CLOS
  64. implementations. Common Lisp implementations with native CLOS include:
  65. MCL, {A}KCL, Allegro CL (including Allegro CL\PC), Ibuki, Lucid,
  66. Medley, Symbolics Genera, CLOE, and Harlequin Lispworks. CMU CL uses a
  67. customized version of PCL as their CLOS. However, not all native CLOS
  68. implementations have as detailed a meta-object protocol as PCL. For
  69. example, MCL 2.0 users sometimes use the july-1d version of PCL
  70. instead of the native CLOS for precisely this reason.
  71.  
  72. The sources for the CLOS Metaobject Protocol specification are
  73. also available from parcftp as /pub/pcl/mop/spec.tar.Z. See also the book
  74. ``The Art of the Metaobject Protocol'' below. The Closette files
  75. related to this book are available from parcftp as /pub/pcl/mop/closette.lisp.
  76.  
  77. The CLOS code repository is available by anonymous ftp to
  78. nervous.cis.ohio-state.edu [128.146.61.200] in the directory
  79. pub/lispusers/clos/. If you've got code you'd like to add to the
  80. repository, send mail to Arun Welch, commonloops-request@cis.ohio-state.edu.
  81.  
  82. ----------------------------------------------------------------
  83. Subject: [5-1] What documentation is available about object-oriented
  84.                programming in Lisp? 
  85.  
  86. Books about object-oriented programming in Lisp include:
  87.  
  88.    1. dpANS CL describes the entire Common Lisp language, which includes the
  89.       CLOS standard.  Informally, CLtL2 can also be used to learn about CLOS, 
  90.       but please remember that CLtL2 is not an official X3J13 committee
  91.       document. (The presentation of CLtL2 differs from that of the draft
  92.       proposed standard, and some matters of fact have changed in the proposed
  93.       standard since the publication of CLtL2.)
  94.  
  95.    2. Sonya E. Keene
  96.       "Object-Oriented Programming in Common Lisp: 
  97.        A Programmer's Guide to CLOS"
  98.       Addison-Wesley (Reading, MA), 1989. 266 pages. ISBN 0-201-17589-4.
  99.            Tutorial introduction to CLOS with many examples and
  100.            a lot of good advice for designing large programs using CLOS.
  101.  
  102.    3. Jo A. Lawless and Molly M. Miller.
  103.       "Understanding CLOS: the Common Lisp Object System"
  104.       Digital Press, 1991. 192 pages.
  105.  
  106.    4. Gregor Kiczales, Jim des Rivieres, and Daniel G. Bobrow.
  107.       "The Art of the Metaobject Protocol"
  108.       MIT Press, 1991. 335 pages. ISBN 0-262-61074-4
  109.            The first part of the book presents a model CLOS implementation,
  110.            introduces the basic principles of metaobject protocols, and 
  111.            works through the key elements of the CLOS Metaobject Protocol.
  112.            The second half is the detailed specification of the CLOS
  113.            Metaobject Protocol. A simple working interpreter suitable
  114.            for experimentation is contained in an appendix.
  115.  
  116.    5. Robert R. Kessler and Amy R. Petajan.
  117.       "LISP, Objects, and Symbolic Programming"
  118.       Scott, Foresman and Company (Glenview, IL), 1988. 644 pages.
  119.            Includes a small Lisp compiler.
  120.  
  121.    6. A short introduction to CLOS written by Jeff Dalton of the
  122.       University of Edinburgh <J.Dalton@ed.ac.uk> is available by
  123.       anonymous ftp from  
  124.          aiai.ed.ac.uk:/lisp/random [192.41.104.6]
  125.       as the file clos-guide.
  126.  
  127.    7. Andreas Paepcke
  128.       "Object-Oriented Programming: the CLOS Perspective"
  129.       MIT Press, 1993, ISBN 0-262-16136-2.
  130.          This book is a collection of essays on the following topics:
  131.            -  Description of CLOS and its design philosophy.
  132.            -  The Metaobject Protocol and its use.
  133.            -  Comparison of CLOS with Smalltalk, Eiffel, Sather, and C++.
  134.            -  CLOS Uses and Methodology. Descriptions of two large CLOS
  135.               applications (Sun's LispView and a hybrid knowledge
  136.               representation tool) and an approach to documenting
  137.               object-oriented protocols (similar to that of AMOP).
  138.            -  Implementation details. Descriptions of TI CLOS for the
  139.               Explorer and PCL's method dispatch mechanism.
  140.  
  141. ----------------------------------------------------------------
  142. Subject: [5-2] How can I write a function that can access defstruct slots 
  143.                by name?  I would like to write something like 
  144.                (STRUCTURE-SLOT <object> '<slot-name>).
  145.  
  146. There is currently no portable, built-in way to access structure slots
  147. given only the name.  If your Common Lisp includes an implementation
  148. of CLOS that supports the meta-object protocol specified in the
  149. original X3J13 draft spec (document X3J13/88-003), then it probably will
  150. allow (SLOT-VALUE <object> '<slot-name>); however, not all
  151. implementations of CLOS currently provide this.  Lacking this, some
  152. implementations may provide implementation-dependent functions that
  153. allow access to structure slots by name; note that this may cause
  154. saved images to be larger, as some implementations normally open-code
  155. structure accessors and discard slot name information.
  156.  
  157. While it is not possible to write a fully general STRUCTURE-SLOT function,
  158. it is not very difficult to write version that handles specific structure
  159. types.  For instance, after defining:
  160.  
  161.    (defstruct spaceship name captain position velocity)
  162.  
  163. one may then define:
  164.  
  165.    (defun spaceship-slot (spaceship slot-name)
  166.      (ecase slot-name
  167.        (name (spaceship-name spaceship))
  168.        (captain (spaceship-captain spaceship))
  169.        (position (spaceship-position spaceship))
  170.        (velocity (spaceship-velocity spaceship))))
  171.  
  172. or using CLOS (generic functions):
  173.  
  174. (defgeneric spaceship-slot (spaceship slot-name)
  175.   (:method ((x spaceship) (slot (eql :name)))
  176.     (spaceship-name x))
  177.   (:method ((x spaceship) (slot (eql :captain)))
  178.     (spaceship-captain x))
  179.   (:method ((x spaceship) (slot (eql :position)))
  180.     (spaceship-position x))
  181.   (:method ((x spaceship) (slot (eql :velocity)))
  182.     (spaceship-velocity x)))
  183.  
  184. Another popular way to define this is:
  185.  
  186.    (defun spaceship-slot (spaceship slot-name)
  187.      (funcall (symbol-function
  188.                 (find-symbol (format nil "SPACESHIP-~A" slot-name)
  189.                              #.(package-name *package*)))
  190.               spaceship))
  191.  
  192. I personally recommend the first version.  It is likely to be much faster
  193. and more memory efficient than the second version.  It's also easy to get
  194. the second one wrong; many people forget to specify the package argument to
  195. FIND-SYMBOL, which can cause incorrect results when the package at run time
  196. is different from the one at compile time.  Even my version assumes that
  197. SPACESHIP-SLOT is being defined in a file that is in the same package as
  198. the one containing the structure definition; if this isn't the case,
  199. #.(PACKAGE-NAME *PACKAGE*) should be replaced by a string naming the
  200. correct package.
  201.  
  202. Another workaround is to define a MY-DEFSTRUCT macro that parses the
  203. defstruct arguments and expands into a call to DEFSTRUCT along with a
  204. definition of the runtime slot-accessor function.
  205.  
  206. Some non-portable techniques include the use of SYSTEM:STRUCTURE-REF
  207. in Lucid (LCL:STRUCTURE-REF in earlier versions of Lucid) and 
  208. EXCL:STRUCTURE-REF in Allegro. 
  209. ----------------------------------------------------------------
  210. Subject: [5-3] How can I list all the CLOS instances in a class?
  211.  
  212. There is no built-in way to enumerate the instances of a class.  If you are
  213. only interested in listing the instances of classes that you have defined,
  214. it is not very difficult to implement it as part of your class definition.
  215. Add a shared slot, e.g. ALL-INSTANCES, with an initial value of NIL, to the
  216. class definition.  Then write an after-method on INITIALIZE-INSTANCE for
  217. this class, which pushes the instance being initialized onto ALL-INSTANCES.
  218. Note that this must be done separately for each class that wants to maintain
  219. such a list; it can't be encapsulated in a mixin class, because all its
  220. dependent classes would share the same ALL-INSTANCES slot.  A compromise
  221. would be to use a mixin to define the INITIALIZE-INSTANCE after-method (and
  222. any other general-purpose methods that use the slot), but not the shared
  223. slot; it would be up to the descendant classes to define the slot at the
  224. level of the class hierarchy that is appropriate. You could also try
  225. defining the classes that need instance-recording as instances of a
  226. metaclass that holds the instance registry on the class object. The
  227. recording behavior could then be built-in to an after method on
  228. initialize-instance for the root class of the metaclass, or even
  229. allocate-instance. To allow for garbage collection of old instances,
  230. you will also need to define a generic function to remove the recorded
  231. instances from the list of instances.
  232. ----------------------------------------------------------------
  233. Subject: [5-4]  How can I store data and CLOS instances (with possibly 
  234.                 circular references) on disk so that they may be
  235.                 retrieved at some later time?
  236.  
  237. There are two main techniques of doing this kind of persistent object
  238. storage. The first involves using #. to compile the data into a file.
  239. The second produces an ASCII representation which, when evaluated,
  240. will reproduce an equivalent set of data.
  241.  
  242. If the data you wish to save is stored in the variable *hash-table*,
  243. create a file containing just the lines
  244.         (in-package "YOUR-PACKAGE")
  245.         (setq *hash-table* '#.*hash-table*)
  246. and compile it. The #. macro performs read-time evaluation of the
  247. expression following the dot, and so this compiles the data into the
  248. file. You may then load the file to restore the data. However, the
  249. resulting binary file is not portable between Lisp implementations,
  250. and sometimes not even for the same Lisp on different platforms. Also,
  251. some Lisps will treat the data as constant, and place it on pages in
  252. memory that are marked read-only (after it is loaded). If one tries to
  253. later modify the data, these Lisps will signal an error. Lucid CL only
  254. puts such constants in a read-only area when they appear inside
  255. functions, so this should be safe. Allegro CL doesn't seem to complain
  256. about modification if the data is a cons. DEC's VAXLisp, however, has
  257. problems with #. circular structures in .fas files. MCL seems to work
  258. well with using #. to save data (and even functions) to a file.
  259.  
  260. The other technique is to produce an ASCII representation of the Lisp
  261. objects which may then be saved to a file. To reproduce the data, one
  262. can load (or compile and load) the file. This technique is portable
  263. between different Lisps and platforms. Unfortunately, the resulting
  264. data is not necessarily EQ to the original. Kerry Koitzsch's
  265. save-object.lisp package is included in the Lisp Utilities Repository,
  266. ftp.cs.cmu.edu:user/ai/lang/lisp/util/extensions/save-object/.
  267. (Note: You must cd to this directory in one atomic operation, as some
  268. of the superior directories on the path are protected from access by
  269. an anonymous ftp.) The Lisp Utilities Repository is described in
  270. detail in the answer to question [6-1].
  271.  
  272. See also the discussion of MAKE-LOAD-FORM and MAKE-LOAD-FORM-SAVING-SLOTS 
  273. in CLtL2.
  274.  
  275. WOOD (William's Object Oriented Database) is a simple persistent object
  276. store for MCL 2.0, written by Bill St. Clair <bill@cambridge.apple.com>.
  277. Its goal is to provide a way to save/restore Lisp objects to/from disk.
  278. It is available by anonymous ftp from the MCL repository on
  279. cambridge.apple.com:pub/MCL2/CONTRIB/. Send bug reports to
  280. bug-wood@cambridge.apple.com. To be added to the mailing list, send mail
  281. to info-wood-request@cambridge.apple.com.
  282.  
  283. Statice is a commercial product from Symbolics, that provides a
  284. powerful persistent ODBMS. It runs on Symbolics Lisp Machines and soon
  285. on Unix boxes under Lucid. 
  286.  
  287. ITASCA ODBMS V2.2 is a distributed active object database management
  288. system. ITASCA allows clients to transparently access data that is
  289. distributed among multiple servers.  ITASCA supports full dynamic
  290. schema modification that can be performed during any phase of the
  291. software lifecycle.  Applications written in dissimilar and
  292. incompatible languages, such as C++ and CLOS, share objects through
  293. ITASCA. ITASCA stores methods inside the database, promoting
  294. reusability and maintainability.  ITASCA is based on work at MCC's
  295. Object-Oriented and Distributed Systems Lab on the ORION system. For
  296. more information, write to Itasca Systems, Inc., 7850 Metro Parkway,
  297. Minneapolis, MN 55425, sales@itasca.com, 612-851-3155.
  298.  
  299. ----------------------------------------------------------------
  300. Subject: [5-5] Given the name of a class, how can I get the names
  301.                of its slots?     
  302.  
  303. (defun class-slot-names (class-name)
  304.   "Given a CLASS-NAME, returns a list of the slots in the class."
  305.   (mapcar #'clos:slot-definition-name
  306.           (clos:class-slots (find-class class-name))))
  307.  
  308. (defmethod class-slot-names ((instance standard-object))
  309.   "Given an INSTANCE, returns a list of the slots in the instance's class."
  310.   (mapcar #'clos:slot-definition-name
  311.           (clos:class-slots (class-of instance))))
  312.  
  313. You can use CLASS-DIRECT-SLOTS instead of CLASS-SLOTS if you don't
  314. want inherited slots. Note that these functions are from the
  315. meta-object protocol specified in the original X3J13 draft spec
  316. (document X3J13/88-003), and may not be supported by all Lisps.
  317.  
  318. ----------------------------------------------------------------
  319. Subject: [5-6] Free CLOS software.
  320.  
  321. Software Repositories:
  322.  
  323.    The CLOS code repository is available by anonymous ftp to
  324.    nervous.cis.ohio-state.edu [128.146.61.200] in the directory
  325.    pub/lispusers/clos/. If you've got code you'd like to add to the
  326.    repository, send mail to Arun Welch, commonloops-request@cis.ohio-state.edu.
  327.    The CLOS code repository includes dag.lisp.Z and 3DGeometry.lisp.
  328.  
  329. CORBA:
  330.  
  331.    The CORBA specification's Dynamic Invocation Interfaces (DII) are a
  332.    collection of interfaces for writing extensible, distributed object
  333.    applications.  NEC Systems Laboratory Inc. has contributed both C and
  334.    CLOS implementations of these specification to the OMG.  The CORBA
  335.    specification as well as the DII implementation can be retrieved via
  336.    anonymous ftp from omg.org:pub/ [192.67.184.64] as the files
  337.    NEC_DII/93-1-2.tar.Z and corba.ps.Z. There are three dependencies in
  338.    the contributed DII implementations.  First, the CLOS images require
  339.    an Allegro CL license due to its use of the Lisp foreign function
  340.    interface.  Second, the C executables require Sun SparcStations, but
  341.    they can be re-compiled and re-linked for other architectures.  Third,
  342.    the implementation of the `Invoke' DII interface is considered
  343.    proprietary.  For further information, write to Don Vines, NEC Systems
  344.    Laboratory, Inc., C&C Software Technology Center (CSTC), 1901 Gateway
  345.    Drive, Irving, Texas 75038, call 214-518-3486, fax 214-518-3552, or
  346.    send email to dvines@syl.dl.nec.com.
  347.  
  348. ----------------------------------------------------------------
  349. ;;; *EOF*
  350.  
  351.