[Openmcl-devel] Redefining Mathematical Operators?

Ron Garret ron at flownet.com
Thu Jul 9 15:22:18 PDT 2015


On Jul 9, 2015, at 2:41 PM, Robert Goldman <rpgoldman at sift.net> wrote:

> On 7/9/15 Jul 9 -4:37 PM, Steven Núñez wrote:
>> Greetings fellow Clozurians,
>> 
>> I have a legacy codebase in xlisp-stat and to run on CCL I need to redefine the mathematics operators to work on both numbers and lists. Ideally I'd be able to do something like this:
>> 
>> (defgeneric + (x y))
>> (defmethod + ((x number) (y number)) ...)	; Add x to y
>> (defmethod + ((x number) (y list)) ...)		; Add x to every element of y
>> (defmethod + ((x list) (y list)) ...)		; Add elements of x and y pairwise
>> 
>> However '+' isn't a generic function:
>> 
>> ? (defmethod + ((x number) (y number)) t)
>>> Error: The function + is defined as something other than a generic function.
>>> While executing: ENSURE-GENERIC-FUNCTION, in process listener(1).
>> 
>> so I thought I would undefine it and then redefine it as a generic. Something like this:
>> 
>> (defparameter *+* #'+)		; Save + for later use
>> (fmakunbound '+)			; Undefine it so we can use a multi-method
>> (defgeneric + (x y))
>> (defmethod + ((x number) (y number))
>>  (funcall *+* x y))
>> 
>> But it looks like the '+' operator is used somewhere in the compiler:
>> 
>> ? (defgeneric + (x y))
>>> Error: Undefined function + called with arguments (0 8) . signaled during assembly-time evaluation of form (+ 0 8)
>>> While executing: CCL::PARSE-X86-LAP-EXPRESSION, in process listener(1).
>> 
>> Has anyone got any ideas how to redefine these operators? I supposed a bit of macro-magic might do the trick, but I thought I'd ask here to see if anyone has done anything like this before and if there's anything I should watch out for if I have to go the macro route?
> 
> 
> I believe what you should probably do is SHADOW these operators in a
> different package, and do your generic-function-ifying in that package
> (or something that uses that package in a way that also shadows the CL
> built-ins).

You can do it in the cl-user package:

Welcome to Clozure Common Lisp Version 1.10-r16304M  (DarwinX8664)!
? (Shadow '+)
T
? (defmethod + (x y) (cl:+ x y))
#<STANDARD-METHOD + (T T)>
? (defmethod + ((x string) (y string)) (concatenate 'string x y))
#<STANDARD-METHOD + (STRING STRING)>
? (+ 1 2)
3
? (+ "foo" "baz")
“foobaz"




More information about the Openmcl-devel mailing list