I'd like to be able to selectively memoize certain methods associated with a
generic function. Given something like the following
(defgeneric foobar (x))
(defmethod foobar ((x (eql 100))) ;; A
100)
(defmethod foobar ((x fixnum)) ;; B
(* x 2))
I'd like to be able to memoize the second foobar method, method B, without
memoizing the one specialized for 100, method A. Also, I'd like the
memoization wrapping to work in the presence of :around methods or other
complications. I need to get control "around" all the methods, including
non-primary ones, in order to store the correct method result. Ideally, the
user would be able to say something like
(defmethod-memo foobar ((x fixnum))
(* x 2))
to memoize only the fixnum method.
At first, I conjectured that the right approach might be to use different
method classes for methods A and B. I'd use standard-method for A and a new
subclass of standard-method, say wrapped-method, for B. I'd then specialize
make-method-lambda for the new wrapped-method subclass. It looks like this
approach might not work, however, since there doesn't seem to be an easy way
to make the methods of one generic function be of different types.
Maybe define-method-combination is the right tool to use, instead of
trying to specialize make-method-lambda.
Sadly, I'm a MOP beginner, so I'd appreciate hints from an expert.
bob
Robert E. Brown wrote:
> ... Ideally, the
> user would be able to say something like
>
> (defmethod-memo foobar ((x fixnum))
> (* x 2))
>
.....
>
> Sadly, I'm a MOP beginner, so I'd appreciate hints from an expert.
As one who took a pet project from non-MOP to MOP and back to non-MOP
implementation (the last for performance and portability to non-MOP
CLs), my non-hint would be just to do the memoizing in the macro
defmethod-memo.
btw, my MOP hacking did not get around to mucking with methods, afraid I
have nothing to offer on that. I suppose you could write a defmethod
just like your CLs defmethod (if it exposes the MOP) and substitute
another method class for generic-function-method-class, but then you may
as well just expand directly into memoizing logic and avoid the
(non-portable, if that matters) MOP-wrestling.
--
kenny tilton
clinisys, inc
http://www.tilton-technology.com/
---------------------------------------------------------------
"Everything is a cell." -- Alan Kay
* Robert E Brown wrote:
> Maybe define-method-combination is the right tool to use, instead of
> trying to specialize make-method-lambda.
yes, it is. Define a method combination which supports a new
super-around method qualifier which gets control outside all the
standard ones, and use that to do the memoization.
--tim
Robert E. Brown wrote:
> (defmethod foobar ((x (eql 100))) ;; A
> 100)
>
> (defmethod foobar ((x fixnum)) ;; B
> (* x 2))
Whatever else you eventually do, take note that the FIXNUM class
is not specified by the ANS. See Section 4.3.7. Some implementations
provide it, which is allowed by Section 11.1.2.1.1, but user code that
depends on this class is not portable Common Lisp.