Fix #4413: In def-simplifier, add noun property
[maxima.git] / src / defmfun-check.lisp
blob8569c2c064cd9efb0ef36283fe085b32b9e417f9
1 ;;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
3 (in-package :maxima)
5 ;; We got this code from cmucl, so we don't actually need all of this.
6 #+cmucl
7 (progn
8 (defun parse-lambda-list (list)
9 (kernel:parse-lambda-list list))
10 (defun parse-body (body environment &optional (doc-string-allowed t))
11 (system:parse-body body environment doc-string-allowed))
14 #-cmucl
15 (eval-when (compile load eval)
16 ;;;; Borrowed from cmucl src/code/extensions.lisp. Used in parsing
17 ;;;; lambda lists.
19 ;;;; The Collect macro:
21 ;;; Collect-Normal-Expander -- Internal
22 ;;;
23 ;;; This function does the real work of macroexpansion for normal collection
24 ;;; macros. N-Value is the name of the variable which holds the current
25 ;;; value. Fun is the function which does collection. Forms is the list of
26 ;;; forms whose values we are supposed to collect.
27 ;;;
28 (defun collect-normal-expander (n-value fun forms)
29 `(progn
30 ,@(mapcar #'(lambda (form) `(setq ,n-value (,fun ,form ,n-value))) forms)
31 ,n-value))
33 ;;; Collect-List-Expander -- Internal
34 ;;;
35 ;;; This function deals with the list collection case. N-Tail is the pointer
36 ;;; to the current tail of the list, which is NIL if the list is empty.
37 ;;;
38 (defun collect-list-expander (n-value n-tail forms)
39 (let ((n-res (gensym)))
40 `(progn
41 ,@(mapcar #'(lambda (form)
42 `(let ((,n-res (cons ,form nil)))
43 (cond (,n-tail
44 (setf (cdr ,n-tail) ,n-res)
45 (setq ,n-tail ,n-res))
47 (setq ,n-tail ,n-res ,n-value ,n-res)))))
48 forms)
49 ,n-value)))
52 ;;; Collect -- Public
53 ;;;
54 ;;; The ultimate collection macro...
55 ;;;
56 (defmacro collect (collections &body body)
57 "Collect ({(Name [Initial-Value] [Function])}*) {Form}*
58 Collect some values somehow. Each of the collections specifies a bunch of
59 things which collected during the evaluation of the body of the form. The
60 name of the collection is used to define a local macro, a la MACROLET.
61 Within the body, this macro will evaluate each of its arguments and collect
62 the result, returning the current value after the collection is done. The
63 body is evaluated as a PROGN; to get the final values when you are done, just
64 call the collection macro with no arguments.
66 Initial-Value is the value that the collection starts out with, which
67 defaults to NIL. Function is the function which does the collection. It is
68 a function which will accept two arguments: the value to be collected and the
69 current collection. The result of the function is made the new value for the
70 collection. As a totally magical special-case, the Function may be Collect,
71 which tells us to build a list in forward order; this is the default. If an
72 Initial-Value is supplied for Collect, the stuff will be rplacd'd onto the
73 end. Note that Function may be anything that can appear in the functional
74 position, including macros and lambdas."
76 (let ((macros ())
77 (binds ()))
78 (dolist (spec collections)
79 (unless (<= 1 (length spec) 3)
80 (error (intl:gettext "Malformed collection specifier: ~S.") spec))
81 (let ((n-value (gensym))
82 (name (first spec))
83 (default (second spec))
84 (kind (or (third spec) 'collect)))
85 (push `(,n-value ,default) binds)
86 (if (eq kind 'collect)
87 (let ((n-tail (gensym)))
88 (if default
89 (push `(,n-tail (last ,n-value)) binds)
90 (push n-tail binds))
91 (push `(,name (&rest args)
92 (collect-list-expander ',n-value ',n-tail args))
93 macros))
94 (push `(,name (&rest args)
95 (collect-normal-expander ',n-value ',kind args))
96 macros))))
97 `(macrolet ,macros (let* ,(nreverse binds) ,@body))))
99 ;;; Borrowed from cmucl src/compiler/proclaim.lisp
101 ;;; Parse-Lambda-List -- Interface
103 ;;; Break a lambda-list into its component parts. We return eleven values:
104 ;;; 1] A list of the required args.
105 ;;; 2] A list of the optional arg specs.
106 ;;; 3] True if a rest arg was specified.
107 ;;; 4] The rest arg.
108 ;;; 5] A boolean indicating whether keywords args are present.
109 ;;; 6] A list of the keyword arg specs.
110 ;;; 7] True if &allow-other-keys was specified.
111 ;;; 8] A list of the &aux specifiers.
112 ;;; 9] True if a more arg was specified.
113 ;;; 10] The &more context var
114 ;;; 11] The &more count var
116 ;;; The top-level lambda-list syntax is checked for validity, but the arg
117 ;;; specifiers are just passed through untouched. If something is wrong, we
118 ;;; use Compiler-Error, aborting compilation to the last recovery point.
120 (defun parse-lambda-list (list)
121 (declare (list list))
122 (collect ((required)
123 (optional)
124 (keys)
125 (aux))
126 (flet ((compiler-error (&rest args)
127 (apply #'error args))
128 (compiler-note (&rest args)
129 (apply #'warn args)))
130 (let ((restp nil)
131 (rest nil)
132 (morep nil)
133 (more-context nil)
134 (more-count nil)
135 (keyp nil)
136 (allowp nil)
137 (state :required))
138 (dolist (arg list)
139 ;; check for arguments that have the syntactic form of a
140 ;; keyword argument without being a recognized lambda-list keyword
141 (when (and (symbolp arg)
142 (let ((name (symbol-name arg)))
143 (and (/= (length name) 0)
144 (char= (char name 0) #\&))))
145 (unless (member arg lambda-list-keywords)
146 (compiler-note
147 "~S uses lambda-list keyword naming convention, but is not a recognized lambda-list keyword."
148 arg)))
149 (if (member arg lambda-list-keywords)
150 (ecase arg
151 (&optional
152 (unless (eq state :required)
153 (compiler-error "Misplaced &optional in lambda-list: ~S." list))
154 (setq state '&optional))
155 (&rest
156 (unless (member state '(:required &optional))
157 (compiler-error "Misplaced &rest in lambda-list: ~S." list))
158 (setq state '&rest))
159 (&more
160 (unless (member state '(:required &optional))
161 (compiler-error "Misplaced &more in lambda-list: ~S." list))
162 (setq morep t state '&more-context))
163 (&key
164 (unless (member state '(:required &optional :post-rest
165 :post-more))
166 (compiler-error "Misplaced &key in lambda-list: ~S." list))
167 (setq keyp t)
168 (setq state '&key))
169 (&allow-other-keys
170 (unless (eq state '&key)
171 (compiler-error "Misplaced &allow-other-keys in lambda-list: ~S." list))
172 (setq allowp t state '&allow-other-keys))
173 (&aux
174 (when (member state '(&rest &more-context &more-count))
175 (compiler-error "Misplaced &aux in lambda-list: ~S." list))
176 (setq state '&aux)))
177 (case state
178 (:required (required arg))
179 (&optional (optional arg))
180 (&rest
181 (setq restp t rest arg state :post-rest))
182 (&more-context
183 (setq more-context arg state '&more-count))
184 (&more-count
185 (setq more-count arg state :post-more))
186 (&key (keys arg))
187 (&aux (aux arg))
189 (compiler-error "Found garbage in lambda-list when expecting a keyword: ~S." arg)))))
191 (when (eq state '&rest)
192 (compiler-error "&rest not followed by required variable."))
194 (values (required) (optional) restp rest keyp (keys) allowp (aux)
195 morep more-context more-count)))))
197 (defun parse-body (body environment &optional (doc-string-allowed t))
198 "This function is to parse the declarations and doc-string out of the body of
199 a defun-like form. Body is the list of stuff which is to be parsed.
200 Environment is ignored. If Doc-String-Allowed is true, then a doc string
201 will be parsed out of the body and returned. If it is false then a string
202 will terminate the search for declarations. Three values are returned: the
203 tail of Body after the declarations and doc strings, a list of declare forms,
204 and the doc-string, or NIL if none."
205 (declare (ignore environment))
206 (let ((decls ())
207 (doc nil))
208 (do ((tail body (cdr tail)))
209 ((endp tail)
210 (values tail (nreverse decls) doc))
211 (let ((form (car tail)))
212 (cond ((and (stringp form) (cdr tail))
213 (if doc-string-allowed
214 (setq doc form
215 ;; Only one doc string is allowed.
216 doc-string-allowed nil)
217 (return (values tail (nreverse decls) doc))))
218 ((not (and (consp form) (symbolp (car form))))
219 (return (values tail (nreverse decls) doc)))
220 ((eq (car form) 'declare)
221 (push form decls))
223 (return (values tail (nreverse decls) doc))))))))
226 (defun defmfun-keywords (fname options valid-keywords)
227 ;; options looks like (((mequal) $opt1 val1) ((mequal) $opt2 val2) ...)
229 ;; Convert to a new list that looks like (:opt1 val1 :opt2 val2 ...)
231 (unless (listp options)
232 (merror "Invalid Maxima keyword options: ~M" options))
233 (when (every #'(lambda (o)
234 ;; Make sure every option has the right form.
235 (let ((ok (and (listp o)
236 (= (length o) 3)
237 (eq (caar o) 'mequal))))
238 (unless ok
239 (merror (intl:gettext "~M: Badly formed keyword option: ~M")
240 fname o))
241 ok))
242 options)
243 (mapcan #'(lambda (o)
244 (destructuring-bind (mequal opt val)
246 (declare (ignore mequal))
247 (if (or (null valid-keywords)
248 ;; The valid keywords are always verb forms
249 ;; ($foo), so we need to convert OPT to a
250 ;; verb form to be able to match the
251 ;; keywords.
252 (member ($verbify opt) valid-keywords))
253 (flet ((keywordify (x)
254 (intern (subseq (symbol-name x) 1) :keyword)))
255 (list (keywordify opt) val))
256 (merror (intl:gettext "~M: Unrecognized keyword: ~M")
257 fname opt))))
258 options)))
260 ;; Internal macro to do the heavy lifting of defining a function that
261 ;; checks the number of arguments of a function. This is intended to
262 ;; give nice error messages to user-callable functions when the number
263 ;; of arguments is incorrect.
265 ;; The function to check arguments is named NAME. The actual
266 ;; implementation is in a new function named IMPL, which is called by
267 ;; NAME. A compiler-macro is also defined so that Lisp calls of NAME
268 ;; get automatically converted to IMPL.
270 ;; If the keyword :DEPRECATED-P is also specified, then the function
271 ;; is deprecated which causes a warning to be printed once when the
272 ;; function NAME is called the first time. The value of :DEPRECATED-P
273 ;; is a symbol naming the function that should be used instead.
275 ;; For example:
277 ;; (defun-checked-form ($foo foo-impl :deprecated-p $bar) ...)
280 ;; The lambda-list supports &optional and &rest args. Keyword args
281 ;; (&key) are also supported. Maxima keyword args (a=b) are converted
282 ;; to Lisp keywords appropriately. Unrecognized keywords signal a
283 ;; Maxima error.
285 ;; The variable %%PRETTY-FNAME is defined such that the body can refer
286 ;; to this variable to get the pretty name of the defined function for
287 ;; use in printing error messages or what not. This allows the
288 ;; implementation to print out the function name that would also be
289 ;; used when printing out error messages for incorrect number of
290 ;; arguments.
292 (defmacro defun-checked-form ((name impl-name &key deprecated-p) lambda-list &body body)
293 ;; Carefully check the number of arguments and print a nice message
294 ;; if the number doesn't match the expected number.
295 (multiple-value-bind (required-args
296 optional-args
297 restp
298 rest-arg
299 keywords-present-p
300 keyword-args
301 allow-other-keys-p)
302 (parse-lambda-list lambda-list)
304 (when (and keywords-present-p
305 (or optional-args restp))
306 (error "Keyword args cannot be used with optional args or rest args"))
308 (let* ((required-len (length required-args))
309 (optional-len (length optional-args))
310 (impl-doc (format nil "Implementation for ~S" name))
311 (nargs (gensym "NARGS-"))
312 (args (gensym "REST-ARG-"))
313 (rest-name (gensym "REST-ARGS"))
314 (pretty-fname
315 (cond (optional-args
316 ;; Can't do much with optional args, so just use the function name.
317 name)
318 (restp
319 ;; Use maxima syntax for rest args: foo(a,b,[c]);
320 `((,name) ,@required-args ((mlist) ,rest-arg)))
321 (keywords-present-p
322 ;; Not exactly sure how to do this
323 (let* ((index 1)
324 (keys (mapcar
325 #'(lambda (k)
326 (multiple-value-bind (name val)
327 (if (consp k)
328 (values
329 (intern (format nil "$~A" (car k)))
330 (second k))
331 (values
332 (intern (format nil "$~A" k))
333 nil))
334 (incf index)
335 `((mequal) ,name ,val)))
336 keyword-args)))
337 `((,name) ,@required-args ,@keys)))
339 ;; Just have required args: foo(a,b)
340 `((,name) ,@required-args))))
341 (maxima-keywords
342 (unless allow-other-keys-p
343 (mapcar #'(lambda (x)
344 (intern (concatenate
345 'string "$"
346 (symbol-name
347 (if (consp x)
348 (car x)
349 x)))))
350 keyword-args)))
351 (warning-done-var (gensym "WARNING-DONE-")))
353 (multiple-value-bind (forms decls doc-string)
354 (parse-body body nil t)
355 (setf doc-string (if doc-string (list doc-string)))
356 `(progn
357 (defun ,impl-name ,lambda-list
358 ,impl-doc
359 ,@decls
360 (block ,name
361 (let ((%%pretty-fname ',pretty-fname))
362 (declare (ignorable %%pretty-fname))
363 ,@forms)))
365 (let ,(when deprecated-p `((,warning-done-var nil)))
366 (defun ,name (&rest ,args)
367 ,@doc-string
368 ,@(when deprecated-p
369 `((unless ,warning-done-var
370 (setf ,warning-done-var t)
371 (mwarning (aformat nil (intl:gettext "~M is deprecated; use ~M.")
372 ',name ',deprecated-p)))))
373 (let ((,nargs (length ,args)))
374 (declare (ignorable ,nargs))
375 ,@(cond
376 ((or restp keywords-present-p)
377 ;; When a rest arg is given, there's no upper
378 ;; limit to the number of args. Just check that
379 ;; we have enough args to satisfy the required
380 ;; args.
381 (unless (null required-args)
382 `((when (< ,nargs ,required-len)
383 (merror (intl:gettext "~M: expected at least ~M arguments but got ~M: ~M")
384 ',pretty-fname
385 ,required-len
386 ,nargs
387 (list* '(mlist) ,args))))))
388 (optional-args
389 ;; There are optional args (but no rest
390 ;; arg). Verify that we don't have too many args,
391 ;; and that we still have all the required args.
393 (when (> ,nargs ,(+ required-len optional-len))
394 (merror (intl:gettext "~M: expected at most ~M arguments but got ~M: ~M")
395 ',pretty-fname
396 ,(+ required-len optional-len)
397 ,nargs
398 (list* '(mlist) ,args)))
399 (when (< ,nargs ,required-len)
400 (merror (intl:gettext "~M: expected at least ~M arguments but got ~M: ~M")
401 ',pretty-fname
402 ,required-len
403 ,nargs
404 (list* '(mlist) ,args)))))
406 ;; We only have required args.
407 `((unless (= ,nargs ,required-len)
408 (merror (intl:gettext "~M: expected exactly ~M arguments but got ~M: ~M")
409 ',pretty-fname
410 ,required-len
411 ,nargs
412 (list* '(mlist) ,args))))))
413 ,(cond
414 (keywords-present-p
415 `(apply #',impl-name
416 (append
417 (subseq ,args 0 ,required-len)
418 (defmfun-keywords ',pretty-fname
419 (nthcdr ,required-len ,args)
420 ',maxima-keywords))))
422 `(apply #',impl-name ,args))))))
423 ,(cond
424 (keywords-present-p
425 `(define-compiler-macro ,name (&rest ,rest-name)
426 ,(format nil "Compiler-macro to convert calls to ~S to ~S" name impl-name)
427 (let ((args (append (subseq ,rest-name 0 ,required-len)
428 (defmfun-keywords ',pretty-fname
429 (nthcdr ,required-len ,rest-name)
430 ',maxima-keywords))))
431 `(,',impl-name ,@args))))
433 `(define-compiler-macro ,name (&rest ,rest-name)
434 ,(format nil "Compiler-macro to convert calls to ~S to ~S" name impl-name)
435 `(,',impl-name ,@,rest-name)))))))))
437 ;; Define a Lisp function that should check the number of arguments to
438 ;; a function and print out a nice Maxima error message instead of
439 ;; signaling a Lisp error. In this case, the function is not
440 ;; explicitly exposed to the user and can just have an impl name of
441 ;; "name-impl".
442 (defmacro defun-checked (name lambda-list &body body)
443 ;; Defun-checked must not be used with functions that are exposed to
444 ;; the (Maxima) user. That is, it can't start with "$".
445 (when (char-equal #\$ (char (string name) 0))
446 (error "DEFUN-CHECKED functions cannot start with $: ~S~%" name))
447 `(defun-checked-form (,name ,(intern (concatenate 'string
448 (string name)
449 "-IMPL")))
450 ,lambda-list ,@body))
452 ;; Define user-exposed functions that are written in Lisp.
454 ;; If the function name NAME starts with #\$ we check the number of
455 ;; arguments. In this case, two functions are created: NAME and
456 ;; NAME-IMPL (without the leading $). NAME is the user function that
457 ;; checks for the argument count and NAME-IMPL is the actual
458 ;; implementation..
460 ;; If the function name doesn't start with $, we still allow it, but
461 ;; these should be replaced with plain defun eventually.
463 (defmacro defmfun (name-maybe-prop lambda-list &body body)
464 ;; NAME-MAYBE-PROP can be either a symbol or a list. If a symbol,
465 ;; it's just the name of the function to be defined. If a list, it
466 ;; must have the form (name &keyword :properties :deprecated-p)
467 ;; where NAME is the name of the function to be defined. The
468 ;; keyword args control what is generated. The value of :PROPERTIES
469 ;; is a list of lists denoting properties that are set for this
470 ;; function. Each element of the list must be of the form (PROPERTY
471 ;; VALUE). The value of :DEPRECATED-P is a symbol (unquoted) naming
472 ;; the function that should be used instead of this function because
473 ;; this function is deprecated.
475 ;; (defmfun ($polarform :properties ((evfun t))) (xx) ...)
477 ;; is the same as (defmfun $polarform (xx) ...) but adds
478 ;; (putprop '$polarform t 'evfun)
480 ;; For deprecated functions:
482 ;; (defmfun ($foo :deprecated-p $bar) () ...)
484 ;; This will print a message stating that "foo" is deprecated and to
485 ;; use "bar" instead.
486 (destructuring-bind (name &key properties deprecated-p)
487 (if (symbolp name-maybe-prop)
488 (list name-maybe-prop)
489 name-maybe-prop)
490 (flet ((add-props ()
491 ;; We make sure that the ARG-LIST property is added
492 ;; first, so that it will end up last in the list.
493 `((putprop ',name ',lambda-list 'arg-list)
494 (defprop ,name t translated)))
495 (func-props ()
496 ;; If any properties were specified for the function,
497 ;; gather them up here into corresponding putprop forms.
498 (mapcar #'(lambda (p)
499 (destructuring-bind (ind val)
501 `(putprop ',name ',val ',ind)))
502 properties)))
504 (let ((impl-name (intern (concatenate 'string
505 (subseq (string name) 1)
506 "-IMPL")))
507 (maclisp-narg-p (and (symbolp lambda-list) (not (null lambda-list)))))
508 (cond
509 ((or (char/= #\$ (aref (string name) 0))
510 maclisp-narg-p)
511 ;; If NAME doesn't start with $, it's an internal function not
512 ;; directly exposed to the user. Basically define the function
513 ;; as is, taking care to support the Maclisp narg syntax.
514 (cond (maclisp-narg-p
515 ;; Support MacLisp narg syntax: (defun foo a ...)
516 `(progn
517 (defun ,name (&rest narg-rest-argument
518 &aux (,lambda-list (length narg-rest-argument)))
519 ,@body)
520 ,@(add-props)))
522 `(progn
523 (defun ,name ,lambda-list ,@body)
524 ,@(add-props)))))
526 ;; Function name begins with $, so it's exposed to the user;
527 ;; carefully check the number of arguments and print a nice
528 ;; message if the number doesn't match the expected number.
529 #+nil
530 (unless (char= #\$ (aref (string name) 0))
531 (warn "First character of function name must start with $: ~S~%" name))
532 `(progn
533 (defun-checked-form (,name ,impl-name :deprecated-p ,deprecated-p) ,lambda-list
534 ,@body)
535 ,@(add-props)
536 ,@(func-props)
537 ;; We don't put this putprop in add-props because
538 ;; add-props is for both user and internal functions
539 ;; while the impl-name property is only for user
540 ;; functions.
541 (putprop ',name ',impl-name 'impl-name))))))))
543 ;; Examples:
544 ;; (defmfun $foobar (a b) (list '(mlist) a b))
545 ;; (defmfun $foobar1 (a b &optional c) (list '(mlist) a b c))
546 ;; (defmfun $foobar1a (a b &optional (c 99)) (list '(mlist) a b c))
547 ;; (defmfun $foobar2 (a b &rest c) (list '(mlist) a b (list* '(mlist) c)))
548 ;; (defmfun $foobar3 (a b &optional c &rest d) "foobar3 function" (list '(mlist) a b c (list* '(mlist) d)))
550 ;; (defmfun $foobar4 (a b &key c) (list '(mlist) a b c))
551 ;; (defmfun $foobar5 (a b &key (c 42)) (list '(mlist) a b c))
552 ;; (defmfun $foobar6 (a b &key (c 42) &allow-other-keys) (list '(mlist) a b c))
554 ;; foobar5(1,2) => [1, 2, 42]
555 ;; foobar5(1,2,c=99) => [1, 2, 99]
556 ;; foobar5(1,2,c=99,d=4) => error: unrecognized keyword d
557 ;; foobar6(1,2,c=42,d=99) => [1, 2, 42]
559 ;; This works by accident, kind of:
560 ;; (defmfun $baz (a &aux (b (1+ a))) (list '(mlist) a b))
562 ;; This should produce compile errors
563 ;; (defmfun $zot (a &optional c &key b) (list '(mlist) a b))
566 ;; Defines a simplifying function for Maxima whose name is BASE-NAME.
567 ;; The noun and verb properties are set up appropriately, along with
568 ;; setting the operator property. The noun form is created from the
569 ;; BASE-NAME by prepending a "%"; the verb form, by prepending "$".
570 ;; The verb function is defined appropriately too.
572 ;; For example, let's say we want to define a Maxima function named
573 ;; foo of two args with a corresponding simplifier to simplify special
574 ;; cases or numerically evaluate it. Then:
576 ;; (def-simplifier foo (x y)
577 ;; (cond ((float-numerical-eval-p x y)
578 ;; (foo-eval x y))
579 ;; (t
580 ;; (give-up))))
582 ;; This expands to
584 ;; (progn
585 ;; (defprop %foo simp-%foo operators)
586 ;; (defprop $foo %foo verb)
587 ;; (defprop %foo $foo noun)
588 ;; (defprop $foo %foo alias)
589 ;; (defprop %foo $foo reversealias)
590 ;; (defun simp-%foo (form #:unused-5230 %%simpflag)
591 ;; (declare (ignore #:unused-5230))
592 ;; (let ((x (simpcheck (nth 1 form) #:z-5229))
593 ;; (y (simpcheck (nth 2 form) #:z-5229)))
594 ;; (arg-count-check 2 form)
595 ;; (macrolet ((give-up ()
596 ;; '(eqtest (list '(%foo) x y) form)))
597 ;; (cond
598 ;; ((float-numerical-eval-p x y)
599 ;; (foo-eval x y))
600 ;; (t
601 ;; (give-up))))))
603 ;; The body can reference FORM and %%SIMPFLAG.
605 ;; The base name can also be a lambda-list of the form (name &key
606 ;; (simpcheck :default)). The NAME is the BASE-NAME of the
607 ;; simpiflier. The keyword arg :SIMPCHECK supports two values:
608 ;; :DEFAULT and :CUSTOM, with :DEFAULT as the default. :CUSTOM means
609 ;; the generated code does not call SIMPCHECK on the args, as shown
610 ;; above. It is up to the body to do the necessary work.
612 ;; Note also that the args for the simplifier only supports a fixed
613 ;; set of required arguments. Not optional or rest arguments are
614 ;; supported. No checks are made for this. If you need this, you'll
615 ;; have to write your own simplifier. Use the above macro expansion
616 ;; to see how to define the appropriate properties for the simplifer.
618 ;; Note carefully that the expansion defines a macro GIVE-UP to
619 ;; handle the default case of the simplifier when we can't do any
620 ;; simplification. Call this in the default case for the COND.
622 (defmacro def-simplifier (base-name-and-options lambda-list &body body)
623 (destructuring-bind (base-name &key (simpcheck :default))
624 (if (symbolp base-name-and-options)
625 (list base-name-and-options)
626 base-name-and-options)
627 (let* ((noun-name (intern (concatenate 'string "%" (string base-name))))
628 (verb-name (intern (concatenate 'string "$" (string base-name))))
629 (simp-name (intern (concatenate 'string "SIMP-" (string noun-name))))
630 (form-arg (intern "FORM"))
631 (z-arg (intern "%%SIMPFLAG"))
632 (unused-arg (gensym "UNUSED-"))
633 (arg-forms (ecase simpcheck
634 (:custom
635 (loop for arg in lambda-list
636 and count from 1
637 collect (list arg `(nth ,count ,form-arg))))
638 (:default
639 (loop for arg in lambda-list
640 and count from 1
641 collect (list arg `(simpcheck (nth ,count ,form-arg) ,z-arg)))))))
642 `(progn
643 ;; Define the noun function.
644 (defmfun ,verb-name (,@lambda-list)
645 (ftake ',noun-name ,@lambda-list))
647 ;; Set up properties
648 (defprop ,noun-name ,simp-name operators)
650 ;; The noun property is needed so that $verbify returns the
651 ;; verb form. Without this, things like ($verbify '%beta)
652 ;; doesn't return $beta because beta is a function and a
653 ;; variable (used by dgemm).
654 (defprop ,noun-name ,verb-name noun)
656 ;; The verb and alias properties are needed to make things like
657 ;; quad_qags(jacobi_sn(x,.5)...) work.
658 (defprop ,verb-name ,noun-name verb)
659 (defprop ,verb-name ,noun-name alias)
660 ;; The reversealias property is needed by grind to print out
661 ;; the right thing. Without it, grind(jacobi_sn(x,m)) prints
662 ;; '?%jacobi_sn(x,m)". Also needed for labels in plots which
663 ;; would show up as %jacobi_sn instead of jacobi_sn.
664 (defprop ,noun-name ,verb-name reversealias)
666 ;; Define the simplifier
667 (defun ,simp-name (,form-arg ,unused-arg ,z-arg)
668 (declare (ignore ,unused-arg)
669 (ignorable ,z-arg))
670 (arg-count-check ,(length lambda-list)
671 ,form-arg)
672 (let ,arg-forms
673 ;; Allow args to give-up if the default args won't work.
674 ;; Useful for the (rare?) case like genfact where we want
675 ;; to give up but want different values for args.
676 (flet ((give-up (&optional ,@(mapcar #'(lambda (a)
677 (list a a))
678 lambda-list))
679 ;; Should this also return from the function?
680 ;; That would fit in better with giving up.
681 (eqtest (list '(,noun-name) ,@lambda-list) ,form-arg)))
682 ,@body)))))))