test clean up. Need to CLOS-ify the numerical= operation
[CommonLispStat.git] / lsobjects.lsp
blobcfd7ce2fbecab917271c731e983950a80ac091ac
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 instance-slots proto-name))
67 (in-package :lisp-stat-object-system)
69 (defun use-lsos ()
70 "Formerly set up to import lisp-stat-object-system into current package."
71 (shadowing-import (package-shadowing-symbols 'lisp-stat-object-system))
72 (use-package 'lisp-stat-object-system))
74 ;;; Structure Implementation of Lisp-Stat Object System
76 (defvar *object-serial* 0)
78 (defstruct (ls-object
79 (:constructor make-object-structure) ;; why not make-ls-object?
80 (:print-function print-object-structure)
81 (:predicate objectp)) ;; why not ls-object-p?
82 slots
83 methods
84 parents
85 preclist
86 (serial (incf *object-serial*)))
88 (defun print-object-structure (object stream depth)
89 (declare (ignore depth))
90 (send object :print stream))
92 (setf (documentation 'objectp 'function)
93 "Args: (x)
94 Returns T if X is an object, NIL otherwise.")
96 (defvar *object* (make-object-structure)
97 "*object* is the global root object.")
99 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
100 ;;;;
101 ;;;; Utility Functions
102 ;;;;
103 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
105 ;;; special variable to hold current value of SELF. Assign to current
106 ;;; object that we are working with. AJR:FIXME:Is this going to cause
107 ;;; issues with concurrency? (need to appropriately handle
108 ;;; interrupts).
109 (defvar *self* nil)
111 ;;; FIXME: better as macro? maybe not?
112 (defun get-self ()
113 (if (not (objectp *self*))
114 (error "not in a method"))
115 *self*)
117 (defun has-duplicates (list)
118 (do ((next list (rest next)))
119 ((not (consp next)) nil)
120 (if (member (first next) (rest next)) (return t))))
122 (defun assoc-eq (item alist)
123 "Version of assoc using eq -- should be faster than regular assoc."
124 (declare (inline car eq))
125 (dolist (i alist)
126 (if (eq (car i) item) (return i))))
128 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
129 ;;;;
130 ;;;; Predicate and Checking Functions
131 ;;;;
132 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
134 (defun check-non-nil-symbol (x)
135 (unless (and x (symbolp x)) (error "bad symbol - ~s" x)))
137 (defun check-object (x)
138 (if (objectp x) x (error "bad object - ~s" x)))
140 (defun kind-of-p (x y)
141 "Args: (x y)
142 Returns T is X and Y are objects and X inherits from Y, NIL otherwise."
143 (if (and (objectp x) (objectp y))
144 (if (member y (ls-object-preclist x)) t nil)
145 nil))
147 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
148 ;;;;
149 ;;;; Precedence List Functions
150 ;;;;
151 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
153 (defun find-SC (object)
154 "find set of object and ancestors. (diff from this and find-S?)"
155 (copy-list (ls-object-preclist (check-object object))))
157 (defun find-S (object)
158 "find set of object and ancestors. (diff from this and find-SC?)"
159 (do ((result nil)
160 (parents (ls-object-parents object) (cdr parents)))
161 ((not (consp parents))
162 (delete-duplicates (cons object result)))
163 (setf result (nconc (find-SC (first parents)) result))))
165 (defun find-RC (object)
166 "find local precedence ordering."
167 (let ((list (copy-list (ls-object-parents (check-object object)))))
168 (do ((next list (rest next)))
169 ((not (consp next)) list)
170 (setf (first next) (cons object (first next)))
171 (setf object (rest (first next))))))
173 (defun find-R (S)
174 "find partial precedence ordering."
175 (do ((result nil)
176 (S S (rest S)))
177 ((not (consp S))
178 (delete-duplicates result))
179 (setf result (nconc result (find-RC (first S))))))
181 (defun has-predecessor (x R)
182 "check if x has a predecessor according to R."
183 (dolist (cell R nil)
184 (if (and (consp cell) (eq x (rest cell))) (return t))))
186 (defun find-no-predecessor-list (S R)
187 "find list of objects in S without predecessors, by R."
188 (let ((result nil))
189 (dolist (x S result)
190 (unless (has-predecessor x R) (setf result (cons x result))))))
192 (defun child-position (x P)
193 "find the position of child, if any, of x in P, the list found so
194 far."
195 (let ((count 0))
196 (declare (fixnum count))
197 (dolist (next P -1)
198 (if (member x (ls-object-parents next)) (return count))
199 (incf count))))
201 (defun next-object (no-preds P)
202 "find the next object in the precedence list from objects with no
203 predecessor and current list."
204 (cond
205 ((not (consp no-preds)) nil)
206 ((not (consp (rest no-preds))) (first no-preds))
208 (let ((count -1)
209 (result nil))
210 (declare (fixnum count))
211 (dolist (x no-preds result)
212 (let ((tcount (child-position x P)))
213 (declare (fixnum tcount))
214 (when (> tcount count)
215 (setf result x)
216 (setf count tcount))))))))
218 (defun trim-S (x S)
219 "Remove object x from S."
220 (delete x S))
222 (defun trim-R (x R)
223 "Remove all pairs containing x from R. x is assumed to have no
224 predecessors, so only the first position is checked."
225 (delete x R :key #'first))
227 (defun precedence-list (object)
228 "Calculate the object's precedence list."
229 (do* ((S (find-S object))
230 (R (find-R S))
231 (P nil)
232 (no-preds nil)
233 (next nil))
234 ((not (consp S)) P)
235 (setf no-preds (find-no-predecessor-list S R))
236 (setf next (next-object no-preds P))
237 (if (null next) (error "inconsistent precedence order"))
238 (setf P (nconc P (list next)))
239 (setf S (trim-S next S))
240 (setf R (trim-R next R))))
242 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
243 ;;;;
244 ;;;; Object Construction Functions
245 ;;;;
246 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
248 (defun calculate-preclist (object)
249 "Return the precedence list for the object."
250 (let ((parents (ls-object-parents (check-object object))))
251 (if (not (consp parents)) (error "bad parent list - ~s" parents))
252 (if (consp (rest parents))
253 (precedence-list object)
254 (let ((parent (check-object (first parents))))
255 (cons object (ls-object-preclist parent))))))
257 (defun check-parents (parents)
258 (cond
259 ((or (null parents) (objectp parents)) parents)
260 ((consp parents)
261 (dolist (x parents) (check-object x))
262 (if (has-duplicates parents)
263 (error "parents may not contain duplicates")))
264 (t (error "bad parents - ~s" parents))))
266 (defun make-basic-object (parents object)
267 (check-parents parents)
269 (if (not (objectp object)) (setf object (make-object-structure)))
271 (setf (ls-object-preclist object) (ls-object-preclist *object*))
272 (setf (ls-object-parents object)
273 (cond ((null parents) (list *object*))
274 ((objectp parents) (list parents))
275 (t parents)))
276 (setf (ls-object-preclist object) (calculate-preclist object))
278 object)
280 (defun make-object (&rest parents)
281 "Args: (&rest parents)
282 Returns a new object with parents PARENTS. If PARENTS is NIL, (list *OBJECT*) is used."
283 (make-basic-object parents NIL))
285 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
286 ;;;;
287 ;;;; Constraint Hook Functions
288 ;;;;
289 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
291 (pushnew :constrainthooks *features*)
293 #+:constrainthooks
294 (progn
295 (defvar *message-hook* nil)
296 (defvar *set-slot-hook* nil)
298 (defun check-constraint-hooks (object sym slot)
299 (let ((hook (if slot *set-slot-hook* *message-hook*)))
300 (if hook
301 (if slot
302 (let ((*set-slot-hook* nil))
303 (funcall hook object sym))
304 (let ((*message-hook* nil))
305 (funcall hook object sym)))))))
307 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
309 ;;; Slot Access Functions
311 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
313 (defun make-slot-entry (x y) (cons x y))
314 (defun slot-entry-p (x) (consp x))
315 (defun slot-entry-key (x) (first x))
316 (defun slot-entry-value (x) (rest x))
317 (defun set-slot-entry-value (x v) (setf (rest x) v))
318 (defsetf slot-entry-value set-slot-entry-value)
320 (defun find-own-slot (x slot)
321 (if (objectp x) (assoc-eq slot (ls-object-slots x))))
323 (defun find-slot (x slot)
324 (if (objectp x)
325 (let ((preclist (ls-object-preclist x)))
326 (dolist (object preclist)
327 (let ((slot-entry (find-own-slot object slot)))
328 (if slot-entry (return slot-entry)))))))
330 (defun add-slot (x slot value)
331 (check-object x)
332 (check-non-nil-symbol slot)
333 (let ((slot-entry (find-own-slot x slot)))
334 (if slot-entry
335 (setf (slot-entry-value slot-entry) value)
336 (setf (ls-object-slots x)
337 (cons (make-slot-entry slot value) (ls-object-slots x)))))
338 nil)
340 (defun delete-slot (x slot)
341 (check-object x)
342 (setf (ls-object-slots x)
343 (delete slot (ls-object-slots x) :key #'slot-entry-key)))
345 (defun get-slot-value (x slot &optional no-err)
346 (check-object x)
347 (let ((slot-entry (find-slot x slot)))
348 (if (slot-entry-p slot-entry)
349 (slot-entry-value slot-entry)
350 (unless no-err (error "no slot named ~s in this object" slot)))))
352 (defun set-slot-value (x slot value)
353 (check-object x)
354 (let ((slot-entry (find-own-slot x slot)))
355 (cond
356 ((slot-entry-p slot-entry)
357 (set-slot-entry-value slot-entry value)
358 #+:constrainthooks (check-constraint-hooks x slot t))
360 (if (find-slot x slot)
361 (error "object does not own slot ~s" slot)
362 (error "no slot named ~s in this object" slot))))))
364 (defun slot-value (slot)
365 "Args: (slot)
366 Must be used in a method. Returns the value of current objects slot
367 named SLOT."
368 (get-slot-value (get-self) slot))
370 (defun slot-value-setf (slot value)
371 (set-slot-value (get-self) slot value))
373 (defsetf slot-value slot-value-setf)
375 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
376 ;;;;
377 ;;;; Method Access Functions;
378 ;;;;
379 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
381 (defun make-method-entry (x y) (cons x y))
382 (defun method-entry-p (x) (consp x))
383 (defun method-entry-key (x) (first x))
384 (defun method-entry-method (x) (rest x))
385 (defun set-method-entry-method (x v) (setf (rest x) v))
386 (defsetf method-entry-method set-method-entry-method)
388 ;(defun find-own-method (x selector)
389 ; (if (objectp x) (assoc selector (ls-object-methods x))))
390 (defun find-own-method (x selector)
391 (if (objectp x) (assoc-eq selector (ls-object-methods x))))
393 (defun find-lsos-method (x selector)
394 (if (objectp x)
395 (let ((preclist (ls-object-preclist x)))
396 (dolist (object preclist)
397 (let ((method-entry (find-own-method object selector)))
398 (if method-entry (return method-entry)))))))
400 (defun add-lsos-method (x selector value)
401 "x = object; selector = name of method; value = form computing the method."
402 (check-object x)
403 (check-non-nil-symbol selector)
404 (let ((method-entry (find-own-method x selector)))
405 (if method-entry
406 (setf (method-entry-method method-entry) value)
407 (setf (ls-object-methods x)
408 (cons (make-method-entry selector value) (ls-object-methods x)))))
409 nil)
411 (defun delete-method (x selector)
412 (check-object x)
413 (setf (ls-object-methods x)
414 (delete selector (ls-object-methods x) :key #'method-entry-key)))
416 (defun get-message-method (x selector &optional no-err)
417 (check-object x)
418 (let ((method-entry (find-lsos-method x selector)))
419 (if (method-entry-p method-entry)
420 (method-entry-method method-entry)
421 (unless no-err (error "no method for selector ~s" selector)))))
423 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
424 ;;;;
425 ;;;; Message Sending Functions
426 ;;;;
427 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
429 (defvar *current-preclist* nil)
430 (defvar *current-selector* nil)
432 (defun sendmsg (object selector preclist args)
433 (let ((method-entry nil)
434 (method nil))
436 ;; look for the message in the precedence list
437 (loop
438 (setf method-entry (find-own-method (first preclist) selector))
439 (if (or method-entry (not (consp preclist))) (return))
440 (setf preclist (rest preclist)))
441 (cond
442 ((null method-entry) (error "no method for selector ~s" selector))
443 ((not (method-entry-p method-entry)) (error "bad method entry"))
444 (t (setf method (method-entry-method method-entry))))
446 ;; invoke the method
447 (let ((*current-preclist* preclist)
448 (*current-selector* selector)
449 (*self* object))
450 (multiple-value-prog1
451 (apply method object args)
452 #+:constrainthooks (check-constraint-hooks object selector nil)))))
454 ;;;; built-in send function
455 (defun send (object selector &rest args)
456 "Args: (object selector &rest args)
457 Applies first method for SELECTOR found in OBJECT's precedence list to
458 OBJECT and ARGS."
459 (sendmsg object selector (ls-object-preclist object) args))
461 ;;;; call-next-method - call inherited version of current method
462 (defun call-next-method (&rest args)
463 "Args (&rest args)
464 Funcalls next method for current selector and precedence list. Can only be
465 used in a method."
466 (sendmsg *self* *current-selector* (rest *current-preclist*) args))
468 ;;;; call-method - call method belonging to another object on current object
470 ;; ugly cruft, need better solution for SBCL packagelocks
471 ;; #+sbcl(declare (sb-ext:disable-package-locks ls-objects:call-method))
473 (defun call-method (object selector &rest args)
474 "Args (object selector &rest args)
475 Funcalls method for SELECTOR found in OBJECT to SELF. Can only be used in
476 a method."
477 (sendmsg *self* selector (ls-object-preclist object) args))
479 ;; #+sbcl(declare (sb-ext:enable-package-locks ls-objects:call-method))
481 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
482 ;;;;
483 ;;;; Object Documentation Functions
484 ;;;;
485 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
487 (defun find-documentation (x sym add)
488 (if (objectp x)
489 (let ((doc (find-own-slot x 'documentation)))
490 (if (and (null doc) add) (add-slot x 'documentation nil))
491 (if (slot-entry-p doc) (assoc sym (slot-entry-value doc))))))
493 (defun add-documentation (x sym value)
494 (check-object x)
495 (check-non-nil-symbol sym)
496 (let ((doc-entry (find-documentation x sym t)))
497 (cond
498 ((not (null doc-entry))
499 (setf (rest doc-entry) value))
501 (set-slot-value x
502 'documentation
503 (cons (cons sym value)
504 (get-slot-value x 'documentation))))))
505 nil)
507 (defun get-documentation (x sym)
508 (check-object x)
509 (dolist (object (ls-object-preclist x))
510 (let ((doc-entry (find-documentation object sym nil))) ;; FIXME: verify
511 (if doc-entry (return (rest doc-entry))))))
513 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
514 ;;;;
515 ;;;; DEFMETH Macro
516 ;;;;
517 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
519 (defmacro defmeth (object name arglist first &rest body)
520 "Syntax: (defmeth object method-name lambda-list [doc] {form}*)
521 OBJECT must evaluate to an existing object. Installs a method for NAME in
522 the value of OBJECT and installs DOC in OBJECTS's documentation.
523 RETURNS: method-name."
524 (declare (ignorable self)) ;; hints for the compiler that sometimes it isn't used
525 (if (and body (stringp first))
526 `(progn ;; first=docstring + body
527 (add-lsos-method ,object ,name
528 #'(lambda (self ,@arglist) (block ,name ,@body)))
529 (add-documentation ,object ,name ,first)
530 ,name)
531 `(progn ;; first=code + body
532 (add-lsos-method ,object ,name
533 #'(lambda (self ,@arglist) (block ,name ,first ,@body)))
534 ,name)))
536 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
537 ;;;;
538 ;;;; Prototype Construction Functions and Macros
539 ;;;;
540 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
542 (defun find-instance-slots (x slots)
543 (let ((result (nreverse (delete-duplicates (copy-list slots)))))
544 (dolist (parent (ls-object-parents x) (nreverse result))
545 (dolist (slot (get-slot-value parent 'instance-slots))
546 (pushnew slot result)))))
548 (defun get-initial-slot-value (object slot)
549 (let ((entry (find-slot object slot)))
550 (if (slot-entry-p entry) (slot-entry-value entry))))
552 (defun make-prototype (object name ivars cvars doc set)
553 (setf ivars (find-instance-slots object ivars))
554 (add-slot object 'instance-slots ivars)
555 (add-slot object 'proto-name name)
557 (dolist (slot ivars)
558 (add-slot object slot (get-initial-slot-value object slot)))
560 (dolist (slot cvars)
561 (add-slot object slot nil))
563 (if (and doc (stringp doc))
564 (add-documentation object 'proto doc))
565 (if set (setf (symbol-value name) object)))
568 (defmacro defproto (name &optional ivars cvars parents doc)
569 "Syntax (defproto name &optional ivars cvars (parent *object*) doc)
570 Makes a new object prototype with instance variables IVARS, 'class'
571 variables CVARS and parents PARENT. PARENT can be a single object or
572 a list of objects. IVARS and CVARS must be lists."
573 (let ((obsym (gensym))
574 (namesym (gensym))
575 (parsym (gensym)))
576 `(progn
577 (let* ((,namesym ',name)
578 (,parsym ,parents)
579 (,obsym (make-basic-object (if (listp ,parsym)
580 ,parsym
581 (list ,parsym)) ;; should this be ,@parsym ?
582 nil)))
583 (make-prototype ,obsym ,namesym ,ivars ,cvars ,doc t)
584 ,namesym))))
588 (defmacro defproto2 (name &optional ivars cvars parents doc)
589 "Syntax (defproto name &optional ivars cvars (parent *object*) doc)
590 Makes a new object prototype with instance variables IVARS, 'class'
591 variables CVARS and parents PARENT. PARENT can be a single object or
592 a list of objects. IVARS and CVARS must be lists."
593 (if (boundp name)
594 (error "name is bound") ; fixme: use real error
595 (let ((obsym (gensym))
596 (namesym (gensym))
597 (parsym (gensym)))
598 `(progn
599 (let* ((,namesym ',name)
600 (,parsym ,parents)
601 (,obsym (make-basic-object (if (listp ,parsym)
602 ,parsym
603 (list ,parsym)) ;; should this be ,@parsym ?
604 nil)))
605 (make-prototype ,obsym ,namesym ,ivars ,cvars ,doc t)
606 ,namesym)))))
611 ;; recall:
612 ;; , => turn on evaluation again (not macro substitution)
613 ;; ` =>
614 ;; ' => regular quote (not special in this context).
617 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
618 ;;;;
619 ;;;; Initialize the Root Object
620 ;;;;
621 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
623 (setf (ls-object-preclist *object*) (list *object*))
624 (add-slot *object* 'instance-slots nil)
625 (add-slot *object* 'proto-name '*object*)
626 (add-slot *object* 'documentation nil) ; AJR - for SBCL compiler
627 ; issues about macro with
628 ; unknown slot
630 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
631 ;;;;
632 ;;;; *OBJECT* Methods
633 ;;;;
634 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
636 (defmeth *object* :isnew (&rest args)
637 "Method args: (&rest args)
638 Checks ARGS for keyword arguments matching slots and uses them to
639 initialize slots."
640 (if args
641 (dolist (slot-entry (ls-object-slots self))
642 (let* ((slot (slot-entry-key slot-entry))
643 (key (intern (symbol-name slot) (find-package 'keyword)))
644 (val (slot-value slot))
645 (new-val (getf args key val)))
646 (unless (eq val new-val) (setf (slot-value slot) new-val)))))
647 self)
649 (defmeth *object* :has-slot (slot &key own)
650 "Method args: (slot &optional own)
651 Returns T if slot SLOT exists, NIL if not. If OWN is not NIL
652 only checks the object; otherwise check the entire precedence list."
653 (let ((entry (if own (find-own-slot self slot) (find-slot self slot))))
654 (if entry t nil)))
656 (defmeth *object* :add-slot (slot &optional value)
657 "Method args: (slot &optional value)
658 Installs slot SLOT in object, if it does not already exist, and
659 sets its value to VLAUE."
660 (add-slot self slot value)
661 value)
663 (defmeth *object* :delete-slot (slot)
664 "Method args: (slot)
665 Deletes slot SLOT from object if it exists."
666 (delete-slot self slot)
667 nil)
669 (defmeth *object* :own-slots ()
670 "Method args: ()
671 Returns list of names of slots owned by object."
672 (mapcar #'slot-entry-key (ls-object-slots self)))
674 (defmeth *object* :has-method (selector &key own)
675 "Method args: (selector &optional own)
676 Returns T if method for SELECTOR exists, NIL if not. If OWN is not NIL
677 only checks the object; otherwise check the entire precedence list."
678 (let ((entry (if own
679 (find-own-method self selector)
680 (find-lsos-method self selector))))
681 (if entry t nil)))
683 (defmeth *object* :add-method (selector method)
684 "Method args: (selector method)
685 Installs METHOD for SELECTOR in object."
686 (add-lsos-method self selector method)
687 nil)
689 (defmeth *object* :delete-method (selector)
690 "Method args: (selector)
691 Deletes method for SELECTOR in object if it exists."
692 (delete-method self selector)
693 nil)
695 (defmeth *object* :get-method (selector)
696 "Method args: (selector)
697 Returns method for SELECTOR symbol from object's precedence list."
698 (get-message-method self selector))
700 (defmeth *object* :own-methods ()
701 "Method args ()
702 Returns copy of selectors for methods owned by object."
703 (mapcar #'method-entry-key (ls-object-methods self)))
705 (defmeth *object* :parents ()
706 "Method args: ()
707 Returns copy of parents list."
708 (copy-list (ls-object-parents self)))
710 (defmeth *object* :precedence-list ()
711 "Method args: ()
712 Returns copy of the precedence list."
713 (copy-list (ls-object-preclist self)))
715 (defmeth *object* :show (&optional (stream t))
716 "Method Args: ()
717 Prints object's internal data."
718 (format stream "Slots = ~s~%" (ls-object-slots self))
719 (format stream "Methods = ~s~%" (ls-object-methods self))
720 (format stream "Parents = ~s~%" (ls-object-parents self))
721 (format stream "Precedence List = ~s~%" (ls-object-preclist self))
722 nil)
724 (defmeth *object* :reparent (&rest parents)
725 "Method args: (&rest parents)
726 Changes precedence list to correspond to PARENTS. Does not change descendants."
727 (make-basic-object parents self))
729 (defmeth *object* :make-prototype (name &optional ivars)
730 (make-prototype self name ivars nil nil nil)
731 self)
733 (defmeth *object* :internal-doc (sym &optional new)
734 "Method args (topic &optional value)
735 Retrieves or installs documentation for topic."
736 (if new (add-documentation self sym new))
737 (get-documentation self sym))
739 (defmeth *object* :new (&rest args)
740 "Method args: (&rest args)
741 Creates new object using self as prototype."
742 (let* ((object (make-object self)))
743 (if (slot-value 'instance-slots)
744 (dolist (s (slot-value 'instance-slots))
745 (send object :add-slot s (slot-value s))))
746 (apply #'send object :isnew args)
747 object))
749 (defmeth *object* :retype (proto &rest args)
750 "Method args: (proto &rest args)
751 Changes object to inherit directly from prototype PROTO. PROTO
752 must be a prototype and SELF must not be one."
753 (if (send self :has-slot 'instance-slots :own t)
754 (error "can't retype a prototype"))
755 (if (not (send proto :has-slot 'instance-slots :own t))
756 (error "not a prototype - ~a" proto))
757 (send self :reparent proto)
758 (dolist (s (send proto :slot-value 'instance-slots))
759 (send self :add-slot s (slot-value s)))
760 (apply #'send self :isnew args)
761 self)
763 (defmeth *object* :print (&optional (stream *standard-output*))
764 "Method args: (&optional (stream *standard-output*))
765 Default object printing method."
766 (cond
767 ((send self :has-slot 'proto-name)
768 (format stream
769 "#<Object: ~D, prototype = ~A>"
770 (ls-object-serial self)
771 (slot-value 'proto-name)))
772 (t (format stream "#<Object: ~D>" (ls-object-serial self)))))
774 (defmeth *object* :slot-value (sym &optional (val nil set))
775 "Method args: (sym &optional val)
776 Sets and retrieves value of slot named SYM. Signals an error if slot
777 does not exist."
778 (if set (setf (slot-value sym) val))
779 (slot-value sym))
781 (defmeth *object* :slot-names ()
782 "Method args: ()
783 Returns list of slots available to the object."
784 (apply #'append
785 (mapcar #'(lambda (x) (send x :own-slots))
786 (send self :precedence-list))))
788 (defmeth *object* :method-selectors ()
789 "Method args: ()
790 Returns list of method selectors available to object."
791 (apply #'append
792 (mapcar #'(lambda (x) (send x :own-methods))
793 (send self :precedence-list))))
796 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
797 ;;;;
798 ;;;; Object Help Methods
799 ;;;;
800 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
802 (defmeth *object* :doc-topics ()
803 "Method args: ()
804 Returns all topics with documentation for this object."
805 (remove-duplicates
806 (mapcar #'car
807 (apply #'append
808 (mapcar
809 #'(lambda (x)
810 (if (send x :has-slot 'documentation :own t)
811 (send x :slot-value (quote documentation))))
812 (send self :precedence-list))))))
814 (defmeth *object* :documentation (topic &optional (val nil set))
815 "Method args: (topic &optional val)
816 Retrieves or sets object documentation for topic."
817 (if set (send self :internal-doc topic val))
818 (let ((val (dolist (i (send self :precedence-list))
819 (let ((val (send i :internal-doc topic)))
820 (if val (return val))))))
821 val))
823 (defmeth *object* :delete-documentation (topic)
824 "Method args: (topic)
825 Deletes object documentation for TOPIC."
826 (setf (slot-value 'documentation)
827 ;;(remove :title nil :test #'(lambda (x y) (eql x (first y)))) ;; original
828 (remove topic (send self :documentation) :test #'(lambda (x y) (eql x (first y))))) ;; AJR:PROBLEM?
829 nil)
831 (defmeth *object* :help (&optional topic)
832 "Method args: (&optional topic)
833 Prints help message for TOPIC, or genreal help if TOPIC is NIL."
834 (if topic
835 (let ((doc (send self :documentation topic)))
836 (cond
837 (doc (princ topic) (terpri) (princ doc) (terpri))
838 (t (format t "Sorry, no help available on ~a~%" topic))))
839 (let ((topics (stable-sort (copy-seq (send self :doc-topics))
840 #'(lambda (x y)
841 (string-lessp (string x) (string y)))))
842 (proto-doc (send self :documentation 'proto)))
843 (if (send self :has-slot 'proto-name)
844 (format t "~s~%" (slot-value 'proto-name)))
845 (when proto-doc (princ proto-doc) (terpri))
846 (format t "Help is available on the following:~%~%")
847 (dolist (i topics) (format t "~s " i))
848 (terpri)))
849 (values))
851 (defmeth *object* :compile-method (name)
852 "Method args: (name)
853 Compiles method NAME unless it is already compiled. The object must
854 own the method."
855 (unless (send self :has-method name)
856 (error "No ~s method in this object" name))
857 (unless (send self :has-method name :own t)
858 (error "Object does not own ~s method" name))
859 (let ((fun (send self :get-method name)))
860 (unless (compiled-function-p fun)
861 (multiple-value-bind (form env) (function-lambda-expression fun)
862 (if env
863 (error
864 "method may have been defined in non-null environment"))
865 (send self :add-method name (compile nil form))))))