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
) (rest 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)
404 ;; This is wrong but has the right flavor of what should be
406 (setf slot
(gensym)))
407 (let ((slot-entry (find-own-slot x slot
)))
409 (setf (slot-entry-value slot-entry
) value
)
410 (setf (proto-object-slots x
)
411 (cons (make-slot-entry slot value
) (proto-object-slots x
)))))
412 nil
) ;; I think we want to return something, but what?
414 ;; This might be more appropriate as a "setter" dispatching on a
415 ;; (proto-object slot)
418 ;; REMOVE ME when obsolete
419 (defun delete-slot (x slot
)
420 (delete-object x slot
))
422 (defmethod delete-object ((x proto-object
)
425 (setf (proto-object-slots x
)
426 (delete slot
(proto-object-slots x
) :key
#'slot-entry-key
)))
428 (defun get-slot-value (x slot
&optional no-err
)
430 (let ((slot-entry (find-slot x slot
)))
431 (if (slot-entry-p slot-entry
)
432 (slot-entry-value slot-entry
)
433 (unless no-err
(error "no slot named ~s in this object" slot
)))))
435 (defun set-slot-value (x slot value
)
437 (let ((slot-entry (find-own-slot x slot
)))
439 ((slot-entry-p slot-entry
)
440 (set-slot-entry-value slot-entry value
)
441 #+:constrainthooks
(check-constraint-hooks x slot t
))
443 (if (find-slot x slot
)
444 (error "object does not own slot ~s" slot
)
445 (error "no slot named ~s in this object" slot
))))))
448 "Return current proto-object being manipulated. We are going to die
449 in a multithreaded environment."
450 (if (not (proto-object-p *proto-self
*))
451 (error "not in a method"))
454 (defun proto-slot-value (slot)
456 Must be used in a method. Returns the value of current objects slot
458 (get-slot-value (get-self) slot
))
460 (defun proto-slot-value-setf (slot value
)
461 (set-slot-value (get-self) slot value
))
463 (defsetf proto-slot-value proto-slot-value-setf
)
465 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
467 ;;;; Method Access Functions;
469 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
471 (defun make-method-entry (x y
) (cons x y
))
472 (defun method-entry-p (x) (consp x
))
473 (defun method-entry-key (x) (first x
))
474 (defun method-entry-method (x) (rest x
))
475 (defun set-method-entry-method (x v
) (setf (rest x
) v
))
476 (defsetf method-entry-method set-method-entry-method
)
478 (defun find-own-method (x selector
)
479 (if (proto-object-p x
) (assoc selector
(proto-object-methods x
))))
481 (defun find-lsos-method (x selector
)
482 (if (proto-object-p x
)
483 (let ((preclist (proto-object-preclist x
)))
484 (dolist (object preclist
)
485 (let ((method-entry (find-own-method object selector
)))
486 (if method-entry
(return method-entry
)))))))
488 (defun add-lsos-method (x selector value
)
489 "x = object; selector = name of method; value = form computing the method."
491 (non-nil-symbol-p selector
)
492 (let ((method-entry (find-own-method x selector
)))
494 (setf (method-entry-method method-entry
) value
)
495 (setf (proto-object-methods x
)
496 (cons (make-method-entry selector value
) (proto-object-methods x
)))))
499 (defun delete-method (x selector
)
501 (setf (proto-object-methods x
)
502 (delete selector
(proto-object-methods x
) :key
#'method-entry-key
)))
504 (defun get-message-method (x selector
&optional no-err
)
506 (let ((method-entry (find-lsos-method x selector
)))
507 (if (method-entry-p method-entry
)
508 (method-entry-method method-entry
)
509 (unless no-err
(error "no method for selector ~s" selector
)))))
511 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
513 ;;; Message Sending Functions
515 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
517 (defvar *current-preclist
* nil
)
518 (defvar *current-selector
* nil
)
520 (defun sendmsg (object selector preclist args
)
521 (let ((method-entry nil
)
524 ;; look for the message in the precedence list
526 (setf method-entry
(find-own-method (first preclist
) selector
))
527 (if (or method-entry
(not (consp preclist
))) (return))
528 (setf preclist
(rest preclist
)))
530 ((null method-entry
) (error "no method for selector ~s" selector
))
531 ((not (method-entry-p method-entry
)) (error "bad method entry"))
532 (t (setf method
(method-entry-method method-entry
))))
535 (let ((*current-preclist
* preclist
)
536 (*current-selector
* selector
)
537 (*proto-self
* object
))
538 (multiple-value-prog1
539 (apply method object args
)
540 #+:constrainthooks
(check-constraint-hooks object selector nil
)))))
542 ;;;; built-in send function
543 (defun send (object selector
&rest args
)
544 "Args: (object selector &rest args)
545 Applies first method for SELECTOR found in OBJECT's precedence list to
547 (sendmsg object selector
(proto-object-preclist object
) args
))
549 ;;;; call-next-proto-method - call inherited version of current method
550 (defun call-next-proto-method (&rest args
)
552 Funcalls next method for current selector and precedence list. Can only be
554 (sendmsg *proto-self
* *current-selector
* (rest *current-preclist
*) args
))
556 (defun call-proto-method (object selector
&rest args
)
557 "Args (object selector &rest args)
558 Funcalls method for SELECTOR found in OBJECT to SELF. Can only be used in
560 Call method belonging to another object on current object."
561 (sendmsg *proto-self
* selector
(proto-object-preclist object
) args
))
563 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
565 ;;; Object Documentation
567 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
569 (defun find-documentation (x sym add
)
570 (if (proto-object-p x
)
571 (let ((doc (find-own-slot x
'documentation
)))
572 (if (and (null doc
) add
) (add-slot x
'documentation nil
))
573 (if (slot-entry-p doc
) (assoc sym
(slot-entry-value doc
))))))
575 (defun add-documentation (x sym value
)
577 (non-nil-symbol-p sym
)
578 (let ((doc-entry (find-documentation x sym t
)))
580 ((not (null doc-entry
))
581 (setf (rest doc-entry
) value
))
585 (cons (cons sym value
)
586 (get-slot-value x
'documentation
))))))
589 (defun get-documentation (x sym
)
591 (dolist (object (proto-object-preclist x
))
592 (let ((doc-entry (find-documentation object sym nil
))) ;; FIXME: verify
593 (if doc-entry
(return (rest doc-entry
))))))
595 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
599 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
601 (defmacro defmeth
(object name arglist first
&rest body
)
602 "Syntax: (defmeth object method-name lambda-list [doc] {form}*)
603 OBJECT must evaluate to an existing object. Installs a method for NAME in
604 the value of OBJECT and installs DOC in OBJECTS's documentation.
605 RETURNS: method-name."
606 (declare (ignorable self
)) ;; hints for the compiler that sometimes it isn't used
607 (if (and body
(stringp first
))
608 `(progn ;; first=docstring + body
609 (add-lsos-method ,object
,name
610 #'(lambda (self ,@arglist
) (block ,name
,@body
)))
611 (add-documentation ,object
,name
,first
)
613 `(progn ;; first=code + body
614 (add-lsos-method ,object
,name
615 #'(lambda (self ,@arglist
) (block ,name
,first
,@body
)))
618 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
620 ;;; Prototype Construction
622 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
624 (defun find-instance-slots (x slots
)
625 (let ((result (nreverse (delete-duplicates (copy-list slots
)))))
626 (dolist (parent (proto-object-parents x
) (nreverse result
))
627 (dolist (slot (get-slot-value parent
'instance-slots
))
628 (pushnew slot result
)))))
630 (defun get-initial-slot-value (object slot
)
631 (let ((entry (find-slot object slot
)))
632 (if (slot-entry-p entry
) (slot-entry-value entry
))))
634 (defun make-prototype (object name ivars cvars doc set
)
635 (setf ivars
(find-instance-slots object ivars
))
636 (add-slot object
'instance-slots ivars
)
637 (add-slot object
'proto-name name
)
639 (add-slot object slot
(get-initial-slot-value object slot
)))
641 (add-slot object slot nil
))
643 (if (and doc
(stringp doc
))
644 (add-documentation object
'proto doc
))
645 (if set
(setf (symbol-value name
) object
)))
648 (defmacro defproto
(name &optional ivars cvars parents doc
)
649 "Syntax (defproto name &optional ivars cvars (parent *proto-object*) doc)
650 Makes a new object prototype with instance variables IVARS, 'class'
651 variables CVARS and parents PARENT. PARENT can be a single object or
652 a list of objects. IVARS and CVARS must be lists."
653 (let ((obsym (gensym))
657 (let* ((,namesym
',name
)
659 (,obsym
(make-basic-object (if (listp ,parsym
)
661 (list ,parsym
)) ;; should this be ,@parsym ?
663 (make-prototype ,obsym
,namesym
,ivars
,cvars
,doc t
)
667 ;; Infrastructure for new defproto from Common-Lisp Cookbook! Thanks!
669 ;(defmacro odd-define (name buildargs)
670 ; `(progn (defun ,(build-symbol make-a- (:< name))
672 ; (vector ,(length buildargs) ',name ,@buildargs))
673 ; (defun ,(build-symbol test-whether- (:< name)) (x)
674 ; (and (vectorp x) (eq (aref x 1) ',name))
675 ; (defun ,(build-symbol (:< name) -copy) (x)
677 ; (defun ,(build-symbol (:< name) -deactivate) (x)
680 ;(defmacro for (listspec exp)
681 ; (cond ((and (= (length listspec) 3)
682 ; (symbolp (car listspec))
683 ; (eq (cadr listspec) ':in))
684 ; `(mapcar (lambda (,(car listspec))
686 ; ,(caddr listspec)))
687 ; (t (error "Ill-formed: ~s" `(for ,listspec ,exp)))))
689 ;(defmacro symstuff (l)
690 ; `(concatenate 'string
695 ; `',(format nil "~a" x))
697 ; `(format nil "~a" ,(cadr x)))
699 ; `(format nil "~a" (incf ,(cadr x))))
701 ; `(format nil "~a" ,x))))))
703 ;(defmacro build-symbol (&rest l)
704 ; (let ((p (find-if (lambda (x)
706 ; (eq (car x) ':package)))
709 ; (setq l (remove p l))))
710 ; (let ((pkg (cond ((eq (cadr p) 'nil)
712 ; (t `(find-package ',(cadr p))))))
715 ; `(values (intern ,(symstuff l) ,pkg)))
717 ; `(make-symbol ,(symstuff l)))))
719 ; `(values (intern ,(symstuff l))))))))
721 (defmacro defproto2
(name &optional ivars cvars parents doc force
)
722 "Syntax (defproto name &optional ivars cvars (parent *proto-object*) doc)
723 Makes a new object prototype with instance variables IVARS, 'class'
724 variables CVARS and parents PARENT. PARENT can be a single object or
725 a list of objects. IVARS and CVARS must be lists. DOC should be a
727 (if (and (boundp name
)
729 (error "Force T to rebind a prototype object.")
730 (let ((obsym (gensym))
733 (defvar ,name
(list) ,doc
)
734 (let* ((,parsym
,parents
)
735 (,obsym
(make-basic-object
738 (list ,@parsym
)) ;; should this be ,@parsym ?
740 (make-prototype ,obsym
,name
,ivars
,cvars
,doc t
)
743 ;; (macro-expand-1 (defproto2 *mytest*))
746 ;; , => turn on evaluation again (not macro substitution)
747 ;; ` => template comes (use , to undo template and restore eval
748 ;; ' => regular quote (not special in this context), 'ted => (quote ted)
751 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
753 ;;; Initialize the Root Object
755 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
757 (setf (proto-object-preclist *proto-object
*) (list *proto-object
*))
758 (add-slot *proto-object
* 'instance-slots nil
)
759 (add-slot *proto-object
* 'proto-name
'*proto-object
*)
760 (add-slot *proto-object
* 'documentation nil
) ; AJR - for SBCL compiler
761 ; issues about macro with
764 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
766 ;;; *PROTO-OBJECT* Methods
768 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
770 (defmeth *proto-object
* :isnew
(&rest args
)
771 "Method args: (&rest args)
772 Checks ARGS for keyword arguments matching slots and uses them to
775 (dolist (slot-entry (proto-object-slots self
))
776 (let* ((slot (slot-entry-key slot-entry
))
777 (key (intern (symbol-name slot
) (find-package 'keyword
)))
778 (val (proto-slot-value slot
))
779 (new-val (getf args key val
)))
780 (unless (eq val new-val
) (setf (proto-slot-value slot
) new-val
)))))
783 (defmeth *proto-object
* :has-slot
(slot &key own
)
784 "Method args: (slot &optional own)
785 Returns T if slot SLOT exists, NIL if not. If OWN is not NIL
786 only checks the object; otherwise check the entire precedence list."
787 (let ((entry (if own
(find-own-slot self slot
) (find-slot self slot
))))
790 (defmeth *proto-object
* :add-slot
(slot &optional value
)
791 "Method args: (slot &optional value)
792 Installs slot SLOT in object, if it does not already exist, and
793 sets its value to VLAUE."
794 (add-slot self slot value
)
797 (defmeth *proto-object
* :delete-slot
(slot)
799 Deletes slot SLOT from object if it exists."
800 (delete-slot self slot
)
803 (defmeth *proto-object
* :own-slots
()
805 Returns list of names of slots owned by object."
806 (mapcar #'slot-entry-key
(proto-object-slots self
)))
808 (defmeth *proto-object
* :has-method
(selector &key own
)
809 "Method args: (selector &optional own)
810 Returns T if method for SELECTOR exists, NIL if not. If OWN is not NIL
811 only checks the object; otherwise check the entire precedence list."
813 (find-own-method self selector
)
814 (find-lsos-method self selector
))))
817 (defmeth *proto-object
* :add-method
(selector method
)
818 "Method args: (selector method)
819 Installs METHOD for SELECTOR in object."
820 (add-lsos-method self selector method
)
823 (defmeth *proto-object
* :delete-method
(selector)
824 "Method args: (selector)
825 Deletes method for SELECTOR in object if it exists."
826 (delete-method self selector
)
829 (defmeth *proto-object
* :get-method
(selector)
830 "Method args: (selector)
831 Returns method for SELECTOR symbol from object's precedence list."
832 (get-message-method self selector
))
834 (defmeth *proto-object
* :own-methods
()
836 Returns copy of selectors for methods owned by object."
837 (mapcar #'method-entry-key
(proto-object-methods self
)))
839 (defmeth *proto-object
* :parents
()
841 Returns copy of parents list."
842 (copy-list (proto-object-parents self
)))
844 (defmeth *proto-object
* :precedence-list
()
846 Returns copy of the precedence list."
847 (copy-list (proto-object-preclist self
)))
849 (defmeth *proto-object
* :show
(&optional
(stream t
))
851 Prints object's internal data."
852 (format stream
"Slots = ~s~%" (proto-object-slots self
))
853 (format stream
"Methods = ~s~%" (proto-object-methods self
))
854 (format stream
"Parents = ~s~%" (proto-object-parents self
))
855 (format stream
"Precedence List = ~s~%" (proto-object-preclist self
))
858 (defmeth *proto-object
* :reparent
(&rest parents
)
859 "Method args: (&rest parents)
860 Changes precedence list to correspond to PARENTS. Does not change descendants."
861 (make-basic-object parents self
))
863 (defmeth *proto-object
* :make-prototype
(name &optional ivars
)
864 (make-prototype self name ivars nil nil nil
)
867 (defmeth *proto-object
* :internal-doc
(sym &optional new
)
868 "Method args (topic &optional value)
869 Retrieves or installs documentation for topic."
870 (if new
(add-documentation self sym new
))
871 (get-documentation self sym
))
873 (defmeth *proto-object
* :new
(&rest args
)
874 "Method args: (&rest args)
875 Creates new object using self as prototype."
876 (let* ((object (make-object self
)))
877 (if (proto-slot-value 'instance-slots
)
878 (dolist (s (proto-slot-value 'instance-slots
))
879 (send object
:add-slot s
(proto-slot-value s
))))
880 (apply #'send object
:isnew args
)
883 (defmeth *proto-object
* :retype
(proto &rest args
)
884 "Method args: (proto &rest args)
885 Changes object to inherit directly from prototype PROTO. PROTO
886 must be a prototype and SELF must not be one."
887 (if (send self
:has-slot
'instance-slots
:own t
)
888 (error "can't retype a prototype"))
889 (if (not (send proto
:has-slot
'instance-slots
:own t
))
890 (error "not a prototype - ~a" proto
))
891 (send self
:reparent proto
)
892 (dolist (s (send proto
:slot-value
'instance-slots
))
893 (send self
:add-slot s
(proto-slot-value s
)))
894 (apply #'send self
:isnew args
)
897 (defmeth *proto-object
* :print
(&optional
(stream *standard-output
*))
898 "Method args: (&optional (stream *standard-output*))
899 Default object printing method."
901 ((send self
:has-slot
'proto-name
)
903 "#<Object: ~D, prototype = ~A>"
904 (proto-object-serial self
)
905 (proto-slot-value 'proto-name
)))
906 (t (format stream
"#<Object: ~D>" (proto-object-serial self
)))))
908 (defmeth *proto-object
* :slot-value
(sym &optional
(val nil set
))
909 "Method args: (sym &optional val)
910 Sets and retrieves value of slot named SYM. Signals an error if slot
912 (if set
(setf (proto-slot-value sym
) val
))
913 (proto-slot-value sym
))
915 (defmeth *proto-object
* :slot-names
()
917 Returns list of slots available to the object."
919 (mapcar #'(lambda (x) (send x
:own-slots
))
920 (send self
:precedence-list
))))
922 (defmeth *proto-object
* :method-selectors
()
924 Returns list of method selectors available to object."
926 (mapcar #'(lambda (x) (send x
:own-methods
))
927 (send self
:precedence-list
))))
930 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
932 ;;;; Object Help Methods
934 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
936 (defmeth *proto-object
* :doc-topics
()
938 Returns all topics with documentation for this object."
944 (if (send x
:has-slot
'documentation
:own t
)
945 (send x
:slot-value
(quote documentation
))))
946 (send self
:precedence-list
))))))
948 (defmeth *proto-object
* :documentation
(topic &optional
(val nil set
))
949 "Method args: (topic &optional val)
950 Retrieves or sets object documentation for topic."
951 (if set
(send self
:internal-doc topic val
))
952 (let ((val (dolist (i (send self
:precedence-list
))
953 (let ((val (send i
:internal-doc topic
)))
954 (if val
(return val
))))))
957 (defmeth *proto-object
* :delete-documentation
(topic)
958 "Method args: (topic)
959 Deletes object documentation for TOPIC."
960 (setf (proto-slot-value 'documentation
)
961 ;;(remove :title nil :test #'(lambda (x y) (eql x (first y)))) ;; original
962 (remove topic
(send self
:documentation
) :test
#'(lambda (x y
) (eql x
(first y
))))) ;; AJR:PROBLEM?
965 (defmeth *proto-object
* :help
(&optional topic
)
966 "Method args: (&optional topic)
967 Prints help message for TOPIC, or genreal help if TOPIC is NIL."
969 (let ((doc (send self
:documentation topic
)))
971 (doc (princ topic
) (terpri) (princ doc
) (terpri))
972 (t (format t
"Sorry, no help available on ~a~%" topic
))))
973 (let ((topics (stable-sort (copy-seq (send self
:doc-topics
))
975 (string-lessp (string x
) (string y
)))))
976 (proto-doc (send self
:documentation
'proto
)))
977 (if (send self
:has-slot
'proto-name
)
978 (format t
"~s~%" (proto-slot-value 'proto-name
)))
979 (when proto-doc
(princ proto-doc
) (terpri))
980 (format t
"Help is available on the following:~%~%")
981 (dolist (i topics
) (format t
"~s " i
))
985 (defmeth *proto-object
* :compile-method
(name)
987 Compiles method NAME unless it is already compiled. The object must
989 (unless (send self
:has-method name
)
990 (error "No ~s method in this object" name
))
991 (unless (send self
:has-method name
:own t
)
992 (error "Object does not own ~s method" name
))
993 (let ((fun (send self
:get-method name
)))
994 (unless (compiled-function-p fun
)
995 (multiple-value-bind (form env
) (function-lambda-expression fun
)
998 "method may have been defined in non-null environment"))
999 (send self
:add-method name
(compile nil form
))))))