moving towards CLOS approach
[CommonLispStat.git] / lsobjects.lsp
bloba50072c1a1f28704af94797176cddac4af947f2b
1 ;;; -*- mode: lisp -*-
2 ;;; Copyright (c) 2005--2007, by A.J. Rossini <blindglobe@gmail.com>
3 ;;; See COPYRIGHT file for any additional restrictions (BSD license).
4 ;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp.
6 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;;;
9 ;;;; LISP-STAT Object System
10 ;;;;
11 ;;;;
12 ;;;; Simple CL implementation of the object system for Lisp-Stat (LSOS)
13 ;;;; as described in Tierney (1990).
14 ;;;;
15 ;;;; Copyright (c) 1991, by Luke Tierney. Permission is granted for
16 ;;;; unrestricted use.
17 ;;;;
18 ;;;;
19 ;;;; NOTES:
20 ;;;;
21 ;;;; If your CL's handling of packages is compliant with CLtL, 2nd
22 ;;;; Edition (like Macintosh CL version 2), add the feature :CLtL2
23 ;;;; before loading or compiling this code.
24 ;;;;
25 ;;;; This implementation does not make use of CLOS. It can coexist
26 ;;;; with CLOS, but there are two name conflicts: slot-value and
27 ;;;; call-next-method. These two symbols are shadowed in the LSOS
28 ;;;; package and must be shadowed in any package that uses LSOS.
29 ;;;; Evaluating the function (lsos::use-lsos) from a package after
30 ;;;; loading this code shadows these two symbols and does a
31 ;;;; use-package for LSOS.
32 ;;;;
33 ;;;; The :compile-method method uses function-lambda-expression
34 ;;;; defined in CLtL, 2nd Edition. (This method is only needed if
35 ;;;; you want to force compilation of an interpreted method. It is
36 ;;;; not used by the compiler.)
37 ;;;;
38 ;;;; The efficiency of this code could be improved by low level
39 ;;;; coding of the dispatching functions send, call-method and
40 ;;;; call-next-method to avoid creating an argument list. Other
41 ;;;; efficiency improvements are possible as well, in particular
42 ;;;; by good use of declarations. It may also be possible to build
43 ;;;; a more efficient implementation using the CLOS metaclass
44 ;;;; protocol.
45 ;;;;
46 ;;;; There are a few minimal tools for experimenting with constraints
47 ;;;; in the code; they are marked by #+:constrainthooks. Sometime
48 ;;;; soon I hope to augment or replace these hooks with a CORAL-like
49 ;;;; constraint system (as used in GARNET).
50 ;;;;
51 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
52 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
55 ;;; AJR sez: above is generally true, except that the proto system
56 ;;; would be built using the MOP (metaobject protocol), not CLOS.
57 ;;; We use CLOS for a few things, but
59 ;;; Package Setup
61 (in-package :cl-user)
63 (defpackage :lisp-stat-object-system
64 (:nicknames :ls-objects :lsos :proto-objects)
65 (:use :common-lisp)
66 (:export proto-object proto-object-p *proto-object*
67 kind-of-p make-proto-object *message-hook*
68 *set-slot-hook* proto-slot-value self send
69 call-next-proto-method call-proto-method
70 defmeth defproto defproto2
71 instance-slots proto-name))
73 (in-package :lisp-stat-object-system)
75 ;;; Structure Implementation of Lisp-Stat Object System
76 ;;; (prototype object system).
78 (defvar *proto-object-serial* 0)
80 ;; (defstruct (proto-object
81 ;; (:constructor make-proto-object-structure)
82 ;; (:print-function print-proto-object-structure)
83 ;; (:predicate proto-object-p)
84 ;; )
85 ;; slots
86 ;; methods
87 ;; parents
88 ;; preclist precedence list
89 ;; (serial (incf *proto-object-serial*)))
91 (defclass slots ()) ;; list of data slots
92 (defclass methods ()) ;; list of functions that can be called
93 (defclass proto-object-list ())
94 (defclass preclist (proto-object-list))
95 (defclass parents (proto-object-list))
98 (defclass proto-object ()
99 ((slots
100 :initform (list)
101 :type list
102 :accessor proto-object-slots )
103 (methods
104 :initform (list)
105 :type list
106 :accessor proto-object-methods )
107 (parents
108 :initform (list)
109 :type list
110 :accessor proto-object-parents )
111 (preclist ;; precedence list
112 :initform (list)
113 :type list
114 :accessor proto-object-preclist
115 :documentation "precedence list." )
116 (serial
117 :initform (incf *proto-object-serial*)
118 :type integer
119 :documentation "Similar idea to serial number." )
120 (self2
121 :initform nil
122 :accessor proto-self
123 :documentation "can we embed the global within the class structure?" )))
125 ;; We denote default-ish proto-object variable names by po or po?.
127 (defvar *proto-object* (make-instance 'proto-object)
128 "*proto-object* is the global root object.")
130 (defun proto-object-p (x)
131 "Args: (x)
132 Returns T if X is an object, NIL otherwise. Do we really need this?"
133 (typep x 'proto-object))
135 (defun print-proto-object-structure (po stream depth)
136 (declare (ignore depth))
137 (send po :print stream))
139 ;;; AJR:FIXME:Is this going to cause issues with concurrency/threading?
140 ;;; (need to appropriately handle interrupts).
141 (defvar *proto-self* nil
142 "special variable to hold current value of SELF.
143 Assign to current object that we are working with. Local to proto package.
144 Currently working to embed within the object structure rather than a global.")
146 ;; The way that self works is that we try to make sure that we set
147 ;; *self* upon message entry and unset at message exit. This is a
148 ;; good strategy provided that concurrency is not in play.
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
152 ;;; Predicates for Consistency Checking
154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
156 (defun check-non-nil-symbol (x)
157 (unless (and x (symbolp x)) (error "bad symbol - ~s" x)))
159 (defun check-object (po)
160 "Returns self if true, throws an error otherwise."
161 (if (proto-object-p po) po (error "bad object - ~s" x)))
163 (defun kind-of-p (pox poy)
164 "Args: (x y)
165 Returns T if X and Y are objects and X inherits from Y, NIL otherwise."
166 (if (and (proto-object-p pox) (proto-object-p poy))
167 (if (member poy (proto-object-preclist pox)) t nil)
168 nil))
170 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
172 ;;; Precedence List Functions
174 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
176 (defgeneric find-SC (po)
177 "Return a copy of the complete precedence list for po.")
179 (defmethod find-SC ((po proto-object))
180 (copy-list (proto-object-preclist po)))
183 (defgeneric find-S (po)
184 "return a reverse-sorted, duplicate-free list of parent objects.")
186 (defmethod find-S ((po proto-object))
187 (do ((result nil)
188 (parents (proto-object-parents po) (cdr parents)))
189 ((not (consp parents))
190 (delete-duplicates (cons po result)))
191 (setf result (nconc (find-SC (first parents)) result))))
194 (defgeneric find-RC (po)
195 "find local precedence ordering.")
197 (defmethod find-RC (object)
198 (let ((list (copy-list (proto-object-parents po))))
199 (do ((next list (rest next)))
200 ((not (consp next)) list)
201 (setf (first next) (cons po (first next)))
202 (setf object (rest (first next))))))
205 (defgeneric find-R (S)
206 "find partial precedence ordering.")
208 (defmethod find-R ((S proto-object))
209 (do ((result nil)
210 (S S (rest S)))
211 ((not (consp S))
212 (delete-duplicates result))
213 (setf result (nconc result (find-RC (first S))))))
216 (defun has-predecessor (x R)
217 "check if x has a predecessor according to R."
218 (dolist (cell R nil)
219 (if (and (consp cell) (eq x (rest cell))) (return t))))
221 (defun find-no-predecessor-list (S R)
222 "find list of objects in S without predecessors, by R."
223 (let ((result nil))
224 (dolist (x S result)
225 (unless (has-predecessor x R) (setf result (cons x result))))))
227 (defun child-position (x P)
228 "find the position of child, if any, of x in P, the list found so
229 far."
230 (let ((count 0))
231 (declare (fixnum count))
232 (dolist (next P -1)
233 (if (member x (proto-object-parents next)) (return count))
234 (incf count))))
236 (defun next-object (no-preds P)
237 "find the next object in the precedence list from objects with no
238 predecessor and current list."
239 (cond
240 ((not (consp no-preds)) nil)
241 ((not (consp (rest no-preds))) (first no-preds))
243 (let ((count -1)
244 (result nil))
245 (declare (fixnum count))
246 (dolist (x no-preds result)
247 (let ((tcount (child-position x P)))
248 (declare (fixnum tcount))
249 (when (> tcount count)
250 (setf result x)
251 (setf count tcount))))))))
253 (defun trim-S (x S)
254 "Remove object x from S."
255 (delete x S))
257 (defun trim-R (x R)
258 "Remove all pairs containing x from R. x is assumed to have no
259 predecessors, so only the first position is checked."
260 (delete x R :key #'first))
262 (defun precedence-list (object)
263 "Calculate the object's precedence list."
264 (do* ((S (find-S object))
265 (R (find-R S))
266 (P nil)
267 (no-preds nil)
268 (next nil))
269 ((not (consp S)) P)
270 (setf no-preds (find-no-predecessor-list S R))
271 (setf next (next-object no-preds P))
272 (if (null next) (error "inconsistent precedence order"))
273 (setf P (nconc P (list next)))
274 (setf S (trim-S next S))
275 (setf R (trim-R next R))))
277 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
279 ;;; Object Construction Functions
281 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
283 (defun calculate-preclist (object)
284 "Return the precedence list for the object."
285 (let ((parents (proto-object-parents (check-object object))))
286 (if (not (consp parents)) (error "bad parent list - ~s" parents))
287 (if (consp (rest parents))
288 (precedence-list object)
289 (let ((parent (check-object (first parents))))
290 (cons object (proto-object-preclist parent))))))
292 (defun has-duplicates (list)
293 "predicate: takes a list, and returns true if duplicates.
294 This should be simpler, right? Used in next function only?"
295 (do ((next list (rest next)))
296 ((not (consp next)) nil)
297 (if (member (first next) (rest next)) (return t))))
299 (defun check-parents (parents)
300 "Ensure valid parents: They must be null, object, or consp without duplicates."
301 (cond
302 ((or (null parents) (proto-object-p parents)) parents)
303 ((consp parents)
304 (dolist (parent parents) (check-object parent))
305 (if (has-duplicates parents)
306 (error "parents may not contain duplicates")))
307 (t (error "bad parents - ~s" parents))))
309 (defun make-basic-object (parents object)
310 "Creates a basic object for the prototype system by ensuring that it
311 can be placed into the storage heirarchy.
312 If object is not initialized, instantiate the structure.
313 Place into parental structure.
314 If parents is null, use root *object*,
315 if parents is a single object, use it (encapsulate as list)
316 otherwise, use parents"
318 (check-parents parents)
319 (if (not (proto-object-p object))
320 (setf object (make-instance
321 'proto-object
322 :preclist (proto-object-preclist *proto-object*)
323 :parents
324 (cond ((null parents) (list *proto-object*))
325 ((proto-object-p parents) (list parents))
326 (t parents)))))
327 (setf (proto-object-preclist object) (calculate-preclist object))
328 object)
330 (defun make-object (&rest parents)
331 "Args: (&rest parents)
332 Returns a new object with parents PARENTS. If PARENTS is NIL,
333 (list *PROTO-OBJECT*) is used."
334 (make-basic-object parents NIL))
336 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
337 ;;;;
338 ;;;; Constraint Hook Functions
339 ;;;;
340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
342 (pushnew :constrainthooks *features*)
344 #+:constrainthooks
345 (progn
346 (defvar *message-hook* nil)
347 (defvar *set-slot-hook* nil)
349 (defun check-constraint-hooks (object sym slot)
350 (let ((hook (if slot *set-slot-hook* *message-hook*)))
351 (if hook
352 (if slot
353 (let ((*set-slot-hook* nil))
354 (funcall hook object sym))
355 (let ((*message-hook* nil))
356 (funcall hook object sym)))))))
358 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
360 ;;; Slot Access Functions
362 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
364 ;;; AJR: should specialize appropriately, the following:
365 (defun make-slot-entry (x y) (cons x y))
366 (defun slot-entry-p (x) (consp x))
367 (defun slot-entry-key (x) (first x))
368 (defun slot-entry-value (x) (rest x))
369 (defun set-slot-entry-value (x v) (setf (rest x) v))
370 (defsetf slot-entry-value set-slot-entry-value)
372 (defun find-own-slot (x slot)
373 (if (proto-object-p x) (assoc slot (proto-object-slots x))))
375 (defun find-slot (x slot)
376 (if (proto-object-p x)
377 (let ((preclist (proto-object-preclist x)))
378 (dolist (object preclist)
379 (let ((slot-entry (find-own-slot object slot)))
380 (if slot-entry (return slot-entry)))))))
382 (defun add-slot (x slot value)
383 (check-object x)
384 (check-non-nil-symbol slot)
385 (let ((slot-entry (find-own-slot x slot)))
386 (if slot-entry
387 (setf (slot-entry-value slot-entry) value)
388 (setf (proto-object-slots x)
389 (cons (make-slot-entry slot value) (proto-object-slots x)))))
390 nil)
392 (defun delete-slot (x slot)
393 (check-object x)
394 (setf (proto-object-slots x)
395 (delete slot (proto-object-slots x) :key #'slot-entry-key)))
397 (defun get-slot-value (x slot &optional no-err)
398 (check-object x)
399 (let ((slot-entry (find-slot x slot)))
400 (if (slot-entry-p slot-entry)
401 (slot-entry-value slot-entry)
402 (unless no-err (error "no slot named ~s in this object" slot)))))
404 (defun set-slot-value (x slot value)
405 (check-object x)
406 (let ((slot-entry (find-own-slot x slot)))
407 (cond
408 ((slot-entry-p slot-entry)
409 (set-slot-entry-value slot-entry value)
410 #+:constrainthooks (check-constraint-hooks x slot t))
412 (if (find-slot x slot)
413 (error "object does not own slot ~s" slot)
414 (error "no slot named ~s in this object" slot))))))
416 (defun get-self ()
417 "FIXME? better as macro?."
418 (if (not (proto-object-p *proto-self*))
419 (error "not in a method"))
420 *proto-self*)
422 (defun proto-slot-value (slot)
423 "Args: (slot)
424 Must be used in a method. Returns the value of current objects slot
425 named SLOT."
426 (get-slot-value (get-self) slot))
428 (defun proto-slot-value-setf (slot value)
429 (set-slot-value (get-self) slot value))
431 (defsetf proto-slot-value proto-slot-value-setf)
433 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
434 ;;;;
435 ;;;; Method Access Functions;
436 ;;;;
437 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
439 (defun make-method-entry (x y) (cons x y))
440 (defun method-entry-p (x) (consp x))
441 (defun method-entry-key (x) (first x))
442 (defun method-entry-method (x) (rest x))
443 (defun set-method-entry-method (x v) (setf (rest x) v))
444 (defsetf method-entry-method set-method-entry-method)
446 (defun find-own-method (x selector)
447 (if (proto-object-p x) (assoc selector (proto-object-methods x))))
449 (defun find-lsos-method (x selector)
450 (if (proto-object-p x)
451 (let ((preclist (proto-object-preclist x)))
452 (dolist (object preclist)
453 (let ((method-entry (find-own-method object selector)))
454 (if method-entry (return method-entry)))))))
456 (defun add-lsos-method (x selector value)
457 "x = object; selector = name of method; value = form computing the method."
458 (check-object x)
459 (check-non-nil-symbol selector)
460 (let ((method-entry (find-own-method x selector)))
461 (if method-entry
462 (setf (method-entry-method method-entry) value)
463 (setf (proto-object-methods x)
464 (cons (make-method-entry selector value) (proto-object-methods x)))))
465 nil)
467 (defun delete-method (x selector)
468 (check-object x)
469 (setf (proto-object-methods x)
470 (delete selector (proto-object-methods x) :key #'method-entry-key)))
472 (defun get-message-method (x selector &optional no-err)
473 (check-object x)
474 (let ((method-entry (find-lsos-method x selector)))
475 (if (method-entry-p method-entry)
476 (method-entry-method method-entry)
477 (unless no-err (error "no method for selector ~s" selector)))))
479 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
481 ;;; Message Sending Functions
483 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
485 (defvar *current-preclist* nil)
486 (defvar *current-selector* nil)
488 (defun sendmsg (object selector preclist args)
489 (let ((method-entry nil)
490 (method nil))
492 ;; look for the message in the precedence list
493 (loop
494 (setf method-entry (find-own-method (first preclist) selector))
495 (if (or method-entry (not (consp preclist))) (return))
496 (setf preclist (rest preclist)))
497 (cond
498 ((null method-entry) (error "no method for selector ~s" selector))
499 ((not (method-entry-p method-entry)) (error "bad method entry"))
500 (t (setf method (method-entry-method method-entry))))
502 ;; invoke the method
503 (let ((*current-preclist* preclist)
504 (*current-selector* selector)
505 (*proto-self* object))
506 (multiple-value-prog1
507 (apply method object args)
508 #+:constrainthooks (check-constraint-hooks object selector nil)))))
510 ;;;; built-in send function
511 (defun send (object selector &rest args)
512 "Args: (object selector &rest args)
513 Applies first method for SELECTOR found in OBJECT's precedence list to
514 OBJECT and ARGS."
515 (sendmsg object selector (proto-object-preclist object) args))
517 ;;;; call-next-proto-method - call inherited version of current method
518 (defun call-next-proto-method (&rest args)
519 "Args (&rest args)
520 Funcalls next method for current selector and precedence list. Can only be
521 used in a method."
522 (sendmsg *proto-self* *current-selector* (rest *current-preclist*) args))
524 (defun call-proto-method (object selector &rest args)
525 "Args (object selector &rest args)
526 Funcalls method for SELECTOR found in OBJECT to SELF. Can only be used in
527 a method.
528 Call method belonging to another object on current object."
529 (sendmsg *proto-self* selector (proto-object-preclist object) args))
531 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
533 ;;; Object Documentation
535 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
537 (defun find-documentation (x sym add)
538 (if (proto-object-p x)
539 (let ((doc (find-own-slot x 'documentation)))
540 (if (and (null doc) add) (add-slot x 'documentation nil))
541 (if (slot-entry-p doc) (assoc sym (slot-entry-value doc))))))
543 (defun add-documentation (x sym value)
544 (check-object x)
545 (check-non-nil-symbol sym)
546 (let ((doc-entry (find-documentation x sym t)))
547 (cond
548 ((not (null doc-entry))
549 (setf (rest doc-entry) value))
551 (set-slot-value x
552 'documentation
553 (cons (cons sym value)
554 (get-slot-value x 'documentation))))))
555 nil)
557 (defun get-documentation (x sym)
558 (check-object x)
559 (dolist (object (proto-object-preclist x))
560 (let ((doc-entry (find-documentation object sym nil))) ;; FIXME: verify
561 (if doc-entry (return (rest doc-entry))))))
563 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
564 ;;;;
565 ;;;; DEFMETH Macro
566 ;;;;
567 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
569 (defmacro defmeth (object name arglist first &rest body)
570 "Syntax: (defmeth object method-name lambda-list [doc] {form}*)
571 OBJECT must evaluate to an existing object. Installs a method for NAME in
572 the value of OBJECT and installs DOC in OBJECTS's documentation.
573 RETURNS: method-name."
574 (declare (ignorable self)) ;; hints for the compiler that sometimes it isn't used
575 (if (and body (stringp first))
576 `(progn ;; first=docstring + body
577 (add-lsos-method ,object ,name
578 #'(lambda (self ,@arglist) (block ,name ,@body)))
579 (add-documentation ,object ,name ,first)
580 ,name)
581 `(progn ;; first=code + body
582 (add-lsos-method ,object ,name
583 #'(lambda (self ,@arglist) (block ,name ,first ,@body)))
584 ,name)))
586 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
588 ;;; Prototype Construction
590 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
592 (defun find-instance-slots (x slots)
593 (let ((result (nreverse (delete-duplicates (copy-list slots)))))
594 (dolist (parent (proto-object-parents x) (nreverse result))
595 (dolist (slot (get-slot-value parent 'instance-slots))
596 (pushnew slot result)))))
598 (defun get-initial-slot-value (object slot)
599 (let ((entry (find-slot object slot)))
600 (if (slot-entry-p entry) (slot-entry-value entry))))
602 (defun make-prototype (object name ivars cvars doc set)
603 (setf ivars (find-instance-slots object ivars))
604 (add-slot object 'instance-slots ivars)
605 (add-slot object 'proto-name name)
606 (dolist (slot ivars)
607 (add-slot object slot (get-initial-slot-value object slot)))
608 (dolist (slot cvars)
609 (add-slot object slot nil))
611 (if (and doc (stringp doc))
612 (add-documentation object 'proto doc))
613 (if set (setf (symbol-value name) object)))
616 (defmacro defproto (name &optional ivars cvars parents doc)
617 "Syntax (defproto name &optional ivars cvars (parent *proto-object*) doc)
618 Makes a new object prototype with instance variables IVARS, 'class'
619 variables CVARS and parents PARENT. PARENT can be a single object or
620 a list of objects. IVARS and CVARS must be lists."
621 (let ((obsym (gensym))
622 (namesym (gensym))
623 (parsym (gensym)))
624 `(progn
625 (let* ((,namesym ',name)
626 (,parsym ,parents)
627 (,obsym (make-basic-object (if (listp ,parsym)
628 ,parsym
629 (list ,parsym)) ;; should this be ,@parsym ?
630 nil)))
631 (make-prototype ,obsym ,namesym ,ivars ,cvars ,doc t)
632 ,namesym))))
635 ;; Infrastructure for new defproto from Common-Lisp Cookbook! Thanks!
637 ;(defmacro odd-define (name buildargs)
638 ; `(progn (defun ,(build-symbol make-a- (:< name))
639 ; ,buildargs
640 ; (vector ,(length buildargs) ',name ,@buildargs))
641 ; (defun ,(build-symbol test-whether- (:< name)) (x)
642 ; (and (vectorp x) (eq (aref x 1) ',name))
643 ; (defun ,(build-symbol (:< name) -copy) (x)
644 ; ...)
645 ; (defun ,(build-symbol (:< name) -deactivate) (x)
646 ; ...))))
648 ;(defmacro for (listspec exp)
649 ; (cond ((and (= (length listspec) 3)
650 ; (symbolp (car listspec))
651 ; (eq (cadr listspec) ':in))
652 ; `(mapcar (lambda (,(car listspec))
653 ; ,exp)
654 ; ,(caddr listspec)))
655 ; (t (error "Ill-formed: ~s" `(for ,listspec ,exp)))))
657 ;(defmacro symstuff (l)
658 ; `(concatenate 'string
659 ; ,@(for (x :in l)
660 ; (cond ((stringp x)
661 ; `',x)
662 ; ((atom x)
663 ; `',(format nil "~a" x))
664 ; ((eq (car x) ':<)
665 ; `(format nil "~a" ,(cadr x)))
666 ; ((eq (car x) ':++)
667 ; `(format nil "~a" (incf ,(cadr x))))
668 ; (t
669 ; `(format nil "~a" ,x))))))
671 ;(defmacro build-symbol (&rest l)
672 ; (let ((p (find-if (lambda (x)
673 ; (and (consp x)
674 ; (eq (car x) ':package)))
675 ; l)))
676 ; (cond (p
677 ; (setq l (remove p l))))
678 ; (let ((pkg (cond ((eq (cadr p) 'nil)
679 ; nil)
680 ; (t `(find-package ',(cadr p))))))
681 ; (cond (p
682 ; (cond (pkg
683 ; `(values (intern ,(symstuff l) ,pkg)))
684 ; (t
685 ; `(make-symbol ,(symstuff l)))))
686 ; (t
687 ; `(values (intern ,(symstuff l))))))))
689 (defmacro defproto2 (name &optional ivars cvars parents doc force)
690 "Syntax (defproto name &optional ivars cvars (parent *proto-object*) doc)
691 Makes a new object prototype with instance variables IVARS, 'class'
692 variables CVARS and parents PARENT. PARENT can be a single object or
693 a list of objects. IVARS and CVARS must be lists. DOC should be a
694 string."
695 (if (and (boundp name)
696 (not force))
697 (error "Force T to rebind a prototype object.")
698 (let ((obsym (gensym))
699 (parsym (gensym)))
700 `(progn
701 (defvar ,name (list) ,doc)
702 (let* ((,parsym ,parents)
703 (,obsym (make-basic-object
704 (if (listp ,parsym)
705 ,parsym
706 (list ,@parsym)) ;; should this be ,@parsym ?
707 nil)))
708 (make-prototype ,obsym ,name ,ivars ,cvars ,doc t)
709 ,name)))))
711 ;; (macro-expand-1 (defproto2 *mytest*))
713 ;; recall:
714 ;; , => turn on evaluation again (not macro substitution)
715 ;; ` => template comes (use , to undo template and restore eval
716 ;; ' => regular quote (not special in this context), 'ted => (quote ted)
719 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
721 ;;; Initialize the Root Object
723 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
725 (setf (proto-object-preclist *proto-object*) (list *proto-object*))
726 (add-slot *proto-object* 'instance-slots nil)
727 (add-slot *proto-object* 'proto-name '*proto-object*)
728 (add-slot *proto-object* 'documentation nil) ; AJR - for SBCL compiler
729 ; issues about macro with
730 ; unknown slot
732 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
734 ;;; *PROTO-OBJECT* Methods
736 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
738 (defmeth *proto-object* :isnew (&rest args)
739 "Method args: (&rest args)
740 Checks ARGS for keyword arguments matching slots and uses them to
741 initialize slots."
742 (if args
743 (dolist (slot-entry (proto-object-slots self))
744 (let* ((slot (slot-entry-key slot-entry))
745 (key (intern (symbol-name slot) (find-package 'keyword)))
746 (val (proto-slot-value slot))
747 (new-val (getf args key val)))
748 (unless (eq val new-val) (setf (proto-slot-value slot) new-val)))))
749 self)
751 (defmeth *proto-object* :has-slot (slot &key own)
752 "Method args: (slot &optional own)
753 Returns T if slot SLOT exists, NIL if not. If OWN is not NIL
754 only checks the object; otherwise check the entire precedence list."
755 (let ((entry (if own (find-own-slot self slot) (find-slot self slot))))
756 (if entry t nil)))
758 (defmeth *proto-object* :add-slot (slot &optional value)
759 "Method args: (slot &optional value)
760 Installs slot SLOT in object, if it does not already exist, and
761 sets its value to VLAUE."
762 (add-slot self slot value)
763 value)
765 (defmeth *proto-object* :delete-slot (slot)
766 "Method args: (slot)
767 Deletes slot SLOT from object if it exists."
768 (delete-slot self slot)
769 nil)
771 (defmeth *proto-object* :own-slots ()
772 "Method args: ()
773 Returns list of names of slots owned by object."
774 (mapcar #'slot-entry-key (proto-object-slots self)))
776 (defmeth *proto-object* :has-method (selector &key own)
777 "Method args: (selector &optional own)
778 Returns T if method for SELECTOR exists, NIL if not. If OWN is not NIL
779 only checks the object; otherwise check the entire precedence list."
780 (let ((entry (if own
781 (find-own-method self selector)
782 (find-lsos-method self selector))))
783 (if entry t nil)))
785 (defmeth *proto-object* :add-method (selector method)
786 "Method args: (selector method)
787 Installs METHOD for SELECTOR in object."
788 (add-lsos-method self selector method)
789 nil)
791 (defmeth *proto-object* :delete-method (selector)
792 "Method args: (selector)
793 Deletes method for SELECTOR in object if it exists."
794 (delete-method self selector)
795 nil)
797 (defmeth *proto-object* :get-method (selector)
798 "Method args: (selector)
799 Returns method for SELECTOR symbol from object's precedence list."
800 (get-message-method self selector))
802 (defmeth *proto-object* :own-methods ()
803 "Method args ()
804 Returns copy of selectors for methods owned by object."
805 (mapcar #'method-entry-key (proto-object-methods self)))
807 (defmeth *proto-object* :parents ()
808 "Method args: ()
809 Returns copy of parents list."
810 (copy-list (proto-object-parents self)))
812 (defmeth *proto-object* :precedence-list ()
813 "Method args: ()
814 Returns copy of the precedence list."
815 (copy-list (proto-object-preclist self)))
817 (defmeth *proto-object* :show (&optional (stream t))
818 "Method Args: ()
819 Prints object's internal data."
820 (format stream "Slots = ~s~%" (proto-object-slots self))
821 (format stream "Methods = ~s~%" (proto-object-methods self))
822 (format stream "Parents = ~s~%" (proto-object-parents self))
823 (format stream "Precedence List = ~s~%" (proto-object-preclist self))
824 nil)
826 (defmeth *proto-object* :reparent (&rest parents)
827 "Method args: (&rest parents)
828 Changes precedence list to correspond to PARENTS. Does not change descendants."
829 (make-basic-object parents self))
831 (defmeth *proto-object* :make-prototype (name &optional ivars)
832 (make-prototype self name ivars nil nil nil)
833 self)
835 (defmeth *proto-object* :internal-doc (sym &optional new)
836 "Method args (topic &optional value)
837 Retrieves or installs documentation for topic."
838 (if new (add-documentation self sym new))
839 (get-documentation self sym))
841 (defmeth *proto-object* :new (&rest args)
842 "Method args: (&rest args)
843 Creates new object using self as prototype."
844 (let* ((object (make-object self)))
845 (if (proto-slot-value 'instance-slots)
846 (dolist (s (proto-slot-value 'instance-slots))
847 (send object :add-slot s (proto-slot-value s))))
848 (apply #'send object :isnew args)
849 object))
851 (defmeth *proto-object* :retype (proto &rest args)
852 "Method args: (proto &rest args)
853 Changes object to inherit directly from prototype PROTO. PROTO
854 must be a prototype and SELF must not be one."
855 (if (send self :has-slot 'instance-slots :own t)
856 (error "can't retype a prototype"))
857 (if (not (send proto :has-slot 'instance-slots :own t))
858 (error "not a prototype - ~a" proto))
859 (send self :reparent proto)
860 (dolist (s (send proto :slot-value 'instance-slots))
861 (send self :add-slot s (proto-slot-value s)))
862 (apply #'send self :isnew args)
863 self)
865 (defmeth *proto-object* :print (&optional (stream *standard-output*))
866 "Method args: (&optional (stream *standard-output*))
867 Default object printing method."
868 (cond
869 ((send self :has-slot 'proto-name)
870 (format stream
871 "#<Object: ~D, prototype = ~A>"
872 (proto-object-serial self)
873 (proto-slot-value 'proto-name)))
874 (t (format stream "#<Object: ~D>" (proto-object-serial self)))))
876 (defmeth *proto-object* :slot-value (sym &optional (val nil set))
877 "Method args: (sym &optional val)
878 Sets and retrieves value of slot named SYM. Signals an error if slot
879 does not exist."
880 (if set (setf (proto-slot-value sym) val))
881 (proto-slot-value sym))
883 (defmeth *proto-object* :slot-names ()
884 "Method args: ()
885 Returns list of slots available to the object."
886 (apply #'append
887 (mapcar #'(lambda (x) (send x :own-slots))
888 (send self :precedence-list))))
890 (defmeth *proto-object* :method-selectors ()
891 "Method args: ()
892 Returns list of method selectors available to object."
893 (apply #'append
894 (mapcar #'(lambda (x) (send x :own-methods))
895 (send self :precedence-list))))
898 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
899 ;;;;
900 ;;;; Object Help Methods
901 ;;;;
902 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
904 (defmeth *proto-object* :doc-topics ()
905 "Method args: ()
906 Returns all topics with documentation for this object."
907 (remove-duplicates
908 (mapcar #'car
909 (apply #'append
910 (mapcar
911 #'(lambda (x)
912 (if (send x :has-slot 'documentation :own t)
913 (send x :slot-value (quote documentation))))
914 (send self :precedence-list))))))
916 (defmeth *proto-object* :documentation (topic &optional (val nil set))
917 "Method args: (topic &optional val)
918 Retrieves or sets object documentation for topic."
919 (if set (send self :internal-doc topic val))
920 (let ((val (dolist (i (send self :precedence-list))
921 (let ((val (send i :internal-doc topic)))
922 (if val (return val))))))
923 val))
925 (defmeth *proto-object* :delete-documentation (topic)
926 "Method args: (topic)
927 Deletes object documentation for TOPIC."
928 (setf (proto-slot-value 'documentation)
929 ;;(remove :title nil :test #'(lambda (x y) (eql x (first y)))) ;; original
930 (remove topic (send self :documentation) :test #'(lambda (x y) (eql x (first y))))) ;; AJR:PROBLEM?
931 nil)
933 (defmeth *proto-object* :help (&optional topic)
934 "Method args: (&optional topic)
935 Prints help message for TOPIC, or genreal help if TOPIC is NIL."
936 (if topic
937 (let ((doc (send self :documentation topic)))
938 (cond
939 (doc (princ topic) (terpri) (princ doc) (terpri))
940 (t (format t "Sorry, no help available on ~a~%" topic))))
941 (let ((topics (stable-sort (copy-seq (send self :doc-topics))
942 #'(lambda (x y)
943 (string-lessp (string x) (string y)))))
944 (proto-doc (send self :documentation 'proto)))
945 (if (send self :has-slot 'proto-name)
946 (format t "~s~%" (proto-slot-value 'proto-name)))
947 (when proto-doc (princ proto-doc) (terpri))
948 (format t "Help is available on the following:~%~%")
949 (dolist (i topics) (format t "~s " i))
950 (terpri)))
951 (values))
953 (defmeth *proto-object* :compile-method (name)
954 "Method args: (name)
955 Compiles method NAME unless it is already compiled. The object must
956 own the method."
957 (unless (send self :has-method name)
958 (error "No ~s method in this object" name))
959 (unless (send self :has-method name :own t)
960 (error "Object does not own ~s method" name))
961 (let ((fun (send self :get-method name)))
962 (unless (compiled-function-p fun)
963 (multiple-value-bind (form env) (function-lambda-expression fun)
964 (if env
965 (error
966 "method may have been defined in non-null environment"))
967 (send self :add-method name (compile nil form))))))