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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9 ;;;; LISP-STAT Object System
12 ;;;; Simple CL implementation of the object system for Lisp-Stat (LSOS)
13 ;;;; as described in Tierney (1990).
15 ;;;; Copyright (c) 1991, by Luke Tierney. Permission is granted for
16 ;;;; unrestricted use.
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.
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.
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.)
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
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).
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
63 (defpackage :lisp-stat-object-system
64 (:nicknames
:ls-objects
:lsos
:proto-objects
)
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)
88 ;; preclist precedence list
89 ;; (serial (incf *proto-object-serial*)))
91 (defclass proto-slots
()) ;; list of data slots
92 (defclass proto-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))
97 (defgeneric add-object
(proto-struct slot value
&key location
)
98 "proto-struct is the prototype structure that we are working with,
99 while slot means either slot or method, and value is the data or the
100 method that we want to add with the name given in slot.")
101 (defgeneric delete-object
(obj proto-struct
))
102 (defgeneric objects
(proto-struct))
103 (defgeneric get-object
(objSym proto-struct
))
108 (defclass proto-object
()
112 :accessor proto-object-slots
)
116 :accessor proto-object-methods
)
119 :type proto-object-list
120 :accessor proto-object-parents
)
121 (preclist ;; precedence list
123 :type proto-object-list
124 :accessor proto-object-preclist
125 :documentation
"precedence list." )
127 :initform
(incf *proto-object-serial
*)
129 :documentation
"Similar idea to serial number." )
133 :documentation
"can we embed the global within the class structure?" )))
135 ;; We denote default-ish proto-object variable names by po or po?.
137 (defvar *proto-object
* (make-instance 'proto-object
)
138 "*proto-object* is the global root object.")
140 (defun proto-object-p (x)
142 Returns T if X is an object, NIL otherwise. Do we really need this?"
143 (typep x
'proto-object
))
145 (defun print-proto-object-structure (po stream depth
)
146 (declare (ignore depth
))
147 (send po
:print stream
))
149 ;;; AJR:FIXME:Is this going to cause issues with concurrency/threading?
150 ;;; (need to appropriately handle interrupts).
151 (defvar *proto-self
* nil
152 "special variable to hold current value of SELF.
153 Assign to current object that we are working with. Local to proto package.
154 Currently working to embed within the object structure rather than a global.")
156 ;; The way that self works is that we try to make sure that we set
157 ;; *self* upon message entry and unset at message exit. This is a
158 ;; good strategy provided that concurrency is not in play.
160 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
162 ;;; Predicates for Consistency Checking
164 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166 (defun non-nil-symbol-p (x)
167 (unless (and x
(symbolp x
)) (error "bad symbol - ~s" x
)))
169 (defun check-object (po)
170 "Returns self if true, throws an error otherwise."
171 (if (proto-object-p po
) po
(error "bad object - ~s" x
)))
173 (defun kind-of-p (pox poy
)
175 Returns T if X and Y are objects and X inherits from Y, NIL otherwise."
176 (if (and (proto-object-p pox
) (proto-object-p poy
))
177 (if (member poy
(proto-object-preclist pox
)) t nil
)
180 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
182 ;;; Precedence List Functions
184 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
186 (defgeneric find-SC
(po)
187 "Return a copy of the complete precedence list for po.")
189 (defmethod find-SC ((po proto-object
))
190 (copy-list (proto-object-preclist po
)))
193 (defgeneric find-S
(po)
194 "return a reverse-sorted, duplicate-free list of parent objects.")
196 (defmethod find-S ((po proto-object
))
198 (parents (proto-object-parents po
) (cdr parents
)))
199 ((not (consp parents
))
200 (delete-duplicates (cons po result
)))
201 (setf result
(nconc (find-SC (first parents
)) result
))))
204 (defgeneric find-RC
(po)
205 "find local precedence ordering.")
207 (defmethod find-RC (object)
208 (let ((list (copy-list (proto-object-parents po
))))
209 (do ((next list
(rest next
)))
210 ((not (consp next
)) list
)
211 (setf (first next
) (cons po
(first next
)))
212 (setf object
(rest (first next
))))))
215 (defgeneric find-R
(S)
216 "find partial precedence ordering.")
218 (defmethod find-R ((S proto-object
))
222 (delete-duplicates result
))
223 (setf result
(nconc result
(find-RC (first S
))))))
226 (defun has-predecessor (x R
)
227 "check if x has a predecessor according to R."
229 (if (and (consp cell
) (eq x
(rest cell
))) (return t
))))
231 (defun find-no-predecessor-list (S R
)
232 "find list of objects in S without predecessors, by R."
235 (unless (has-predecessor x R
) (setf result
(cons x result
))))))
237 (defun child-position (x P
)
238 "find the position of child, if any, of x in P, the list found so
241 (declare (fixnum count
))
243 (if (member x
(proto-object-parents next
)) (return count
))
246 (defun next-object (no-preds P
)
247 "find the next object in the precedence list from objects with no
248 predecessor and current list."
250 ((not (consp no-preds
)) nil
)
251 ((not (consp (rest no-preds
))) (first no-preds
))
255 (declare (fixnum count
))
256 (dolist (x no-preds result
)
257 (let ((tcount (child-position x P
)))
258 (declare (fixnum tcount
))
259 (when (> tcount count
)
261 (setf count tcount
))))))))
264 "Remove object x from S."
268 "Remove all pairs containing x from R. x is assumed to have no
269 predecessors, so only the first position is checked."
270 (delete x R
:key
#'first
))
272 (defun precedence-list (object)
273 "Calculate the object's precedence list."
274 (do* ((S (find-S object
))
280 (setf no-preds
(find-no-predecessor-list S R
))
281 (setf next
(next-object no-preds P
))
282 (if (null next
) (error "inconsistent precedence order"))
283 (setf P
(nconc P
(list next
)))
284 (setf S
(trim-S next S
))
285 (setf R
(trim-R next R
))))
287 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
289 ;;; Object Construction Functions
291 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
293 (defun calculate-preclist (object)
294 "Return the precedence list for the object."
295 (let ((parents (proto-object-parents (check-object object
))))
296 (if (not (consp parents
)) (error "bad parent list - ~s" parents
))
297 (if (consp (rest parents
))
298 (precedence-list object
)
299 (let ((parent (check-object (first parents
))))
300 (cons object
(proto-object-preclist parent
))))))
302 (defun has-duplicates (list)
303 "predicate: takes a list, and returns true if duplicates.
304 This should be simpler, right? Used in next function only?"
305 (do ((next list
(rest next
)))
306 ((not (consp next
)) nil
)
307 (if (member (first next
) (rest next
)) (return t
))))
309 (defun check-parents (parents)
310 "Ensure valid parents: They must be null, object, or consp without duplicates."
312 ((or (null parents
) (proto-object-p parents
)) parents
)
314 (dolist (parent parents
) (check-object parent
))
315 (if (has-duplicates parents
)
316 (error "parents may not contain duplicates")))
317 (t (error "bad parents - ~s" parents
))))
319 (defun make-basic-object (parents object
)
320 "Creates a basic object for the prototype system by ensuring that it
321 can be placed into the storage heirarchy.
322 If object is not initialized, instantiate the structure.
323 Place into parental structure.
324 If parents is null, use root *object*,
325 if parents is a single object, use it (encapsulate as list)
326 otherwise, use parents"
328 (check-parents parents
)
329 (if (not (proto-object-p object
))
330 (setf object
(make-instance
332 :preclist
(proto-object-preclist *proto-object
*)
334 (cond ((null parents
) (list *proto-object
*))
335 ((proto-object-p parents
) (list parents
))
337 (setf (proto-object-preclist object
) (calculate-preclist object
))
340 (defun make-object (&rest parents
)
341 "Args: (&rest parents)
342 Returns a new object with parents PARENTS. If PARENTS is NIL,
343 (list *PROTO-OBJECT*) is used."
344 (make-basic-object parents NIL
))
346 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
348 ;;;; Constraint Hook Functions
350 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
352 (pushnew :constrainthooks
*features
*)
356 (defvar *message-hook
* nil
)
357 (defvar *set-slot-hook
* nil
)
359 (defun check-constraint-hooks (object sym slot
)
360 (let ((hook (if slot
*set-slot-hook
* *message-hook
*)))
363 (let ((*set-slot-hook
* nil
))
364 (funcall hook object sym
))
365 (let ((*message-hook
* nil
))
366 (funcall hook object sym
)))))))
368 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
370 ;;; Slot Access Functions
372 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
374 ;;; AJR: should specialize appropriately, the following:
375 (defun make-slot-entry (x y
) (cons x y
))
376 (defun slot-entry-p (x) (consp x
))
377 (defun slot-entry-key (x) (first x
))
378 (defun slot-entry-value (x) (rest x
))
379 (defun set-slot-entry-value (x v
) (setf (rest x
) v
))
380 (defsetf slot-entry-value set-slot-entry-value
)
382 (defun find-own-slot (x slot
)
383 (if (proto-object-p x
) (assoc slot
(proto-object-slots x
))))
385 (defun find-slot (x slot
)
386 (if (proto-object-p x
)
387 (let ((preclist (proto-object-preclist x
)))
388 (dolist (object preclist
)
389 (let ((slot-entry (find-own-slot object slot
)))
390 (if slot-entry
(return slot-entry
)))))))
393 (defun add-slot (x slot value
)
394 "Remove when completely replaced by add-object methods."
395 (add-object x slot value
))
397 (defmethod add-object ((x proto-object
)
402 (non-nil-symbol-p slot
)
403 (let ((slot-entry (find-own-slot x slot
)))
405 (setf (slot-entry-value slot-entry
) value
)
406 (setf (proto-object-slots x
)
407 (cons (make-slot-entry slot value
) (proto-object-slots x
)))))
410 (defun delete-slot (x slot
)
412 (setf (proto-object-slots x
)
413 (delete slot
(proto-object-slots x
) :key
#'slot-entry-key
)))
415 (defun get-slot-value (x slot
&optional no-err
)
417 (let ((slot-entry (find-slot x slot
)))
418 (if (slot-entry-p slot-entry
)
419 (slot-entry-value slot-entry
)
420 (unless no-err
(error "no slot named ~s in this object" slot
)))))
422 (defun set-slot-value (x slot value
)
424 (let ((slot-entry (find-own-slot x slot
)))
426 ((slot-entry-p slot-entry
)
427 (set-slot-entry-value slot-entry value
)
428 #+:constrainthooks
(check-constraint-hooks x slot t
))
430 (if (find-slot x slot
)
431 (error "object does not own slot ~s" slot
)
432 (error "no slot named ~s in this object" slot
))))))
435 "FIXME? better as macro?."
436 (if (not (proto-object-p *proto-self
*))
437 (error "not in a method"))
440 (defun proto-slot-value (slot)
442 Must be used in a method. Returns the value of current objects slot
444 (get-slot-value (get-self) slot
))
446 (defun proto-slot-value-setf (slot value
)
447 (set-slot-value (get-self) slot value
))
449 (defsetf proto-slot-value proto-slot-value-setf
)
451 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
453 ;;;; Method Access Functions;
455 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
457 (defun make-method-entry (x y
) (cons x y
))
458 (defun method-entry-p (x) (consp x
))
459 (defun method-entry-key (x) (first x
))
460 (defun method-entry-method (x) (rest x
))
461 (defun set-method-entry-method (x v
) (setf (rest x
) v
))
462 (defsetf method-entry-method set-method-entry-method
)
464 (defun find-own-method (x selector
)
465 (if (proto-object-p x
) (assoc selector
(proto-object-methods x
))))
467 (defun find-lsos-method (x selector
)
468 (if (proto-object-p x
)
469 (let ((preclist (proto-object-preclist x
)))
470 (dolist (object preclist
)
471 (let ((method-entry (find-own-method object selector
)))
472 (if method-entry
(return method-entry
)))))))
474 (defun add-lsos-method (x selector value
)
475 "x = object; selector = name of method; value = form computing the method."
477 (non-nil-symbol-p selector
)
478 (let ((method-entry (find-own-method x selector
)))
480 (setf (method-entry-method method-entry
) value
)
481 (setf (proto-object-methods x
)
482 (cons (make-method-entry selector value
) (proto-object-methods x
)))))
485 (defun delete-method (x selector
)
487 (setf (proto-object-methods x
)
488 (delete selector
(proto-object-methods x
) :key
#'method-entry-key
)))
490 (defun get-message-method (x selector
&optional no-err
)
492 (let ((method-entry (find-lsos-method x selector
)))
493 (if (method-entry-p method-entry
)
494 (method-entry-method method-entry
)
495 (unless no-err
(error "no method for selector ~s" selector
)))))
497 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
499 ;;; Message Sending Functions
501 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
503 (defvar *current-preclist
* nil
)
504 (defvar *current-selector
* nil
)
506 (defun sendmsg (object selector preclist args
)
507 (let ((method-entry nil
)
510 ;; look for the message in the precedence list
512 (setf method-entry
(find-own-method (first preclist
) selector
))
513 (if (or method-entry
(not (consp preclist
))) (return))
514 (setf preclist
(rest preclist
)))
516 ((null method-entry
) (error "no method for selector ~s" selector
))
517 ((not (method-entry-p method-entry
)) (error "bad method entry"))
518 (t (setf method
(method-entry-method method-entry
))))
521 (let ((*current-preclist
* preclist
)
522 (*current-selector
* selector
)
523 (*proto-self
* object
))
524 (multiple-value-prog1
525 (apply method object args
)
526 #+:constrainthooks
(check-constraint-hooks object selector nil
)))))
528 ;;;; built-in send function
529 (defun send (object selector
&rest args
)
530 "Args: (object selector &rest args)
531 Applies first method for SELECTOR found in OBJECT's precedence list to
533 (sendmsg object selector
(proto-object-preclist object
) args
))
535 ;;;; call-next-proto-method - call inherited version of current method
536 (defun call-next-proto-method (&rest args
)
538 Funcalls next method for current selector and precedence list. Can only be
540 (sendmsg *proto-self
* *current-selector
* (rest *current-preclist
*) args
))
542 (defun call-proto-method (object selector
&rest args
)
543 "Args (object selector &rest args)
544 Funcalls method for SELECTOR found in OBJECT to SELF. Can only be used in
546 Call method belonging to another object on current object."
547 (sendmsg *proto-self
* selector
(proto-object-preclist object
) args
))
549 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
551 ;;; Object Documentation
553 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
555 (defun find-documentation (x sym add
)
556 (if (proto-object-p x
)
557 (let ((doc (find-own-slot x
'documentation
)))
558 (if (and (null doc
) add
) (add-slot x
'documentation nil
))
559 (if (slot-entry-p doc
) (assoc sym
(slot-entry-value doc
))))))
561 (defun add-documentation (x sym value
)
563 (non-nil-symbol-p sym
)
564 (let ((doc-entry (find-documentation x sym t
)))
566 ((not (null doc-entry
))
567 (setf (rest doc-entry
) value
))
571 (cons (cons sym value
)
572 (get-slot-value x
'documentation
))))))
575 (defun get-documentation (x sym
)
577 (dolist (object (proto-object-preclist x
))
578 (let ((doc-entry (find-documentation object sym nil
))) ;; FIXME: verify
579 (if doc-entry
(return (rest doc-entry
))))))
581 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
585 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
587 (defmacro defmeth
(object name arglist first
&rest body
)
588 "Syntax: (defmeth object method-name lambda-list [doc] {form}*)
589 OBJECT must evaluate to an existing object. Installs a method for NAME in
590 the value of OBJECT and installs DOC in OBJECTS's documentation.
591 RETURNS: method-name."
592 (declare (ignorable self
)) ;; hints for the compiler that sometimes it isn't used
593 (if (and body
(stringp first
))
594 `(progn ;; first=docstring + body
595 (add-lsos-method ,object
,name
596 #'(lambda (self ,@arglist
) (block ,name
,@body
)))
597 (add-documentation ,object
,name
,first
)
599 `(progn ;; first=code + body
600 (add-lsos-method ,object
,name
601 #'(lambda (self ,@arglist
) (block ,name
,first
,@body
)))
604 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
606 ;;; Prototype Construction
608 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
610 (defun find-instance-slots (x slots
)
611 (let ((result (nreverse (delete-duplicates (copy-list slots
)))))
612 (dolist (parent (proto-object-parents x
) (nreverse result
))
613 (dolist (slot (get-slot-value parent
'instance-slots
))
614 (pushnew slot result
)))))
616 (defun get-initial-slot-value (object slot
)
617 (let ((entry (find-slot object slot
)))
618 (if (slot-entry-p entry
) (slot-entry-value entry
))))
620 (defun make-prototype (object name ivars cvars doc set
)
621 (setf ivars
(find-instance-slots object ivars
))
622 (add-slot object
'instance-slots ivars
)
623 (add-slot object
'proto-name name
)
625 (add-slot object slot
(get-initial-slot-value object slot
)))
627 (add-slot object slot nil
))
629 (if (and doc
(stringp doc
))
630 (add-documentation object
'proto doc
))
631 (if set
(setf (symbol-value name
) object
)))
634 (defmacro defproto
(name &optional ivars cvars parents doc
)
635 "Syntax (defproto name &optional ivars cvars (parent *proto-object*) doc)
636 Makes a new object prototype with instance variables IVARS, 'class'
637 variables CVARS and parents PARENT. PARENT can be a single object or
638 a list of objects. IVARS and CVARS must be lists."
639 (let ((obsym (gensym))
643 (let* ((,namesym
',name
)
645 (,obsym
(make-basic-object (if (listp ,parsym
)
647 (list ,parsym
)) ;; should this be ,@parsym ?
649 (make-prototype ,obsym
,namesym
,ivars
,cvars
,doc t
)
653 ;; Infrastructure for new defproto from Common-Lisp Cookbook! Thanks!
655 ;(defmacro odd-define (name buildargs)
656 ; `(progn (defun ,(build-symbol make-a- (:< name))
658 ; (vector ,(length buildargs) ',name ,@buildargs))
659 ; (defun ,(build-symbol test-whether- (:< name)) (x)
660 ; (and (vectorp x) (eq (aref x 1) ',name))
661 ; (defun ,(build-symbol (:< name) -copy) (x)
663 ; (defun ,(build-symbol (:< name) -deactivate) (x)
666 ;(defmacro for (listspec exp)
667 ; (cond ((and (= (length listspec) 3)
668 ; (symbolp (car listspec))
669 ; (eq (cadr listspec) ':in))
670 ; `(mapcar (lambda (,(car listspec))
672 ; ,(caddr listspec)))
673 ; (t (error "Ill-formed: ~s" `(for ,listspec ,exp)))))
675 ;(defmacro symstuff (l)
676 ; `(concatenate 'string
681 ; `',(format nil "~a" x))
683 ; `(format nil "~a" ,(cadr x)))
685 ; `(format nil "~a" (incf ,(cadr x))))
687 ; `(format nil "~a" ,x))))))
689 ;(defmacro build-symbol (&rest l)
690 ; (let ((p (find-if (lambda (x)
692 ; (eq (car x) ':package)))
695 ; (setq l (remove p l))))
696 ; (let ((pkg (cond ((eq (cadr p) 'nil)
698 ; (t `(find-package ',(cadr p))))))
701 ; `(values (intern ,(symstuff l) ,pkg)))
703 ; `(make-symbol ,(symstuff l)))))
705 ; `(values (intern ,(symstuff l))))))))
707 (defmacro defproto2
(name &optional ivars cvars parents doc force
)
708 "Syntax (defproto name &optional ivars cvars (parent *proto-object*) doc)
709 Makes a new object prototype with instance variables IVARS, 'class'
710 variables CVARS and parents PARENT. PARENT can be a single object or
711 a list of objects. IVARS and CVARS must be lists. DOC should be a
713 (if (and (boundp name
)
715 (error "Force T to rebind a prototype object.")
716 (let ((obsym (gensym))
719 (defvar ,name
(list) ,doc
)
720 (let* ((,parsym
,parents
)
721 (,obsym
(make-basic-object
724 (list ,@parsym
)) ;; should this be ,@parsym ?
726 (make-prototype ,obsym
,name
,ivars
,cvars
,doc t
)
729 ;; (macro-expand-1 (defproto2 *mytest*))
732 ;; , => turn on evaluation again (not macro substitution)
733 ;; ` => template comes (use , to undo template and restore eval
734 ;; ' => regular quote (not special in this context), 'ted => (quote ted)
737 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
739 ;;; Initialize the Root Object
741 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
743 (setf (proto-object-preclist *proto-object
*) (list *proto-object
*))
744 (add-slot *proto-object
* 'instance-slots nil
)
745 (add-slot *proto-object
* 'proto-name
'*proto-object
*)
746 (add-slot *proto-object
* 'documentation nil
) ; AJR - for SBCL compiler
747 ; issues about macro with
750 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
752 ;;; *PROTO-OBJECT* Methods
754 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
756 (defmeth *proto-object
* :isnew
(&rest args
)
757 "Method args: (&rest args)
758 Checks ARGS for keyword arguments matching slots and uses them to
761 (dolist (slot-entry (proto-object-slots self
))
762 (let* ((slot (slot-entry-key slot-entry
))
763 (key (intern (symbol-name slot
) (find-package 'keyword
)))
764 (val (proto-slot-value slot
))
765 (new-val (getf args key val
)))
766 (unless (eq val new-val
) (setf (proto-slot-value slot
) new-val
)))))
769 (defmeth *proto-object
* :has-slot
(slot &key own
)
770 "Method args: (slot &optional own)
771 Returns T if slot SLOT exists, NIL if not. If OWN is not NIL
772 only checks the object; otherwise check the entire precedence list."
773 (let ((entry (if own
(find-own-slot self slot
) (find-slot self slot
))))
776 (defmeth *proto-object
* :add-slot
(slot &optional value
)
777 "Method args: (slot &optional value)
778 Installs slot SLOT in object, if it does not already exist, and
779 sets its value to VLAUE."
780 (add-slot self slot value
)
783 (defmeth *proto-object
* :delete-slot
(slot)
785 Deletes slot SLOT from object if it exists."
786 (delete-slot self slot
)
789 (defmeth *proto-object
* :own-slots
()
791 Returns list of names of slots owned by object."
792 (mapcar #'slot-entry-key
(proto-object-slots self
)))
794 (defmeth *proto-object
* :has-method
(selector &key own
)
795 "Method args: (selector &optional own)
796 Returns T if method for SELECTOR exists, NIL if not. If OWN is not NIL
797 only checks the object; otherwise check the entire precedence list."
799 (find-own-method self selector
)
800 (find-lsos-method self selector
))))
803 (defmeth *proto-object
* :add-method
(selector method
)
804 "Method args: (selector method)
805 Installs METHOD for SELECTOR in object."
806 (add-lsos-method self selector method
)
809 (defmeth *proto-object
* :delete-method
(selector)
810 "Method args: (selector)
811 Deletes method for SELECTOR in object if it exists."
812 (delete-method self selector
)
815 (defmeth *proto-object
* :get-method
(selector)
816 "Method args: (selector)
817 Returns method for SELECTOR symbol from object's precedence list."
818 (get-message-method self selector
))
820 (defmeth *proto-object
* :own-methods
()
822 Returns copy of selectors for methods owned by object."
823 (mapcar #'method-entry-key
(proto-object-methods self
)))
825 (defmeth *proto-object
* :parents
()
827 Returns copy of parents list."
828 (copy-list (proto-object-parents self
)))
830 (defmeth *proto-object
* :precedence-list
()
832 Returns copy of the precedence list."
833 (copy-list (proto-object-preclist self
)))
835 (defmeth *proto-object
* :show
(&optional
(stream t
))
837 Prints object's internal data."
838 (format stream
"Slots = ~s~%" (proto-object-slots self
))
839 (format stream
"Methods = ~s~%" (proto-object-methods self
))
840 (format stream
"Parents = ~s~%" (proto-object-parents self
))
841 (format stream
"Precedence List = ~s~%" (proto-object-preclist self
))
844 (defmeth *proto-object
* :reparent
(&rest parents
)
845 "Method args: (&rest parents)
846 Changes precedence list to correspond to PARENTS. Does not change descendants."
847 (make-basic-object parents self
))
849 (defmeth *proto-object
* :make-prototype
(name &optional ivars
)
850 (make-prototype self name ivars nil nil nil
)
853 (defmeth *proto-object
* :internal-doc
(sym &optional new
)
854 "Method args (topic &optional value)
855 Retrieves or installs documentation for topic."
856 (if new
(add-documentation self sym new
))
857 (get-documentation self sym
))
859 (defmeth *proto-object
* :new
(&rest args
)
860 "Method args: (&rest args)
861 Creates new object using self as prototype."
862 (let* ((object (make-object self
)))
863 (if (proto-slot-value 'instance-slots
)
864 (dolist (s (proto-slot-value 'instance-slots
))
865 (send object
:add-slot s
(proto-slot-value s
))))
866 (apply #'send object
:isnew args
)
869 (defmeth *proto-object
* :retype
(proto &rest args
)
870 "Method args: (proto &rest args)
871 Changes object to inherit directly from prototype PROTO. PROTO
872 must be a prototype and SELF must not be one."
873 (if (send self
:has-slot
'instance-slots
:own t
)
874 (error "can't retype a prototype"))
875 (if (not (send proto
:has-slot
'instance-slots
:own t
))
876 (error "not a prototype - ~a" proto
))
877 (send self
:reparent proto
)
878 (dolist (s (send proto
:slot-value
'instance-slots
))
879 (send self
:add-slot s
(proto-slot-value s
)))
880 (apply #'send self
:isnew args
)
883 (defmeth *proto-object
* :print
(&optional
(stream *standard-output
*))
884 "Method args: (&optional (stream *standard-output*))
885 Default object printing method."
887 ((send self
:has-slot
'proto-name
)
889 "#<Object: ~D, prototype = ~A>"
890 (proto-object-serial self
)
891 (proto-slot-value 'proto-name
)))
892 (t (format stream
"#<Object: ~D>" (proto-object-serial self
)))))
894 (defmeth *proto-object
* :slot-value
(sym &optional
(val nil set
))
895 "Method args: (sym &optional val)
896 Sets and retrieves value of slot named SYM. Signals an error if slot
898 (if set
(setf (proto-slot-value sym
) val
))
899 (proto-slot-value sym
))
901 (defmeth *proto-object
* :slot-names
()
903 Returns list of slots available to the object."
905 (mapcar #'(lambda (x) (send x
:own-slots
))
906 (send self
:precedence-list
))))
908 (defmeth *proto-object
* :method-selectors
()
910 Returns list of method selectors available to object."
912 (mapcar #'(lambda (x) (send x
:own-methods
))
913 (send self
:precedence-list
))))
916 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
918 ;;;; Object Help Methods
920 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
922 (defmeth *proto-object
* :doc-topics
()
924 Returns all topics with documentation for this object."
930 (if (send x
:has-slot
'documentation
:own t
)
931 (send x
:slot-value
(quote documentation
))))
932 (send self
:precedence-list
))))))
934 (defmeth *proto-object
* :documentation
(topic &optional
(val nil set
))
935 "Method args: (topic &optional val)
936 Retrieves or sets object documentation for topic."
937 (if set
(send self
:internal-doc topic val
))
938 (let ((val (dolist (i (send self
:precedence-list
))
939 (let ((val (send i
:internal-doc topic
)))
940 (if val
(return val
))))))
943 (defmeth *proto-object
* :delete-documentation
(topic)
944 "Method args: (topic)
945 Deletes object documentation for TOPIC."
946 (setf (proto-slot-value 'documentation
)
947 ;;(remove :title nil :test #'(lambda (x y) (eql x (first y)))) ;; original
948 (remove topic
(send self
:documentation
) :test
#'(lambda (x y
) (eql x
(first y
))))) ;; AJR:PROBLEM?
951 (defmeth *proto-object
* :help
(&optional topic
)
952 "Method args: (&optional topic)
953 Prints help message for TOPIC, or genreal help if TOPIC is NIL."
955 (let ((doc (send self
:documentation topic
)))
957 (doc (princ topic
) (terpri) (princ doc
) (terpri))
958 (t (format t
"Sorry, no help available on ~a~%" topic
))))
959 (let ((topics (stable-sort (copy-seq (send self
:doc-topics
))
961 (string-lessp (string x
) (string y
)))))
962 (proto-doc (send self
:documentation
'proto
)))
963 (if (send self
:has-slot
'proto-name
)
964 (format t
"~s~%" (proto-slot-value 'proto-name
)))
965 (when proto-doc
(princ proto-doc
) (terpri))
966 (format t
"Help is available on the following:~%~%")
967 (dolist (i topics
) (format t
"~s " i
))
971 (defmeth *proto-object
* :compile-method
(name)
973 Compiles method NAME unless it is already compiled. The object must
975 (unless (send self
:has-method name
)
976 (error "No ~s method in this object" name
))
977 (unless (send self
:has-method name
:own t
)
978 (error "Object does not own ~s method" name
))
979 (let ((fun (send self
:get-method name
)))
980 (unless (compiled-function-p fun
)
981 (multiple-value-bind (form env
) (function-lambda-expression fun
)
984 "method may have been defined in non-null environment"))
985 (send self
:add-method name
(compile nil form
))))))