lift to ASDF section for autoload.
[CommonLispStat.git] / lsobjects.lsp
blob9aee7e13b6facdae31ab09beb75852b750d8f5ee
1 ;;; -*- mode: lisp -*-
2 ;;; Copyright (c) 2005--2007, by A.J. Rossini <blindglobe@gmail.com>
3 ;;; See COPYRIGHT file for any additional restrictions (BSD license).
4 ;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp.
6 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;;;
9 ;;;; LISP-STAT Object System
10 ;;;;
11 ;;;;
12 ;;;; Simple CL implementation of the object system for Lisp-Stat (LSOS)
13 ;;;; as described in Tierney (1990).
14 ;;;;
15 ;;;; Copyright (c) 1991, by Luke Tierney. Permission is granted for
16 ;;;; unrestricted use.
17 ;;;;
18 ;;;;
19 ;;;; NOTES:
20 ;;;;
21 ;;;; If your CL's handling of packages is compliant with CLtL, 2nd
22 ;;;; Edition (like Macintosh CL version 2), add the feature :CLtL2
23 ;;;; before loading or compiling this code.
24 ;;;;
25 ;;;; This implementation does not make use of CLOS. It can coexist
26 ;;;; with CLOS, but there are two name conflicts: slot-value and
27 ;;;; call-next-method. These two symbols are shadowed in the LSOS
28 ;;;; package and must be shadowed in any package that uses LSOS.
29 ;;;; Evaluating the function (lsos::use-lsos) from a package after
30 ;;;; loading this code shadows these two symbols and does a
31 ;;;; use-package for LSOS.
32 ;;;;
33 ;;;; The :compile-method method uses function-lambda-expression
34 ;;;; defined in CLtL, 2nd Edition. (This method is only needed if
35 ;;;; you want to force compilation of an interpreted method. It is
36 ;;;; not used by the compiler.)
37 ;;;;
38 ;;;; The efficiency of this code could be improved by low level
39 ;;;; coding of the dispatching functions send, call-method and
40 ;;;; call-next-method to avoid creating an argument list. Other
41 ;;;; efficiency improvements are possible as well, in particular
42 ;;;; by good use of declarations. It may also be possible to build
43 ;;;; a more efficient implementation using the CLOS metaclass
44 ;;;; protocol.
45 ;;;;
46 ;;;; There are a few minimal tools for experimenting with constraints
47 ;;;; in the code; they are marked by #+:constreinthooks. Sometime
48 ;;;; soon I hope to augment or replace these hooks with a CORAL-like
49 ;;;; constraint system (as used in GARNET).
50 ;;;;
51 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
52 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
55 ;;; Package Setup
57 (in-package :cl-user)
59 (defpackage :lisp-stat-object-system
60 (:nicknames :ls-objects :lsos)
61 (:use :common-lisp)
62 (:shadow :call-method :call-next-method :slot-value)
63 (:export ls-object objectp *object* kind-of-p make-object *message-hook*
64 *set-slot-hook* slot-value self send call-next-method call-method
65 defmeth defproto defproto2
66 instance-slots proto-name))
68 (in-package :lisp-stat-object-system)
70 (defun use-lsos ()
71 "Formerly set up to import lisp-stat-object-system into current package."
72 (shadowing-import (package-shadowing-symbols 'lisp-stat-object-system))
73 (use-package 'lisp-stat-object-system))
75 ;;; Structure Implementation of Lisp-Stat Object System
77 ;; We might consider a global rewrite if it doesn't seem to break
78 ;; anything. In particular, the real name ought to be
79 ;; proto-sys-object or similar so that we can ensure that the right
80 ;; interpretation is made for this. Call it the prototype object
81 ;; system, and possibly be done with it then.
83 (defvar *object-serial* 0)
85 (defstruct (ls-object
86 (:constructor make-object-structure) ;; why not make-ls-object?
87 (:print-function print-object-structure)
88 (:predicate objectp)) ;; why not ls-object-p?
89 slots
90 methods
91 parents
92 preclist ;; precedence list
93 (serial (incf *object-serial*)))
95 (defun print-object-structure (object stream depth)
96 (declare (ignore depth))
97 (send object :print stream))
99 (setf (documentation 'objectp 'function)
100 "Args: (x)
101 Returns T if X is an object, NIL otherwise.")
103 (defvar *object* (make-object-structure)
104 "*object* is the global root object.")
106 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
107 ;;;;
108 ;;;; Utilities
109 ;;;;
110 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
112 ;;;
113 ;;; AJR:FIXME:Is this going to cause issues with concurrency/threading?
114 ;;; (need to appropriately handle interrupts).
115 (defvar *self* nil
116 "special variable to hold current value of SELF.
117 Assign to current object that we are working with.")
119 (defun get-self ()
120 "FIXME? better as macro?."
121 (if (not (objectp *self*))
122 (error "not in a method"))
123 *self*)
125 (defun has-duplicates (list)
126 "predicate: takes a list, and returns true if duplicates.
127 This should be simpler, right?"
128 (do ((next list (rest next)))
129 ((not (consp next)) nil)
130 (if (member (first next) (rest next)) (return t))))
132 (defun assoc-eq (item alist)
133 "Version of assoc using eq -- should be faster than regular assoc."
134 (declare (inline car eq))
135 (dolist (i alist)
136 (if (eq (car i) item) (return i))))
138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
140 ;;; Predicates for Consistency Checking
142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
144 (defun check-non-nil-symbol (x)
145 (unless (and x (symbolp x)) (error "bad symbol - ~s" x)))
147 (defun check-object (x)
148 "Returns self if true, throws an error otherwise."
149 (if (objectp x) x (error "bad object - ~s" x)))
151 (defun kind-of-p (x y)
152 "Args: (x y)
153 Returns T if X and Y are objects and X inherits from Y, NIL otherwise."
154 (if (and (objectp x) (objectp y))
155 (if (member y (ls-object-preclist x)) t nil)
156 nil))
158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159 ;;;;
160 ;;;; Precedence List Functions
161 ;;;;
162 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164 (defun find-SC (object)
165 "find set of object and ancestors. (diff from this and find-S?)"
166 (copy-list (ls-object-preclist (check-object object))))
168 (defun find-S (object)
169 "find set of object and ancestors. (diff from this and find-SC?)"
170 (do ((result nil)
171 (parents (ls-object-parents object) (cdr parents)))
172 ((not (consp parents))
173 (delete-duplicates (cons object result)))
174 (setf result (nconc (find-SC (first parents)) result))))
176 (defun find-RC (object)
177 "find local precedence ordering."
178 (let ((list (copy-list (ls-object-parents (check-object object)))))
179 (do ((next list (rest next)))
180 ((not (consp next)) list)
181 (setf (first next) (cons object (first next)))
182 (setf object (rest (first next))))))
184 (defun find-R (S)
185 "find partial precedence ordering."
186 (do ((result nil)
187 (S S (rest S)))
188 ((not (consp S))
189 (delete-duplicates result))
190 (setf result (nconc result (find-RC (first S))))))
192 (defun has-predecessor (x R)
193 "check if x has a predecessor according to R."
194 (dolist (cell R nil)
195 (if (and (consp cell) (eq x (rest cell))) (return t))))
197 (defun find-no-predecessor-list (S R)
198 "find list of objects in S without predecessors, by R."
199 (let ((result nil))
200 (dolist (x S result)
201 (unless (has-predecessor x R) (setf result (cons x result))))))
203 (defun child-position (x P)
204 "find the position of child, if any, of x in P, the list found so
205 far."
206 (let ((count 0))
207 (declare (fixnum count))
208 (dolist (next P -1)
209 (if (member x (ls-object-parents next)) (return count))
210 (incf count))))
212 (defun next-object (no-preds P)
213 "find the next object in the precedence list from objects with no
214 predecessor and current list."
215 (cond
216 ((not (consp no-preds)) nil)
217 ((not (consp (rest no-preds))) (first no-preds))
219 (let ((count -1)
220 (result nil))
221 (declare (fixnum count))
222 (dolist (x no-preds result)
223 (let ((tcount (child-position x P)))
224 (declare (fixnum tcount))
225 (when (> tcount count)
226 (setf result x)
227 (setf count tcount))))))))
229 (defun trim-S (x S)
230 "Remove object x from S."
231 (delete x S))
233 (defun trim-R (x R)
234 "Remove all pairs containing x from R. x is assumed to have no
235 predecessors, so only the first position is checked."
236 (delete x R :key #'first))
238 (defun precedence-list (object)
239 "Calculate the object's precedence list."
240 (do* ((S (find-S object))
241 (R (find-R S))
242 (P nil)
243 (no-preds nil)
244 (next nil))
245 ((not (consp S)) P)
246 (setf no-preds (find-no-predecessor-list S R))
247 (setf next (next-object no-preds P))
248 (if (null next) (error "inconsistent precedence order"))
249 (setf P (nconc P (list next)))
250 (setf S (trim-S next S))
251 (setf R (trim-R next R))))
253 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
254 ;;;;
255 ;;;; Object Construction Functions
256 ;;;;
257 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
259 (defun calculate-preclist (object)
260 "Return the precedence list for the object."
261 (let ((parents (ls-object-parents (check-object object))))
262 (if (not (consp parents)) (error "bad parent list - ~s" parents))
263 (if (consp (rest parents))
264 (precedence-list object)
265 (let ((parent (check-object (first parents))))
266 (cons object (ls-object-preclist parent))))))
268 (defun check-parents (parents)
269 "Ensure valid parents: They must be null, object, or consp without duplicates."
270 (cond
271 ((or (null parents) (objectp parents)) parents)
272 ((consp parents)
273 (dolist (x parents) (check-object x))
274 (if (has-duplicates parents)
275 (error "parents may not contain duplicates")))
276 (t (error "bad parents - ~s" parents))))
278 (defun make-basic-object (parents object)
279 "Creates a basic object for the prototype system by ensuring that it
280 can be placed into the storage heirarchy.
281 If object is not initialized, instantiate the structure.
282 Place into parental structure.
283 If parents is null, use root *object*,
284 if parents is a single object, use it (encapsulate as list)
285 otherwise, use parents"
287 (check-parents parents)
289 (if (not (objectp object)) (setf object (make-object-structure)))
291 (setf (ls-object-preclist object) (ls-object-preclist *object*))
292 (setf (ls-object-parents object)
293 (cond ((null parents) (list *object*))
294 ((objectp parents) (list parents))
295 (t parents)))
296 (setf (ls-object-preclist object) (calculate-preclist object))
298 object)
300 (defun make-object (&rest parents)
301 "Args: (&rest parents)
302 Returns a new object with parents PARENTS. If PARENTS is NIL, (list *OBJECT*) is used."
303 (make-basic-object parents NIL))
305 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
306 ;;;;
307 ;;;; Constraint Hook Functions
308 ;;;;
309 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
311 (pushnew :constrainthooks *features*)
313 #+:constrainthooks
314 (progn
315 (defvar *message-hook* nil)
316 (defvar *set-slot-hook* nil)
318 (defun check-constraint-hooks (object sym slot)
319 (let ((hook (if slot *set-slot-hook* *message-hook*)))
320 (if hook
321 (if slot
322 (let ((*set-slot-hook* nil))
323 (funcall hook object sym))
324 (let ((*message-hook* nil))
325 (funcall hook object sym)))))))
327 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
329 ;;; Slot Access Functions
331 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
333 (defun make-slot-entry (x y) (cons x y))
334 (defun slot-entry-p (x) (consp x))
335 (defun slot-entry-key (x) (first x))
336 (defun slot-entry-value (x) (rest x))
337 (defun set-slot-entry-value (x v) (setf (rest x) v))
338 (defsetf slot-entry-value set-slot-entry-value)
340 (defun find-own-slot (x slot)
341 (if (objectp x) (assoc-eq slot (ls-object-slots x))))
343 (defun find-slot (x slot)
344 (if (objectp x)
345 (let ((preclist (ls-object-preclist x)))
346 (dolist (object preclist)
347 (let ((slot-entry (find-own-slot object slot)))
348 (if slot-entry (return slot-entry)))))))
350 (defun add-slot (x slot value)
351 (check-object x)
352 (check-non-nil-symbol slot)
353 (let ((slot-entry (find-own-slot x slot)))
354 (if slot-entry
355 (setf (slot-entry-value slot-entry) value)
356 (setf (ls-object-slots x)
357 (cons (make-slot-entry slot value) (ls-object-slots x)))))
358 nil)
360 (defun delete-slot (x slot)
361 (check-object x)
362 (setf (ls-object-slots x)
363 (delete slot (ls-object-slots x) :key #'slot-entry-key)))
365 (defun get-slot-value (x slot &optional no-err)
366 (check-object x)
367 (let ((slot-entry (find-slot x slot)))
368 (if (slot-entry-p slot-entry)
369 (slot-entry-value slot-entry)
370 (unless no-err (error "no slot named ~s in this object" slot)))))
372 (defun set-slot-value (x slot value)
373 (check-object x)
374 (let ((slot-entry (find-own-slot x slot)))
375 (cond
376 ((slot-entry-p slot-entry)
377 (set-slot-entry-value slot-entry value)
378 #+:constrainthooks (check-constraint-hooks x slot t))
380 (if (find-slot x slot)
381 (error "object does not own slot ~s" slot)
382 (error "no slot named ~s in this object" slot))))))
384 (defun slot-value (slot)
385 "Args: (slot)
386 Must be used in a method. Returns the value of current objects slot
387 named SLOT."
388 (get-slot-value (get-self) slot))
390 (defun slot-value-setf (slot value)
391 (set-slot-value (get-self) slot value))
393 (defsetf slot-value slot-value-setf)
395 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
396 ;;;;
397 ;;;; Method Access Functions;
398 ;;;;
399 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
401 (defun make-method-entry (x y) (cons x y))
402 (defun method-entry-p (x) (consp x))
403 (defun method-entry-key (x) (first x))
404 (defun method-entry-method (x) (rest x))
405 (defun set-method-entry-method (x v) (setf (rest x) v))
406 (defsetf method-entry-method set-method-entry-method)
408 (defun find-own-method (x selector)
409 (if (objectp x) (assoc-eq selector (ls-object-methods x)))) ;; prev was assoc not assoc-eq
411 (defun find-lsos-method (x selector)
412 (if (objectp x)
413 (let ((preclist (ls-object-preclist x)))
414 (dolist (object preclist)
415 (let ((method-entry (find-own-method object selector)))
416 (if method-entry (return method-entry)))))))
418 (defun add-lsos-method (x selector value)
419 "x = object; selector = name of method; value = form computing the method."
420 (check-object x)
421 (check-non-nil-symbol selector)
422 (let ((method-entry (find-own-method x selector)))
423 (if method-entry
424 (setf (method-entry-method method-entry) value)
425 (setf (ls-object-methods x)
426 (cons (make-method-entry selector value) (ls-object-methods x)))))
427 nil)
429 (defun delete-method (x selector)
430 (check-object x)
431 (setf (ls-object-methods x)
432 (delete selector (ls-object-methods x) :key #'method-entry-key)))
434 (defun get-message-method (x selector &optional no-err)
435 (check-object x)
436 (let ((method-entry (find-lsos-method x selector)))
437 (if (method-entry-p method-entry)
438 (method-entry-method method-entry)
439 (unless no-err (error "no method for selector ~s" selector)))))
441 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
443 ;;; Message Sending Functions
445 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
447 (defvar *current-preclist* nil)
448 (defvar *current-selector* nil)
450 (defun sendmsg (object selector preclist args)
451 (let ((method-entry nil)
452 (method nil))
454 ;; look for the message in the precedence list
455 (loop
456 (setf method-entry (find-own-method (first preclist) selector))
457 (if (or method-entry (not (consp preclist))) (return))
458 (setf preclist (rest preclist)))
459 (cond
460 ((null method-entry) (error "no method for selector ~s" selector))
461 ((not (method-entry-p method-entry)) (error "bad method entry"))
462 (t (setf method (method-entry-method method-entry))))
464 ;; invoke the method
465 (let ((*current-preclist* preclist)
466 (*current-selector* selector)
467 (*self* object))
468 (multiple-value-prog1
469 (apply method object args)
470 #+:constrainthooks (check-constraint-hooks object selector nil)))))
472 ;;;; built-in send function
473 (defun send (object selector &rest args)
474 "Args: (object selector &rest args)
475 Applies first method for SELECTOR found in OBJECT's precedence list to
476 OBJECT and ARGS."
477 (sendmsg object selector (ls-object-preclist object) args))
479 ;;;; call-next-method - call inherited version of current method
480 (defun call-next-method (&rest args)
481 "Args (&rest args)
482 Funcalls next method for current selector and precedence list. Can only be
483 used in a method."
484 (sendmsg *self* *current-selector* (rest *current-preclist*) args))
486 (defun call-method (object selector &rest args)
487 "Args (object selector &rest args)
488 Funcalls method for SELECTOR found in OBJECT to SELF. Can only be used in
489 a method.
490 Call method belonging to another object on current object."
491 (sendmsg *self* selector (ls-object-preclist object) args))
493 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
495 ;;; Object Documentation
497 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
499 (defun find-documentation (x sym add)
500 (if (objectp x)
501 (let ((doc (find-own-slot x 'documentation)))
502 (if (and (null doc) add) (add-slot x 'documentation nil))
503 (if (slot-entry-p doc) (assoc sym (slot-entry-value doc))))))
505 (defun add-documentation (x sym value)
506 (check-object x)
507 (check-non-nil-symbol sym)
508 (let ((doc-entry (find-documentation x sym t)))
509 (cond
510 ((not (null doc-entry))
511 (setf (rest doc-entry) value))
513 (set-slot-value x
514 'documentation
515 (cons (cons sym value)
516 (get-slot-value x 'documentation))))))
517 nil)
519 (defun get-documentation (x sym)
520 (check-object x)
521 (dolist (object (ls-object-preclist x))
522 (let ((doc-entry (find-documentation object sym nil))) ;; FIXME: verify
523 (if doc-entry (return (rest doc-entry))))))
525 ;;;;
526 ;;;; DEFMETH Macro
527 ;;;;
529 (defmacro defmeth (object name arglist first &rest body)
530 "Syntax: (defmeth object method-name lambda-list [doc] {form}*)
531 OBJECT must evaluate to an existing object. Installs a method for NAME in
532 the value of OBJECT and installs DOC in OBJECTS's documentation.
533 RETURNS: method-name."
534 (declare (ignorable self)) ;; hints for the compiler that sometimes it isn't used
535 (if (and body (stringp first))
536 `(progn ;; first=docstring + body
537 (add-lsos-method ,object ,name
538 #'(lambda (self ,@arglist) (block ,name ,@body)))
539 (add-documentation ,object ,name ,first)
540 ,name)
541 `(progn ;; first=code + body
542 (add-lsos-method ,object ,name
543 #'(lambda (self ,@arglist) (block ,name ,first ,@body)))
544 ,name)))
547 ;;; Prototype Construction
550 (defun find-instance-slots (x slots)
551 (let ((result (nreverse (delete-duplicates (copy-list slots)))))
552 (dolist (parent (ls-object-parents x) (nreverse result))
553 (dolist (slot (get-slot-value parent 'instance-slots))
554 (pushnew slot result)))))
556 (defun get-initial-slot-value (object slot)
557 (let ((entry (find-slot object slot)))
558 (if (slot-entry-p entry) (slot-entry-value entry))))
560 (defun make-prototype (object name ivars cvars doc set)
561 (setf ivars (find-instance-slots object ivars))
562 (add-slot object 'instance-slots ivars)
563 (add-slot object 'proto-name name)
564 (dolist (slot ivars)
565 (add-slot object slot (get-initial-slot-value object slot)))
566 (dolist (slot cvars)
567 (add-slot object slot nil))
569 (if (and doc (stringp doc))
570 (add-documentation object 'proto doc))
571 (if set (setf (symbol-value name) object)))
574 (defmacro defproto (name &optional ivars cvars parents doc)
575 "Syntax (defproto name &optional ivars cvars (parent *object*) doc)
576 Makes a new object prototype with instantiated/set variables IVARS,
577 'class' variables CVARS (class or 'cleared'??)) and parents
578 PARENT. PARENT can be a single object or a list of objects. IVARS and
579 CVARS must be lists."
580 (let ((obsym (gensym))
581 (namesym (gensym))
582 (parsym (gensym)))
583 `(progn
584 (let* ((,namesym ',name)
585 (,parsym ,parents)
586 (,obsym (make-basic-object (if (listp ,parsym)
587 ,parsym
588 (list ,parsym)) ;; should this be ,@parsym ?
589 nil)))
590 (make-prototype ,obsym ,namesym ,ivars ,cvars ,doc t)
591 ,namesym))))
594 ;; Infrastructure for new defproto from Common-Lisp Cookbook! Thanks!
596 ;(defmacro odd-define (name buildargs)
597 ; `(progn (defun ,(build-symbol make-a- (:< name))
598 ; ,buildargs
599 ; (vector ,(length buildargs) ',name ,@buildargs))
600 ; (defun ,(build-symbol test-whether- (:< name)) (x)
601 ; (and (vectorp x) (eq (aref x 1) ',name))
602 ; (defun ,(build-symbol (:< name) -copy) (x)
603 ; ...)
604 ; (defun ,(build-symbol (:< name) -deactivate) (x)
605 ; ...))))
607 ;(defmacro for (listspec exp)
608 ; (cond ((and (= (length listspec) 3)
609 ; (symbolp (car listspec))
610 ; (eq (cadr listspec) ':in))
611 ; `(mapcar (lambda (,(car listspec))
612 ; ,exp)
613 ; ,(caddr listspec)))
614 ; (t (error "Ill-formed: ~s" `(for ,listspec ,exp)))))
616 ;(defmacro symstuff (l)
617 ; `(concatenate 'string
618 ; ,@(for (x :in l)
619 ; (cond ((stringp x)
620 ; `',x)
621 ; ((atom x)
622 ; `',(format nil "~a" x))
623 ; ((eq (car x) ':<)
624 ; `(format nil "~a" ,(cadr x)))
625 ; ((eq (car x) ':++)
626 ; `(format nil "~a" (incf ,(cadr x))))
627 ; (t
628 ; `(format nil "~a" ,x))))))
630 ;(defmacro build-symbol (&rest l)
631 ; (let ((p (find-if (lambda (x)
632 ; (and (consp x)
633 ; (eq (car x) ':package)))
634 ; l)))
635 ; (cond (p
636 ; (setq l (remove p l))))
637 ; (let ((pkg (cond ((eq (cadr p) 'nil)
638 ; nil)
639 ; (t `(find-package ',(cadr p))))))
640 ; (cond (p
641 ; (cond (pkg
642 ; `(values (intern ,(symstuff l) ,pkg)))
643 ; (t
644 ; `(make-symbol ,(symstuff l)))))
645 ; (t
646 ; `(values (intern ,(symstuff l))))))))
648 (defmacro defproto2 (name &optional ivars cvars parents doc)
649 "Syntax (defproto name &optional ivars cvars (parent *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 (if (boundp name)
654 (error "can not rebind a prototype object yet")
655 (let ((namesym (gensym))
656 (obsym (gensym))
657 (parsym (gensym)))
658 `(progn
659 (let* ((,namesym ,name)
660 (,parsym ,parents)
661 (,obsym (make-basic-object
662 (if (listp ,parsym)
663 ,parsym
664 (list ,@parsym)) ;; should this be ,@parsym ?
665 nil)))
666 (make-prototype ,obsym ,name ,ivars ,cvars ,doc t)
667 ,name)))))
669 ;; recall:
670 ;; , => turn on evaluation again (not macro substitution)
671 ;; ` => template comes (use , to undo template and restore eval
672 ;; ' => regular quote (not special in this context), 'ted => (quote ted)
675 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
677 ;;; Initialize the Root Object
679 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
681 (setf (ls-object-preclist *object*) (list *object*))
682 (add-slot *object* 'instance-slots nil)
683 (add-slot *object* 'proto-name '*object*)
684 (add-slot *object* 'documentation nil) ; AJR - for SBCL compiler
685 ; issues about macro with
686 ; unknown slot
688 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
690 ;;; *OBJECT* Methods
692 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
694 (defmeth *object* :isnew (&rest args)
695 "Method args: (&rest args)
696 Checks ARGS for keyword arguments matching slots and uses them to
697 initialize slots."
698 (if args
699 (dolist (slot-entry (ls-object-slots self))
700 (let* ((slot (slot-entry-key slot-entry))
701 (key (intern (symbol-name slot) (find-package 'keyword)))
702 (val (slot-value slot))
703 (new-val (getf args key val)))
704 (unless (eq val new-val) (setf (slot-value slot) new-val)))))
705 self)
707 (defmeth *object* :has-slot (slot &key own)
708 "Method args: (slot &optional own)
709 Returns T if slot SLOT exists, NIL if not. If OWN is not NIL
710 only checks the object; otherwise check the entire precedence list."
711 (let ((entry (if own (find-own-slot self slot) (find-slot self slot))))
712 (if entry t nil)))
714 (defmeth *object* :add-slot (slot &optional value)
715 "Method args: (slot &optional value)
716 Installs slot SLOT in object, if it does not already exist, and
717 sets its value to VLAUE."
718 (add-slot self slot value)
719 value)
721 (defmeth *object* :delete-slot (slot)
722 "Method args: (slot)
723 Deletes slot SLOT from object if it exists."
724 (delete-slot self slot)
725 nil)
727 (defmeth *object* :own-slots ()
728 "Method args: ()
729 Returns list of names of slots owned by object."
730 (mapcar #'slot-entry-key (ls-object-slots self)))
732 (defmeth *object* :has-method (selector &key own)
733 "Method args: (selector &optional own)
734 Returns T if method for SELECTOR exists, NIL if not. If OWN is not NIL
735 only checks the object; otherwise check the entire precedence list."
736 (let ((entry (if own
737 (find-own-method self selector)
738 (find-lsos-method self selector))))
739 (if entry t nil)))
741 (defmeth *object* :add-method (selector method)
742 "Method args: (selector method)
743 Installs METHOD for SELECTOR in object."
744 (add-lsos-method self selector method)
745 nil)
747 (defmeth *object* :delete-method (selector)
748 "Method args: (selector)
749 Deletes method for SELECTOR in object if it exists."
750 (delete-method self selector)
751 nil)
753 (defmeth *object* :get-method (selector)
754 "Method args: (selector)
755 Returns method for SELECTOR symbol from object's precedence list."
756 (get-message-method self selector))
758 (defmeth *object* :own-methods ()
759 "Method args ()
760 Returns copy of selectors for methods owned by object."
761 (mapcar #'method-entry-key (ls-object-methods self)))
763 (defmeth *object* :parents ()
764 "Method args: ()
765 Returns copy of parents list."
766 (copy-list (ls-object-parents self)))
768 (defmeth *object* :precedence-list ()
769 "Method args: ()
770 Returns copy of the precedence list."
771 (copy-list (ls-object-preclist self)))
773 (defmeth *object* :show (&optional (stream t))
774 "Method Args: ()
775 Prints object's internal data."
776 (format stream "Slots = ~s~%" (ls-object-slots self))
777 (format stream "Methods = ~s~%" (ls-object-methods self))
778 (format stream "Parents = ~s~%" (ls-object-parents self))
779 (format stream "Precedence List = ~s~%" (ls-object-preclist self))
780 nil)
782 (defmeth *object* :reparent (&rest parents)
783 "Method args: (&rest parents)
784 Changes precedence list to correspond to PARENTS. Does not change descendants."
785 (make-basic-object parents self))
787 (defmeth *object* :make-prototype (name &optional ivars)
788 (make-prototype self name ivars nil nil nil)
789 self)
791 (defmeth *object* :internal-doc (sym &optional new)
792 "Method args (topic &optional value)
793 Retrieves or installs documentation for topic."
794 (if new (add-documentation self sym new))
795 (get-documentation self sym))
797 (defmeth *object* :new (&rest args)
798 "Method args: (&rest args)
799 Creates new object using self as prototype."
800 (let* ((object (make-object self)))
801 (if (slot-value 'instance-slots)
802 (dolist (s (slot-value 'instance-slots))
803 (send object :add-slot s (slot-value s))))
804 (apply #'send object :isnew args)
805 object))
807 (defmeth *object* :retype (proto &rest args)
808 "Method args: (proto &rest args)
809 Changes object to inherit directly from prototype PROTO. PROTO
810 must be a prototype and SELF must not be one."
811 (if (send self :has-slot 'instance-slots :own t)
812 (error "can't retype a prototype"))
813 (if (not (send proto :has-slot 'instance-slots :own t))
814 (error "not a prototype - ~a" proto))
815 (send self :reparent proto)
816 (dolist (s (send proto :slot-value 'instance-slots))
817 (send self :add-slot s (slot-value s)))
818 (apply #'send self :isnew args)
819 self)
821 (defmeth *object* :print (&optional (stream *standard-output*))
822 "Method args: (&optional (stream *standard-output*))
823 Default object printing method."
824 (cond
825 ((send self :has-slot 'proto-name)
826 (format stream
827 "#<Object: ~D, prototype = ~A>"
828 (ls-object-serial self)
829 (slot-value 'proto-name)))
830 (t (format stream "#<Object: ~D>" (ls-object-serial self)))))
832 (defmeth *object* :slot-value (sym &optional (val nil set))
833 "Method args: (sym &optional val)
834 Sets and retrieves value of slot named SYM. Signals an error if slot
835 does not exist."
836 (if set (setf (slot-value sym) val))
837 (slot-value sym))
839 (defmeth *object* :slot-names ()
840 "Method args: ()
841 Returns list of slots available to the object."
842 (apply #'append
843 (mapcar #'(lambda (x) (send x :own-slots))
844 (send self :precedence-list))))
846 (defmeth *object* :method-selectors ()
847 "Method args: ()
848 Returns list of method selectors available to object."
849 (apply #'append
850 (mapcar #'(lambda (x) (send x :own-methods))
851 (send self :precedence-list))))
854 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
855 ;;;;
856 ;;;; Object Help Methods
857 ;;;;
858 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
860 (defmeth *object* :doc-topics ()
861 "Method args: ()
862 Returns all topics with documentation for this object."
863 (remove-duplicates
864 (mapcar #'car
865 (apply #'append
866 (mapcar
867 #'(lambda (x)
868 (if (send x :has-slot 'documentation :own t)
869 (send x :slot-value (quote documentation))))
870 (send self :precedence-list))))))
872 (defmeth *object* :documentation (topic &optional (val nil set))
873 "Method args: (topic &optional val)
874 Retrieves or sets object documentation for topic."
875 (if set (send self :internal-doc topic val))
876 (let ((val (dolist (i (send self :precedence-list))
877 (let ((val (send i :internal-doc topic)))
878 (if val (return val))))))
879 val))
881 (defmeth *object* :delete-documentation (topic)
882 "Method args: (topic)
883 Deletes object documentation for TOPIC."
884 (setf (slot-value 'documentation)
885 ;;(remove :title nil :test #'(lambda (x y) (eql x (first y)))) ;; original
886 (remove topic (send self :documentation) :test #'(lambda (x y) (eql x (first y))))) ;; AJR:PROBLEM?
887 nil)
889 (defmeth *object* :help (&optional topic)
890 "Method args: (&optional topic)
891 Prints help message for TOPIC, or genreal help if TOPIC is NIL."
892 (if topic
893 (let ((doc (send self :documentation topic)))
894 (cond
895 (doc (princ topic) (terpri) (princ doc) (terpri))
896 (t (format t "Sorry, no help available on ~a~%" topic))))
897 (let ((topics (stable-sort (copy-seq (send self :doc-topics))
898 #'(lambda (x y)
899 (string-lessp (string x) (string y)))))
900 (proto-doc (send self :documentation 'proto)))
901 (if (send self :has-slot 'proto-name)
902 (format t "~s~%" (slot-value 'proto-name)))
903 (when proto-doc (princ proto-doc) (terpri))
904 (format t "Help is available on the following:~%~%")
905 (dolist (i topics) (format t "~s " i))
906 (terpri)))
907 (values))
909 (defmeth *object* :compile-method (name)
910 "Method args: (name)
911 Compiles method NAME unless it is already compiled. The object must
912 own the method."
913 (unless (send self :has-method name)
914 (error "No ~s method in this object" name))
915 (unless (send self :has-method name :own t)
916 (error "Object does not own ~s method" name))
917 (let ((fun (send self :get-method name)))
918 (unless (compiled-function-p fun)
919 (multiple-value-bind (form env) (function-lambda-expression fun)
920 (if env
921 (error
922 "method may have been defined in non-null environment"))
923 (send self :add-method name (compile nil form))))))