Using (eql eql-specializer-form) in defmethod

Discussion of Common Lisp
Post Reply
wvxvw
Posts: 127
Joined: Sat Mar 26, 2011 6:23 am

Using (eql eql-specializer-form) in defmethod

Post by wvxvw » Wed Oct 05, 2011 2:47 am

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.

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

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

Post by ramarren » Wed Oct 05, 2011 8:11 am

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.

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

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

Post by nuntius » Wed Oct 05, 2011 8:16 pm

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)
  ...)

wvxvw
Posts: 127
Joined: Sat Mar 26, 2011 6:23 am

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

Post by wvxvw » Thu Oct 06, 2011 12:16 am

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.

Post Reply