Page 1 of 1

Using (eql eql-specializer-form) in defmethod

Posted: Wed Oct 05, 2011 2:47 am
by wvxvw
Hi. I'm trying to come up with something like this (which doesn't work):

Code: Select all

(defmethod write-int-to-string ((x (eql (mod x #x7FFFFFFF))))
  (write-to-string x))
but I think it illustrates the problem. It wouldn't work because it doesn't know what `x' is in the mod application. How would I write that in a general case? That is when I needed an eql-specializer-form to know / use the argument.
Thanks in advance.

Re: Using (eql eql-specializer-form) in defmethod

Posted: Wed Oct 05, 2011 8:11 am
by ramarren
This would be equivalent to predicate dispatch, which is problematic because there is no general way to create a deterministic ordering for method application.

One way to solve this is to use something like filtered-dispatch, but I have never used that in practice. The simpler solution is to just use a single function/method with a COND. This will provide explicit ordering at the cost of extensibility.

Re: Using (eql eql-specializer-form) in defmethod

Posted: Wed Oct 05, 2011 8:16 pm
by nuntius
It can be useful to write generic functions that dispatch to other generic functions. (looks similar to the filtered-dispatch library, but not hard to hand-roll many uses)

Example from some simple computer algebra code:

Code: Select all

(defgeneric simplify (form))
(defgeneric simplify-list (car cdr))

(defmethod simplify ((form list))
  (simplify-list (car form) (cdr form)))
(defmethod simplify ((form array))
  ...)

(defmethod simplify-list ((car (eql '+)) cdr)
  ...)
(defmethod simplify-list ((car (eql '*)) cdr)
  ...)

Re: Using (eql eql-specializer-form) in defmethod

Posted: Thu Oct 06, 2011 12:16 am
by wvxvw
Aha, thank you. So it's not supposed to be possible that way :) I wasn't after a complicated solution, I think the simple cond will do just fine in my case.
Also, it's a nice example, I wouldn't work out on my own, nuntius.