Eliminate spurious redefinition of derivabbrev in Ctensor, fix documentation of diagm...
[maxima/cygwin.git] / src / mlisp.lisp
blobbc1d2450722ba86db3be293c6d75f343d5a0db9b
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
4 ;;; ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9 (in-package :maxima)
11 ;; ** (c) Copyright 1982 Massachusetts Institute of Technology **
13 (macsyma-module mlisp)
15 (eval-when
16 #+gcl (compile eval)
17 #-gcl (:compile-toplevel :execute)
19 (defvar *old-read-base* *read-base*)
20 (setq *read-base* 10.))
22 (defmvar $mapprint t
23 "If TRUE, messages about map/fullmap truncating on the shortest list
24 or if apply is being used are printed.")
26 (declare-top (special derivflag derivlist $labels $values $functions $arrays
27 $rules $gradefs $dependencies $aliases
28 $myoptions $props genvar $maxposex $maxnegex $expop $expon
29 $numer state-pdl *mdebug* *refchkl* *baktrcl*
30 $norepeat $detout $doallmxops $doscmxops opers
31 *mopl* *alphabet* $%% %e-val
32 $macros linel $ratfac $ratwtlvl
33 $operators $partswitch *gcdl*
34 *builtin-$props* $infolists))
36 (declare-top (unspecial args))
38 (defvar mspeclist nil)
39 (defvar bindlist nil)
40 (defvar loclist nil)
41 (defvar mproplist nil)
42 (defvar *nounl* nil)
43 (defvar scanmapp nil)
44 (defvar maplp nil)
45 (defvar mprogp nil)
46 (defvar evp nil)
47 (defvar mdop nil)
48 (defvar mlocp nil)
49 (defvar aexprp nil)
50 (defvar fmaplvl 0)
51 (defvar dsksetp nil)
52 (defvar aryp nil)
53 (defvar msump nil)
54 (defvar evarrp nil)
55 (defvar factlist nil)
56 (defvar mfexprp t)
57 (defvar *nounsflag* nil)
58 (defvar transp nil)
59 (defvar noevalargs nil)
60 (defvar rulefcnl nil)
61 (defvar featurel
62 '($integer $noninteger $even $odd $rational $irrational $real $imaginary $complex
63 $analytic $increasing $decreasing $oddfun $evenfun $posfun $constant
64 $commutative $lassociative $rassociative $symmetric $antisymmetric))
66 (defmvar $features (cons '(mlist simp) (append featurel nil)))
67 (defmvar $%enumer nil)
68 (defmvar $float nil)
69 (defmvar $refcheck nil)
70 (defmvar $translate nil)
71 (defmvar $transrun t)
72 (defmvar $savedef t)
73 (defmvar $maperror t)
74 (defmvar $optionset nil)
75 (defmvar $setcheckbreak nil)
76 (defmvar $infeval nil)
77 (defmvar $piece '$piece)
78 (defmvar $setval '$setval)
80 ;; These three variables are what get stuck in array slots as magic
81 ;; unbound objects. They are for T, FIXNUM, and FLONUM type arrays
82 ;; respectively.
84 (defvar munbound '|#####|)
86 (defvar fixunbound most-negative-fixnum)
88 (defvar flounbound most-negative-flonum)
90 (defmvar munbindp nil
91 "Used for safely `munbind'ing incorrectly-bound variables."
92 no-reset)
94 (defmvar $setcheck nil)
96 (mapc #'(lambda (x) (setf (symbol-value x) (ncons '(mlist simp))))
97 '($values $functions $macros $arrays $myoptions $rules $props))
99 (defmfun mapply1 (fn args fnname form)
100 (declare (special aryp))
101 (cond ((atom fn)
102 (cond ((atom fn)
103 (cond ((functionp fn)
104 (apply fn args))
105 ((and (symbolp fn) (fboundp fn) (not (macro-function fn)))
106 (mapply1 (symbol-function fn) args fn form))
107 ((and (symbolp fn) (symbol-array fn))
108 (mapply1 (symbol-array fn) args fn form))
110 (setq fn (getopr fn))
111 (badfunchk fnname fn nil)
112 (let ((noevalargs t))
113 (meval (cons (ncons fn) args))))))))
115 ;; GCL considers interpreted functions and lambdas to be non-atoms
116 #+gcl((functionp fn)
117 (apply fn args))
119 ;; extension for pdiff; additional extension are welcomed.
120 ;; (AND (CONSP FN) (CONSP (CAR FN)) ...) is an attempt to identify
121 ;; conventional Maxima expressions ((FOO) X Y Z); probably should
122 ;; encapsulate somewhere, maybe it is already ??
123 ((and (consp fn) (consp (car fn)) (symbolp (mop fn)) (get (mop fn) 'mapply1-extension)
124 (apply (get (mop fn) 'mapply1-extension) (list fn args fnname form))))
125 ((eq (car fn) 'lambda)
126 (apply (coerce fn 'function) args))
127 ((eq (caar fn) 'lambda) (mlambda fn args fnname t form))
128 ((eq (caar fn) 'mquote) (cons (cdr fn) args))
129 ((and aryp (member (caar fn) '(mlist $matrix) :test #'eq))
130 (if (not (or (= (length args) 1)
131 (and (eq (caar fn) '$matrix) (= (length args) 2))))
132 (merror (intl:gettext "apply: wrong number of indices; found: ~M") (cons '(mlist) args)))
133 (if (member 0 args :test #'eq)
134 (merror (intl:gettext "apply: no such ~M element: ~M") (if (eq (caar fn) 'mlist) (intl:gettext "list") (intl:gettext "matrix"))
135 `((mlist) ,@args)))
136 (do ((args1 args (cdr args1)))
137 ((null args1) (let (($piece $piece) ($partswitch 'mapply))
138 (apply #'$inpart (cons fn args))))
139 (unless (fixnump (car args1))
140 (if evarrp (throw 'evarrp 'notexist))
141 (merror (intl:gettext "apply: subscript must be an integer; found: ~M") (car args1)))))
142 (aryp
143 (cons '(mqapply array) (cons fn args)))
145 (cons '(mqapply) (cons fn args)))))
147 ;; the last argument to mapply1 for the lineinfo is not correct here..
148 (defmfun mcall (fn &rest args)
149 (mapply1 fn args fn nil))
151 (defun mevalargs (args)
152 (cond (noevalargs (setq noevalargs nil) args)
153 (t (mapcar #'meval args))))
155 ;;Function Call stack each element is
156 ;; (fname . bindlist) where bindlist was the value at time of entry.
157 ;; So you can use this to compute what the bindings were at any
158 ;; function call.
159 (defvar *mlambda-call-stack* (make-array 30 :fill-pointer 0 :adjustable t ))
161 ;;; The frame info for a function call consists of 5 consecutive
162 ;;; entries in *MLAMBDA-CALL-STACK*. I call the topmost object of
163 ;;; such a quintuple the `function designator' belonging to this
164 ;;; frame.
166 (defun pop-mlambda-call-stack (&optional fnname)
167 "Deactivate the topmost function call frame info.
168 Return the function designator for this frame and check that it
169 is EQ to FNNAME if the latter is non-NIL."
170 (let ((ar *mlambda-call-stack*) mlambda)
171 (symbol-macrolet ((mlambda-pointer (fill-pointer ar)))
172 (prog1
173 (setq mlambda (aref ar (1- mlambda-pointer)))
174 (when fnname
175 ;; Different frames can have the same function designator,
176 ;; so this doesn't prove anything, it's just a check.
177 (assert (eq mlambda fnname)
178 (*mlambda-call-stack*)
179 "Expected ~a but got ~a on mlambda call stack."
180 fnname mlambda))
181 (decf mlambda-pointer 5)))))
183 (defun mlambda (fn args fnname noeval form)
184 (cond ((not ($listp (cadr fn)))
185 (merror (intl:gettext "lambda: first argument must be a list; found: ~M") (cadr fn))))
186 (setq noevalargs nil)
187 (let ((params (cdadr fn))( mlocp t))
188 (setq loclist (cons nil loclist))
189 (do ((a) (p))
190 ((or (null params) (and (null args) (not (mdeflistp params))))
191 (setq args (nreconc a args) params (nreconc p params)))
192 (cond ((mdeflistp params)
193 (setq params (cdar params) args (ncons (cons '(mlist) args)))))
194 (cond ((and mfexprp (mquotep (car params)))
195 (setq a (cons (car args) a) p (cons (cadar params) p)))
196 ((atom (car params))
197 (setq p (cons (car params) p)
198 a (cons (cond (noeval (car args))
199 (t (meval (car args)))) a)))
200 (t (merror (intl:gettext "lambda: formal argument must be a symbol or quoted symbol; found: ~M") (car params))))
201 (setq args (cdr args) params (cdr params)))
202 (let (finish2033 (finish2032 params) (ar *mlambda-call-stack*))
203 (declare (type (vector t) ar))
204 (unwind-protect
205 (progn
206 (unless (> (array-total-size ar) (+ (fill-pointer ar) 10))
207 (setq ar (adjust-array ar (+ (array-total-size ar) 50) :fill-pointer (fill-pointer ar))))
208 (vector-push bindlist ar)
209 ;; rather than pushing all on *baktrcl* it might be good
210 ;; to make a *last-form* global that is set in meval1
211 ;; and is pushed here.
212 (vector-push form ar)
213 (vector-push params ar)
214 (vector-push args ar)
215 (vector-push fnname ar)
216 (mbind finish2032 args fnname)
217 (setq finish2033 t)
218 (prog1
219 (let ((aexprp (and aexprp (not (atom (caddr fn)))
220 (eq (caar (caddr fn)) 'lambda))))
221 (cond ((null (cddr fn)) (merror (intl:gettext "lambda: no body present.")))
222 ((cdddr fn) (mevaln (cddr fn)))
223 (t (meval (caddr fn)))))
224 nil))
225 (if finish2033
226 (progn
227 (incf (fill-pointer *mlambda-call-stack*) -5)
228 (munlocal)
229 (munbind finish2032)))))))
232 (defmspec mprogn (form)
233 (mevaln (cdr form)))
235 (defmfun mevaln (l) ;; called in a few places externally.
236 (do ((body l (cdr body))
237 ($%% '$%%))
238 ((null (cdr body)) (meval (car body)))
239 (setq $%% (meval (car body)))))
241 (defun mqapply1 (form)
242 (declare (special aryp))
243 (destructuring-let (((fn . argl) (cdr form)) (aexprp))
244 (unless (mquotep fn) (setq fn (meval fn)))
245 (cond ((atom fn)
246 (meval (cons (cons fn aryp) argl)))
247 ((eq (caar fn) 'lambda)
248 (if aryp
249 (merror (intl:gettext "lambda: cannot apply lambda as an array function."))
250 (mlambda fn argl (cadr form) noevalargs form)))
252 (mapply1 fn (mevalargs argl) (cadr form) form)))))
254 (defmfun meval (form)
255 (simplifya (meval1 form) nil))
257 ;;temporary hack to see what's going on:
258 (defmfun safe-mgetl (atom inds)
259 (and (symbolp atom)
260 (let ((props (get atom 'mprops)))
261 (and props (getl props inds)))))
263 (defmfun safe-mget (atom inds)
264 (and (symbolp atom)
265 (let ((props (get atom 'mprops)))
266 (and props (getf (cdr props) inds)))))
268 (defvar *last-meval1-form* nil)
270 (defmfun meval1 (form)
271 (declare (special *nounl* *break-points* *break-step*))
272 (cond
273 ((atom form)
274 (prog (val)
275 (cond ((not (symbolp form)) (return form))
276 ((and $numer
277 (setq val (safe-mget form '$numer))
278 (or (not (eq form '$%e)) $%enumer))
279 (return (meval1 val)))
280 ((not (boundp form))
281 (if (safe-get form 'bindtest)
282 (merror (intl:gettext "evaluation: unbound variable ~:M")
283 form)
284 (return form))))
285 (setq val (symbol-value form))
286 (when (and $refcheck
287 (member form (cdr $values) :test #'eq)
288 (not (member form *refchkl* :test #'eq)))
289 (setq *refchkl* (cons form *refchkl*))
290 (mtell (intl:gettext "evaluation: ~:M has a value.~%") form))
291 (return val)))
292 ((or (and (atom (car form))
293 (setq form (cons (ncons (car form)) (cdr form))))
294 (atom (caar form)))
295 (let ((*baktrcl* *baktrcl*) transp)
296 (prog (u aryp)
297 (declare (special aryp))
298 (setq *last-meval1-form* form)
299 (setq aryp (member 'array (cdar form) :test #'eq))
300 (cond ((and (not aryp)
301 (member (caar form)
302 '(mplus mtimes mexpt mnctimes) :test #'eq))
303 (go c))
304 ;; dont bother pushing mplus and friends on *baktrcl*
305 ;; should maybe even go below aryp.
306 ((and *mdebug*
307 (progn
308 ;; if wanting to step, the *break-points*
309 ;; variable will be set to a vector (possibly empty).
310 (when (and *break-points*
311 (or (null *break-step*)
312 (null (funcall *break-step* form))))
313 (let ((ar *break-points*))
314 (declare (type (vector t) ar))
315 (loop for i below (fill-pointer ar)
316 when (eq (car (aref ar i)) form)
317 do (*break-points* form)
318 (loop-finish))))
319 nil)))
320 ((eq (caar form) 'mqapply) (return (mqapply1 form))))
321 (badfunchk (caar form) (caar form) nil)
323 (setq u
324 (or (safe-getl (caar form) '(noun))
325 (and *nounsflag*
326 (eq (getcharn (caar form) 1) #\%)
327 (not (or (getl-lm-fcn-prop (caar form) '(subr))
328 (safe-getl (caar form) '(mfexpr*))))
329 (prog2 ($verbify (caar form))
330 (safe-getl (caar form) '(noun))))
331 (and (not aryp)
332 $transrun
333 (setq transp
334 (safe-getl (caar form) '(translated-mmacro))))
335 (and (not aryp)
336 (setq u
337 (or (safe-mget (caar form) 'trace)
338 (and $transrun
339 (safe-get (caar form) 'translated)
340 (not (safe-mget (caar form) 'local-fun))
341 (setq transp t)
342 (caar form))))
343 (getl-lm-fcn-prop u '(subr)))
344 (cond (aryp (safe-mgetl (caar form) '(hashar array)))
345 ((safe-mgetl (caar form) '(mexpr mmacro)))
347 (or (safe-getl (caar form) '(mfexpr*))
348 (getl-lm-fcn-prop (caar form) '(subr macro)))))))
349 (when (null u) (go b))
350 (return
351 (cond ((eq (car u) 'hashar)
352 (harrfind (cons (car form) (mevalargs (cdr form)))))
353 ((eq (car u) 'subr)
354 (apply (caar form) (mevalargs (cdr form))))
355 ((eq (car u) 'noun)
356 (cond ((or (member (caar form) *nounl* :test #'eq) *nounsflag*)
357 (setq form (cons (cons (cadr u) (cdar form))
358 (cdr form)))
359 (go a))
360 (aryp (go b))
361 ((member (caar form) '(%sum %product) :test #'eq)
362 (setq u (do%sum (cdr form) (caar form))
363 noevalargs nil)
364 (cons (ncons (caar form)) u))
365 (t (meval2 (mevalargs (cdr form)) form))))
366 ((eq (car u) 'array)
367 (arrfind (cons (car form) (mevalargs (cdr form)))))
368 ((eq (car u) 'mexpr)
369 (mlambda (cadr u) (cdr form) (caar form) noevalargs form))
370 ((member (car u) '(mmacro translated-mmacro) :test #'eq)
371 (setq noevalargs nil)
372 (meval (mmacro-apply (cadr u) form)))
373 ((eq (car u) 'mfexpr*)
374 (setq noevalargs nil)
375 (apply (cadr u) (ncons form)))
376 ((eq (car u) 'macro)
377 (setq noevalargs nil)
378 (setq form (cons(caar form) (cdr form)))
379 (eval form))
381 (apply (cadr u) (mevalargs (cdr form))))))
383 (if (and (not aryp) (load-function (caar form) t)) (go a))
384 (badfunchk (caar form) (caar form) nil)
385 (if (symbolp (caar form))
386 (setq u (boundp (caar form)))
387 (return (meval1-extend form)))
389 (cond ((or (null u)
390 (and (safe-get (caar form) 'operators) (not aryp))
391 (eq (caar form) (setq u (symbol-value (caar form)))))
392 (setq form (meval2 (mevalargs (cdr form)) form))
393 (return (or (and (safe-mget (caar form) 'atvalues)
394 (at1 form))
395 form)))
396 ((and aryp
397 (safe-get (caar form) 'nonarray))
398 (return (cons (cons (caar form) aryp)
399 (mevalargs (cdr form)))))
400 ((atom u)
401 (badfunchk (caar form) u nil)
402 (setq form (cons (cons (getopr u) aryp) (cdr form)))
403 (go a))
404 ((eq (caar u) 'lambda)
405 (if aryp
406 (merror (intl:gettext "lambda: cannot apply lambda as an array function."))
407 (return (mlambda u (cdr form)
408 (caar form) noevalargs form))))
410 (return
411 (mapply1 u (mevalargs (cdr form)) (caar form) form)))))))
413 (mapply1 (caar form) (mevalargs (cdr form)) (caar form) form))))
415 (defun getl-lm-fcn-prop (sym props &aux fn typ)
416 (check-arg sym symbolp "symbol")
417 (setq fn sym)
418 (cond ((functionp fn)
419 (setq typ 'subr))
420 ((macro-function sym)
421 (setq typ 'macro))
422 ((setq fn (symbol-array sym))
423 (setq typ 'array))
424 ((setq fn (get sym 'mfexpr*))
425 (setq typ 'mfexpr*)))
426 (and typ (member typ props :test #'eq) (list typ fn)))
429 (defmfun meval2 (newargs old)
430 (declare (special aryp))
431 (let ((new (cons (car old) newargs)) nosimp)
432 (cond ((not (member 'simp (cdar old) :test #'eq))
433 (if (and (not (eq (caar new) 'mlist)) (equal new old)) old new))
434 ((prog2 (setq nosimp (not (get (caar new) 'operators))) (alike1 new old))
435 (if nosimp old (cons (delsimp (car old)) (cdr old))))
436 (nosimp (if aryp new (cons (cons (caar new) '(simp)) newargs)))
437 (t (cons (cons (caar new) aryp) newargs)))))
439 (defun mparams (vars)
440 (mapcar #'(lambda (x)
441 (cond ((atom x) x)
442 ((atom (cadr x)) (cadr x))
443 (t (cadadr x))))
444 (cdr vars)))
446 (defmfun mop (form)
447 (if (eq (caar form) 'mqapply)
448 (cadr form)
449 (caar form)))
451 (defmfun margs (form)
452 (if (eq (caar form) 'mqapply)
453 (cddr form)
454 (cdr form)))
456 (defun badfunchk (name val flag)
457 (if (or flag (numberp val) (member val '(t nil $%e $%pi $%i) :test #'eq))
458 (if (and (atom name) (not (equal val name)))
459 (merror (intl:gettext "apply: found ~M evaluates to ~M where a function was expected.") name val)
460 (merror (intl:gettext "apply: found ~M where a function was expected.") val))))
462 ;; To store the value of $errormsg in mbind. This value is looked up in the
463 ;; routine mbind-doit. This is a hack to get the expected behavior, when the
464 ;; option variable $errormsg is used as a local variable in a block.
465 (defvar *$errormsg-value* nil)
467 (defun mbind-doit (lamvars fnargs fnname)
468 "Makes a new frame where the variables in the list LAMVARS are bound
469 to the corresponding elements in FNARGS. Note that these elements are
470 used tels quels, without calling MEVAL.
471 If FNNAME is non-NIL, it designates a function call frame.
472 This function does not handle errors properly, use the MBIND
473 wrapper for this."
474 (declare (special bindlist mspeclist))
475 (do ((vars lamvars (cdr vars))
476 (args fnargs (cdr args)))
477 ((cond ((and vars args) nil)
478 ((and (null vars) (null args)))
479 (t (assert fnname (fnname)
480 "Expected a maxima function designator but got NIL.")
481 (merror (intl:gettext "~M arguments supplied to ~M; found: ~M")
482 (if vars (intl:gettext "Too few") (intl:gettext "Too many"))
483 (cons (ncons fnname) lamvars)
484 (cons '(mlist) fnargs)))))
485 (let ((var (car vars)))
486 (if (not (symbolp var))
487 (merror (intl:gettext "Only symbols can be bound; found: ~M") var))
488 (let ((value (if (boundp var)
489 (if (eq var '$errormsg)
490 ;; Do not take the actual value of $errormsg. It is
491 ;; always NIL at this point, but the value which
492 ;; is stored in *$errormsg-value*.
493 *$errormsg-value*
494 (symbol-value var))
495 munbound)))
496 (mset var (car args))
497 (psetq bindlist (cons var bindlist)
498 mspeclist (cons value mspeclist))))))
500 (defun mbind (lamvars fnargs fnname)
501 "Error-handling wrapper around MBIND-DOIT."
502 (handler-case
503 (let ((old-bindlist bindlist) win)
504 (declare (special bindlist))
505 ;; At this point store the value of $errormsg in a global. The macro
506 ;; with-$error sets the value of $errormsg to NIL, but we need the
507 ;; actual value in the routine mbind-doit.
508 (setq *$errormsg-value* $errormsg)
509 (unwind-protect
510 (prog1
511 (with-$error (mbind-doit lamvars fnargs fnname))
512 (setq win t))
513 (unless win
514 (unless (eq bindlist old-bindlist)
515 (munbind (nreverse (ldiff bindlist old-bindlist))))
516 (when fnname
517 (pop-mlambda-call-stack fnname)))))
518 (maxima-$error (c)
519 ;; HMM, HERE'S A CALL TO MERROR. I CAN'T TELL WHERE ARE THE ERROR MESSAGES.
520 ;; IF I DID, I'D WRAP THEM IN A CALL TO GETTEXT
521 (apply #'merror (cdr (the-$error c)))
522 ;; Make absolutely sure that this handler (and mbind) doesn't
523 ;; return in this situation since other code depends on this
524 ;; behaviour.
525 (throw 'macsyma-quit t))))
527 ;;; For testing purposes
529 #+ignore
530 (defmfun $show_mbind_data ()
531 (format t "~&~{~a = ~a~%~}"
532 (mapcan #'(lambda (x) (list x (symbol-value x)))
533 '(bindlist mspeclist $values *mlambda-call-stack*)))
534 (finish-output)
535 (values))
537 (defmfun munbind (vars)
538 (dolist (var (reverse vars))
539 (cond ((eq (car mspeclist) munbound)
540 (makunbound var)
541 (setf $values (delete var $values :count 1 :test #'eq)))
542 (t (let ((munbindp t)) (mset var (car mspeclist)))))
543 (setq mspeclist (cdr mspeclist) bindlist (cdr bindlist))))
545 ;;This takes the place of something like
546 ;; (DELETE (ASSOC (NCONS VAR) $DEPENDENCIES) $DEPENDENCIES 1)
548 (defun mfunction-delete (var fn-a-list)
549 (delete (assoc (ncons var) fn-a-list :test #'equal) fn-a-list :count 1 :test #'equal))
551 (defmspec mlocal (l)
552 (push nil loclist)
553 (let ((mlocp t))
554 (meval `(($local) ,@(cdr l)))))
556 (defmspec $local (l)
557 (setq l (cdr l))
558 (unless mlocp
559 (merror (intl:gettext "local: must be called within a block or lambda.")))
560 (dolist (var l)
561 (cond ((not (symbolp var))
562 (improper-arg-err var '$local))
563 ((and (mget var 'array)
564 (arrayp (symbol-array var)))
565 ;; HMM. I DON'T UNDERSTAND WHY DECLARED ARRAYS ARE OFF-LIMITS:
566 ;; THE ARRAY IS JUST A PROPERTY LIKE ANY OTHER, IS IT NOT ??
567 (merror (intl:gettext "local: argument cannot be a declared array; found: ~M") var)))
568 (setq mproplist (cons (get var 'mprops) mproplist)
569 factlist (cons (get var 'data) factlist))
570 (dolist (fact (car factlist))
571 (putprop fact -1 'ulabs))
572 (progn
573 (mfunction-delete var $functions)
574 (mfunction-delete var $macros)
575 (mfunction-delete var $dependencies))
576 (setf $arrays (delete var $arrays :count 1 :test #'eq))
577 (zl-remprop var 'mprops)
578 (zl-remprop var 'data))
579 (rplaca loclist (reverse l))
580 (setq mlocp nil)
581 '$done)
583 (defun munlocal ()
584 (dolist (var (car loclist))
585 (let ((mprop (car mproplist))
586 (y nil)
587 (fact (car factlist)))
588 (remcompary var)
589 (cput var mprop 'mprops)
590 (cond ((setq y (old-get mprop 'mexpr))
591 (add2lnc (cons (ncons var) (cdadr y)) $functions))
592 (t (mfunction-delete var $functions)))
593 (cond ((setq y (old-get mprop 'mmacro))
594 (add2lnc (cons (ncons var) (cdadr y)) $macros))
595 (t (mfunction-delete var $macros)))
596 (cond ((or (old-get mprop 'array) (old-get mprop 'hashar))
597 (add2lnc var $arrays))
598 (t (setf $arrays (delete var $arrays :count 1 :test #'eq))))
599 (cond ((setq y (old-get mprop 'depends))
600 (add2lnc (cons (ncons var) y) $dependencies))
601 (t (mfunction-delete var $dependencies)))
602 (rempropchk var)
603 (mapc #'remov (get var 'data))
604 (cput var fact 'data)
605 (dolist (u fact)
606 (zl-remprop u 'ulabs))
607 (setq mproplist (cdr mproplist)
608 factlist (cdr factlist))))
609 (setq loclist (cdr loclist)))
611 (defmacro msetq (a b)
612 `(mset ',a ,b))
614 ;; A "run-time macro" needed by MATCOM/MATRUN.
615 ;;works with the defms
616 (defmspec msetq (l)
617 (twoargcheck l)
618 (mset (simplifya (cadr l) nil) (meval (caddr l))))
620 (defun mset (x y)
621 (prog ()
622 (cond ((or (null $setcheck)
623 (eq $setcheck '$setcheck)))
624 ((and (or (atom $setcheck)
625 (memalike x (cdr $setcheck))
626 (and (not (atom x))
627 (memalike (caar x) (cdr $setcheck))))
628 (not (eq x y)))
629 (displa (list '(mtext) (disp2 x) '| set to | y))
630 (if (and $setcheckbreak (not (eq x '$setval)))
631 (let (($setval y))
632 (merrbreak t)
633 (setq y $setval)))))
634 (cond ((atom x)
635 (when (or (not (symbolp x))
636 (member x '(t nil) :test #'eq)
637 (mget x '$numer)
638 (get x 'sysconst))
639 (if munbindp (return nil))
640 (if (mget x '$numer)
641 (merror (intl:gettext "assignment: cannot assign to ~M; it is a declared numeric quantity.") x)
642 (merror (intl:gettext "assignment: cannot assign to ~M") x)))
643 (let ((f (get x 'assign)))
644 (if (and f (or (not (eq x y))
645 (member f '(neverset read-only-assign) :test #'eq)))
646 (if (eq (funcall f x y) 'munbindp) (return nil))))
647 (cond ((and (not (boundp x))
648 (not dsksetp))
649 (add2lnc x $values))
650 ((and (not (eq x y))
651 (optionp x))
652 (if $optionset (mtell (intl:gettext "assignment: assigning to option ~M") x))
653 (if (not (eq x '$linenum)) (add2lnc x $myoptions))))
654 (return (setf (symbol-value x) y)))
656 ;; ---------- begin code copied & modified from defstruct.lisp
658 ;; Check to see if the operator has an mset_extension_operator.
659 ;; If so, this says how to do assignments. Examples, a@b:x. Put mset_extension_operator
660 ;; of mrecord-assign on the atom $@. To allow [a,b]:[3,4] put op on mlist.
661 ;; arguably we could use mget, mfuncall, and $mset_extension_operator and
662 ;; allow this to be done at the maxima level instead of lisp.
664 ;; X is could be something like (($FOO ARRAY) 42), in which case it is meaningful
665 ;; to look for an assignment operator associated either with $FOO itself or with
666 ;; $FOO's object type, with "object type" = (CAAR (SYMBOL-VALUE '$FOO)).
668 ((let*
669 ((x-value (if (boundp (caar x)) (symbol-value (caar x))))
670 (mset-extension-op
671 (cond
672 ((get (caar x) 'mset_extension_operator))
673 ((and
674 (not (atom x-value))
675 (get (caar x-value) 'defstruct-template)
676 (get (caar x-value) 'mset_extension_operator))))))
677 (if mset-extension-op
678 (return-from mset (funcall mset-extension-op x y)))))
680 ;; ---------- end code copied & modified from defstruct.lisp
682 ((member 'array (cdar x) :test #'eq)
683 (return (arrstore x y)))
684 (t (merror (intl:gettext "assignment: cannot assign to ~M") x)))))
686 ;; ---------- begin code copied & modified from defstruct.lisp
688 ;; CHANGES WRT FATEMAN'S STUFF.
689 ;; (1) $NEW BARFS IF #ARGUMENTS != 1, OR ARGUMENT HAS NO DEFSTRUCT, OR WRONG NUMBER OF INITIALIZERS.
690 ;; (2) $DEFSTRUCT ALLOWS 1 OR MORE ARGUMENTS, RETURNS A LIST OF DEFSTRUCTS.
691 ;; (3) USE $PUT AND $GET TO MAINTAIN DEFSTRUCT PROPERTIES
692 ;; (RENAMED TO $DEFSTRUCT_DEFAULT AND $DEFSTRUCT_TEMPLATE).
693 ;; THIS MAKES DEFSTRUCT PROPERTIES VISIBLE TO USER VIA GET AND PROPVARS.
694 ;; ALSO, THIS MAKES `KILL' KILL DEFSTRUCTS.
695 ;; (4) @ EVALUATES LHS AND QUOTES RHS
696 ;; (5) $STRUCTURES INFOLIST
697 ;; (6) LBP = 200, RBP = 201 (HIGHER PRECEDENCE, LEFT-ASSOCIATIVE)
698 ;; (7) A@B => A@B WHEN B IS NOT BOUND TO SOMETHING OTHER THAN ITSELF
699 ;; (8) DISALLOW @ APPLIED TO EXPRESSIONS W/ OPERATOR NOT DECLARED BY DEFSTRUCT
700 ;; (9) MAKE RECORD AND LIST ASSIGNMENT FUNCTIONS LISP FUNCTIONS (STRIP OFF $ FROM NAME)
701 ;; ALSO MAKE PROPERTY SYMBOLS LISP SYMBOLS (STRIP OFF $ FROM NAME)
702 ;; (10) EXTEND KILL TO TAKE ITEMS OFF $STRUCTURES AND REMOVE DEFSTRUCT PROPERTIES
703 ;; (11) EXTEND KILL TO RECOGNIZE KILL(X@Y)
704 ;; (12) EVALUATE INITIALIZERS IN $DEFSTRUCT AND IN $NEW
705 ;; (13) DISPLAY FIELDS WHICH HAVE BEEN ASSIGNED VALUES AS FOO(X = BAR, Y = BAZ)
706 ;; (14) ASSIGN TRANSLATION PROPERTY TO 'DEFSTRUCT AND DEF-SAME%TR ALL STRUCTURES
708 (setf (get '$@ 'mset_extension_operator) 'mrecord-assign)
710 ;; defstruct(f(x,y,z));
711 ;; myrecord: new(f);
712 ;; myrecord@y:45;
713 ;; myrecord; ==> f(x,45,z)
715 ;; initializers are possible
716 ;; defstruct(f(x,y=3.14159, z));
717 ;; ff:new(f) ==> f(x,3.14159,z)
718 ;; ff@y:2.71828 ==> ff is f(x,2.71828,z).
720 ;; the @ syntax can also be used instead of substinpart.
722 ;; k: h(g(aa,bb),cc);
723 ;; k@1@2:dd; change aa to dd.
724 ;; k;
726 (defmfun mrecord-assign (@-expr value)
727 ;; assume @-expr is (($@..) instance-name field-name)
728 (let*
729 ((instance (cadr @-expr))
730 (field (caddr @-expr))
731 (object (meval instance))
732 template)
733 (if (not (and (consp object) (consp (car object)) (setq template (get (caar object) 'defstruct-template))))
734 (merror "MRECORD-ASSIGN: left-hand side doesn't appear to be a defstruct object:~%~M" instance)
735 (let
736 ((index
737 (if (integerp field)
738 field ;;; allow foo@3, also
739 (position field template)))) ;field->integer
740 (if (null index) (merror (intl:gettext "assignment: no such field: ~M @ ~M") instance field))
741 (if (< 0 index (length object)) (setf (elt object index) value)
742 (merror (intl:gettext "assignment: no such field: ~M @ ~M") instance field))
743 value))))
745 ;; MRECORD-KILL is very similar to MRECORD-ASSIGN. Might consider merging the two somehow.
747 (defmfun mrecord-kill (@-expr)
748 (let*
749 ((instance (cadr @-expr))
750 (field (caddr @-expr))
751 (object (meval instance))
752 template)
753 (if (not (and (consp object) (consp (car object)) (setq template (get (caar object) 'defstruct-template))))
754 (merror "MRECORD-KILL: left-hand side doesn't appear to be a defstruct object:~%~M" instance)
755 (let
756 ((index
757 (if (integerp field)
758 field
759 (position field template))))
760 (if (null index) (merror (intl:gettext "kill: no such field: ~M @ ~M") instance field))
761 (if (< 0 index (length object)) (setf (elt object index) (elt template index))
762 (merror (intl:gettext "kill: no such field: ~M @ ~M") instance field))))))
764 (defmspec $@ (L)
765 (let*
766 ((a (cadr L))
767 (b (caddr L))
768 (e ($@-function (meval a) b)))
769 (if (eq e b) L e)))
771 (defun $@-function (in fn)
772 (cond
773 ((not (listp in))
774 (list '(%@) in fn)) ;; noun form
775 ((get (caar in) 'defstruct-template)
776 (let*
777 ((index
778 (if (integerp fn) fn ;; allow foo@3
779 (position fn (get (caar in) 'defstruct-template))))) ;; field->integer
780 (if (null index) (merror (intl:gettext "@: no such field: ~M @ ~M") in fn))
781 (if (< 0 index (length in))
782 (elt in index)
783 (merror (intl:gettext "@: no such field: ~M @ ~M") in fn))))
785 (list '($@) in fn))))
787 (defun dimension-defstruct (form result)
788 (let
789 ((L1 (cdr (get (caar form) 'defstruct-template)))
790 (L2 (cdr form)))
791 (dimension-function (cons (car form) (mapcar #'(lambda (e1 e2) (if (eq e1 e2) e1 `((mequal) ,e1 ,e2))) L1 L2)) result)))
793 ;; L looks like defstruct (foo(...), bar(...), baz(...)).
794 ;; Process each argument and return a list of declared structures.
796 (defmspec $defstruct (L)
797 `((mlist) ,@(mapcar 'defstruct1 (cdr L))))
799 ;; trivial translation to quiet complaint about lack of translation for this defmspec
800 (def%tr $defstruct (x) `($any . (meval ',x)))
802 (defvar $structures '((mlist)))
804 (defun defstruct-translate (form)
805 (let ((translated-args (mapcar #'translate (cdr form))))
806 `($any simplify (list '(,(caar form)) ,@(mapcar #'cdr translated-args)))))
808 (defun defstruct1 (z) ;; z will look like (($whatever) $a $b $c)
809 ;; store the template
810 (putprop (caar z) (namesonly z) 'defstruct-template)
811 ;; set the initialization
812 (putprop (caar z) (initializersmostly z) 'defstruct-default)
813 (setf (get (caar z) 'dimension) 'dimension-defstruct)
814 (nconc $structures (list (get (caar z) 'defstruct-default)))
815 (setf (get (caar z) 'translate) #'defstruct-translate)
816 (get (caar z) 'defstruct-default))
818 (defun namesonly(r) ; f(a,b,c) unchanged, f(a=3,b=4,c=5) -> f(a,b,c)
819 (cons (car r)(mapcar #'(lambda(z)
820 (cond((symbolp z) z)
821 ((eq (caar z) 'mequal)(second z))
822 (t (merror (intl:gettext "defstruct: expected a record initializer; found: ~M") z))))
823 (cdr r))))
825 (defun initializersmostly(r);; f(a=3,b,c=5) -> f(3,b,5)
826 (cons (car r)(mapcar #'(lambda(z)
827 (cond((symbolp z) z)
828 ((eq (caar z) 'mequal) (meval (third z)))
829 (t (merror (intl:gettext "defstruct: expected a record initializer; found: ~M") z))))
830 (cdr r))))
832 (defmspec $new (h)
833 (unless (= (length (cdr h)) 1)
834 (merror (intl:gettext "new: expected exactly one argument; found: ~M") (length (cdr h))))
836 (let ((recordname (cadr h)))
837 (cond
838 ((symbolp recordname) ;; the case of, e.g. new(f);
839 (if (null (get recordname 'defstruct-default))
840 (merror (intl:gettext "new: no such structure ~M") recordname))
842 (copy-tree (get recordname 'defstruct-default)))
844 ;; assume there is some initialization here e.g. new (f(5,6,7))
846 (let ((recordop (caar recordname)) (recordargs (cdr recordname)))
847 (if (null (get recordop 'defstruct-default))
848 (merror (intl:gettext "new: no such structure ~M") recordop))
850 (if (not (= (length recordargs) (length (cdr (get recordop 'defstruct-default)))))
851 (merror (intl:gettext "new: wrong number of arguments in initializer; expected ~M, not ~M.")
852 (length (cdr (get recordop 'defstruct-default))) (length recordargs)))
854 `(,(car recordname) ,@(mapcar #'meval (cdr recordname))))))))
856 ;; trivial translation to quiet complaint about lack of translation for this defmspec
857 (def%tr $new (x) `($any . (meval ',x)))
859 ;; Following property assignments comprise the Lisp code equivalent to infix("@", 200, 201)
861 (defprop $@ %@ verb)
862 (defprop $@ "@" op)
863 (putopr "@" '$@)
864 ;; !! FOLLOWING LINE MOVED TO NPARSE.LISP TO AVOID COMPILER ERROR
865 ;; !! (MOVING SUPRV1.LISP HIGHER IN MAXIMA.SYSTEM CAUSES MYSTERIOUS ERROR)
866 ;; !! (define-symbol "@")
867 (defprop $@ dimension-infix dimension)
868 (defprop $@ (#\@) dissym)
869 (defprop $@ tex-infix tex)
870 (defprop $@ ("@") texsym)
871 (defprop $@ msize-infix grind)
872 (defprop $@ 200 lbp)
873 (defprop $@ 201 rbp)
874 (defprop $@ parse-infix led)
875 (defprop %@ dimension-infix dimension)
876 (defprop %@ (#\@) dissym)
877 (defprop %@ $@ noun)
879 ;; The follow code implements PARALLEL LIST assignment.
880 ;; it is consistent with commercial macsyma. [a,b,c]:[x,y,z] means
881 ;; about the same as a:x, b:y, c:z. Actually it
882 ;; evaluates x,y,z BEFORE any assignments to a,b,c, hence parallel.
883 ;; Also implemented is [a,b,c]:x which evaluates x once and assigns
884 ;; to a,b,c.
885 ;; value returned is (evaluated x to ex) [ex,ex,ex].
887 ;; quiz . [a,b]:[b,2*a]. produces values a=b, b= 2*a.
888 ;; re-execute the statement 4 times. what do you get? [4b, 8a]
890 ;; a neat application of parallel assignment is this version of
891 ;; a gcd algorithm (for integers)...
892 ;; kgcd(a,b):=(while b#0 do [a,b]:[b,remainder(a,b)], abs(a));
893 ;; The extended euclidean algorithm looks even better with parallel
894 ;; assignment.
896 ;; add MLIST to possible operators on the left hand side of
897 ;; an assignment statement.
899 (setf (get 'mlist 'mset_extension_operator) 'mlist-assign)
901 (defmfun mlist-assign (tlist vlist)
902 ;; tlist is ((mlist..) var[0]... var[n]) of targets
903 ;; vlist is either((mlist..) val[0]... val[n]) of values
904 ;; or possibly just one value.
905 ;; should insert some checking code here
906 (if (and (listp vlist)
907 (eq (caar vlist) 'mlist)
908 (not (= (length tlist)(length vlist))))
909 (merror (intl:gettext "assignment: lists must be the same length; found: ~M, ~M") tlist vlist))
910 (setq tlist
911 `((mlist)
912 ,@(mapcar
913 #'(lambda (x)
914 (if (or (symbolp x) (get (caar x) 'mset_extension_operator))
916 `(,(car x) ,@(mapcar #'meval (cdr x)))))
917 (cdr tlist))))
918 (unless (and (listp vlist)
919 (eq (caar vlist) 'mlist))
920 (setf vlist (cons (car tlist) ;; if [a,b,c]:v then make a list [v,v,v]
921 (make-sequence 'list (1-(length tlist)) :initial-element vlist))))
922 (map nil #'mset (cdr tlist)(cdr vlist))
923 vlist)
925 ;; ---------- end code copied & modified from defstruct.lisp
927 (defmspec $ev (l)
928 (setq l (cdr l))
929 (let ((evp t) (*nounl* *nounl*) ($float $float) ($numer $numer)
930 ($expop $expop) ($expon $expon) ($doallmxops $doallmxops)
931 ($doscmxops $doscmxops) (derivflag derivflag) ($detout $detout)
932 (*nounsflag* *nounsflag*) (rulefcnl rulefcnl))
933 (if (and (cdr l) (null (cddr l)) (eq (car l) '$%e) (eq (cadr l) '$numer))
934 (setq l (append l '($%enumer))))
935 (do ((l (cdr l) (cdr l)) (bndvars) (bndvals) (locvars) (exp (car l))
936 (subsl) (evflg 0) (ratf) (derivlist) (evfunl) (funcl) (predflg)
937 (noeval (member '$noeval (cdr l) :test #'eq)))
938 ((null l)
939 (mbinding (bndvars bndvars)
940 (meval `((mlocal) ,@locvars))
941 (let ($translate) (mapc #'meval1 funcl))
942 (let ($numer) (setq exp (mevalatoms exp)))
943 (if ($ratp exp) (setq ratf t exp ($ratdisrep exp)))
944 (if (specrepp exp) (setq exp (specdisrep exp)))
945 (when subsl
946 (setq exp (simplify exp))
947 (dolist (item subsl)
948 (setq exp (maxima-substitute (meval (car item))
949 (meval (cdr item))
950 exp)))))
951 (mbinding (bndvars bndvals)
952 (if (and $numer noeval $%enumer)
953 (setq exp (maxima-substitute %e-val '$%e exp)))
954 (setq exp (if noeval
955 (resimplify exp)
956 (simplify (if predflg (mevalp exp) (meval1 exp)))))
957 (if (or (> evflg 0) $infeval)
958 (prog (exp1)
959 (setq exp (specrepcheck exp))
960 loop (do ((l evfunl (cdr l)) (exp2 exp))
961 ((null l) (setq exp1 (meval exp2)))
962 (setq exp2 (list (ncons (car l)) exp2)))
963 (dolist (item subsl)
964 (setq exp1 (maxima-substitute (meval (car item))
965 (meval (cdr item))
966 exp1)))
967 (cond ((or (and (not $infeval)
968 (= (setq evflg (1- evflg)) 0))
969 (prog2 (setq exp1 (specrepcheck exp1))
970 (alike1 exp exp1)))
971 (setq exp exp1))
972 (t (setq exp exp1) (go loop)))))
973 (if (and ratf (not $numer) (not $float))
974 (setq exp (let ($norepeat) (ratf exp)))))
975 (munlocal)
976 exp)
977 (if (not (or (atom (car l))
978 (member 'array (cdaar l) :test #'eq)
979 (member (caaar l) '(mquote msetq mlist mequal mdefine mset
980 mdefmacro $expand $local $derivlist) :test #'eq)))
981 (setq l (cons (meval (car l)) (cdr l))))
982 (cond ((or (atom (car l)) (member 'array (cdaar l) :test #'eq) (eq (caaar l) 'mquote))
983 (or (and (symbolp (car l))
984 (cond ((eq (car l) '$eval) (setq evflg (1+ evflg)))
985 ((member (car l) '($noeval $rescan) :test #'eq))
986 ((eq (car l) '$detout)
987 (setq $doallmxops nil $doscmxops nil $detout t))
988 ((eq (car l) '$numer) (setq $numer t $float t))
989 ((eq (car l) '$nouns) (setq *nounsflag* t))
990 ((eq (car l) '$pred) (setq predflg t))
991 ((eq (car l) '$expand)
992 (setq $expop $maxposex $expon $maxnegex))
993 ((eq (car l) '%derivative)
994 (setq derivflag t derivlist nil))
995 ((get (car l) 'evflag)
996 (setq bndvars (cons (car l) bndvars)
997 bndvals (cons (get (car l) 'evflag) bndvals)))
998 ((get (car l) 'evfun)
999 (setq exp (evfunmake (car l) exp)
1000 evfunl (nconc evfunl (ncons (car l)))))))
1001 (let ((fl (meval (car l))))
1002 (cond ((symbolp fl)
1003 (cond ((eq fl '$diff)
1004 (setq l (list* nil '$del (cdr l))))
1005 ((eq fl '$risch)
1006 (setq l (list* nil '$integrate (cdr l)))))
1007 (setq *nounl* (cons ($nounify fl) *nounl*)))
1008 ((numberp fl) (improper-arg-err (car l) '$ev))
1009 ((stringp fl) (improper-arg-err (car l) '$ev))
1010 ((eq (caar fl) 'mlist)
1011 (setq l (append fl (cdr l))))
1012 ((member (caar fl)
1013 '(msetq mequal mdefine mdefmacro mset) :test #'eq)
1014 (setq l (list* nil fl (cdr l))))
1015 (t (improper-arg-err (car l) '$ev))))))
1016 ((not (member (caaar l) '(msetq mlist mequal mdefine mdefmacro
1017 $expand $local $derivlist mset) :test #'eq))
1018 (improper-arg-err (car l) '$ev))
1019 ((eq (caaar l) '$expand)
1020 (cond ((null (cdar l)) (setq $expop $maxposex $expon $maxnegex))
1021 ((null (cddar l)) (setq $expop (cadar l) $expon $maxnegex))
1022 (t (setq $expop (cadar l) $expon (caddar l)))))
1023 ((member (caaar l) '(mdefine mdefmacro) :test #'eq)
1024 (let ((fun (cadar l)) $use_fast_arrays)
1025 (if (eq (caar fun) 'mqapply) (setq fun (cadr fun)))
1026 (setq fun ($verbify (caar fun)))
1027 (setq funcl (nconc funcl (ncons (car l)))
1028 locvars (append locvars (ncons fun)))
1029 (if (rulechk fun) (setq rulefcnl (cons fun rulefcnl)))))
1030 ((eq (caaar l) '$local) (setq locvars (append locvars (cdar l))))
1031 ((eq (caaar l) '$derivlist) (setq derivflag t derivlist (cdar l)))
1032 ((and (eq (caaar l) 'mset)
1033 (setq l (cons (list '(msetq) (meval (cadar l)) (caddar l))
1034 (cdr l)))
1035 nil))
1036 ((member (caaar l) '(msetq mequal) :test #'eq)
1037 (if (and (msetqp (car l)) (msetqp (caddar l)))
1038 (setq l (nconc (|:SPREAD| (car l)) (cdr l))))
1039 (if (or noeval (not (atom (cadar l))))
1040 (setq subsl (nconc subsl (list (cons (caddar l) (cadar l))))))
1041 (if (atom (cadar l))
1042 (setq bndvars (cons (cadar l) bndvars)
1043 bndvals (cons (meval (specrepcheck (caddar l))) bndvals))))
1044 (t (setq l (append (car l) (cdr l))))))))
1046 (defmfun mevalatoms (exp)
1047 (cond ((atom exp) (meval1 exp))
1048 ((member 'array (cdar exp) :test #'eq)
1049 (let (exp1)
1050 (let ((evarrp t)) (setq exp1 (catch 'evarrp (meval1 exp))))
1051 (if (eq exp1 'notexist)
1052 (cons (car exp) (mapcar #'mevalatoms (cdr exp)))
1053 exp1)))
1054 ((eq (caar exp) 'mquote) (cadr exp))
1055 ((member (caar exp) '(msetq $define) :test #'eq)
1056 (list (car exp) (cadr exp) (mevalatoms (caddr exp))))
1057 ((or (and (eq (caar exp) '$ev)
1058 (cdr exp)
1059 (or (null (cddr exp)) (equal (cddr exp) '($eval))))
1060 (eq (caar exp) 'mprogn))
1061 (cons (car exp) (cons (mevalatoms (cadr exp)) (cddr exp))))
1062 ((member (caar exp) '($sum $product %sum %product) :test #'eq)
1063 (if msump
1064 (meval exp)
1065 (list (car exp) (cadr exp) (caddr exp)
1066 (mevalatoms (cadddr exp)) (mevalatoms (car (cddddr exp))))))
1067 ((and (eq (caar exp) '$%th) (eq (ml-typep (simplify (cadr exp))) 'fixnum))
1068 (meval1 exp))
1069 ((prog2 (autoldchk (caar exp))
1070 (and (getl (caar exp) '(mfexpr*))
1071 (not (get (caar exp) 'evok))))
1072 exp)
1073 ((mgetl (caar exp) '(mfexprp))
1074 (cons (car exp)
1075 (do ((a (cdadr (mget (caar exp) 'mexpr)) (cdr a))
1076 (b (cdr exp) (cdr b)) (l))
1077 ((not (and a b)) (nreverse l))
1078 (cond ((mdeflistp a)
1079 (return (nreconc l (if (mquotep (cadar a))
1081 (mapcar #'mevalatoms b)))))
1082 ((mquotep (car a)) (setq l (cons (car b) l)))
1083 (t (setq l (cons (mevalatoms (car b)) l)))))))
1084 ((or (eq (caar exp) 'mmacroexpanded)
1085 (and $transrun (get (caar exp) 'translated-mmacro))
1086 (mget (caar exp) 'mmacro))
1087 (mevalatoms (mmacroexpand exp)))
1088 (t (cons (car exp) (mapcar #'mevalatoms (cdr exp))))))
1090 ;; evok properties
1091 (mapc #'(lambda (x) (putprop x t 'evok))
1092 '($map $maplist $fullmap $matrixmap $fullmapl $outermap $scanmap $apply))
1094 (defun evfunmake (fun exp)
1095 (if (msetqp exp)
1096 (list (car exp) (cadr exp) (evfunmake fun (caddr exp)))
1097 (list (ncons fun) exp)))
1099 (defun |:SPREAD| (x)
1100 (do ((val (do ((x x (caddr x))) (nil)
1101 (if (not (msetqp (caddr x))) (return (caddr x)))))
1102 (x x (caddr x)) (l))
1103 ((not (msetqp x)) l)
1104 (setq l (cons (list (car x) (cadr x) val) l))))
1106 (defmfun msetqp (x)
1107 (and (not (atom x)) (eq (caar x) 'msetq)))
1109 (defmfun mquotep (x)
1110 (and (not (atom x)) (eq (caar x) 'mquote)))
1112 (defmspec mquote (form)
1113 (cadr form))
1115 (defmfun $subvarp (x)
1116 (and (not (atom x)) (member 'array (cdar x) :test #'eq) t))
1118 (defmfun mseterr (x y)
1119 (if munbindp
1120 'munbindp
1121 (merror (intl:gettext "assignment: cannot assign ~M to ~:M") y x)))
1123 ;; assign properties
1124 (mapc #'(lambda (x) (putprop (car x) (cadr x) 'assign))
1125 '(($linel msetchk) (*read-base* msetchk) (*print-base* msetchk) (modulus msetchk)
1126 ($infolists neverset) ($trace neverset) ($ratweights msetchk)
1127 ($ratvars msetchk) ($setcheck msetchk) ($gcd msetchk)
1128 ($dotassoc msetchk) ($ratwtlvl msetchk) ($ratfac msetchk)
1129 ($all neverset) ($numer numerset) ($fortindent msetchk)
1130 ($gensumnum msetchk) ($genindex msetchk) ($fpprintprec msetchk)
1131 ($floatwidth msetchk) ($parsewindow msetchk) ($optimprefix msetchk)
1132 ($ttyintnum msetchk)))
1134 (defmfun msetchk (x y)
1135 (cond ((member x '(*read-base* *print-base*) :test #'eq)
1136 (cond ((eq y 'roman))
1137 ((or (not (fixnump y)) (< y 2) (> y 36)) (mseterr x y))
1138 ((eq x '*read-base*))))
1139 ((member x '($linel $fortindent $gensumnum $fpprintprec $floatwidth
1140 $parsewindow $ttyintnum) :test #'eq)
1141 (if (not (fixnump y)) (mseterr x y))
1142 (if (eq x '$linel)
1143 (cond ((not (and (> y 0) ; at least one char per line
1144 (< y 1000001))) ; arbitrary chosen big value
1145 (mseterr x y))
1147 (setq linel y))))
1148 (cond ((and (member x '($fortindent $gensumnum $floatwidth $ttyintnum) :test #'eq) (< y 0))
1149 (mseterr x y))
1150 ((and (eq x '$parsewindow) (< y -1)) (mseterr x y))
1151 ((and (eq x '$fpprintprec) (or (< y 0) (= y 1))) (mseterr x y))))
1152 ((member x '($genindex $optimprefix) :test #'eq) (if (not (symbolp y)) (mseterr x y)))
1153 ((eq x '$dotassoc) (cput 'mnctimes y 'associative))
1154 ((eq x 'modulus)
1155 (cond ((null y))
1156 ((and (integerp y) (plusp y))
1157 ;; modulus must be an integer > 0. Give a warning if not
1158 ;; a prime number.
1159 (if (not (primep y))
1160 (mtell (intl:gettext "warning: assigning ~:M, a non-prime, to 'modulus'~&") y)))
1161 (t (mseterr x y))))
1162 ((eq x '$setcheck)
1163 (if (not (or (member y '($all t nil) :test #'eq) ($listp y))) (mseterr x y)))
1164 ((eq x '$gcd) (if (not (or (null y) (member y *gcdl* :test #'eq))) (mseterr x y)))
1165 ((eq x '$ratvars)
1166 (if ($listp y) (apply #'$ratvars (cdr y)) (mseterr x y)))
1167 ((eq x '$ratfac)
1168 (if (and y $ratwtlvl)
1169 (merror (intl:gettext "assignment: 'ratfac' and 'ratwtlvl' may not both be used at the same time."))))
1170 ((eq x '$ratweights)
1171 (cond ((not ($listp y)) (mseterr x y))
1172 ((null (cdr y)) (kill1 '$ratweights))
1173 (t (apply #'$ratweight (cdr y)))))
1174 ((eq x '$ratwtlvl)
1175 (if (and y (not (fixnump y))) (mseterr x y))
1176 (if (and y $ratfac)
1177 (merror (intl:gettext "assignment: 'ratfac' and 'ratwtlvl' may not both be used at the same time."))))))
1179 (defmfun numerset (assign-var y)
1180 (declare (ignore assign-var))
1181 (mset '$float y))
1183 (defmfun neverset (x assign-val)
1184 (declare (ignore assign-val))
1185 (if munbindp
1186 'munbindp
1187 (merror (intl:gettext "assignment: cannot assign to ~:M") x)))
1189 ;; Check assignment to be a positive integer including zero
1190 (defun posintegerset (x y)
1191 (if (or (not (integerp y))
1192 (not (>= y 0)))
1193 (merror
1194 (intl:gettext "assignment: '~:M must be a positive integer. Found: ~:M")
1195 x y)))
1197 (defmfun mmapev (l)
1198 (if (null (cddr l))
1199 (merror (intl:gettext "~:M: expected two or more arguments; found: ~M") (caar l) (cdr l)))
1200 (let ((op (getopr (meval (cadr l)))))
1201 (autoldchk op)
1202 (badfunchk (cadr l) op nil)
1203 (cons op (mapcar #'meval (cddr l)))))
1205 (defmspec $map (l)
1206 (apply #'map1 (mmapev l)))
1208 (defmfun map1 n
1209 (do ((i n (1- i))
1210 (argi (setarg n (format1 (arg n))) (format1 (arg (1- i))))
1211 (op (or (mapatom (arg n)) (mop (arg n))))
1212 (flag (mapatom (arg n))
1213 (or flag
1214 (setq flag (mapatom argi))
1215 (and (not maplp) (not (alike1 (mop argi) op)))))
1216 (argl nil (cons argi argl))
1217 (cdrl nil (or flag (cons (margs argi) cdrl))))
1218 ((= i 1) (if flag
1219 (cond ((not $maperror)
1220 (when $mapprint (mtell (intl:gettext "map: calling 'apply'")))
1221 (funcer (arg 1) argl))
1222 ((and (= n 2) (mapatom (arg 2)))
1223 (improper-arg-err (arg 2) '$map))
1224 (t (merror (intl:gettext "map: arguments must have same main operator; found: ~M, ~M") op (mop (first argl)))))
1225 (mcons-op-args op (apply #'mmapcar (cons (arg 1) cdrl)))))))
1227 (defmspec $maplist (l)
1228 (let ((maplp t) res)
1229 (setq res (apply #'map1 (mmapev l)))
1230 (cond ((atom res) (list '(mlist) res))
1231 ((eq (caar res) 'mlist) res)
1232 (t (cons '(mlist) (margs res))))))
1234 (defmfun mmapcar n
1235 (do ((ans nil (cons (funcer (arg 1) argl) ans))
1236 (argl nil nil))
1237 ((do ((i n (1- i)))
1238 ((= i 1) nil)
1239 (when (null (arg i))
1240 (when (or (< i n)
1241 (do ((j 2 (1+ j)))
1242 ((= j n) nil)
1243 (when (arg j) (return t))))
1244 (when $maperror
1245 (merror (intl:gettext "map: arguments must be the same length.")))
1246 (when $mapprint (mtell (intl:gettext "map: truncating one or more arguments."))))
1247 (return t))
1248 (push (car (arg i)) argl)
1249 (setarg i (cdr (arg i))))
1250 (nreverse ans))))
1252 (defun mapatom (x)
1253 (or (symbolp x) (mnump x) ($subvarp x) (stringp x)))
1255 (defmfun $mapatom (x)
1256 (if (mapatom (specrepcheck x)) t))
1258 (defmspec $fullmap (l)
1259 (setq l (mmapev l))
1260 (fmap1 (car l) (cdr l) nil))
1262 (defun fmap1 (fn argl fmapcaarl)
1263 (setq argl (mapcar #'format1 argl))
1264 (do ((op (or (mapatom (car argl)) (mop (car argl))))
1265 (fmaplvl (1- fmaplvl)) (cdr1 argl (cdr cdr1)) (argi nil nil)
1266 (cdrl nil (cons (margs (car cdr1)) cdrl)))
1267 ((null cdr1)
1268 (do ((ans nil (cons (if bottom (funcer fn carargl)
1269 (fmap1 fn carargl fmapcaarl))
1270 ans))
1271 (carargl nil nil) (cdrargl nil nil)
1272 (cdrl cdrl cdrargl) (bottom nil nil)
1273 (done (when (member nil cdrl :test #'eq)
1274 (when (dolist (e cdrl) (if e (return t)))
1275 (when $maperror
1276 (merror (intl:gettext "fullmap: arguments must have same formal structure.")))
1277 (when $mapprint
1278 (mtell (intl:gettext "fullmap: truncating one or more arguments.~%"))))
1279 t)))
1280 (done (mcons-op-args op (nreverse ans)))
1281 (do ((op (or (setq bottom (or (zerop fmaplvl) (mapatom (caar cdrl))))
1282 (mop (caar cdrl))))
1283 (eleml cdrl (cdr eleml)) (caareleml nil nil))
1284 ((null eleml)
1285 (when (and done (dolist (e cdrargl) (if e (return t))))
1286 (if $maperror
1287 (merror (intl:gettext "fullmap: arguments must have same formal structure.")))
1288 (if $mapprint (mtell (intl:gettext "fullmap: truncating one or more arguments.~%")))))
1289 (setq caareleml (caar eleml))
1290 (or bottom
1291 (setq bottom
1292 (or (mapatom caareleml)
1293 (not (alike1 op (mop caareleml)))
1294 (and fmapcaarl (not (eq (caar caareleml) fmapcaarl))))))
1295 (or done (setq done (null (cdar eleml))))
1296 (setq carargl (nconc (ncons caareleml) carargl)
1297 cdrargl (nconc cdrargl (ncons (cdar eleml)))))))
1298 (setq argi (car cdr1))
1299 (if (or (mapatom argi)
1300 (not (alike1 op (mop argi)))
1301 (and fmapcaarl (not (eq (caar argi) fmapcaarl))))
1302 (cond ($maperror (merror (intl:gettext "fullmap: arguments must have same operators.")))
1303 (t (if $mapprint (mtell (intl:gettext "fullmap: calling 'apply'.~%")))
1304 (return (funcer fn argl)))))))
1306 (defmspec $matrixmap (l)
1307 (let ((fmaplvl 2))
1308 (apply #'fmapl1 (mmapev l))))
1310 (defmspec $fullmapl (l)
1311 (apply #'fmapl1 (mmapev l)))
1313 (defmfun fmapl1 (fun &rest args)
1314 (let* ((header '(mlist))
1315 (argl (fmap1 fun
1316 (mapcar #'(lambda (z)
1317 (cond ((not (mxorlistp z))
1318 (merror (intl:gettext "fullmapl: argument must be a list or matrix; found: ~M") (or (and (consp z) (mop z)) z)))
1319 ((eq (caar z) '$matrix)
1320 (setq header '($matrix))
1321 (cons '(mlist simp) (cdr z)))
1322 (t z)))
1323 args)
1324 'mlist)))
1325 (if (dolist (e (cdr argl))
1326 (unless ($listp e) (return t)))
1327 argl
1328 (cons header (cdr argl)))))
1330 (defun $outermap (x y &rest z)
1331 (if z
1332 (apply #'outermap1 x y z)
1333 (fmapl1 x y)))
1335 (defmfun outermap1 n
1336 (let (outargs1 outargs2)
1337 (declare (special outargs1 outargs2))
1338 (cond ((mxorlistp (arg 2))
1339 (setq outargs1 (ncons (arg 1))
1340 outargs2 (listify (- 2 n)))
1341 (fmapl1 #'outermap2 (arg 2)))
1342 (t (do ((i 3 (1+ i)))
1343 ((> i n) (funcer (arg 1) (listify (- 1 n))))
1344 (when (mxorlistp (arg i))
1345 (setq outargs1 (listify (1- i))
1346 outargs2 (if (< i n) (listify (- i n))))
1347 (return (fmapl1 #'outermap2 (arg i)))))))))
1349 (defmfun outermap2 (&rest args)
1350 (declare (special outargs1 outargs2))
1351 (unless (null args)
1352 (apply #'outermap1 (append outargs1 (list (first args)) outargs2))))
1354 (defmfun funcer (fn args)
1355 (cond ((member fn '(mplus mtimes mexpt mnctimes) :test #'eq)
1356 (simplify (cons (ncons fn) args)))
1357 ((or (member fn '(outermap2 constfun) :test #'eq)
1358 (and $transrun (symbolp fn) (get fn 'translated)
1359 (not (mget fn 'local-fun)) (fboundp fn)))
1360 (apply fn (mapcar #'simplify args)))
1361 (t (mapply1 fn (mapcar #'simplify args) fn
1362 nil ;; try to get more info to pass
1363 ))))
1365 (defmspec $qput (l)
1366 (setq l (cdr l))
1367 (unless (= (length l) 3)
1368 (wna-err '$qput))
1369 ($put (car l) (cadr l) (caddr l)))
1371 (defmfun $rem (atom ind)
1372 (prop1 '$rem atom nil ind))
1374 (defmfun $put (atom val ind)
1375 (prog1
1376 (prop1 '$put atom val ind)
1377 (add2lnc atom $props)))
1379 (defun prop1 (fun atom val ind)
1380 (unless (or (symbolp atom) (stringp atom))
1381 (merror (intl:gettext "~:M: argument must be a symbol or a string; found: ~M") fun atom))
1382 (unless (or (symbolp ind) (stringp ind))
1383 (merror (intl:gettext "~:M: indicator must be a symbol or a string; found: ~M") fun ind))
1384 (unless (symbolp atom)
1385 (if (symbolp (getopr atom))
1386 (setq atom (getopr atom))
1387 (setq atom (intern atom))))
1388 (unless (symbolp ind)
1389 (setq ind (intern ind)))
1390 (let ((u (mget atom '$props)))
1391 (cond ((eq fun '$get) (and u (old-get u ind)))
1392 ((eq fun '$rem) (and u (zl-remprop u ind) '$done))
1393 ((not u) (mputprop atom (list nil ind val) '$props) val)
1394 (t (putprop u val ind)))))
1396 (defmspec $declare (l)
1397 (setq l (cdr l))
1398 (when (oddp (length l))
1399 (merror (intl:gettext "declare: number of arguments must be a multiple of 2.")))
1400 (do ((l l (cddr l)) (vars) (flag nil nil))
1401 ((null l)
1402 '$done)
1403 (cond (($listp (cadr l))
1404 (do ((l1 (cdadr l) (cdr l1))) ((if (null l1) (setq flag t)))
1405 (meval `(($declare) ,(car l) ,(car l1)))))
1406 ((nonsymchk (cadr l) '$declare))
1407 (t (setq vars (declsetup (car l) '$declare))))
1408 (cond (flag)
1409 ((member (cadr l) '($evfun $evflag $nonarray $bindtest) :test #'eq)
1410 (declare1 vars t (stripdollar (cadr l)) nil))
1411 ((eq (cadr l) '$noun)
1412 (dolist (var vars) (alias (getopr var) ($nounify var))))
1413 ((member (cadr l) '($nonscalar $scalar $mainvar) :test #'eq)
1414 (declare1 vars t (cadr l) t))
1415 ((eq (cadr l) '$alphabetic) (declare1 vars t t '$alphabetic))
1416 ((member (cadr l) opers :test #'eq)
1417 (if (member (cadr l) (cdr $features) :test #'eq) (declare1 vars t (cadr l) 'kind))
1418 (declare1 (mapcar #'getopr vars) t (cadr l) 'opers))
1419 ((member (cadr l) (cdr $features) :test #'eq) (declare1 vars t (cadr l) 'kind))
1420 ((eq (cadr l) '$feature)
1421 (dolist (var vars) (nonsymchk var '$declare) (add2lnc var $features)))
1422 (t (merror (intl:gettext "declare: unknown property ~:M") (cadr l))))))
1424 (defun declare1 (vars val prop mpropp)
1425 (dolist (var vars)
1426 (setq var (getopr var))
1427 (unless (or (symbolp var) (stringp var))
1428 (merror (intl:gettext "declare: argument must be a symbol or a string; found: ~M") var))
1429 (cond ((eq mpropp 'kind) (declarekind var prop))
1430 ((eq mpropp 'opers)
1431 (putprop (setq var (linchk var)) t prop) (putprop var t 'opers)
1432 (if (and (symbolp var) (not (get var 'operators)))
1433 (putprop var 'simpargs1 'operators)))
1435 ((eq mpropp '$alphabetic)
1436 ; Explode var into characters and put each one on the *alphabet* list,
1437 ; which is used by src/nparse.lisp .
1438 (dolist (1-char (coerce var 'list))
1439 (add2lnc 1-char *alphabet*)))
1441 (mpropp
1442 (if (and (member prop '($scalar $nonscalar) :test #'eq)
1443 (mget var (if (eq prop '$scalar) '$nonscalar '$scalar)))
1444 (merror (intl:gettext "declare: inconsistent declaration ~:M") `(($declare) ,var ,prop)))
1445 (mputprop var val prop))
1446 (t (putprop var val prop)))
1447 (if (and (safe-get var 'op) (operatorp1 var)
1448 (not (member (setq var (get var 'op)) (cdr $props) :test #'eq)))
1449 (setq *mopl* (cons var *mopl*)))
1450 (add2lnc (getop var) $props)))
1452 (defun linchk (var)
1453 (if (member var '($sum $integrate $limit $diff $transpose) :test #'eq)
1454 ($nounify var)
1455 var))
1457 (defmspec $remove (form)
1458 (i-$remove (cdr form)))
1460 (defmfun i-$remove (l)
1461 (when (oddp (length l))
1462 (merror (intl:gettext "remove: number of arguments must be a multiple of 2.")))
1463 (do ((l l (cddr l)) (vars) (flag nil nil)) ((null l) '$done)
1464 (cond (($listp (cadr l))
1465 (do ((l1 (cdadr l) (cdr l1))) ((if (null l1) (setq flag t)))
1466 (i-$remove (list (car l) (car l1)))))
1467 ((unless (or (symbolp (cadr l)) (stringp (cadr l)))
1468 (merror (intl:gettext "remove: argument must be a symbol or a string; found: ~M") (cadr l))))
1469 (t (setq vars (declsetup (car l) '$remove))))
1470 (cond (flag)
1471 ((eq (cadr l) '$value) (i-$remvalue vars))
1472 ((eq (cadr l) '$function)
1473 (remove1 (mapcar #'$verbify vars) 'mexpr t $functions t))
1474 ((eq (cadr l) '$macro)
1475 (remove1 (mapcar #'$verbify vars) 'mmacro t $macros t))
1476 ((eq (cadr l) '$array) (meval `(($remarray) ,@vars)))
1477 ((member (cadr l) '($alias $noun) :test #'eq) (remalias1 vars (eq (cadr l) '$alias)))
1478 ((eq (cadr l) '$matchdeclare) (remove1 vars 'matchdeclare t t nil))
1479 ((eq (cadr l) '$rule) (remrule vars))
1480 ((member (cadr l) '($evfun $evflag $nonarray $bindtest
1481 $autoload $assign) :test #'eq)
1482 (remove1 vars (stripdollar (cadr l)) nil t nil))
1483 ((member (cadr l) '($mode $modedeclare) :test #'eq) (remove1 vars 'mode nil 'foo nil))
1484 ((eq (cadr l) '$atvalue) (remove1 vars 'atvalues t t nil))
1485 ((member (cadr l) '($nonscalar $scalar $mainvar $numer $atomgrad) :test #'eq)
1486 (remove1 vars (cadr l) t t nil))
1487 ((member (cadr l) opers :test #'eq) (remove1 (mapcar #'linchk vars) (cadr l) nil t nil))
1488 ((member (cadr l) (cdr $features) :test #'eq) (remove1 vars (cadr l) nil t nil))
1489 ((eq (cadr l) '$feature)
1490 (dolist (var vars)
1491 (setf $features (delete var $features :count 1 :test #'eq))))
1492 ((member (cadr l) '($alphabetic $transfun) :test #'eq)
1493 (remove1 vars (cadr l) nil t nil))
1494 ((member (cadr l) '($gradef $grad) :test #'eq) (remove1 vars 'grad nil $gradefs t))
1495 ((member (cadr l) '($dependency $depend $depends) :test #'eq)
1496 (remove1 vars 'depends t $dependencies t))
1497 ((member (cadr l) '($op $operator) :test #'eq) (remove1 vars '$op nil 'foo nil))
1498 ((member (cadr l) '($deftaylor $taylordef) :test #'eq) (remove1 vars 'sp2 nil t nil))
1499 (t (merror (intl:gettext "remove: unknown property ~:M") (cadr l))))))
1501 (defun declsetup (x fn)
1502 (cond ((atom x) (ncons x))
1503 ((eq (caar x) '$nounify) (ncons (meval x)))
1504 ((eq (caar x) 'mlist)
1505 (mapcar #'(lambda (var)
1506 (cond ((atom var) var)
1507 ((eq (caar var) '$nounify) (meval var))
1508 (t (improper-arg-err var fn))))
1509 (cdr x)))
1510 (t (improper-arg-err x fn))))
1512 (defmfun remove1 (vars prop mpropp info funp)
1513 (do ((vars vars (cdr vars)) (allflg))
1514 ((null vars))
1515 (unless (or (symbolp (car vars)) (stringp (car vars)))
1516 (merror (intl:gettext "remove: argument must be a symbol or a string; found: ~M") (car vars)))
1517 (cond ((and (eq (car vars) '$all) (null allflg))
1518 (setq vars (append vars (cond ((atom info) (cdr $props))
1519 (funp (mapcar #'caar (cdr info)))
1520 (t (cdr info))))
1521 allflg t))
1523 (if (and (stringp (car vars)) (eq prop '$op) (getopr0 (car vars)))
1524 (kill-operator (getopr0 (car vars))))
1526 (let ((var (getopr (car vars)))( flag nil))
1527 (cond (mpropp (mremprop var prop)
1528 (when (member prop '(mexpr mmacro) :test #'eq)
1529 (mremprop var 'mlexprp)
1530 (mremprop var 'mfexprp)
1531 (remprop var 'lineinfo)
1532 (if (mget var 'trace)
1533 (macsyma-untrace var))))
1534 ((and (eq prop '$alphabetic) (stringp var))
1535 (dolist (1-char (coerce var 'list))
1536 (setf *alphabet* (delete 1-char *alphabet* :count 1 :test #'equal))))
1537 ((eq prop '$transfun)
1538 (remove-transl-fun-props var)
1539 (remove-transl-array-fun-props var))
1540 ((or (setq flag (member prop (cdr $features) :test #'eq)) (member prop opers :test #'eq))
1541 (if flag (unkind var prop))
1542 (zl-remprop var prop)
1543 (if (not (getl var (delete prop (copy-list opers) :count 1 :test #'eq)))
1544 (zl-remprop var 'opers)))
1545 (t (zl-remprop var prop)))
1546 (cond ((eq info t) (rempropchk var))
1547 ((eq info 'foo))
1548 (funp
1549 (mfunction-delete var info))
1551 (setf info (delete var info :count 1 :test #'eq)))))))))
1553 (defun remove-transl-fun-props (fun)
1554 (if (mget fun 'trace)
1555 (macsyma-untrace fun))
1556 (when (and (get fun 'translated) (not (eq $savedef '$all)))
1557 (fmakunbound fun)
1558 (zl-remprop fun 'translated-mmacro)
1559 (zl-remprop fun 'function-mode)
1560 (when (not (getl fun '(a-expr a-subr)))
1561 (zl-remprop fun 'once-translated)
1562 (zl-remprop fun 'translated))))
1564 (defun remove-transl-array-fun-props (fun)
1565 (when (and (get fun 'translated) (not (eq $savedef '$all)))
1566 (zl-remprop fun 'a-expr)
1567 (zl-remprop fun 'a-subr)
1568 (if (not (fboundp fun)) (zl-remprop fun 'translated))))
1570 (defmfun rempropchk (var)
1571 (if (and
1573 (not (symbolp var))
1574 (and
1575 (not (mgetl var '($nonscalar $scalar $mainvar $numer
1576 matchdeclare $atomgrad atvalues)))
1577 (not (getl var '(evfun evflag translated nonarray bindtest
1578 sp2 operators opers data autoload mode)))))
1579 (not (member var *builtin-$props* :test #'equal)))
1580 (delete var $props :count 1 :test #'equal)))
1582 (defmspec $remfunction (l)
1583 (setq l (cdr l))
1584 (cond ((member '$all l :test #'eq)
1585 (setq l (nconc (mapcar #'caar (cdr $functions))
1586 (mapcar #'caar (cdr $macros)))))
1587 (t (setq l (mapcar #'$verbify l))
1588 (do ((l1 l (cdr l1))) ((null l1) t)
1589 (if (not (or (assoc (ncons (car l1)) (cdr $functions) :test #'equal)
1590 (assoc (ncons (car l1)) (cdr $macros) :test #'equal)))
1591 (rplaca l1 nil)))))
1592 (remove1 l 'mexpr t $functions t)
1593 (remove1 l 'mmacro t $macros t)
1594 (cons '(mlist) l))
1596 (defmspec $remarray (l)
1597 (setq l (cdr l))
1598 (cons '(mlist)
1599 (do ((l l (cdr l)) (x) (pred)) ((null l) (nreverse x))
1600 (cond ((eq (car l) '$all) (setq l (append l (cdr $arrays))))
1601 (t (remcompary (car l)) (setq pred (mremprop (car l) 'array))
1602 (setq pred (or (mremprop (car l) 'hashar) pred))
1603 (setq pred (or (mremprop (car l) 'aexpr) pred))
1604 (setq x (cons (and pred (prog2
1605 (setf $arrays (delete (car l) $arrays :count 1 :test #'eq))
1606 (car l)))
1607 x)))))))
1609 (defun remcompary (x)
1610 (cond ((eq x (mget x 'array))
1611 (zl-remprop x 'array-mode)
1612 (zl-remprop x 'array))))
1614 (defmspec $remvalue (form)
1615 (i-$remvalue (cdr form)))
1617 (defmfun i-$remvalue (l)
1618 (cons '(mlist)
1619 (do ((l l (cdr l)) (x) (y)) ((null l) (nreverse x))
1620 (cond ((eq (car l) '$all) (setq l (append l (cdr $values))))
1621 (t (setq x (cons (cond ((atom (car l))
1622 (if (remvalue (car l) '$remvalue) (car l)))
1623 ((setq y (mgetl (caaar l) '(hashar array)))
1624 (remarrelem y (car l)) (car l)))
1625 x)))))))
1627 (defmfun remarrelem (ary form)
1628 (let ((y (car (arraydims (cadr ary)))))
1629 (arrstore form (cond ((eq y 'fixnum) 0) ((eq y 'flonum) 0.0) (t munbound)))))
1631 (defmfun remrule (l)
1632 (do ((l l (cdr l)) (u))
1633 ((null l))
1634 (cond ((eq (car l) '$all) (setq l (append l (cdr $rules))))
1635 ((get (car l) 'operators) ($remrule (car l) '$all))
1636 ((setq u (ruleof (car l))) ($remrule u (car l)))
1637 ((mget (car l) '$rule)
1638 (zl-remprop (car l) 'expr) (mremprop (car l) '$rule)
1639 (setf $rules (delete (car l) $rules :count 1 :test #'eq))))))
1641 (defmfun remalias1 (l aliasp)
1642 (do ((l l (cdr l)) (u)) ((null l))
1643 (cond ((eq (car l) '$all) (setq l (append l (cdr $aliases))))
1644 ((or aliasp (get (car l) 'noun)) (remalias (car l) t))
1645 ((setq u (get (car l) 'verb))
1646 (zl-remprop (car l) 'verb) (zl-remprop u 'noun)))))
1648 (defmfun mremprop (atom ind)
1649 (let ((props (get atom 'mprops))) (and props (zl-remprop props ind))))
1651 (defmfun mgetl (atom inds)
1652 (let ((props (get atom 'mprops))) (and props (getl props inds))))
1654 ;;; Define $matrix so that apply(matrix,...) does not need to use Lisp
1655 ;;; apply -- in GCL, apply is limited to 63 arguments.
1657 ;;; Equivalent to matrix([?rows]) := ?matrixhelper(?rows)$
1658 #+gcl (mputprop '$matrix '((lambda) ((mlist) ((mlist) rows)) ((matrixhelper) rows)) 'mexpr)
1659 #+gcl (mputprop '$matrix t 'mlexprp)
1660 #+gcl (mputprop '$matrix '$matrix 'pname)
1662 #-gcl (defun $matrix (&rest rows) (matrixhelper rows))
1664 ;; Call ONLY from $matrix
1665 (defun matrixhelper (rows)
1666 #+gcl
1667 (progn
1668 (if (not ($listp rows)) (merror "internal error: MATRIXHELPER expects a Maxima list."))
1669 (setq rows (cdr rows)))
1670 (dolist (row rows)
1671 (if (not ($listp row))
1672 (merror (intl:gettext "matrix: row must be a list; found: ~M") row)))
1673 (matcheck rows)
1674 (cons '($matrix) rows))
1676 (defmfun matcheck (l)
1677 (do ((l1 (cdr l) (cdr l1)) (n (length (car l)))) ((null l1))
1678 (if (not (= n (length (car l1))))
1679 (merror (intl:gettext "matrix: all rows must be the same length.")))))
1681 (defun harrfind (form)
1682 (prog (ary y lispsub iteml sub ncells nitems)
1683 (setq ary (symbol-array (mget (caar form) 'hashar)))
1684 (cond ((not (= (aref ary 2) (length (cdr form))))
1685 (merror (intl:gettext "evaluation: array ~:M must have ~:M indices; found: ~M")
1686 (caar form) (aref ary 2) form)))
1687 (setq sub (cdr form))
1688 (setq iteml (aref ary (setq lispsub (+ 3 (rem (hasher sub) (aref ary 0))))))
1689 a (cond ((null iteml) (go b))
1690 ((alike (caar iteml) sub) (return (cdar iteml))))
1691 (setq iteml (cdr iteml))
1692 (go a)
1693 b (cond (evarrp (throw 'evarrp 'notexist))
1694 ((null (setq y (arrfunp (caar form)))) (return (meval2 sub form))))
1695 (setq y (arrfuncall y sub form))
1696 (setq ary (symbol-array (mget (caar form) 'hashar)))
1697 (setq iteml (aref ary (setq lispsub (+ 3 (rem (hasher sub) (aref ary 0))))))
1698 (setq sub (ncons (cons sub y)))
1699 (cond (iteml (nconc iteml sub)) (t (setf (aref ary lispsub) sub)))
1700 (setf (aref ary 1) (setq nitems (1+ (aref ary 1))))
1701 (cond ((> nitems (setq ncells (aref ary 0)))
1702 (arraysize (caar form) (+ ncells ncells))))
1703 (return y)))
1705 (defun arrfind (form)
1706 (let ((sub (cdr form)) u v type)
1707 (setq v (dimcheck (caar form) sub nil))
1708 (cond (v (setq type (car (arraydims (mget (caar form) 'array))))))
1709 (cond ((and v (prog2
1710 (setq u (apply 'aref (symbol-array (mget (caar form) 'array)) sub))
1711 (cond ((eq type 'flonum) (not (= u flounbound)))
1712 ((eq type 'fixnum) (not (= u fixunbound)))
1713 (t (not (eq u munbound))))))
1715 (evarrp (throw 'evarrp 'notexist))
1716 ((or (not v) (null (setq u (arrfunp (caar form)))))
1717 (cond ((eq type 'flonum) 0.0)
1718 ((eq type 'fixnum) 0)
1719 (t (meval2 sub form))))
1720 (t (setq u (arrfuncall u sub form))
1721 (setf (apply #'aref (symbol-array (mget (caar form) 'array))
1722 sub) u)
1724 u))))
1726 (defmspec $array (x)
1727 (setq x (cdr x))
1728 (cond ($use_fast_arrays
1729 (let ((type (if (symbolp (cadr x)) (cadr x) '$any))
1730 (name (car x))
1731 (diml (if (symbolp (cadr x)) (cddr x) (cdr x))))
1732 (mset name
1733 (apply '$make_array
1734 type
1735 (mapcar #'(lambda (dim)
1736 ;; let make_array catch bad vals
1737 (add 1 (meval dim)))
1738 diml)))))
1739 ((symbolp (car x))
1740 (let ((compp (assoc (cadr x) '(($complete . t) ($integer . fixnum) ($fixnum . fixnum)
1741 ($float . flonum) ($flonum . flonum)))))
1742 (let ((fun (car x))
1743 (diml (cond (compp (setq compp (cdr compp))
1744 (cddr x))
1745 (t (cdr x))))
1746 funp
1749 (ncells 0))
1750 (when (member '$function diml :test #'eq)
1751 (setq diml (delete '$function diml :count 1 :test #'eq)
1752 funp t))
1753 (setq diml (mapcar #'meval diml))
1754 (cond ((null diml)
1755 (wna-err '$array))
1756 ((> (length diml) 5)
1757 (merror (intl:gettext "array: number of dimensions must be 5 or less; found: ~M") (length diml)))
1758 ((member nil (mapcar #'(lambda (u) (eq (ml-typep u) 'fixnum)) diml)
1759 :test #'eq)
1760 (merror (intl:gettext "array: all dimensions must be integers."))))
1761 (setq diml (mapcar #'1+ diml))
1762 (setq new (if compp fun (gensym)))
1763 (setf (symbol-array new)
1764 (make-array diml :initial-element (case compp
1765 (fixnum 0)
1766 (flonum 0.0)
1767 (otherwise munbound))))
1768 (when (or funp (arrfunp fun))
1769 (fillarray new (list (if (eq compp 'fixnum) fixunbound flounbound))))
1770 (cond ((null (setq old (mget fun 'hashar)))
1771 (mputprop fun new 'array))
1772 (t (unless (= (aref (symbol-array old) 2) (length diml))
1773 (merror (intl:gettext "array: array ~:M must have ~:M dimensions; found: ~M") fun (aref (symbol-array old) 2) (length diml)))
1774 (setq ncells (+ 2 (aref (symbol-array old) 0)))
1775 (do ((n 3 (1+ n)))
1776 ((> n ncells))
1777 (do ((items (aref (symbol-array old) n) (cdr items)))
1778 ((null items))
1779 (do ((x (caar items) (cdr x)) (y diml (cdr y)))
1780 ((null x)
1781 (if (and (member compp '(fixnum flonum) :test #'eq)
1782 (not (eq (ml-typep (cdar items)) compp)))
1783 (merror (intl:gettext "array: existing elements must be ~M; found: ~M") compp (cdar items)))
1784 (setf (apply #'aref (symbol-array new) (caar items))
1785 (cdar items)))
1786 (if (or (not (eq (ml-typep (car x)) 'fixnum))
1787 (< (car x) 0)
1788 (not (< (car x) (car y))))
1789 (merror (intl:gettext "array: index must be nonnegative integer less than ~M; found: ~M") (car y) (car x))))))
1790 (mremprop fun 'hashar)
1791 (mputprop fun new 'array)))
1792 (add2lnc fun $arrays)
1793 (when (eq compp 'fixnum)
1794 (putprop fun '$fixnum 'array-mode))
1795 (when (eq compp 'flonum)
1796 (putprop fun '$float 'array-mode))
1797 fun)))
1798 (($listp (car x))
1799 (dolist (u (cdar x))
1800 (meval `(($array) ,u ,@(cdr x))))
1801 (car x))
1803 (merror (intl:gettext "array: first argument must be a symbol or a list; found: ~M") (car x)))))
1806 (defmfun $show_hash_array (x)
1807 (maphash #'(lambda (k v) (format t "~%~A-->~A" k v)) x))
1809 ;; If this is T then arrays are stored in the value cell,
1810 ;; whereas if it is false they are stored in the function cell
1811 (defmvar $use_fast_arrays nil)
1813 (defmfun arrstore (l r &aux tem index)
1814 (cond ($use_fast_arrays
1815 (cond ((and (boundp (caar l)) (setq tem (symbol-value (caar l))))
1816 (setq index (mevalargs (cdr l)))
1817 (let ((the-type (ml-typep tem)))
1818 (cond ((eq the-type 'array)
1819 (setf (apply #'aref tem index) r))
1820 ((eq the-type 'hash-table)
1821 ;; SINCE A "FAST ARRAY" IS JUST A HASH TABLE, SEEMS LIKE THESE !!
1822 ;; TESTS ON THE NUMBER OF INDICES OBSTRUCTS LEGITIMATE HASH TABLE !!
1823 ;; USE; I RECOMMEND CUTTING THEM OUT !!
1824 (cond ((gethash 'dim1 tem)
1825 (if (cdr index)
1826 (merror (intl:gettext "assignment: array has one dimension, but two or more indices supplied."))))
1827 (t (or (cdr index)
1828 (merror (intl:gettext "assignment: array has two or more dimensions, but one index supplied.")))))
1829 (setf (gethash (if (cdr index)
1830 index
1831 (car index))
1832 tem) r))
1833 ((eq the-type 'list)
1834 (cond ((eq (caar tem) 'mlist)
1835 (setq index (car index))
1836 (setf (nth index tem) r)
1838 ((eq (caar tem) '$matrix)
1839 (setf (nth (second index) (nth (first index) tem)) r)
1842 (merror (intl:gettext "assignment: ~A is not a hash table, an array, Maxima list, or a matrix.")
1843 (caar l)))))
1845 (cond ((eq tem (caar l))
1846 (meval* `((mset) ,(caar l)
1847 ,(make-equal-hash-table
1848 (cdr (mevalargs (cdr l))))))
1849 (arrstore l r))
1851 (merror (intl:gettext "assignment: ~A is not a hash table, an array, a Maxima list, or a matrix.")
1852 (caar l))))))))
1854 (cond ((mget (caar l) 'hashar)
1855 (let ($use_fast_arrays)
1856 (arrstore l r)))
1858 (meval* `((mset) ,(caar l)
1859 ,(make-equal-hash-table
1860 (cdr (mevalargs (cdr l))))))
1861 (arrstore l r))))))
1863 (let ((fun (caar l)) ary sub (lispsub 0) hashl mqapplyp)
1864 (cond ((setq ary (mget fun 'array))
1865 (dimcheck fun (setq sub (mapcar #'meval (cdr l))) t)
1866 (if (and (member (setq fun (car (arraydims ary))) '(fixnum flonum) :test #'eq)
1867 (not (eq (ml-typep r) fun)))
1868 (merror (intl:gettext "assignment: attempt to assign ~M to an array of type ~M.") r fun))
1869 (setf (apply #'aref (symbol-array ary) sub) r))
1870 ((setq ary (mget fun 'hashar))
1871 (if (not (= (aref (symbol-array ary) 2) (length (cdr l))))
1872 (merror (intl:gettext "assignment: array ~:M has dimension ~:M, but it was called by ~:M")
1873 fun (aref (symbol-array ary) 2) l))
1874 (setq sub (mapcar #'meval (cdr l)))
1875 (setq hashl (aref (symbol-array ary)
1876 (setq lispsub (+ 3 (rem (hasher sub)
1877 (aref (symbol-array ary) 0))))))
1878 (do ((hashl1 hashl (cdr hashl1)))
1879 ((null hashl1)
1880 (cond ((not (eq r munbound))
1881 (setq sub (ncons (cons sub r)))
1882 (cond ((null hashl) (setf (aref (symbol-array ary) lispsub) sub))
1883 (t (nconc hashl sub)))
1884 (setf (aref (symbol-array ary) 1) (1+ (aref (symbol-array ary) 1))))))
1885 (cond ((alike (caar hashl1) sub)
1886 (cond ((eq r munbound) (setf (aref (symbol-array ary) 1)
1887 (1- (aref (symbol-array ary) 1))))
1888 (t (nconc hashl (ncons (cons sub r)))))
1889 (setf (aref (symbol-array ary) lispsub)
1890 (delete (car hashl1) hashl :count 1 :test #'equal))
1891 (return nil))))
1892 (if (> (aref (symbol-array ary) 1) (aref (symbol-array ary) 0))
1893 (arraysize fun (* 2 (aref (symbol-array ary) 0))))
1895 ((and (eq fun 'mqapply) (mxorlistp (setq ary (meval (cadr l))))
1896 (prog2
1897 (setq mqapplyp t l (cdr l))
1898 nil)))
1899 ((and (not mqapplyp)
1900 (or (not (boundp fun))
1901 (not (or (mxorlistp (setq ary (symbol-value fun)))
1902 (arrayp ary)
1903 (typep ary 'hash-table)
1904 (eq (type-of ary) 'mgenarray)))))
1905 (if (member fun '(mqapply $%) :test #'eq) (merror (intl:gettext "assignment: cannot assign to ~M") l))
1906 (add2lnc fun $arrays)
1907 (setq ary (gensym))
1908 (mputprop fun ary 'hashar)
1909 (setf (symbol-array ary) (make-array 7 :initial-element nil))
1910 (setf (aref (symbol-array ary) 0) 4)
1911 (setf (aref (symbol-array ary) 1) 0)
1912 (setf (aref (symbol-array ary) 2) (length (cdr l)))
1913 (arrstore l r))
1914 ((or (arrayp ary)
1915 (typep ary 'hash-table)
1916 (eq (type-of ary) 'mgenarray))
1917 (arrstore-extend ary (mevalargs (cdr l)) r))
1918 ((or (eq (caar ary) 'mlist) (= (length l) 2))
1919 (cond ((eq (caar ary) '$matrix)
1920 (cond ((or (not ($listp r)) (not (= (length (cadr ary)) (length r))))
1921 (merror (intl:gettext "assignment: matrix row must be a list, and same length as first row;~%found:~%~M") r))))
1922 ((not (= (length l) 2))
1923 (merror (intl:gettext "assignment: matrix row must have one index; found: ~M") (cons '(mlist) (cdr l)))))
1924 (let ((index (meval (cadr l))))
1925 (cond ((not (eq (ml-typep index) 'fixnum))
1926 (merror (intl:gettext "assignment: matrix row index must be an integer; found: ~M") index))
1927 ((and (> index 0) (< index (length ary)))
1928 (rplaca (nthcdr (1- index) (cdr ary)) r))
1929 (t (merror (intl:gettext "assignment: matrix row index ~A out of range.") index))))
1931 (t (if (not (= (length l) 3))
1932 (merror (intl:gettext "assignment: matrix must have two indices; found: ~M") (cons '(mlist) (cdr l))))
1933 ($setelmx r (meval (cadr l)) (meval (caddr l)) ary)
1934 r))))))
1936 (defun arrfunp (x)
1937 (or (and $transrun (getl x '(a-expr))) (mgetl x '(aexpr))))
1939 (defun arrfuncall (arrfun subs form)
1940 (let ((aexprp t))
1941 (case (car arrfun)
1942 (aexpr (mapply1 (cadr arrfun) subs (cadr arrfun) form))
1943 (a-expr (apply (cadr arrfun) subs))
1944 (a-subr (apply (cadr arrfun) subs)))))
1946 (defun hasher (l) ; This is not the best way to write a hasher. But,
1947 (if (null l) ; please don't change this code or you're liable to
1948 0 ; break SAVE files.
1949 (logand #o77777
1950 (let ((x (car l)))
1951 (cond (($ratp x) (merror (intl:gettext "hash function: cannot hash a special expression (CRE or Taylor).")))
1952 ((or (fixnump x) (floatp x))
1953 (+ (if (fixnump x) x (floor (+ x 5e-4)))
1954 (* 7 (hasher (cdr l)))))
1955 ((atom x) (+ (sxhash x) (hasher (cdr l))))
1956 (t (+ 1 (sxhash (caar x)) (hasher (cdr x))
1957 (hasher (cdr l)))))))))
1959 (defun arraysize (fun n)
1960 (prog (old new indx ncells cell item i y)
1961 (setq old (symbol-array (mget fun 'hashar)))
1962 (setq new (gensym))
1963 (mputprop fun new 'hashar)
1964 (setf (symbol-array new) (make-array (+ n 3) :initial-element nil))
1965 (setq new (symbol-array new))
1966 (setf (aref new 0) n)
1967 (setf (aref new 1) (aref old 1))
1968 (setf (aref new 2) (aref old 2))
1969 (setq indx 2 ncells (+ 2 (aref old 0)))
1970 a (if (> (setq indx (1+ indx)) ncells) (return t))
1971 (setq cell (aref old indx))
1972 b (if (null cell) (go a))
1973 (setq i (+ 3 (rem (hasher (car (setq item (car cell)))) n)))
1974 (if (setq y (aref new i))
1975 (nconc y (ncons item))
1976 (setf (aref new i) (ncons item)))
1977 (setq cell (cdr cell))
1978 (go b)))
1980 (defun dimcheck (ary sub fixpp)
1981 (do ((x sub (cdr x))
1982 (ret t)
1983 (y (cdr (arraydims (mget ary 'array))) (cdr y)))
1984 ((null y)
1985 (if x (merror (intl:gettext "Array ~:M has dimensions ~:M, but was called with ~:M")
1987 `((mlist) ,@(mapcar #'1- (cdr (arraydims (mget ary 'array)))))
1988 `((mlist) ,@sub))
1989 ret))
1990 (cond ((or (null x) (and (eq (ml-typep (car x)) 'fixnum)
1991 (or (< (car x) 0) (not (< (car x) (car y))))))
1992 (setq y nil x (cons nil t)))
1993 ((not (fixnump (car x)) )
1994 (if fixpp (setq y nil x (cons nil t)) (setq ret nil))))))
1996 (defun constlam (x &aux (lam x))
1997 (if aexprp
1998 `(,(car lam) ,(cadr lam) ,@(mbinding ((mparams (cadr lam)))
1999 (mapcar #'meval (cddr lam))))
2001 lam))
2003 (defmspec $define (l)
2004 (twoargcheck l)
2005 (setq l (cdr l))
2006 (meval `((mdefine)
2007 ,(cond ((mquotep (car l)) (cadar l))
2008 ((and (not (atom (car l)))
2009 (member (caaar l) '($ev $funmake $arraymake) :test #'eq))
2010 (meval (car l)))
2011 (t (disp2 (car l))))
2012 ,(meval (cadr l)))))
2014 (defun set-lineinfo (fnname lineinfo body)
2015 (cond ((and (consp lineinfo) (eq 'src (third lineinfo)))
2016 (setf (cdddr lineinfo) (list fnname (first lineinfo)))
2017 (setf (get fnname 'lineinfo) body))
2018 (t (remprop fnname 'lineinfo))))
2020 (defmspec mdefine (l )
2021 (let ($use_fast_arrays) ;;for mdefine's we allow use the oldstyle hasharrays
2022 (twoargcheck l)
2023 (setq l (cdr l))
2024 (let ((fun (car l)) (body (cadr l)) args subs ary fnname mqdef)
2025 (cond ((or (atom fun)
2026 (and (setq mqdef (eq (caar fun) 'mqapply))
2027 (member 'array (cdar fun) :test #'eq)))
2028 (merror (intl:gettext "define: argument cannot be an atom or a subscripted memoizing function; found: ~M") fun))
2029 (mqdef (if (or (atom (cadr fun))
2030 (not (setq ary (member 'array (cdaadr fun) :test #'eq))))
2031 (merror (intl:gettext "define: expected a subscripted expression; found: ~M") (cadr fun)))
2032 (setq subs (cdadr fun) args (cddr fun) fun (cadr fun)
2033 fnname (caar fun))
2034 (if (and (not (mgetl fnname '(hashar array)))
2035 (get fnname 'specsimp))
2036 (mtell (intl:gettext "define: warning: redefining built-in subscripted function ~:M~%")
2037 fnname)))
2038 ((prog2 (setq fnname (caar fun))
2039 (or (mopp fnname) (member fnname '($all $allbut $%) :test #'eq)))
2040 (merror (intl:gettext "define: function name cannot be a built-in operator or special symbol; found: ~:@M") fnname))
2041 ((setq ary (member 'array (cdar fun) :test #'eq)) (setq subs (cdr fun)))
2043 (setq args (cdr fun))
2044 (mredef-check fnname)))
2045 (if (not ary) (remove1 (ncons fnname) 'mmacro t $macros t))
2046 (mdefchk fnname (or args (and (not mqdef) subs)) ary mqdef)
2047 (if (not (eq fnname (caar fun))) (rplaca (car fun) fnname))
2048 (cond ((not ary) (if (and evp (member fnname (car loclist) :test #'eq))
2049 (mputprop fnname t 'local-fun)
2050 (remove-transl-fun-props fnname))
2051 (add2lnc (cons (ncons fnname) args) $functions)
2052 (set-lineinfo fnname (cadar fun) body)
2053 (mputprop fnname (mdefine1 args body) 'mexpr)
2054 (if $translate (translate-function fnname)))
2055 ((prog2 (add2lnc fnname $arrays)
2056 (setq ary (mgetl fnname '(hashar array)))
2057 (remove-transl-array-fun-props fnname))
2058 (if (not (= (if (eq (car ary) 'hashar)
2059 (aref (symbol-array (cadr ary)) 2)
2060 (length (cdr (arraydims (cadr ary)))))
2061 (length subs)))
2062 (merror (intl:gettext "define: ~:M already defined with different number of subscripts.")
2063 fnname))
2064 (mdefarray fnname subs args body mqdef))
2066 (setq ary (gensym))
2067 (mputprop fnname ary 'hashar)
2068 (setf (symbol-array ary) (make-array 7 :initial-element nil))
2069 (setf (aref (symbol-array ary) 0) 4)
2070 (setf (aref (symbol-array ary) 1) 0)
2071 (setf (aref (symbol-array ary) 2) (length subs))
2072 (mdefarray fnname subs args body mqdef)))
2073 (cons '(mdefine simp) (copy-list l)))))
2075 ;; Checks to see if a user is clobbering the name of a system function.
2076 ;; Prints a warning and returns T if he is, and NIL if he isn't.
2077 (defun mredef-check (fnname)
2078 (when (and (not (mget fnname 'mexpr))
2079 (or (and (or (get fnname 'autoload)
2080 (getl-lm-fcn-prop fnname '(subr)))
2081 (not (get fnname 'translated)))
2082 (mopp fnname)))
2083 (format t (intl:gettext "define: warning: redefining the built-in ~:[function~;operator~] ~a~%")
2084 (getl fnname '(verb operators))
2085 (print-invert-case (stripdollar fnname)))
2088 (defun mdefarray (fun subs args body mqdef)
2089 (when (hash-table-p fun)
2090 ;; PRETTY SURE THIS NEXT MESSAGE IS UNREACHABLE (FUN IS ALWAYS A SYMBOL FROM WHAT I CAN TELL) !!
2091 (error "~a is already a hash table. Make it a function first" fun))
2092 (cond ((and (null args) (not mqdef)) (mputprop fun (mdefine1 subs body) 'aexpr))
2093 ((null (dolist (u subs)
2094 (unless (or (consp u) ($constantp u) (stringp u))
2095 (return t))))
2096 (arrstore (cons (ncons fun) subs) (mdefine1 args body)))
2097 (t (mdefchk fun subs t nil)
2098 (mputprop fun (mdefine1 subs (mdefine1 args body)) 'aexpr))))
2100 (defmfun mspecfunp (fun)
2101 (and (or (getl-lm-fcn-prop fun '(macro))
2102 (getl fun '(mfexpr*))
2103 (and $transrun (get fun 'translated-mmacro))
2104 (mget fun 'mmacro))
2105 (not (get fun 'evok))))
2107 (defun mdefine1 (args body)
2108 (list '(lambda) (cons '(mlist) args) body))
2110 (defun mdefchk (fun args ary mqdef)
2111 (do ((l args (cdr l)) (mfex) (mlex))
2112 ((null l) (and mfex (not mqdef) (mputprop fun mfex 'mfexprp))
2113 (and mlex (not mqdef) (mputprop fun mlex 'mlexprp)))
2114 (if (not (or (mdefparam (car l))
2115 (and (or (not ary) mqdef)
2116 (or (and mfexprp (mquotep (car l))
2117 (mdefparam (cadar l)) (setq mfex t))
2118 (and (mdeflistp l)
2119 (or (mdefparam (cadar l))
2120 (and mfexprp (mquotep (cadar l))
2121 (mdefparam (cadr (cadar l)))
2122 (setq mfex t)))
2123 (setq mlex t))))))
2124 (merror (intl:gettext "define: in definition of ~:M, found bad argument ~M") fun (car l)))))
2126 (defun mdefparam (x)
2127 (and (atom x) (not (maxima-constantp x)) (not (stringp x))))
2129 (defun mdeflistp (l)
2130 (and (null (cdr l)) ($listp (car l)) (cdar l) (null (cddar l))))
2132 (defmfun mopp (fun)
2133 (and (not (eq fun 'mqapply))
2134 (or (mopp1 fun)
2135 (and (get fun 'operators) (not (rulechk fun))
2136 (not (member fun rulefcnl :test #'eq)) (not (get fun 'opers))))))
2138 (defmfun mopp1 (fun)
2139 (and (setq fun (get fun 'op)) (not (member fun (cdr $props) :test #'eq))))
2141 ;; maybe should have a separate version, or a macro..
2142 (defun mapply (a b c)
2143 (mapply1 a b c nil))
2145 (defun $apply (fun arg)
2146 (unless ($listp arg)
2147 (merror (intl:gettext "apply: second argument must be a list; found: ~M") arg))
2148 (let ((fun-opr (getopr fun)))
2149 (autoldchk fun-opr)
2150 (mapply1 fun-opr (cdr arg) fun `(($apply) ,fun ,arg))))
2152 (defun autoldchk (fun)
2153 (if (and (symbolp fun)
2154 (get fun 'autoload)
2155 (not (or (fboundp fun) (mfboundp fun))))
2156 (load-function fun t)))
2158 (defmspec $dispfun (l)
2159 (setq l (cdr l))
2160 (cond ((or (cdr l) (not (eq (car l) '$all))) (dispfun1 l nil nil))
2162 `((mlist simp)
2163 ,@(apply #'append
2164 (list (cdr (dispfun1 (cdr $functions) t nil))
2165 (cdr (dispfun1
2166 (mapcan #'(lambda (x) (if (mget x 'aexpr) (ncons x)))
2167 (cdr $arrays)) nil t))
2168 (cdr (dispfun1 (cdr $macros) t nil))))))))
2170 (defun dispfun1 (l flag maexprp)
2171 `((mlist simp)
2172 ,@(loop for fun in l collect
2173 (cadr ($ldisp (consfundef (if flag (caar fun) fun) maexprp nil))))))
2175 (defmspec $fundef (x)
2176 (consfundef (fexprcheck x) nil nil))
2178 (defun consfundef (x maexprp stringp)
2179 (prog (arryp name fun)
2180 (setq arryp (and (not (atom x)) (not (eq (caar x) 'mqapply)) (member 'array (cdar x) :test #'eq)))
2181 (cond ((atom x) (setq name (if (stringp x) ($verbify x) x)
2182 fun (or (and (not maexprp) (mgetl name '(mexpr mmacro)))
2183 (mgetl name '(aexpr)))))
2184 (arryp (setq fun (meval1 (setq name (cons (list (caar x) 'array) (cdr x)))))
2185 (if (or (atom fun) (not (eq (caar fun) 'lambda))) (setq fun nil))))
2186 (cond ((not fun)
2187 (cond (stringp (return x))
2188 ((member 'edit state-pdl :test #'eq) (terpri)))
2189 (merror (intl:gettext "fundef: no such function: ~:M") x)))
2190 (return
2191 (cons (if (eq (car fun) 'mmacro) '(mdefmacro simp) '(mdefine simp))
2192 (cond (arryp (cons (cons '(mqapply) (cons name (cdadr fun))) (cddr fun)))
2193 (t (funcall #'(lambda (body)
2194 (cond ((and (eq (car fun) 'aexpr) (not (atom body))
2195 (eq (caar body) 'lambda))
2196 (list (cons '(mqapply) (cons (cons (cons name '(array))
2197 (cdr (cadadr fun)))
2198 (cdadr body)))
2199 (caddr body)))
2200 (t (list (cons (cons name (if (eq (car fun) 'aexpr) '(array)))
2201 (cdr (cadadr fun)))
2202 body))))
2203 (caddr (cadr fun)))))))))
2206 (defmfun $funmake (fun args)
2207 (if (not (or (stringp fun) (symbolp fun) ($subvarp fun)
2208 (and (not (atom fun)) (eq (caar fun) 'lambda))))
2209 (merror (intl:gettext "funmake: first argument must be a symbol, subscripted symbol, string, or lambda expression; found: ~M") fun))
2210 (if (not ($listp args)) (merror (intl:gettext "funmake: second argument must be a list; found: ~M") args))
2211 (mcons-op-args (getopr fun) (cdr args)))
2213 (defmfun mcons-op-args (op args)
2214 (if (symbolp op)
2215 (cons (ncons op) args)
2216 (list* '(mqapply) op args)))
2218 (defmfun optionp (x)
2219 (and (boundp x)
2220 (not (member x (cdr $values) :test #'eq))
2221 (not (member x (cdr $labels) :test #'eq))))
2223 (defmspec mcond (form)
2224 (setq form (cdr form))
2225 (do ((u form (cddr u)) (v))
2226 ((null u) nil)
2227 (cond ((eq (setq v (mevalp (car u))) t) (return (meval (cadr u))))
2228 (v (return (list* '(mcond) v (mapcar #'meval-atoms (cdr u))))))))
2230 (defun meval-atoms (form)
2231 (cond ((atom form) (meval1 form))
2232 ((eq (caar form) 'mquote) (cadr form))
2233 ((and (getl (caar form) '(mfexpr*))
2234 (not (member (caar form) '(mcond mand mor mnot mprogn mdo mdoin) :test #'eq)))
2235 form)
2236 (t (recur-apply #'meval-atoms form))))
2238 (defmspec mdo (form)
2239 (setq form (cdr form))
2240 (let ((mdop t) (my-var (or (car form) 'mdo)) my-step next test do-body)
2241 (setq my-step (if (caddr form) (meval (caddr form)) 1)
2242 next (or (cadddr form) (list '(mplus) my-step my-var))
2243 test (list '(mor)
2244 (cond ((null (car (cddddr form))) nil)
2245 (t (list (if (mnegp ($numfactor my-step))
2246 '(mlessp)
2247 '(mgreaterp))
2248 my-var (car (cddddr form)))))
2249 (cadr (cddddr form)))
2250 do-body (caddr (cddddr form)))
2251 (mbinding ((ncons my-var)
2252 (ncons (if (null (cadr form)) 1 (meval (cadr form)))))
2253 (do ((val) (bindl bindlist))
2254 ((is test) '$done)
2255 (cond ((null (setq val (catch 'mprog (prog2 (meval do-body) nil))))
2256 (mset my-var (meval next)))
2257 ((atom val) (merror (intl:gettext "do loop: 'go' not within 'block': ~M") val))
2258 ((not (eq bindl bindlist))
2259 (merror (intl:gettext "do loop: illegal 'return': ~M") (car val)))
2260 (t (return (car val))))))))
2262 (defmspec mdoin (form)
2263 (setq form (cdr form))
2264 (funcall #'(lambda (mdop my-var set test action)
2265 (setq set (if ($atom (setq set (format1 (meval (cadr form)))))
2266 (merror (intl:gettext "do loop: 'in' argument must be a nonatomic expression; found: ~M") set)
2267 (margs set))
2268 test (list '(mor)
2269 (if (car (cddddr form))
2270 (list '(mgreaterp) my-var (car (cddddr form))))
2271 (cadr (cddddr form)))
2272 action (caddr (cddddr form)))
2273 (cond ((atom set) '$done)
2274 (t (mbinding ((ncons my-var) (ncons (car set)))
2275 (do ((val) (bindl bindlist))
2276 ((or (atom set) (is test))
2277 '$done)
2278 (cond ((null (setq val (catch 'mprog (prog2 (meval action) nil))))
2279 (if (setq set (cdr set)) (mset my-var (car set))))
2280 ((atom val) (merror (intl:gettext "do loop: 'go' not within 'block': ~M") val))
2281 ((not (eq bindl bindlist))
2282 (merror (intl:gettext "do loop: illegal 'return': ~M") (car val)))
2283 (t (return (car val)))))))))
2284 t (or (car form) 'mdo) nil nil nil))
2286 (defmspec mprog (prog)
2287 (setq prog (cdr prog))
2288 (let (vars vals (mlocp t))
2289 (if ($listp (car prog)) (setq vars (cdar prog) prog (cdr prog)))
2290 (setq loclist (cons nil loclist))
2291 (do ((l vars (cdr l))) ((null l) (setq vals vars))
2292 (if (not (atom (car l))) (return (setq vals t))))
2293 (if (eq vals t)
2294 (setq vals (mapcar #'(lambda (v)
2295 (cond ((atom v) v)
2296 ((eq (caar v) 'msetq) (meval (caddr v)))
2297 (t (merror
2298 (intl:gettext "block: variable list must comprise only atoms and assignment expressions; found: ~M")
2299 v))))
2300 vars)
2301 vars (mapcar #'(lambda (v) (if (atom v) v (cadr v))) vars)))
2302 (mbinding (vars vals)
2303 (do ((prog prog (cdr prog)) (mprogp prog)
2304 (bindl bindlist) (val '$done) (retp) (x) ($%% '$%%))
2305 ((null prog) (munlocal) val)
2306 (cond ((atom (car prog))
2307 (if (null (cdr prog))
2308 (setq retp t val (meval (car prog)))))
2309 ((null (setq x (catch 'mprog
2310 (prog2 (setq val (setq $%% (meval (car prog))))
2311 nil)))))
2312 ((not (eq bindl bindlist))
2313 (if (not (atom x))
2314 ;; DUNNO WHAT'S "ILLEGAL" HERE
2315 (merror (intl:gettext "block: illegal 'return': ~M") (car x))
2316 ;; DUNNO WHAT'S "ILLEGAL" HERE
2317 (merror (intl:gettext "block: illegal 'go': ~M") x)))
2318 ((not (atom x)) (setq retp t val (car x)))
2319 ((not (setq prog (member x mprogp :test #'equal)))
2320 (merror (intl:gettext "block: no such tag: ~:M") x)))
2321 (if retp (setq prog '(nil)))))))
2323 (defmfun mreturn (&optional (x nil) &rest args)
2324 (cond
2325 ((not (null args))
2326 (merror (intl:gettext "return: too many arguments; found: ~M") `((mlist) ,x ,@args) ))
2327 ((and (not mprogp) (not mdop))
2328 (merror (intl:gettext "return: not within 'block'")))
2329 (t (throw 'mprog (ncons x)) ) ))
2331 (defmspec mgo (tag)
2332 (setq tag (fexprcheck tag))
2333 (cond ((not mprogp) (merror (intl:gettext "go: not within 'block'")))
2334 ((atom tag) (throw 'mprog tag))
2335 (t (merror (intl:gettext "go: argument must be an atom; found: ~M") tag))))
2337 (defmspec $subvar (l)
2338 (setq l (cdr l))
2339 (if (null l)
2340 (wna-err '$subvar))
2341 (meval (cons '(mqapply array) l)))
2343 (defmfun rat (x y)
2344 `((rat simp) ,x ,y))
2346 (defmfun add2lnc (item llist)
2347 (unless (memalike item (if ($listp llist) (cdr llist) llist))
2348 (unless (atom item)
2349 (setf llist (delete (assoc (car item) llist :test #'equal) llist :count 1 :test #'equal)))
2350 (nconc llist (ncons item))))
2352 (defmfun bigfloatm* (bf)
2353 (unless (member 'simp (cdar bf) :test #'eq)
2354 (setq bf (cons (list* (caar bf) 'simp (cdar bf)) (cdr bf))))
2355 (if $float ($float bf) bf))
2357 (defmfun $allbut (&rest args)
2358 (cons '($allbut) args))
2360 (defquote dsksetq (&rest l)
2361 (let ((dsksetp t))
2362 (mset (car l) (eval (cadr l)))))
2364 (defmfun dskrat (x)
2365 (orderpointer (caddar x))
2366 (mapc #'(lambda (a b) (dskrat-subst a b (cddddr (car x))) ; for TAYLOR forms
2367 (dskrat-subst a b (cdr x)))
2368 genvar (cadddr (car x)))
2369 (rplaca (cdddar x) genvar)
2370 (if (member 'trunc (car x) :test #'eq)
2371 (srconvert x) x)) ; temporary
2373 (defun dskrat-subst (x y z)
2374 (cond ((atom z) z)
2375 (t (if (eq y (car z)) (rplaca z x) (dskrat-subst x y (car z)))
2376 (dskrat-subst x y (cdr z))
2377 z)))
2379 (defmfun |''MAKE-FUN| (noun-name x)
2380 (simplifya (list (ncons noun-name) (resimplify x)) t))
2382 (macrolet ((|''MAKE| (fun noun)
2383 `(defmfun ,fun (x) (|''MAKE-FUN| ',noun x))))
2384 (|''MAKE| $log %log)
2385 (|''MAKE| $sin %sin) (|''MAKE| $cos %cos) (|''MAKE| $tan %tan)
2386 (|''MAKE| $cot %cot) (|''MAKE| $sec %sec) (|''MAKE| $csc %csc)
2387 (|''MAKE| $sinh %sinh) (|''MAKE| $cosh %cosh) (|''MAKE| $tanh %tanh)
2388 (|''MAKE| $coth %coth) (|''MAKE| $sech %sech) (|''MAKE| $csch %csch)
2389 (|''MAKE| $asin %asin) (|''MAKE| $acos %acos) (|''MAKE| $atan %atan)
2390 (|''MAKE| $acot %acot) (|''MAKE| $asec %asec) (|''MAKE| $acsc %acsc)
2391 (|''MAKE| $asinh %asinh) (|''MAKE| $acosh %acosh) (|''MAKE| $atanh %atanh)
2392 (|''MAKE| $acoth %acoth) (|''MAKE| $asech %asech) (|''MAKE| $acsch %acsch)
2393 (|''MAKE| $round %round) (|''MAKE| $truncate %truncate) (|''MAKE| $plog %plog)
2394 (|''MAKE| $signum %signum) (|''MAKE| $gamma %gamma))
2396 ;; evfun properties
2397 (mapc #'(lambda (x) (putprop x t 'evfun))
2398 '($radcan $factor $ratsimp $trigexpand $trigreduce $logcontract
2399 $rootscontract $bfloat $ratexpand $fullratsimp $rectform $polarform))
2401 ;; evflag properties
2402 (mapc #'(lambda (x) (putprop x t 'evflag))
2403 '($exponentialize $%emode $demoivre $logexpand $logarc
2404 $radexpand $keepfloat $listarith $float $ratsimpexpons $ratmx
2405 $simp $simpsum $simpproduct $algebraic $ratalgdenom $factorflag $ratfac
2406 $infeval $%enumer $programmode $lognegint $logabs $letrat
2407 $halfangles $exptisolate $isolate_wrt_times $sumexpand
2408 $cauchysum $numer_pbranch $m1pbranch $dotscrules $trigexpand))
2410 ;;; Float constants, to 2048 bits of precision.
2411 ;;; (EXP 1)
2412 (mdefprop $%e 2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320030599218174135966290435729003342952605956307381323286279434907632338298807531952510190115738341879307021540891499348841675092447614606680822648001684774118537423454424371075390777449920695517027618386062613313845830007520449338265602976067371132007093287091274437470472306969772093101416928368190255151086574637721112523897844250569536967707854499699679468644549059879316368892300987931277361782154249992295763514822082698951936680331825288693984964651058209392398294887933203625094431173012381970684161404
2413 $numer)
2414 ;;; (ATAN 0 -1)
2415 (mdefprop $%pi 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608
2416 $numer)
2417 ;;; (1+sqrt(5))/2
2418 (mdefprop $%phi 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144381497587012203408058879544547492461856953648644492410443207713449470495658467885098743394422125448770664780915884607499887124007652170575179788341662562494075890697040002812104276217711177780531531714101170466659914669798731761356006708748071013179523689427521948435305678300228785699782977834784587822891109762500302696156170025046433824377648610283831268330372
2419 $numer)
2420 ;;; Euler's constant
2421 (mdefprop $%gamma 0.57721566490153286060651209008240243104215933593992359880576723488486772677766467093694706329174674951463144724980708248096050401448654283622417399764492353625350033374293733773767394279259525824709491600873520394816567085323315177661152862119950150798479374508570574002992135478614669402960432542151905877553526733139925401296742051375413954911168510280798423487758720503843109399736137255306088933126760017247953783675927135157722610273492913940798430103417771778088154957066107501016191663340152278935867965497252036212879226555953669628176388792726801324310104765059637039473949576389065729679296010090151251959509223
2422 $numer)
2424 (mdefprop $herald_package (nil $transload t) $props)
2425 (mdefprop $load_package (nil $transload t) $props)
2427 (defprop bigfloat bigfloatm* mfexpr*)
2428 (defprop lambda constlam mfexpr*)
2429 (defprop quote cadr mfexpr*) ; Needed by MATCOM/MATRUN.
2431 (eval-when
2432 #+gcl (compile eval)
2433 #-gcl (:compile-toplevel :execute)
2435 (setq *read-base* *old-read-base*))