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
(obj proto-struct
))
98 (defgeneric remove-object
(obj proto-struct
))
99 (defgeneric objects
(proto-struct))
103 (defclass proto-object
()
107 :accessor proto-object-slots
)
111 :accessor proto-object-methods
)
114 :type proto-object-list
115 :accessor proto-object-parents
)
116 (preclist ;; precedence list
118 :type proto-object-list
119 :accessor proto-object-preclist
120 :documentation
"precedence list." )
122 :initform
(incf *proto-object-serial
*)
124 :documentation
"Similar idea to serial number." )
128 :documentation
"can we embed the global within the class structure?" )))
130 ;; We denote default-ish proto-object variable names by po or po?.
132 (defvar *proto-object
* (make-instance 'proto-object
)
133 "*proto-object* is the global root object.")
135 (defun proto-object-p (x)
137 Returns T if X is an object, NIL otherwise. Do we really need this?"
138 (typep x
'proto-object
))
140 (defun print-proto-object-structure (po stream depth
)
141 (declare (ignore depth
))
142 (send po
:print stream
))
144 ;;; AJR:FIXME:Is this going to cause issues with concurrency/threading?
145 ;;; (need to appropriately handle interrupts).
146 (defvar *proto-self
* nil
147 "special variable to hold current value of SELF.
148 Assign to current object that we are working with. Local to proto package.
149 Currently working to embed within the object structure rather than a global.")
151 ;; The way that self works is that we try to make sure that we set
152 ;; *self* upon message entry and unset at message exit. This is a
153 ;; good strategy provided that concurrency is not in play.
155 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157 ;;; Predicates for Consistency Checking
159 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
161 (defun non-nil-symbol-p (x)
162 (unless (and x
(symbolp x
)) (error "bad symbol - ~s" x
)))
164 (defun check-object (po)
165 "Returns self if true, throws an error otherwise."
166 (if (proto-object-p po
) po
(error "bad object - ~s" x
)))
168 (defun kind-of-p (pox poy
)
170 Returns T if X and Y are objects and X inherits from Y, NIL otherwise."
171 (if (and (proto-object-p pox
) (proto-object-p poy
))
172 (if (member poy
(proto-object-preclist pox
)) t nil
)
175 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
177 ;;; Precedence List Functions
179 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
181 (defgeneric find-SC
(po)
182 "Return a copy of the complete precedence list for po.")
184 (defmethod find-SC ((po proto-object
))
185 (copy-list (proto-object-preclist po
)))
188 (defgeneric find-S
(po)
189 "return a reverse-sorted, duplicate-free list of parent objects.")
191 (defmethod find-S ((po proto-object
))
193 (parents (proto-object-parents po
) (cdr parents
)))
194 ((not (consp parents
))
195 (delete-duplicates (cons po result
)))
196 (setf result
(nconc (find-SC (first parents
)) result
))))
199 (defgeneric find-RC
(po)
200 "find local precedence ordering.")
202 (defmethod find-RC (object)
203 (let ((list (copy-list (proto-object-parents po
))))
204 (do ((next list
(rest next
)))
205 ((not (consp next
)) list
)
206 (setf (first next
) (cons po
(first next
)))
207 (setf object
(rest (first next
))))))
210 (defgeneric find-R
(S)
211 "find partial precedence ordering.")
213 (defmethod find-R ((S proto-object
))
217 (delete-duplicates result
))
218 (setf result
(nconc result
(find-RC (first S
))))))
221 (defun has-predecessor (x R
)
222 "check if x has a predecessor according to R."
224 (if (and (consp cell
) (eq x
(rest cell
))) (return t
))))
226 (defun find-no-predecessor-list (S R
)
227 "find list of objects in S without predecessors, by R."
230 (unless (has-predecessor x R
) (setf result
(cons x result
))))))
232 (defun child-position (x P
)
233 "find the position of child, if any, of x in P, the list found so
236 (declare (fixnum count
))
238 (if (member x
(proto-object-parents next
)) (return count
))
241 (defun next-object (no-preds P
)
242 "find the next object in the precedence list from objects with no
243 predecessor and current list."
245 ((not (consp no-preds
)) nil
)
246 ((not (consp (rest no-preds
))) (first no-preds
))
250 (declare (fixnum count
))
251 (dolist (x no-preds result
)
252 (let ((tcount (child-position x P
)))
253 (declare (fixnum tcount
))
254 (when (> tcount count
)
256 (setf count tcount
))))))))
259 "Remove object x from S."
263 "Remove all pairs containing x from R. x is assumed to have no
264 predecessors, so only the first position is checked."
265 (delete x R
:key
#'first
))
267 (defun precedence-list (object)
268 "Calculate the object's precedence list."
269 (do* ((S (find-S object
))
275 (setf no-preds
(find-no-predecessor-list S R
))
276 (setf next
(next-object no-preds P
))
277 (if (null next
) (error "inconsistent precedence order"))
278 (setf P
(nconc P
(list next
)))
279 (setf S
(trim-S next S
))
280 (setf R
(trim-R next R
))))
282 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
284 ;;; Object Construction Functions
286 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
288 (defun calculate-preclist (object)
289 "Return the precedence list for the object."
290 (let ((parents (proto-object-parents (check-object object
))))
291 (if (not (consp parents
)) (error "bad parent list - ~s" parents
))
292 (if (consp (rest parents
))
293 (precedence-list object
)
294 (let ((parent (check-object (first parents
))))
295 (cons object
(proto-object-preclist parent
))))))
297 (defun has-duplicates (list)
298 "predicate: takes a list, and returns true if duplicates.
299 This should be simpler, right? Used in next function only?"
300 (do ((next list
(rest next
)))
301 ((not (consp next
)) nil
)
302 (if (member (first next
) (rest next
)) (return t
))))
304 (defun check-parents (parents)
305 "Ensure valid parents: They must be null, object, or consp without duplicates."
307 ((or (null parents
) (proto-object-p parents
)) parents
)
309 (dolist (parent parents
) (check-object parent
))
310 (if (has-duplicates parents
)
311 (error "parents may not contain duplicates")))
312 (t (error "bad parents - ~s" parents
))))
314 (defun make-basic-object (parents object
)
315 "Creates a basic object for the prototype system by ensuring that it
316 can be placed into the storage heirarchy.
317 If object is not initialized, instantiate the structure.
318 Place into parental structure.
319 If parents is null, use root *object*,
320 if parents is a single object, use it (encapsulate as list)
321 otherwise, use parents"
323 (check-parents parents
)
324 (if (not (proto-object-p object
))
325 (setf object
(make-instance
327 :preclist
(proto-object-preclist *proto-object
*)
329 (cond ((null parents
) (list *proto-object
*))
330 ((proto-object-p parents
) (list parents
))
332 (setf (proto-object-preclist object
) (calculate-preclist object
))
335 (defun make-object (&rest parents
)
336 "Args: (&rest parents)
337 Returns a new object with parents PARENTS. If PARENTS is NIL,
338 (list *PROTO-OBJECT*) is used."
339 (make-basic-object parents NIL
))
341 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
343 ;;;; Constraint Hook Functions
345 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
347 (pushnew :constrainthooks
*features
*)
351 (defvar *message-hook
* nil
)
352 (defvar *set-slot-hook
* nil
)
354 (defun check-constraint-hooks (object sym slot
)
355 (let ((hook (if slot
*set-slot-hook
* *message-hook
*)))
358 (let ((*set-slot-hook
* nil
))
359 (funcall hook object sym
))
360 (let ((*message-hook
* nil
))
361 (funcall hook object sym
)))))))
363 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
365 ;;; Slot Access Functions
367 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
369 ;;; AJR: should specialize appropriately, the following:
370 (defun make-slot-entry (x y
) (cons x y
))
371 (defun slot-entry-p (x) (consp x
))
372 (defun slot-entry-key (x) (first x
))
373 (defun slot-entry-value (x) (rest x
))
374 (defun set-slot-entry-value (x v
) (setf (rest x
) v
))
375 (defsetf slot-entry-value set-slot-entry-value
)
377 (defun find-own-slot (x slot
)
378 (if (proto-object-p x
) (assoc slot
(proto-object-slots x
))))
380 (defun find-slot (x slot
)
381 (if (proto-object-p x
)
382 (let ((preclist (proto-object-preclist x
)))
383 (dolist (object preclist
)
384 (let ((slot-entry (find-own-slot object slot
)))
385 (if slot-entry
(return slot-entry
)))))))
387 (defun add-slot (x slot value
)
389 (non-nil-symbol-p slot
)
390 (let ((slot-entry (find-own-slot x slot
)))
392 (setf (slot-entry-value slot-entry
) value
)
393 (setf (proto-object-slots x
)
394 (cons (make-slot-entry slot value
) (proto-object-slots x
)))))
397 (defun delete-slot (x slot
)
399 (setf (proto-object-slots x
)
400 (delete slot
(proto-object-slots x
) :key
#'slot-entry-key
)))
402 (defun get-slot-value (x slot
&optional no-err
)
404 (let ((slot-entry (find-slot x slot
)))
405 (if (slot-entry-p slot-entry
)
406 (slot-entry-value slot-entry
)
407 (unless no-err
(error "no slot named ~s in this object" slot
)))))
409 (defun set-slot-value (x slot value
)
411 (let ((slot-entry (find-own-slot x slot
)))
413 ((slot-entry-p slot-entry
)
414 (set-slot-entry-value slot-entry value
)
415 #+:constrainthooks
(check-constraint-hooks x slot t
))
417 (if (find-slot x slot
)
418 (error "object does not own slot ~s" slot
)
419 (error "no slot named ~s in this object" slot
))))))
422 "FIXME? better as macro?."
423 (if (not (proto-object-p *proto-self
*))
424 (error "not in a method"))
427 (defun proto-slot-value (slot)
429 Must be used in a method. Returns the value of current objects slot
431 (get-slot-value (get-self) slot
))
433 (defun proto-slot-value-setf (slot value
)
434 (set-slot-value (get-self) slot value
))
436 (defsetf proto-slot-value proto-slot-value-setf
)
438 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
440 ;;;; Method Access Functions;
442 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
444 (defun make-method-entry (x y
) (cons x y
))
445 (defun method-entry-p (x) (consp x
))
446 (defun method-entry-key (x) (first x
))
447 (defun method-entry-method (x) (rest x
))
448 (defun set-method-entry-method (x v
) (setf (rest x
) v
))
449 (defsetf method-entry-method set-method-entry-method
)
451 (defun find-own-method (x selector
)
452 (if (proto-object-p x
) (assoc selector
(proto-object-methods x
))))
454 (defun find-lsos-method (x selector
)
455 (if (proto-object-p x
)
456 (let ((preclist (proto-object-preclist x
)))
457 (dolist (object preclist
)
458 (let ((method-entry (find-own-method object selector
)))
459 (if method-entry
(return method-entry
)))))))
461 (defun add-lsos-method (x selector value
)
462 "x = object; selector = name of method; value = form computing the method."
464 (non-nil-symbol-p selector
)
465 (let ((method-entry (find-own-method x selector
)))
467 (setf (method-entry-method method-entry
) value
)
468 (setf (proto-object-methods x
)
469 (cons (make-method-entry selector value
) (proto-object-methods x
)))))
472 (defun delete-method (x selector
)
474 (setf (proto-object-methods x
)
475 (delete selector
(proto-object-methods x
) :key
#'method-entry-key
)))
477 (defun get-message-method (x selector
&optional no-err
)
479 (let ((method-entry (find-lsos-method x selector
)))
480 (if (method-entry-p method-entry
)
481 (method-entry-method method-entry
)
482 (unless no-err
(error "no method for selector ~s" selector
)))))
484 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
486 ;;; Message Sending Functions
488 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
490 (defvar *current-preclist
* nil
)
491 (defvar *current-selector
* nil
)
493 (defun sendmsg (object selector preclist args
)
494 (let ((method-entry nil
)
497 ;; look for the message in the precedence list
499 (setf method-entry
(find-own-method (first preclist
) selector
))
500 (if (or method-entry
(not (consp preclist
))) (return))
501 (setf preclist
(rest preclist
)))
503 ((null method-entry
) (error "no method for selector ~s" selector
))
504 ((not (method-entry-p method-entry
)) (error "bad method entry"))
505 (t (setf method
(method-entry-method method-entry
))))
508 (let ((*current-preclist
* preclist
)
509 (*current-selector
* selector
)
510 (*proto-self
* object
))
511 (multiple-value-prog1
512 (apply method object args
)
513 #+:constrainthooks
(check-constraint-hooks object selector nil
)))))
515 ;;;; built-in send function
516 (defun send (object selector
&rest args
)
517 "Args: (object selector &rest args)
518 Applies first method for SELECTOR found in OBJECT's precedence list to
520 (sendmsg object selector
(proto-object-preclist object
) args
))
522 ;;;; call-next-proto-method - call inherited version of current method
523 (defun call-next-proto-method (&rest args
)
525 Funcalls next method for current selector and precedence list. Can only be
527 (sendmsg *proto-self
* *current-selector
* (rest *current-preclist
*) args
))
529 (defun call-proto-method (object selector
&rest args
)
530 "Args (object selector &rest args)
531 Funcalls method for SELECTOR found in OBJECT to SELF. Can only be used in
533 Call method belonging to another object on current object."
534 (sendmsg *proto-self
* selector
(proto-object-preclist object
) args
))
536 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
538 ;;; Object Documentation
540 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
542 (defun find-documentation (x sym add
)
543 (if (proto-object-p x
)
544 (let ((doc (find-own-slot x
'documentation
)))
545 (if (and (null doc
) add
) (add-slot x
'documentation nil
))
546 (if (slot-entry-p doc
) (assoc sym
(slot-entry-value doc
))))))
548 (defun add-documentation (x sym value
)
550 (non-nil-symbol-p sym
)
551 (let ((doc-entry (find-documentation x sym t
)))
553 ((not (null doc-entry
))
554 (setf (rest doc-entry
) value
))
558 (cons (cons sym value
)
559 (get-slot-value x
'documentation
))))))
562 (defun get-documentation (x sym
)
564 (dolist (object (proto-object-preclist x
))
565 (let ((doc-entry (find-documentation object sym nil
))) ;; FIXME: verify
566 (if doc-entry
(return (rest doc-entry
))))))
568 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
572 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
574 (defmacro defmeth
(object name arglist first
&rest body
)
575 "Syntax: (defmeth object method-name lambda-list [doc] {form}*)
576 OBJECT must evaluate to an existing object. Installs a method for NAME in
577 the value of OBJECT and installs DOC in OBJECTS's documentation.
578 RETURNS: method-name."
579 (declare (ignorable self
)) ;; hints for the compiler that sometimes it isn't used
580 (if (and body
(stringp first
))
581 `(progn ;; first=docstring + body
582 (add-lsos-method ,object
,name
583 #'(lambda (self ,@arglist
) (block ,name
,@body
)))
584 (add-documentation ,object
,name
,first
)
586 `(progn ;; first=code + body
587 (add-lsos-method ,object
,name
588 #'(lambda (self ,@arglist
) (block ,name
,first
,@body
)))
591 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
593 ;;; Prototype Construction
595 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
597 (defun find-instance-slots (x slots
)
598 (let ((result (nreverse (delete-duplicates (copy-list slots
)))))
599 (dolist (parent (proto-object-parents x
) (nreverse result
))
600 (dolist (slot (get-slot-value parent
'instance-slots
))
601 (pushnew slot result
)))))
603 (defun get-initial-slot-value (object slot
)
604 (let ((entry (find-slot object slot
)))
605 (if (slot-entry-p entry
) (slot-entry-value entry
))))
607 (defun make-prototype (object name ivars cvars doc set
)
608 (setf ivars
(find-instance-slots object ivars
))
609 (add-slot object
'instance-slots ivars
)
610 (add-slot object
'proto-name name
)
612 (add-slot object slot
(get-initial-slot-value object slot
)))
614 (add-slot object slot nil
))
616 (if (and doc
(stringp doc
))
617 (add-documentation object
'proto doc
))
618 (if set
(setf (symbol-value name
) object
)))
621 (defmacro defproto
(name &optional ivars cvars parents doc
)
622 "Syntax (defproto name &optional ivars cvars (parent *proto-object*) doc)
623 Makes a new object prototype with instance variables IVARS, 'class'
624 variables CVARS and parents PARENT. PARENT can be a single object or
625 a list of objects. IVARS and CVARS must be lists."
626 (let ((obsym (gensym))
630 (let* ((,namesym
',name
)
632 (,obsym
(make-basic-object (if (listp ,parsym
)
634 (list ,parsym
)) ;; should this be ,@parsym ?
636 (make-prototype ,obsym
,namesym
,ivars
,cvars
,doc t
)
640 ;; Infrastructure for new defproto from Common-Lisp Cookbook! Thanks!
642 ;(defmacro odd-define (name buildargs)
643 ; `(progn (defun ,(build-symbol make-a- (:< name))
645 ; (vector ,(length buildargs) ',name ,@buildargs))
646 ; (defun ,(build-symbol test-whether- (:< name)) (x)
647 ; (and (vectorp x) (eq (aref x 1) ',name))
648 ; (defun ,(build-symbol (:< name) -copy) (x)
650 ; (defun ,(build-symbol (:< name) -deactivate) (x)
653 ;(defmacro for (listspec exp)
654 ; (cond ((and (= (length listspec) 3)
655 ; (symbolp (car listspec))
656 ; (eq (cadr listspec) ':in))
657 ; `(mapcar (lambda (,(car listspec))
659 ; ,(caddr listspec)))
660 ; (t (error "Ill-formed: ~s" `(for ,listspec ,exp)))))
662 ;(defmacro symstuff (l)
663 ; `(concatenate 'string
668 ; `',(format nil "~a" x))
670 ; `(format nil "~a" ,(cadr x)))
672 ; `(format nil "~a" (incf ,(cadr x))))
674 ; `(format nil "~a" ,x))))))
676 ;(defmacro build-symbol (&rest l)
677 ; (let ((p (find-if (lambda (x)
679 ; (eq (car x) ':package)))
682 ; (setq l (remove p l))))
683 ; (let ((pkg (cond ((eq (cadr p) 'nil)
685 ; (t `(find-package ',(cadr p))))))
688 ; `(values (intern ,(symstuff l) ,pkg)))
690 ; `(make-symbol ,(symstuff l)))))
692 ; `(values (intern ,(symstuff l))))))))
694 (defmacro defproto2
(name &optional ivars cvars parents doc force
)
695 "Syntax (defproto name &optional ivars cvars (parent *proto-object*) doc)
696 Makes a new object prototype with instance variables IVARS, 'class'
697 variables CVARS and parents PARENT. PARENT can be a single object or
698 a list of objects. IVARS and CVARS must be lists. DOC should be a
700 (if (and (boundp name
)
702 (error "Force T to rebind a prototype object.")
703 (let ((obsym (gensym))
706 (defvar ,name
(list) ,doc
)
707 (let* ((,parsym
,parents
)
708 (,obsym
(make-basic-object
711 (list ,@parsym
)) ;; should this be ,@parsym ?
713 (make-prototype ,obsym
,name
,ivars
,cvars
,doc t
)
716 ;; (macro-expand-1 (defproto2 *mytest*))
719 ;; , => turn on evaluation again (not macro substitution)
720 ;; ` => template comes (use , to undo template and restore eval
721 ;; ' => regular quote (not special in this context), 'ted => (quote ted)
724 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
726 ;;; Initialize the Root Object
728 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
730 (setf (proto-object-preclist *proto-object
*) (list *proto-object
*))
731 (add-slot *proto-object
* 'instance-slots nil
)
732 (add-slot *proto-object
* 'proto-name
'*proto-object
*)
733 (add-slot *proto-object
* 'documentation nil
) ; AJR - for SBCL compiler
734 ; issues about macro with
737 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
739 ;;; *PROTO-OBJECT* Methods
741 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
743 (defmeth *proto-object
* :isnew
(&rest args
)
744 "Method args: (&rest args)
745 Checks ARGS for keyword arguments matching slots and uses them to
748 (dolist (slot-entry (proto-object-slots self
))
749 (let* ((slot (slot-entry-key slot-entry
))
750 (key (intern (symbol-name slot
) (find-package 'keyword
)))
751 (val (proto-slot-value slot
))
752 (new-val (getf args key val
)))
753 (unless (eq val new-val
) (setf (proto-slot-value slot
) new-val
)))))
756 (defmeth *proto-object
* :has-slot
(slot &key own
)
757 "Method args: (slot &optional own)
758 Returns T if slot SLOT exists, NIL if not. If OWN is not NIL
759 only checks the object; otherwise check the entire precedence list."
760 (let ((entry (if own
(find-own-slot self slot
) (find-slot self slot
))))
763 (defmeth *proto-object
* :add-slot
(slot &optional value
)
764 "Method args: (slot &optional value)
765 Installs slot SLOT in object, if it does not already exist, and
766 sets its value to VLAUE."
767 (add-slot self slot value
)
770 (defmeth *proto-object
* :delete-slot
(slot)
772 Deletes slot SLOT from object if it exists."
773 (delete-slot self slot
)
776 (defmeth *proto-object
* :own-slots
()
778 Returns list of names of slots owned by object."
779 (mapcar #'slot-entry-key
(proto-object-slots self
)))
781 (defmeth *proto-object
* :has-method
(selector &key own
)
782 "Method args: (selector &optional own)
783 Returns T if method for SELECTOR exists, NIL if not. If OWN is not NIL
784 only checks the object; otherwise check the entire precedence list."
786 (find-own-method self selector
)
787 (find-lsos-method self selector
))))
790 (defmeth *proto-object
* :add-method
(selector method
)
791 "Method args: (selector method)
792 Installs METHOD for SELECTOR in object."
793 (add-lsos-method self selector method
)
796 (defmeth *proto-object
* :delete-method
(selector)
797 "Method args: (selector)
798 Deletes method for SELECTOR in object if it exists."
799 (delete-method self selector
)
802 (defmeth *proto-object
* :get-method
(selector)
803 "Method args: (selector)
804 Returns method for SELECTOR symbol from object's precedence list."
805 (get-message-method self selector
))
807 (defmeth *proto-object
* :own-methods
()
809 Returns copy of selectors for methods owned by object."
810 (mapcar #'method-entry-key
(proto-object-methods self
)))
812 (defmeth *proto-object
* :parents
()
814 Returns copy of parents list."
815 (copy-list (proto-object-parents self
)))
817 (defmeth *proto-object
* :precedence-list
()
819 Returns copy of the precedence list."
820 (copy-list (proto-object-preclist self
)))
822 (defmeth *proto-object
* :show
(&optional
(stream t
))
824 Prints object's internal data."
825 (format stream
"Slots = ~s~%" (proto-object-slots self
))
826 (format stream
"Methods = ~s~%" (proto-object-methods self
))
827 (format stream
"Parents = ~s~%" (proto-object-parents self
))
828 (format stream
"Precedence List = ~s~%" (proto-object-preclist self
))
831 (defmeth *proto-object
* :reparent
(&rest parents
)
832 "Method args: (&rest parents)
833 Changes precedence list to correspond to PARENTS. Does not change descendants."
834 (make-basic-object parents self
))
836 (defmeth *proto-object
* :make-prototype
(name &optional ivars
)
837 (make-prototype self name ivars nil nil nil
)
840 (defmeth *proto-object
* :internal-doc
(sym &optional new
)
841 "Method args (topic &optional value)
842 Retrieves or installs documentation for topic."
843 (if new
(add-documentation self sym new
))
844 (get-documentation self sym
))
846 (defmeth *proto-object
* :new
(&rest args
)
847 "Method args: (&rest args)
848 Creates new object using self as prototype."
849 (let* ((object (make-object self
)))
850 (if (proto-slot-value 'instance-slots
)
851 (dolist (s (proto-slot-value 'instance-slots
))
852 (send object
:add-slot s
(proto-slot-value s
))))
853 (apply #'send object
:isnew args
)
856 (defmeth *proto-object
* :retype
(proto &rest args
)
857 "Method args: (proto &rest args)
858 Changes object to inherit directly from prototype PROTO. PROTO
859 must be a prototype and SELF must not be one."
860 (if (send self
:has-slot
'instance-slots
:own t
)
861 (error "can't retype a prototype"))
862 (if (not (send proto
:has-slot
'instance-slots
:own t
))
863 (error "not a prototype - ~a" proto
))
864 (send self
:reparent proto
)
865 (dolist (s (send proto
:slot-value
'instance-slots
))
866 (send self
:add-slot s
(proto-slot-value s
)))
867 (apply #'send self
:isnew args
)
870 (defmeth *proto-object
* :print
(&optional
(stream *standard-output
*))
871 "Method args: (&optional (stream *standard-output*))
872 Default object printing method."
874 ((send self
:has-slot
'proto-name
)
876 "#<Object: ~D, prototype = ~A>"
877 (proto-object-serial self
)
878 (proto-slot-value 'proto-name
)))
879 (t (format stream
"#<Object: ~D>" (proto-object-serial self
)))))
881 (defmeth *proto-object
* :slot-value
(sym &optional
(val nil set
))
882 "Method args: (sym &optional val)
883 Sets and retrieves value of slot named SYM. Signals an error if slot
885 (if set
(setf (proto-slot-value sym
) val
))
886 (proto-slot-value sym
))
888 (defmeth *proto-object
* :slot-names
()
890 Returns list of slots available to the object."
892 (mapcar #'(lambda (x) (send x
:own-slots
))
893 (send self
:precedence-list
))))
895 (defmeth *proto-object
* :method-selectors
()
897 Returns list of method selectors available to object."
899 (mapcar #'(lambda (x) (send x
:own-methods
))
900 (send self
:precedence-list
))))
903 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
905 ;;;; Object Help Methods
907 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
909 (defmeth *proto-object
* :doc-topics
()
911 Returns all topics with documentation for this object."
917 (if (send x
:has-slot
'documentation
:own t
)
918 (send x
:slot-value
(quote documentation
))))
919 (send self
:precedence-list
))))))
921 (defmeth *proto-object
* :documentation
(topic &optional
(val nil set
))
922 "Method args: (topic &optional val)
923 Retrieves or sets object documentation for topic."
924 (if set
(send self
:internal-doc topic val
))
925 (let ((val (dolist (i (send self
:precedence-list
))
926 (let ((val (send i
:internal-doc topic
)))
927 (if val
(return val
))))))
930 (defmeth *proto-object
* :delete-documentation
(topic)
931 "Method args: (topic)
932 Deletes object documentation for TOPIC."
933 (setf (proto-slot-value 'documentation
)
934 ;;(remove :title nil :test #'(lambda (x y) (eql x (first y)))) ;; original
935 (remove topic
(send self
:documentation
) :test
#'(lambda (x y
) (eql x
(first y
))))) ;; AJR:PROBLEM?
938 (defmeth *proto-object
* :help
(&optional topic
)
939 "Method args: (&optional topic)
940 Prints help message for TOPIC, or genreal help if TOPIC is NIL."
942 (let ((doc (send self
:documentation topic
)))
944 (doc (princ topic
) (terpri) (princ doc
) (terpri))
945 (t (format t
"Sorry, no help available on ~a~%" topic
))))
946 (let ((topics (stable-sort (copy-seq (send self
:doc-topics
))
948 (string-lessp (string x
) (string y
)))))
949 (proto-doc (send self
:documentation
'proto
)))
950 (if (send self
:has-slot
'proto-name
)
951 (format t
"~s~%" (proto-slot-value 'proto-name
)))
952 (when proto-doc
(princ proto-doc
) (terpri))
953 (format t
"Help is available on the following:~%~%")
954 (dolist (i topics
) (format t
"~s " i
))
958 (defmeth *proto-object
* :compile-method
(name)
960 Compiles method NAME unless it is already compiled. The object must
962 (unless (send self
:has-method name
)
963 (error "No ~s method in this object" name
))
964 (unless (send self
:has-method name
:own t
)
965 (error "Object does not own ~s method" name
))
966 (let ((fun (send self
:get-method name
)))
967 (unless (compiled-function-p fun
)
968 (multiple-value-bind (form env
) (function-lambda-expression fun
)
971 "method may have been defined in non-null environment"))
972 (send self
:add-method name
(compile nil form
))))))