more CLOS issues.
[CommonLispStat.git] / lsobjects.lsp
blob8325e54de383b0f2cf7494c10bcaa61b6cb239f3
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 #+:constrainthooks. 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 ;;; 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
59 ;;; Package Setup
61 (in-package :cl-user)
63 (defpackage :lisp-stat-object-system
64 (:nicknames :ls-objects :lsos :proto-objects)
65 (:use :common-lisp)
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)
84 ;; )
85 ;; slots
86 ;; methods
87 ;; parents
88 ;; preclist precedence list
89 ;; (serial (incf *proto-object-serial*)))
91 (defclass proto-slots ()
92 ((contents) ;; list of data slots
93 (name)
94 (documentation)))
95 (defclass proto-methods () ;; list of functions that can be called
96 ((contents) ;; list of data slots
97 (name)
98 (documentation)))
99 (defclass proto-object-list ()
100 ((contents) ;; list of data slots
101 (name)
102 (documentation)))
103 (defclass preclist (proto-object-list) ())
104 (defclass parent-list (proto-object-list) ())
106 (defgeneric add-object (proto-struct slot &optional init) ;; location)
107 (:documentation "proto-struct is the prototype structure that we are
108 working with, while slot means either slot or method, and value is
109 the data or the method that we want to add with the name given in
110 slot. What the heck was implied by the location arg?"))
112 (defgeneric delete-object (obj proto-struct)
113 (:documentation "remove the symbol from the proto object slot or
114 method list."))
116 (defgeneric objects (proto-struct)
117 (:documentation "return list of object symbols, perhaps slots,
118 methods, parents, objects in precedence, as suggested by the arg."))
120 (defgeneric get-object (objSym proto-struct)
121 (:documentation "accessor for the value of the symbol in the
122 particular instance of the prototyping object."))
126 (defclass proto-object ()
127 ((slots
128 :initform (list)
129 :type proto-slots
130 :accessor proto-object-slots )
131 (methods
132 :initform (list)
133 :type proto-methods
134 :accessor proto-object-methods )
135 (parents
136 :initform (make-instance 'parent-list)
137 :type parent-list
138 :accessor proto-object-parents )
139 (preclist ;; precedence list
140 :initform (make-instance 'preclist)
141 :type preclist
142 :accessor proto-object-preclist
143 :documentation "precedence list." )
144 (serial
145 :initform (incf *proto-object-serial*)
146 :type integer
147 :accessor proto-object-serial
148 :documentation "Similar idea to serial number." )
149 (self2
150 :initform nil
151 :type integer
152 :accessor proto-self
153 :documentation "can we embed the global within the class structure?" )))
155 ;; We denote default-ish proto-object variable names by po or po?.
157 (defvar *proto-object* (make-instance 'proto-object)
158 "*proto-object* is the global root object.")
160 (defun proto-object-p (x)
161 "Args: (x)
162 Returns T if X is an object, NIL otherwise. Do we really need this?"
163 (typep x 'proto-object))
165 (defun print-proto-object-structure (po stream depth)
166 (declare (ignore depth))
167 (send po :print stream))
169 ;;; AJR:FIXME:Is this going to cause issues with concurrency/threading?
170 ;;; (need to appropriately handle interrupts).
171 (defvar *proto-self* nil
172 "special variable to hold current value of SELF.
173 Assign to current object that we are working with. Local to proto package.
174 Currently working to embed within the object structure rather than a global.")
176 ;; The way that self works is that we try to make sure that we set
177 ;; *self* upon message entry and unset at message exit. This is a
178 ;; good strategy provided that concurrency is not in play.
180 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
182 ;;; Predicates for Consistency Checking
184 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
186 (defun non-nil-symbol-p (x)
187 (unless (and x (symbolp x)) (error "bad symbol - ~s" x)))
189 (defun check-object (po)
190 "Returns self if true, throws an error otherwise."
191 (if (proto-object-p po) po (error "bad object - ~s" po)))
193 (defun kind-of-p (pox poy)
194 "Args: (x y)
195 Returns T if X and Y are objects and X inherits from Y, NIL otherwise."
196 (if (and (proto-object-p pox) (proto-object-p poy))
197 (if (member poy (proto-object-preclist pox)) t nil)
198 nil))
200 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
202 ;;; Precedence List Functions
204 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
206 (defgeneric find-SC (po)
207 (:documentation "Return a copy of the complete precedence list for po."))
209 (defmethod find-SC ((po proto-object))
210 (copy-list (proto-object-preclist po)))
213 (defgeneric find-S (po)
214 (:documentation "return a reverse-sorted, duplicate-free list of
215 parent objects."))
217 (defmethod find-S ((po proto-object))
218 (do ((result nil)
219 (parents (proto-object-parents po) (rest parents)))
220 ((not (consp parents))
221 (delete-duplicates (cons po result)))
222 (setf result (nconc (find-SC (first parents)) result))))
225 (defgeneric find-RC (po)
226 (:documentation "find local precedence ordering."))
228 (defmethod find-RC (po)
229 (let ((list (copy-list (proto-object-parents po))))
230 (do ((next list (rest next)))
231 ((not (consp next)) list)
232 (setf (first next) (cons po (first next)))
233 (setf po (rest (first next))))))
236 (defgeneric find-R (S)
237 (:documentation "find partial precedence ordering."))
239 (defmethod find-R ((S proto-object))
240 (do ((result nil)
241 (S S (rest S)))
242 ((not (consp S))
243 (delete-duplicates result))
244 (setf result (nconc result (find-RC (first S))))))
247 (defun has-predecessor (x R)
248 "check if x has a predecessor according to R."
249 (dolist (cell R nil)
250 (if (and (consp cell) (eq x (rest cell))) (return t))))
252 (defun find-no-predecessor-list (S R)
253 "find list of objects in S without predecessors, by R."
254 (let ((result nil))
255 (dolist (x S result)
256 (unless (has-predecessor x R) (setf result (cons x result))))))
258 (defun child-position (x P)
259 "find the position of child, if any, of x in P, the list found so
260 far."
261 (let ((count 0))
262 (declare (fixnum count))
263 (dolist (next P -1)
264 (if (member x (proto-object-parents next)) (return count))
265 (incf count))))
267 (defun next-object (no-preds P)
268 "find the next object in the precedence list from objects with no
269 predecessor and current list."
270 (cond
271 ((not (consp no-preds)) nil)
272 ((not (consp (rest no-preds))) (first no-preds))
274 (let ((count -1)
275 (result nil))
276 (declare (fixnum count))
277 (dolist (x no-preds result)
278 (let ((tcount (child-position x P)))
279 (declare (fixnum tcount))
280 (when (> tcount count)
281 (setf result x)
282 (setf count tcount))))))))
284 (defun trim-S (x S)
285 "Remove object x from S."
286 (delete x S))
288 (defun trim-R (x R)
289 "Remove all pairs containing x from R. x is assumed to have no
290 predecessors, so only the first position is checked."
291 (delete x R :key #'first))
293 (defun precedence-list (object)
294 "Calculate the object's precedence list."
295 (do* ((S (find-S object))
296 (R (find-R S))
297 (P nil)
298 (no-preds nil)
299 (next nil))
300 ((not (consp S)) P)
301 (setf no-preds (find-no-predecessor-list S R))
302 (setf next (next-object no-preds P))
303 (if (null next) (error "inconsistent precedence order"))
304 (setf P (nconc P (list next)))
305 (setf S (trim-S next S))
306 (setf R (trim-R next R))))
308 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
310 ;;; Object Construction Functions
312 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
314 (defun calculate-preclist (object)
315 "Return the precedence list for the object."
316 (let ((parents (proto-object-parents (check-object object))))
317 (if (not (consp parents)) (error "bad parent list - ~s" parents))
318 (if (consp (rest parents))
319 (precedence-list object)
320 (let ((parent (check-object (first parents))))
321 (cons object (proto-object-preclist parent))))))
323 (defun has-duplicates (list)
324 "predicate: takes a list, and returns true if duplicates.
325 This should be simpler, right? Used in next function only?"
326 (do ((next list (rest next)))
327 ((not (consp next)) nil)
328 (if (member (first next) (rest next)) (return t))))
330 (defun check-parents (parents)
331 "Ensure valid parents: They must be null, object, or consp without duplicates."
332 (cond
333 ((or (null parents) (proto-object-p parents)) parents)
334 ((consp parents)
335 (dolist (parent parents) (check-object parent))
336 (if (has-duplicates parents)
337 (error "parents may not contain duplicates")))
338 (t (error "bad parents - ~s" parents))))
340 (defun make-basic-object (parents object)
341 "Creates a basic object for the prototype system by ensuring that it
342 can be placed into the storage heirarchy.
343 If object is not initialized, instantiate the structure.
344 Place into parental structure.
345 If parents is null, use root *object*,
346 if parents is a single object, use it (encapsulate as list)
347 otherwise, use parents"
349 (check-parents parents)
350 (if (not (proto-object-p object))
351 (setf object (make-instance
352 'proto-object
353 :preclist (proto-object-preclist *proto-object*)
354 :parents
355 (cond ((null parents) (list *proto-object*))
356 ((proto-object-p parents) (list parents))
357 (t parents)))))
358 (setf (proto-object-preclist object) (calculate-preclist object))
359 object)
361 (defun make-object (&rest parents)
362 "Args: (&rest parents)
363 Returns a new object with parents PARENTS. If PARENTS is NIL,
364 (list *PROTO-OBJECT*) is used."
365 (make-basic-object parents NIL))
367 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
368 ;;;;
369 ;;;; Constraint Hook Functions
370 ;;;;
371 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
373 (pushnew :constrainthooks *features*)
375 #+:constrainthooks
376 (progn
377 (defvar *message-hook* nil)
378 (defvar *set-slot-hook* nil)
380 (defun check-constraint-hooks (object sym slot)
381 (let ((hook (if slot *set-slot-hook* *message-hook*)))
382 (if hook
383 (if slot
384 (let ((*set-slot-hook* nil))
385 (funcall hook object sym))
386 (let ((*message-hook* nil))
387 (funcall hook object sym)))))))
389 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
391 ;;; Slot Access Functions
393 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
395 ;;; AJR: should specialize appropriately, the following:
396 (defun make-slot-entry (x y) (cons x y))
397 (defun slot-entry-p (x) (consp x))
398 (defun slot-entry-key (x) (first x))
399 (defun slot-entry-value (x) (rest x))
400 (defun set-slot-entry-value (x v) (setf (rest x) v))
401 (defsetf slot-entry-value set-slot-entry-value)
403 (defun find-own-slot (x slot)
404 (if (proto-object-p x) (assoc slot (proto-object-slots x))))
406 (defun find-slot (x slot)
407 (if (proto-object-p x)
408 (let ((preclist (proto-object-preclist x)))
409 (dolist (object preclist)
410 (let ((slot-entry (find-own-slot object slot)))
411 (if slot-entry (return slot-entry)))))))
413 ;; To remove.
414 (defun add-slot (x slot value)
415 "Remove when completely replaced by add-object methods."
416 (add-object x slot value))
418 (defmethod add-object ((x (type proto-object)
419 (slot symbol)
420 &optional
421 init)) ;; location))
422 ;; (check-object x)
423 ;; (non-nil-symbol-p slot)
424 ; #+nil(if (nilp slot)
425 ; ;; This is wrong but has the right flavor of what should be
426 ; ;; happening.
427 ; (setf slot (gensym)))
428 (let ((slot-entry (find-own-slot x slot)))
429 (if slot-entry
430 (setf (slot-entry-value slot-entry) init)
431 (setf (proto-object-slots x)
432 (cons (make-slot-entry slot init) (proto-object-slots x)))))
433 nil) ;; I think we want to return something, but what?
435 ;; This might be more appropriate as a "setter" dispatching on a
436 ;; (proto-object slot)
437 ;; argument.
439 ;; REMOVE ME when obsolete
440 (defun delete-slot (x slot)
441 (delete-object x slot))
443 (defmethod delete-object ((x proto-object)
444 (slot symbol))
445 ;; (check-object x)
446 (setf (proto-object-slots x)
447 (delete slot (proto-object-slots x) :key #'slot-entry-key)))
449 (defun get-slot-value (x slot &optional no-err)
450 (check-object x)
451 (let ((slot-entry (find-slot x slot)))
452 (if (slot-entry-p slot-entry)
453 (slot-entry-value slot-entry)
454 (unless no-err (error "no slot named ~s in this object" slot)))))
456 (defun set-slot-value (x slot value)
457 (check-object x)
458 (let ((slot-entry (find-own-slot x slot)))
459 (cond
460 ((slot-entry-p slot-entry)
461 (set-slot-entry-value slot-entry value)
462 #+:constrainthooks (check-constraint-hooks x slot t))
464 (if (find-slot x slot)
465 (error "object does not own slot ~s" slot)
466 (error "no slot named ~s in this object" slot))))))
468 (defun get-self ()
469 "Return current proto-object being manipulated. We are going to die
470 in a multithreaded environment."
471 (if (not (proto-object-p *proto-self*))
472 (error "not in a method"))
473 *proto-self*)
475 (defun proto-slot-value (slot)
476 "Args: (slot)
477 Must be used in a method. Returns the value of current objects slot
478 named SLOT."
479 (get-slot-value (get-self) slot))
481 (defun proto-slot-value-setf (slot value)
482 (set-slot-value (get-self) slot value))
484 (defsetf proto-slot-value proto-slot-value-setf)
486 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
487 ;;;;
488 ;;;; Method Access Functions;
489 ;;;;
490 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
492 (defun make-method-entry (x y) (cons x y))
493 (defun method-entry-p (x) (consp x))
494 (defun method-entry-key (x) (first x))
495 (defun method-entry-method (x) (rest x))
496 (defun set-method-entry-method (x v) (setf (rest x) v))
497 (defsetf method-entry-method set-method-entry-method)
499 (defun find-own-method (x selector)
500 (if (proto-object-p x) (assoc selector (proto-object-methods x))))
502 (defun find-lsos-method (x selector)
503 (if (proto-object-p x)
504 (let ((preclist (proto-object-preclist x)))
505 (dolist (object preclist)
506 (let ((method-entry (find-own-method object selector)))
507 (if method-entry (return method-entry)))))))
509 (defun add-lsos-method (x selector value)
510 "x = object; selector = name of method; value = form computing the method."
511 (check-object x)
512 (non-nil-symbol-p selector)
513 (let ((method-entry (find-own-method x selector)))
514 (if method-entry
515 (setf (method-entry-method method-entry) value)
516 (setf (proto-object-methods x)
517 (cons (make-method-entry selector value) (proto-object-methods x)))))
518 nil)
520 (defun delete-method (x selector)
521 (check-object x)
522 (setf (proto-object-methods x)
523 (delete selector (proto-object-methods x) :key #'method-entry-key)))
525 (defun get-message-method (x selector &optional no-err)
526 (check-object x)
527 (let ((method-entry (find-lsos-method x selector)))
528 (if (method-entry-p method-entry)
529 (method-entry-method method-entry)
530 (unless no-err (error "no method for selector ~s" selector)))))
532 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
534 ;;; Message Sending Functions
536 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
538 (defvar *current-preclist* nil)
539 (defvar *current-selector* nil)
541 (defun sendmsg (object selector preclist args)
542 (let ((method-entry nil)
543 (method nil))
545 ;; look for the message in the precedence list
546 (loop
547 (setf method-entry (find-own-method (first preclist) selector))
548 (if (or method-entry (not (consp preclist))) (return))
549 (setf preclist (rest preclist)))
550 (cond
551 ((null method-entry) (error "no method for selector ~s" selector))
552 ((not (method-entry-p method-entry)) (error "bad method entry"))
553 (t (setf method (method-entry-method method-entry))))
555 ;; invoke the method
556 (let ((*current-preclist* preclist)
557 (*current-selector* selector)
558 (*proto-self* object))
559 (multiple-value-prog1
560 (apply method object args)
561 #+:constrainthooks (check-constraint-hooks object selector nil)))))
563 ;;;; built-in send function
564 (defun send (object selector &rest args)
565 "Args: (object selector &rest args)
566 Applies first method for SELECTOR found in OBJECT's precedence list to
567 OBJECT and ARGS."
568 (sendmsg object selector (proto-object-preclist object) args))
570 ;;;; call-next-proto-method - call inherited version of current method
571 (defun call-next-proto-method (&rest args)
572 "Args (&rest args)
573 Funcalls next method for current selector and precedence list. Can only be
574 used in a method."
575 (sendmsg *proto-self* *current-selector* (rest *current-preclist*) args))
577 (defun call-proto-method (object selector &rest args)
578 "Args (object selector &rest args)
579 Funcalls method for SELECTOR found in OBJECT to SELF. Can only be used in
580 a method.
581 Call method belonging to another object on current object."
582 (sendmsg *proto-self* selector (proto-object-preclist object) args))
584 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
586 ;;; Object Documentation
588 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
590 (defun find-documentation (x sym add)
591 (if (proto-object-p x)
592 (let ((doc (find-own-slot x 'documentation)))
593 (if (and (null doc) add) (add-slot x 'documentation nil))
594 (if (slot-entry-p doc) (assoc sym (slot-entry-value doc))))))
596 (defun add-documentation (x sym value)
597 (check-object x)
598 (non-nil-symbol-p sym)
599 (let ((doc-entry (find-documentation x sym t)))
600 (cond
601 ((not (null doc-entry))
602 (setf (rest doc-entry) value))
604 (set-slot-value x
605 'documentation
606 (cons (cons sym value)
607 (get-slot-value x 'documentation))))))
608 nil)
610 (defun get-documentation (x sym)
611 (check-object x)
612 (dolist (object (proto-object-preclist x))
613 (let ((doc-entry (find-documentation object sym nil))) ;; FIXME: verify
614 (if doc-entry (return (rest doc-entry))))))
616 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
617 ;;;;
618 ;;;; DEFMETH Macro
619 ;;;;
620 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
622 (defmacro defmeth (object name arglist first &rest body)
623 "Syntax: (defmeth object method-name lambda-list [doc] {form}*)
624 OBJECT must evaluate to an existing object. Installs a method for NAME in
625 the value of OBJECT and installs DOC in OBJECTS's documentation.
626 RETURNS: method-name."
627 (declare (ignorable self)) ;; hints for the compiler that sometimes it isn't used
628 (if (and body (stringp first))
629 `(progn ;; first=docstring + body
630 (add-lsos-method ,object ,name
631 #'(lambda (self ,@arglist) (block ,name ,@body)))
632 (add-documentation ,object ,name ,first)
633 ,name)
634 `(progn ;; first=code + body
635 (add-lsos-method ,object ,name
636 #'(lambda (self ,@arglist) (block ,name ,first ,@body)))
637 ,name)))
639 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
641 ;;; Prototype Construction
643 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
645 (defun find-instance-slots (x slots)
646 (let ((result (nreverse (delete-duplicates (copy-list slots)))))
647 (dolist (parent (proto-object-parents x) (nreverse result))
648 (dolist (slot (get-slot-value parent 'instance-slots))
649 (pushnew slot result)))))
651 (defun get-initial-slot-value (object slot)
652 (let ((entry (find-slot object slot)))
653 (if (slot-entry-p entry) (slot-entry-value entry))))
655 (defun make-prototype (object name ivars cvars doc set)
656 (setf ivars (find-instance-slots object ivars))
657 (add-slot object 'instance-slots ivars)
658 (add-slot object 'proto-name name)
659 (dolist (slot ivars)
660 (add-slot object slot (get-initial-slot-value object slot)))
661 (dolist (slot cvars)
662 (add-slot object slot nil))
664 (if (and doc (stringp doc))
665 (add-documentation object 'proto doc))
666 (if set (setf (symbol-value name) object)))
669 (defmacro defproto (name &optional ivars cvars parents doc)
670 "Syntax (defproto name &optional ivars cvars (parent *proto-object*) doc)
671 Makes a new object prototype with instance variables IVARS, 'class'
672 variables CVARS and parents PARENT. PARENT can be a single object or
673 a list of objects. IVARS and CVARS must be lists."
674 (let ((obsym (gensym))
675 (namesym (gensym))
676 (parsym (gensym)))
677 `(progn
678 (let* ((,namesym ',name)
679 (,parsym ,parents)
680 (,obsym (make-basic-object (if (listp ,parsym)
681 ,parsym
682 (list ,parsym)) ;; should this be ,@parsym ?
683 nil)))
684 (make-prototype ,obsym ,namesym ,ivars ,cvars ,doc t)
685 ,namesym))))
688 ;; Infrastructure for new defproto from Common-Lisp Cookbook! Thanks!
690 ;(defmacro odd-define (name buildargs)
691 ; `(progn (defun ,(build-symbol make-a- (:< name))
692 ; ,buildargs
693 ; (vector ,(length buildargs) ',name ,@buildargs))
694 ; (defun ,(build-symbol test-whether- (:< name)) (x)
695 ; (and (vectorp x) (eq (aref x 1) ',name))
696 ; (defun ,(build-symbol (:< name) -copy) (x)
697 ; ...)
698 ; (defun ,(build-symbol (:< name) -deactivate) (x)
699 ; ...))))
701 ;(defmacro for (listspec exp)
702 ; (cond ((and (= (length listspec) 3)
703 ; (symbolp (car listspec))
704 ; (eq (cadr listspec) ':in))
705 ; `(mapcar (lambda (,(car listspec))
706 ; ,exp)
707 ; ,(caddr listspec)))
708 ; (t (error "Ill-formed: ~s" `(for ,listspec ,exp)))))
710 ;(defmacro symstuff (l)
711 ; `(concatenate 'string
712 ; ,@(for (x :in l)
713 ; (cond ((stringp x)
714 ; `',x)
715 ; ((atom x)
716 ; `',(format nil "~a" x))
717 ; ((eq (car x) ':<)
718 ; `(format nil "~a" ,(cadr x)))
719 ; ((eq (car x) ':++)
720 ; `(format nil "~a" (incf ,(cadr x))))
721 ; (t
722 ; `(format nil "~a" ,x))))))
724 ;(defmacro build-symbol (&rest l)
725 ; (let ((p (find-if (lambda (x)
726 ; (and (consp x)
727 ; (eq (car x) ':package)))
728 ; l)))
729 ; (cond (p
730 ; (setq l (remove p l))))
731 ; (let ((pkg (cond ((eq (cadr p) 'nil)
732 ; nil)
733 ; (t `(find-package ',(cadr p))))))
734 ; (cond (p
735 ; (cond (pkg
736 ; `(values (intern ,(symstuff l) ,pkg)))
737 ; (t
738 ; `(make-symbol ,(symstuff l)))))
739 ; (t
740 ; `(values (intern ,(symstuff l))))))))
742 (defmacro defproto2 (name &optional ivars cvars parents doc force)
743 "Syntax (defproto name &optional ivars cvars (parent *proto-object*) doc)
744 Makes a new object prototype with instance variables IVARS, 'class'
745 variables CVARS and parents PARENT. PARENT can be a single object or
746 a list of objects. IVARS and CVARS must be lists. DOC should be a
747 string."
748 (if (and (boundp name)
749 (not force))
750 (error "Force T to rebind a prototype object.")
751 (let ((obsym (gensym))
752 (parsym (gensym)))
753 `(progn
754 (defvar ,name (list) ,doc)
755 (let* ((,parsym ,parents)
756 (,obsym (make-basic-object
757 (if (listp ,parsym)
758 ,parsym
759 (list ,@parsym)) ;; should this be ,@parsym ?
760 nil)))
761 (make-prototype ,obsym ,name ,ivars ,cvars ,doc t)
762 ,name)))))
764 ;; (macro-expand-1 (defproto2 *mytest*))
766 ;; recall:
767 ;; , => turn on evaluation again (not macro substitution)
768 ;; ` => template comes (use , to undo template and restore eval
769 ;; ' => regular quote (not special in this context), 'ted => (quote ted)
772 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
774 ;;; Initialize the Root Object
776 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
778 (setf (proto-object-preclist *proto-object*) (list *proto-object*))
779 (add-slot *proto-object* 'instance-slots nil)
780 (add-slot *proto-object* 'proto-name '*proto-object*)
781 (add-slot *proto-object* 'documentation nil) ; AJR - for SBCL compiler
782 ; issues about macro with
783 ; unknown slot
785 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
787 ;;; *PROTO-OBJECT* Methods
789 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
791 (defmeth *proto-object* :isnew (&rest args)
792 "Method args: (&rest args)
793 Checks ARGS for keyword arguments matching slots and uses them to
794 initialize slots."
795 (if args
796 (dolist (slot-entry (proto-object-slots self))
797 (let* ((slot (slot-entry-key slot-entry))
798 (key (intern (symbol-name slot) (find-package 'keyword)))
799 (val (proto-slot-value slot))
800 (new-val (getf args key val)))
801 (unless (eq val new-val) (setf (proto-slot-value slot) new-val)))))
802 self)
804 (defmeth *proto-object* :has-slot (slot &key own)
805 "Method args: (slot &optional own)
806 Returns T if slot SLOT exists, NIL if not. If OWN is not NIL
807 only checks the object; otherwise check the entire precedence list."
808 (let ((entry (if own (find-own-slot self slot) (find-slot self slot))))
809 (if entry t nil)))
811 (defmeth *proto-object* :add-slot (slot &optional value)
812 "Method args: (slot &optional value)
813 Installs slot SLOT in object, if it does not already exist, and
814 sets its value to VLAUE."
815 (add-slot self slot value)
816 value)
818 (defmeth *proto-object* :delete-slot (slot)
819 "Method args: (slot)
820 Deletes slot SLOT from object if it exists."
821 (delete-slot self slot)
822 nil)
824 (defmeth *proto-object* :own-slots ()
825 "Method args: ()
826 Returns list of names of slots owned by object."
827 (mapcar #'slot-entry-key (proto-object-slots self)))
829 (defmeth *proto-object* :has-method (selector &key own)
830 "Method args: (selector &optional own)
831 Returns T if method for SELECTOR exists, NIL if not. If OWN is not NIL
832 only checks the object; otherwise check the entire precedence list."
833 (let ((entry (if own
834 (find-own-method self selector)
835 (find-lsos-method self selector))))
836 (if entry t nil)))
838 (defmeth *proto-object* :add-method (selector method)
839 "Method args: (selector method)
840 Installs METHOD for SELECTOR in object."
841 (add-lsos-method self selector method)
842 nil)
844 (defmeth *proto-object* :delete-method (selector)
845 "Method args: (selector)
846 Deletes method for SELECTOR in object if it exists."
847 (delete-method self selector)
848 nil)
850 (defmeth *proto-object* :get-method (selector)
851 "Method args: (selector)
852 Returns method for SELECTOR symbol from object's precedence list."
853 (get-message-method self selector))
855 (defmeth *proto-object* :own-methods ()
856 "Method args ()
857 Returns copy of selectors for methods owned by object."
858 (mapcar #'method-entry-key (proto-object-methods self)))
860 (defmeth *proto-object* :parents ()
861 "Method args: ()
862 Returns copy of parents list."
863 (copy-list (proto-object-parents self)))
865 (defmeth *proto-object* :precedence-list ()
866 "Method args: ()
867 Returns copy of the precedence list."
868 (copy-list (proto-object-preclist self)))
870 (defmeth *proto-object* :show (&optional (stream t))
871 "Method Args: ()
872 Prints object's internal data."
873 (format stream "Slots = ~s~%" (proto-object-slots self))
874 (format stream "Methods = ~s~%" (proto-object-methods self))
875 (format stream "Parents = ~s~%" (proto-object-parents self))
876 (format stream "Precedence List = ~s~%" (proto-object-preclist self))
877 nil)
879 (defmeth *proto-object* :reparent (&rest parents)
880 "Method args: (&rest parents)
881 Changes precedence list to correspond to PARENTS. Does not change descendants."
882 (make-basic-object parents self))
884 (defmeth *proto-object* :make-prototype (name &optional ivars)
885 (make-prototype self name ivars nil nil nil)
886 self)
888 (defmeth *proto-object* :internal-doc (sym &optional new)
889 "Method args (topic &optional value)
890 Retrieves or installs documentation for topic."
891 (if new (add-documentation self sym new))
892 (get-documentation self sym))
894 (defmeth *proto-object* :new (&rest args)
895 "Method args: (&rest args)
896 Creates new object using self as prototype."
897 (let* ((object (make-object self)))
898 (if (proto-slot-value 'instance-slots)
899 (dolist (s (proto-slot-value 'instance-slots))
900 (send object :add-slot s (proto-slot-value s))))
901 (apply #'send object :isnew args)
902 object))
904 (defmeth *proto-object* :retype (proto &rest args)
905 "Method args: (proto &rest args)
906 Changes object to inherit directly from prototype PROTO. PROTO
907 must be a prototype and SELF must not be one."
908 (if (send self :has-slot 'instance-slots :own t)
909 (error "can't retype a prototype"))
910 (if (not (send proto :has-slot 'instance-slots :own t))
911 (error "not a prototype - ~a" proto))
912 (send self :reparent proto)
913 (dolist (s (send proto :slot-value 'instance-slots))
914 (send self :add-slot s (proto-slot-value s)))
915 (apply #'send self :isnew args)
916 self)
918 (defmeth *proto-object* :print (&optional (stream *standard-output*))
919 "Method args: (&optional (stream *standard-output*))
920 Default object printing method."
921 (cond
922 ((send self :has-slot 'proto-name)
923 (format stream
924 "#<Object: ~D, prototype = ~A>"
925 (proto-object-serial self)
926 (proto-slot-value 'proto-name)))
927 (t (format stream "#<Object: ~D>" (proto-object-serial self)))))
929 (defmeth *proto-object* :slot-value (sym &optional (val nil set))
930 "Method args: (sym &optional val)
931 Sets and retrieves value of slot named SYM. Signals an error if slot
932 does not exist."
933 (if set (setf (proto-slot-value sym) val))
934 (proto-slot-value sym))
936 (defmeth *proto-object* :slot-names ()
937 "Method args: ()
938 Returns list of slots available to the object."
939 (apply #'append
940 (mapcar #'(lambda (x) (send x :own-slots))
941 (send self :precedence-list))))
943 (defmeth *proto-object* :method-selectors ()
944 "Method args: ()
945 Returns list of method selectors available to object."
946 (apply #'append
947 (mapcar #'(lambda (x) (send x :own-methods))
948 (send self :precedence-list))))
951 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
952 ;;;;
953 ;;;; Object Help Methods
954 ;;;;
955 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
957 (defmeth *proto-object* :doc-topics ()
958 "Method args: ()
959 Returns all topics with documentation for this object."
960 (remove-duplicates
961 (mapcar #'car
962 (apply #'append
963 (mapcar
964 #'(lambda (x)
965 (if (send x :has-slot 'documentation :own t)
966 (send x :slot-value (quote documentation))))
967 (send self :precedence-list))))))
969 (defmeth *proto-object* :documentation (topic &optional (val nil set))
970 "Method args: (topic &optional val)
971 Retrieves or sets object documentation for topic."
972 (if set (send self :internal-doc topic val))
973 (let ((val (dolist (i (send self :precedence-list))
974 (let ((val (send i :internal-doc topic)))
975 (if val (return val))))))
976 val))
978 (defmeth *proto-object* :delete-documentation (topic)
979 "Method args: (topic)
980 Deletes object documentation for TOPIC."
981 (setf (proto-slot-value 'documentation)
982 ;;(remove :title nil :test #'(lambda (x y) (eql x (first y)))) ;; original
983 (remove topic (send self :documentation) :test #'(lambda (x y) (eql x (first y))))) ;; AJR:PROBLEM?
984 nil)
986 (defmeth *proto-object* :help (&optional topic)
987 "Method args: (&optional topic)
988 Prints help message for TOPIC, or genreal help if TOPIC is NIL."
989 (if topic
990 (let ((doc (send self :documentation topic)))
991 (cond
992 (doc (princ topic) (terpri) (princ doc) (terpri))
993 (t (format t "Sorry, no help available on ~a~%" topic))))
994 (let ((topics (stable-sort (copy-seq (send self :doc-topics))
995 #'(lambda (x y)
996 (string-lessp (string x) (string y)))))
997 (proto-doc (send self :documentation 'proto)))
998 (if (send self :has-slot 'proto-name)
999 (format t "~s~%" (proto-slot-value 'proto-name)))
1000 (when proto-doc (princ proto-doc) (terpri))
1001 (format t "Help is available on the following:~%~%")
1002 (dolist (i topics) (format t "~s " i))
1003 (terpri)))
1004 (values))
1006 (defmeth *proto-object* :compile-method (name)
1007 "Method args: (name)
1008 Compiles method NAME unless it is already compiled. The object must
1009 own the method."
1010 (unless (send self :has-method name)
1011 (error "No ~s method in this object" name))
1012 (unless (send self :has-method name :own t)
1013 (error "Object does not own ~s method" name))
1014 (let ((fun (send self :get-method name)))
1015 (unless (compiled-function-p fun)
1016 (multiple-value-bind (form env) (function-lambda-expression fun)
1017 (if env
1018 (error
1019 "method may have been defined in non-null environment"))
1020 (send self :add-method name (compile nil form))))))