Bug fix: simplode on a singleton list could return a non-string
[maxima.git] / src / mlisp.lisp
blobc8387dba9400fdb7856033398787b3d7120e1db3
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
65 $integervalued))
67 (defmvar $features (cons '(mlist simp) (append featurel nil)))
68 (defmvar $%enumer nil)
69 (defmvar $float nil)
70 (defmvar $refcheck nil)
71 (defmvar $translate nil)
72 (defmvar $transrun t)
73 (defmvar $savedef t)
74 (defmvar $maperror t)
75 (defmvar $optionset nil)
76 (defmvar $setcheckbreak nil)
77 (defmvar $infeval nil)
78 (defmvar $piece '$piece)
79 (defmvar $setval '$setval)
81 ;; These three variables are what get stuck in array slots as magic
82 ;; unbound objects. They are for T, FIXNUM, and FLONUM type arrays
83 ;; respectively.
85 (defvar munbound '|#####|)
87 (defvar fixunbound most-negative-fixnum)
89 (defvar flounbound most-negative-flonum)
91 (defmvar munbindp nil
92 "Used for safely `munbind'ing incorrectly-bound variables."
93 no-reset)
95 (defmvar $setcheck nil)
97 (mapc #'(lambda (x) (setf (symbol-value x) (ncons '(mlist simp))))
98 '($values $functions $macros $arrays $myoptions $rules $props))
100 (defmfun mapply1 (fn args fnname form)
101 (declare (special aryp))
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 (append (cdr fn) aryp) 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)
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 ; We assume that the lambda expression handed to us has been simplified,
185 ; or at least that it's well-formed. This is because various checks are
186 ; performed during simplification instead of every time lambda expressions
187 ; are applied to arguments.
188 (setq noevalargs nil)
189 (let ((params (cdadr fn))( mlocp t))
190 (setq loclist (cons nil loclist))
191 (do ((a) (p))
192 ((or (null params) (and (null args) (not (mdeflistp params))))
193 (setq args (nreconc a args) params (nreconc p params)))
194 (cond ((mdeflistp params)
195 (setq params (cdar params) args (ncons (cons '(mlist) args)))))
196 (cond ((and mfexprp (mquotep (car params)))
197 (setq a (cons (car args) a) p (cons (cadar params) p)))
198 ((atom (car params))
199 (setq p (cons (car params) p)
200 a (cons (cond (noeval (car args))
201 (t (meval (car args)))) a)))
202 (t (merror (intl:gettext "lambda: formal argument must be a symbol or quoted symbol; found: ~M") (car params))))
203 (setq args (cdr args) params (cdr params)))
204 (let (finish2033 (finish2032 params) (ar *mlambda-call-stack*))
205 (declare (type (vector t) ar))
206 (unwind-protect
207 (progn
208 (unless (> (array-total-size ar) (+ (fill-pointer ar) 10))
209 (setq ar (adjust-array ar (+ (array-total-size ar) 50) :fill-pointer (fill-pointer ar))))
210 (vector-push bindlist ar)
211 ;; rather than pushing all on *baktrcl* it might be good
212 ;; to make a *last-form* global that is set in meval1
213 ;; and is pushed here.
214 (vector-push form ar)
215 (vector-push params ar)
216 (vector-push args ar)
217 (vector-push fnname ar)
218 (mbind finish2032 args fnname)
219 (setq finish2033 t)
220 (let ((aexprp (and aexprp (not (atom (caddr fn)))
221 (eq (caar (caddr fn)) 'lambda))))
222 (cond ((null (cddr fn)) (merror (intl:gettext "lambda: no body present.")))
223 ((cdddr fn) (mevaln (cddr fn)))
224 (t (meval (caddr fn))))))
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 (amperchk 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 the value ~:M.~%") form val))
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 ;; don't 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 (and (symbolp (caar form)) (char= (get-first-char (caar form)) #\%))
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 mfexpr)))
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) 'mfexpr)
377 (mlambda (cadr u) (cdr form) (caar form) noevalargs form))
378 ((eq (car u) 'macro)
379 (setq noevalargs nil)
380 (setq form (cons(caar form) (cdr form)))
381 (eval form))
383 (apply (cadr u) (mevalargs (cdr form))))))
385 (if (and (not aryp) (load-function (caar form) t)) (go a))
386 (badfunchk (caar form) (caar form) nil)
387 (if (symbolp (caar form))
388 (setq u (boundp (caar form)))
389 (return (meval1-extend form)))
391 (cond ((or (null u)
392 (and (safe-get (caar form) 'operators) (not aryp))
393 (eq (caar form) (setq u (symbol-value (caar form)))))
394 (setq form (meval2 (mevalargs (cdr form)) form))
395 (return (or (and (safe-mget (caar form) 'atvalues)
396 (at1 form))
397 form)))
398 ((and aryp
399 (safe-get (caar form) 'nonarray))
400 (return (cons (cons (caar form) aryp)
401 (mevalargs (cdr form)))))
402 ((atom u)
403 (badfunchk (caar form) u nil)
404 (setq form (cons (cons (getopr u) aryp) (cdr form)))
405 (go a))
406 ((eq (caar u) 'lambda)
407 (if aryp
408 (merror (intl:gettext "lambda: cannot apply lambda as an array function."))
409 (return (mlambda u (cdr form)
410 (caar form) noevalargs form))))
412 (return
413 (mapply1 u (mevalargs (cdr form)) (caar form) form)))))))
415 (mapply1 (caar form) (mevalargs (cdr form)) (caar form) form))))
417 (defun getl-lm-fcn-prop (sym props &aux fn typ)
418 (setq fn sym)
419 (cond ((functionp fn)
420 (setq typ 'subr))
421 ((not (symbolp sym))) ;; eventually return nil if not a symbol
422 ((macro-function sym)
423 (setq typ 'macro))
424 ((setq fn (symbol-array sym))
425 (setq typ 'array))
426 ((setq fn (get sym 'mfexpr*))
427 (setq typ 'mfexpr*))
428 ((setq fn (get sym 'mfexpr))
429 (setq typ 'mfexpr)))
430 (and typ (member typ props :test #'eq) (list typ fn)))
433 (defmfun meval2 (newargs old)
434 (declare (special aryp))
435 (let ((new (cons (car old) newargs)) nosimp)
436 (cond ((not (member 'simp (cdar old) :test #'eq))
437 (if (and (not (eq (caar new) 'mlist)) (equal new old)) old new))
438 ((prog2 (setq nosimp (not (get (caar new) 'operators))) (alike1 new old))
439 (if nosimp old (cons (delsimp (car old)) (cdr old))))
440 (nosimp (if aryp new (cons (cons (caar new) '(simp)) newargs)))
441 (t (cons (cons (caar new) aryp) newargs)))))
443 (defun mparam (var)
444 (cond ((atom var)
445 var)
446 ((atom (cadr var))
447 (cadr var))
449 (cadadr var))))
451 (defun mparams (vars)
452 (mapcar #'mparam (cdr vars)))
454 (defmfun mop (form)
455 (if (eq (caar form) 'mqapply)
456 (cadr form)
457 (caar form)))
459 (defmfun margs (form)
460 (if (eq (caar form) 'mqapply)
461 (cddr form)
462 (cdr form)))
464 (defun badfunchk (name val flag)
465 (if (or flag (numberp val) (member val '(t nil $%e $%pi $%i) :test #'eq))
466 (if (and (atom name) (not (equal val name)))
467 (merror (intl:gettext "apply: found ~M evaluates to ~M where a function was expected.") name val)
468 (merror (intl:gettext "apply: found ~M where a function was expected.") val))))
470 ;; To store the value of $errormsg in mbind. This value is looked up in the
471 ;; routine mbind-doit. This is a hack to get the expected behavior, when the
472 ;; option variable $errormsg is used as a local variable in a block.
473 (defvar *$errormsg-value* nil)
475 (defun mbind-doit (lamvars fnargs fnname)
476 "Makes a new frame where the variables in the list LAMVARS are bound
477 to the corresponding elements in FNARGS. Note that these elements are
478 used tels quels, without calling MEVAL.
479 If FNNAME is non-NIL, it designates a function call frame.
480 This function does not handle errors properly, use the MBIND
481 wrapper for this."
482 (declare (special bindlist mspeclist))
483 (do ((vars lamvars (cdr vars))
484 (args fnargs (cdr args)))
485 ((cond ((and vars args) nil)
486 ((and (null vars) (null args)))
487 (t (assert fnname (fnname)
488 "Expected a maxima function designator but got NIL.")
489 (merror (intl:gettext "~M arguments supplied to ~M; found: ~M")
490 (if vars (intl:gettext "Too few") (intl:gettext "Too many"))
491 (if (and (consp fnname)
492 (consp (car fnname))
493 (eq (caar fnname) 'lambda))
494 fnname
495 (cons (ncons fnname) lamvars))
496 (cons '(mlist) fnargs)))))
497 (let ((var (car vars)))
498 (if (not (symbolp var))
499 (merror (intl:gettext "Only symbols can be bound; found: ~M") var))
500 (let ((value (if (boundp var)
501 (if (eq var '$errormsg)
502 ;; Do not take the actual value of $errormsg. It is
503 ;; always NIL at this point, but the value which
504 ;; is stored in *$errormsg-value*.
505 *$errormsg-value*
506 (symbol-value var))
507 munbound)))
508 (mset var (car args))
509 (psetq bindlist (cons var bindlist)
510 mspeclist (cons value mspeclist))))))
512 (defun mbind (lamvars fnargs fnname)
513 "Error-handling wrapper around MBIND-DOIT."
514 (handler-case
515 (let ((old-bindlist bindlist) win)
516 (declare (special bindlist))
517 ;; At this point store the value of $errormsg in a global. The macro
518 ;; with-$error sets the value of $errormsg to NIL, but we need the
519 ;; actual value in the routine mbind-doit.
520 (setq *$errormsg-value* $errormsg)
521 (unwind-protect
522 (prog1
523 (with-$error (mbind-doit lamvars fnargs fnname))
524 (setq win t))
525 (unless win
526 (unless (eq bindlist old-bindlist)
527 (munbind (nreverse (ldiff bindlist old-bindlist))))
528 (when fnname
529 (pop-mlambda-call-stack fnname)))))
530 (maxima-$error (c)
531 ;; HMM, HERE'S A CALL TO MERROR. I CAN'T TELL WHERE ARE THE ERROR MESSAGES.
532 ;; IF I DID, I'D WRAP THEM IN A CALL TO GETTEXT
533 (apply #'merror (cdr (the-$error c)))
534 ;; Make absolutely sure that this handler (and mbind) doesn't
535 ;; return in this situation since other code depends on this
536 ;; behaviour.
537 (throw 'macsyma-quit t))))
539 ;;; For testing purposes
541 #+ignore
542 (defmfun $show_mbind_data ()
543 (format t "~&~{~a = ~a~%~}"
544 (mapcan #'(lambda (x) (list x (symbol-value x)))
545 '(bindlist mspeclist $values *mlambda-call-stack*)))
546 (finish-output)
547 (values))
549 (defmfun munbind (vars)
550 (dolist (var (reverse vars))
551 (cond ((eq (car mspeclist) munbound)
552 (makunbound var)
553 (setf $values (delete var $values :count 1 :test #'eq)))
554 (t (let ((munbindp t)) (mset var (car mspeclist)))))
555 (setq mspeclist (cdr mspeclist) bindlist (cdr bindlist))))
557 ;;This takes the place of something like
558 ;; (DELETE (ASSOC (NCONS VAR) $DEPENDENCIES) $DEPENDENCIES 1)
560 (defun mfunction-delete (var fn-a-list)
561 (delete (assoc (ncons var) fn-a-list :test #'equal) fn-a-list :count 1 :test #'equal))
563 (defmspec mlocal (l)
564 (push nil loclist)
565 (let ((mlocp t))
566 (meval `(($local) ,@(cdr l)))))
568 (defmspec $local (l)
569 (setq l (cdr l))
570 (unless mlocp
571 (merror (intl:gettext "local: must be called within a block or lambda.")))
572 (dolist (var l)
573 (cond ((not (symbolp var))
574 (improper-arg-err var '$local))
575 ((and (mget var 'array)
576 (arrayp (symbol-array var)))
577 ;; HMM. I DON'T UNDERSTAND WHY DECLARED ARRAYS ARE OFF-LIMITS:
578 ;; THE ARRAY IS JUST A PROPERTY LIKE ANY OTHER, IS IT NOT ??
579 (merror (intl:gettext "local: argument cannot be a declared array; found: ~M") var)))
580 (setq mproplist (cons (get var 'mprops) mproplist)
581 factlist (cons (get var 'data) factlist))
582 (dolist (fact (car factlist))
583 (putprop fact -1 'ulabs))
584 (progn
585 (mfunction-delete var $functions)
586 (mfunction-delete var $macros)
587 (mfunction-delete var $dependencies))
588 (setf $arrays (delete var $arrays :count 1 :test #'eq))
589 (zl-remprop var 'mprops)
590 (zl-remprop var 'data))
591 (rplaca loclist (reverse l))
592 (setq mlocp nil)
593 '$done)
595 (defun munlocal ()
596 (dolist (var (car loclist))
597 (let ((mprop (car mproplist))
598 (y nil)
599 (fact (car factlist)))
600 (remcompary var)
601 (cput var mprop 'mprops)
602 (cond ((setq y (old-get mprop 'mexpr))
603 (add2lnc (cons (ncons var) (cdadr y)) $functions))
604 (t (mfunction-delete var $functions)))
605 (cond ((setq y (old-get mprop 'mmacro))
606 (add2lnc (cons (ncons var) (cdadr y)) $macros))
607 (t (mfunction-delete var $macros)))
608 (cond ((or (old-get mprop 'array) (old-get mprop 'hashar))
609 (add2lnc var $arrays))
610 (t (setf $arrays (delete var $arrays :count 1 :test #'eq))))
611 (cond ((setq y (old-get mprop 'depends))
612 (add2lnc (cons (ncons var) y) $dependencies))
613 (t (mfunction-delete var $dependencies)))
614 (rempropchk var)
615 (mapc #'remov (get var 'data))
616 (cput var fact 'data)
617 (dolist (u fact)
618 (zl-remprop u 'ulabs))
619 (setq mproplist (cdr mproplist)
620 factlist (cdr factlist))))
621 (setq loclist (cdr loclist)))
623 (defmacro msetq (a b)
624 `(mset ',a ,b))
626 ;; A "run-time macro" needed by MATCOM/MATRUN.
627 ;;works with the defms
628 (defmspec msetq (l)
629 (twoargcheck l)
630 (mset (simplifya (cadr l) nil) (meval (caddr l))))
632 (defun mset (x y)
633 (prog ()
634 (cond ((or (null $setcheck)
635 (eq $setcheck '$setcheck)))
636 ((and (or (atom $setcheck)
637 (memalike x (cdr $setcheck))
638 (and (not (atom x))
639 (memalike (caar x) (cdr $setcheck))))
640 (not (eq x y)))
641 (mtell (intl:gettext "~:M is being set to ~:M.~%") x y)
642 (if (and $setcheckbreak (not (eq x '$setval)))
643 (let (($setval y))
644 (merrbreak t)
645 (setq y $setval)))))
646 (cond ((atom x)
647 (when (or (not (symbolp x))
648 (member x '(t nil) :test #'eq)
649 (mget x '$numer)
650 (get x 'sysconst))
651 (if munbindp (return nil))
652 (if (mget x '$numer)
653 (merror (intl:gettext "assignment: cannot assign to ~M; it is a declared numeric quantity.") x)
654 (merror (intl:gettext "assignment: cannot assign to ~M") x)))
655 (let ((f (get x 'assign)))
656 (if (and f (or (not (eq x y))
657 (member f '(neverset read-only-assign) :test #'eq)))
658 (if (eq (funcall f x y) 'munbindp) (return nil))))
659 (cond ((and (not (boundp x))
660 (not dsksetp))
661 (add2lnc x $values))
662 ((and (not (eq x y))
663 (optionp x))
664 (if $optionset (mtell (intl:gettext "assignment: assigning to option ~M") x))
665 (if (not (eq x '$linenum)) (add2lnc x $myoptions))))
666 (return (setf (symbol-value x) y)))
668 ;; ---------- begin code copied & modified from defstruct.lisp
670 ;; Check to see if the operator has an mset_extension_operator.
671 ;; If so, this says how to do assignments. Examples, a@b:x. Put mset_extension_operator
672 ;; of mrecord-assign on the atom $@. To allow [a,b]:[3,4] put op on mlist.
673 ;; arguably we could use mget, mfuncall, and $mset_extension_operator and
674 ;; allow this to be done at the maxima level instead of lisp.
676 ;; X is could be something like (($FOO ARRAY) 42), in which case it is meaningful
677 ;; to look for an assignment operator associated either with $FOO itself or with
678 ;; $FOO's object type, with "object type" = (CAAR (SYMBOL-VALUE '$FOO)).
680 ((let*
681 ((x-value (if (boundp (caar x)) (symbol-value (caar x))))
682 (mset-extension-op
683 (cond
684 ((get (caar x) 'mset_extension_operator))
685 ((and
686 (not (atom x-value))
687 (get (caar x-value) 'defstruct-template)
688 (get (caar x-value) 'mset_extension_operator))))))
689 (if mset-extension-op
690 (return-from mset (funcall mset-extension-op x y)))))
692 ;; ---------- end code copied & modified from defstruct.lisp
694 ((member 'array (cdar x) :test #'eq)
695 (return (arrstore x y)))
696 (t (merror (intl:gettext "assignment: cannot assign to ~M") x)))))
698 ;; ---------- begin code copied & modified from defstruct.lisp
700 ;; CHANGES WRT FATEMAN'S STUFF.
701 ;; (1) $NEW BARFS IF #ARGUMENTS != 1, OR ARGUMENT HAS NO DEFSTRUCT, OR WRONG NUMBER OF INITIALIZERS.
702 ;; (2) $DEFSTRUCT ALLOWS 1 OR MORE ARGUMENTS, RETURNS A LIST OF DEFSTRUCTS.
703 ;; (3) USE $PUT AND $GET TO MAINTAIN DEFSTRUCT PROPERTIES
704 ;; (RENAMED TO $DEFSTRUCT_DEFAULT AND $DEFSTRUCT_TEMPLATE).
705 ;; THIS MAKES DEFSTRUCT PROPERTIES VISIBLE TO USER VIA GET AND PROPVARS.
706 ;; ALSO, THIS MAKES `KILL' KILL DEFSTRUCTS.
707 ;; (4) @ EVALUATES LHS AND QUOTES RHS
708 ;; (5) $STRUCTURES INFOLIST
709 ;; (6) LBP = 200, RBP = 201 (HIGHER PRECEDENCE, LEFT-ASSOCIATIVE)
710 ;; (7) A@B => A@B WHEN B IS NOT BOUND TO SOMETHING OTHER THAN ITSELF
711 ;; (8) DISALLOW @ APPLIED TO EXPRESSIONS W/ OPERATOR NOT DECLARED BY DEFSTRUCT
712 ;; (9) MAKE RECORD AND LIST ASSIGNMENT FUNCTIONS LISP FUNCTIONS (STRIP OFF $ FROM NAME)
713 ;; ALSO MAKE PROPERTY SYMBOLS LISP SYMBOLS (STRIP OFF $ FROM NAME)
714 ;; (10) EXTEND KILL TO TAKE ITEMS OFF $STRUCTURES AND REMOVE DEFSTRUCT PROPERTIES
715 ;; (11) EXTEND KILL TO RECOGNIZE KILL(X@Y)
716 ;; (12) EVALUATE INITIALIZERS IN $DEFSTRUCT AND IN $NEW
717 ;; (13) DISPLAY FIELDS WHICH HAVE BEEN ASSIGNED VALUES AS FOO(X = BAR, Y = BAZ)
718 ;; (14) ASSIGN TRANSLATION PROPERTY TO 'DEFSTRUCT AND DEF-SAME%TR ALL STRUCTURES
720 (setf (get '$@ 'mset_extension_operator) 'mrecord-assign)
722 ;; defstruct(f(x,y,z));
723 ;; myrecord: new(f);
724 ;; myrecord@y:45;
725 ;; myrecord; ==> f(x,45,z)
727 ;; initializers are possible
728 ;; defstruct(f(x,y=3.14159, z));
729 ;; ff:new(f) ==> f(x,3.14159,z)
730 ;; ff@y:2.71828 ==> ff is f(x,2.71828,z).
732 ;; the @ syntax can also be used instead of substinpart.
734 ;; k: h(g(aa,bb),cc);
735 ;; k@1@2:dd; change aa to dd.
736 ;; k;
738 (defmfun mrecord-assign (@-expr value)
739 ;; assume @-expr is (($@..) instance-name field-name)
740 (let*
741 ((instance (cadr @-expr))
742 (field (caddr @-expr))
743 (object (meval instance))
744 template)
745 (if (not (and (consp object) (consp (car object)) (setq template (get (caar object) 'defstruct-template))))
746 (merror "MRECORD-ASSIGN: left-hand side doesn't appear to be a defstruct object:~%~M" instance)
747 (let
748 ((index
749 (if (integerp field)
750 field ;;; allow foo@3, also
751 (position field template)))) ;field->integer
752 (if (null index) (merror (intl:gettext "assignment: no such field: ~M @ ~M") instance field))
753 (if (< 0 index (length object)) (setf (elt object index) value)
754 (merror (intl:gettext "assignment: no such field: ~M @ ~M") instance field))
755 value))))
757 ;; MRECORD-KILL is very similar to MRECORD-ASSIGN. Might consider merging the two somehow.
759 (defmfun mrecord-kill (@-expr)
760 (let*
761 ((instance (cadr @-expr))
762 (field (caddr @-expr))
763 (object (meval instance))
764 template)
765 (if (not (and (consp object) (consp (car object)) (setq template (get (caar object) 'defstruct-template))))
766 (merror "MRECORD-KILL: left-hand side doesn't appear to be a defstruct object:~%~M" instance)
767 (let
768 ((index
769 (if (integerp field)
770 field
771 (position field template))))
772 (if (null index) (merror (intl:gettext "kill: no such field: ~M @ ~M") instance field))
773 (if (< 0 index (length object)) (setf (elt object index) (elt template index))
774 (merror (intl:gettext "kill: no such field: ~M @ ~M") instance field))))))
776 (defmspec $@ (L)
777 (let*
778 ((a (cadr L))
779 (b (caddr L))
780 (e ($@-function (meval a) b)))
781 (if (eq e b) L e)))
783 (defun $@-function (in fn)
784 (cond
785 ((not (listp in))
786 (list '(%@) in fn)) ;; noun form
787 ((get (caar in) 'defstruct-template)
788 (let*
789 ((index
790 (if (integerp fn) fn ;; allow foo@3
791 (position fn (get (caar in) 'defstruct-template))))) ;; field->integer
792 (if (null index) (merror (intl:gettext "@: no such field: ~M @ ~M") in fn))
793 (if (< 0 index (length in))
794 (elt in index)
795 (merror (intl:gettext "@: no such field: ~M @ ~M") in fn))))
797 (list '($@) in fn))))
799 (defun dimension-defstruct (form result)
800 (let
801 ((L1 (cdr (get (caar form) 'defstruct-template)))
802 (L2 (cdr form)))
803 (dimension-function (cons (car form) (mapcar #'(lambda (e1 e2) (if (eq e1 e2) e1 `((mequal) ,e1 ,e2))) L1 L2)) result)))
805 ;; L looks like defstruct (foo(...), bar(...), baz(...)).
806 ;; Process each argument and return a list of declared structures.
808 (defmspec $defstruct (L)
809 `((mlist) ,@(mapcar 'defstruct1 (cdr L))))
811 ;; trivial translation to quiet complaint about lack of translation for this defmspec
812 (def%tr $defstruct (x) `($any . (meval ',x)))
814 (defvar $structures '((mlist)))
816 (defun defstruct-translate (form)
817 (let ((translated-args (mapcar #'translate (cdr form))))
818 `($any simplify (list '(,(caar form)) ,@(mapcar #'cdr translated-args)))))
820 (defun defstruct1 (z) ;; z should look like (($whatever) $a $b $c)
821 (unless (and (consp z) (consp (car z)))
822 (merror (intl:gettext "defstruct: expected a structure template; found ~M") z))
823 ;; store the template
824 (putprop (caar z) (namesonly z) 'defstruct-template)
825 ;; set the initialization
826 (putprop (caar z) (initializersmostly z) 'defstruct-default)
827 (setf (get (caar z) 'dimension) 'dimension-defstruct)
828 (nconc $structures (list (get (caar z) 'defstruct-default)))
829 (setf (get (caar z) 'translate) #'defstruct-translate)
830 (get (caar z) 'defstruct-default))
832 (defun namesonly(r) ; f(a,b,c) unchanged, f(a=3,b=4,c=5) -> f(a,b,c)
833 (cons (car r)(mapcar #'(lambda(z)
834 (cond((symbolp z) z)
835 ((mequalp z) (second z))
836 (t (merror (intl:gettext "defstruct: expected a record initializer; found: ~M") z))))
837 (cdr r))))
839 (defun initializersmostly(r);; f(a=3,b,c=5) -> f(3,b,5)
840 (cons (car r)(mapcar #'(lambda(z)
841 (cond((symbolp z) z)
842 ((mequalp z) (meval (third z)))
843 (t (merror (intl:gettext "defstruct: expected a record initializer; found: ~M") z))))
844 (cdr r))))
846 (defmspec $new (h)
847 (unless (= (length (cdr h)) 1)
848 (merror (intl:gettext "new: expected exactly one argument; found: ~M") (length (cdr h))))
850 (let ((recordname (cadr h)))
851 (cond
852 ((symbolp recordname) ;; the case of, e.g. new(f);
853 (if (null (get recordname 'defstruct-default))
854 (merror (intl:gettext "new: no such structure ~M") recordname))
856 (copy-tree (get recordname 'defstruct-default)))
858 ;; assume there is some initialization here e.g. new (f(5,6,7))
860 (let ((recordop (caar recordname)) (recordargs (cdr recordname)))
861 (if (null (get recordop 'defstruct-default))
862 (merror (intl:gettext "new: no such structure ~M") recordop))
864 (if (not (= (length recordargs) (length (cdr (get recordop 'defstruct-default)))))
865 (merror (intl:gettext "new: wrong number of arguments in initializer; expected ~M, not ~M.")
866 (length (cdr (get recordop 'defstruct-default))) (length recordargs)))
868 `(,(car recordname) ,@(mapcar #'meval (cdr recordname))))))))
870 ;; trivial translation to quiet complaint about lack of translation for this defmspec
871 (def%tr $new (x) `($any . (meval ',x)))
873 ;; Following property assignments comprise the Lisp code equivalent to infix("@", 200, 201)
875 (defprop $@ %@ verb)
876 (defprop $@ "@" op)
877 (putopr "@" '$@)
878 ;; !! FOLLOWING LINE MOVED TO NPARSE.LISP TO AVOID COMPILER ERROR
879 ;; !! (MOVING SUPRV1.LISP HIGHER IN MAXIMA.SYSTEM CAUSES MYSTERIOUS ERROR)
880 ;; !! (define-symbol "@")
881 (defprop $@ dimension-infix dimension)
882 (defprop $@ (#\@) dissym)
883 (defprop $@ tex-infix tex)
884 (defprop $@ ("@") texsym)
885 (defprop $@ msize-infix grind)
886 (defprop $@ 200 lbp)
887 (defprop $@ 201 rbp)
888 (defprop $@ parse-infix led)
889 (defprop %@ dimension-infix dimension)
890 (defprop %@ (#\@) dissym)
891 (defprop %@ $@ noun)
893 ;; The follow code implements PARALLEL LIST assignment.
894 ;; it is consistent with commercial macsyma. [a,b,c]:[x,y,z] means
895 ;; about the same as a:x, b:y, c:z. Actually it
896 ;; evaluates x,y,z BEFORE any assignments to a,b,c, hence parallel.
897 ;; Also implemented is [a,b,c]:x which evaluates x once and assigns
898 ;; to a,b,c.
899 ;; value returned is (evaluated x to ex) [ex,ex,ex].
901 ;; quiz . [a,b]:[b,2*a]. produces values a=b, b= 2*a.
902 ;; re-execute the statement 4 times. what do you get? [4b, 8a]
904 ;; a neat application of parallel assignment is this version of
905 ;; a gcd algorithm (for integers)...
906 ;; kgcd(a,b):=(while b#0 do [a,b]:[b,remainder(a,b)], abs(a));
907 ;; The extended euclidean algorithm looks even better with parallel
908 ;; assignment.
910 ;; add MLIST to possible operators on the left hand side of
911 ;; an assignment statement.
913 (setf (get 'mlist 'mset_extension_operator) 'mlist-assign)
915 (defmfun mlist-assign (tlist vlist)
916 ;; tlist is ((mlist..) var[0]... var[n]) of targets
917 ;; vlist is either((mlist..) val[0]... val[n]) of values
918 ;; or possibly just one value.
919 ;; should insert some checking code here
920 (if (and (listp vlist)
921 (eq (caar vlist) 'mlist)
922 (not (= (length tlist)(length vlist))))
923 (merror (intl:gettext "assignment: lists must be the same length; found: ~M, ~M") tlist vlist))
924 (setq tlist
925 `((mlist)
926 ,@(mapcar
927 #'(lambda (x)
928 (if (or (symbolp x) (get (caar x) 'mset_extension_operator))
930 `(,(car x) ,@(mapcar #'meval (cdr x)))))
931 (cdr tlist))))
932 (unless (and (listp vlist)
933 (eq (caar vlist) 'mlist))
934 (setf vlist (cons (car tlist) ;; if [a,b,c]:v then make a list [v,v,v]
935 (make-sequence 'list (1-(length tlist)) :initial-element vlist))))
936 (map nil #'mset (cdr tlist)(cdr vlist))
937 vlist)
939 ;; ---------- end code copied & modified from defstruct.lisp
941 (defmspec $ev (l)
942 (setq l (cdr l))
943 (let ((evp t) (*nounl* *nounl*) ($float $float) ($numer $numer)
944 ($expop $expop) ($expon $expon) ($doallmxops $doallmxops)
945 ($doscmxops $doscmxops) (derivflag derivflag) ($detout $detout)
946 (*nounsflag* *nounsflag*) (rulefcnl rulefcnl))
947 (if (and (cdr l) (null (cddr l)) (eq (car l) '$%e) (eq (cadr l) '$numer))
948 (setq l (append l '($%enumer))))
949 (do ((l (cdr l) (cdr l)) (bndvars) (bndvals) (locvars) (exp (car l))
950 (subsl) (evflg 0) (ratf) (derivlist) (evfunl) (funcl) (predflg)
951 (noeval (member '$noeval (cdr l) :test #'eq)))
952 ((null l)
953 (mbinding (bndvars bndvars)
954 (meval `((mlocal) ,@locvars))
955 (let ($translate) (mapc #'meval1 funcl))
956 (let ($numer) (setq exp (mevalatoms exp)))
957 (if ($ratp exp) (setq ratf t exp ($ratdisrep exp)))
958 (if (specrepp exp) (setq exp (specdisrep exp)))
959 (when subsl
960 (setq exp (simplify exp))
961 (dolist (item subsl)
962 (setq exp (maxima-substitute (meval (car item))
963 (meval (cdr item))
964 exp)))))
965 ; Ensure that MUNLOCAL gets called so that we don't leak any local
966 ; function definitions if we run into an error
967 (unwind-protect
968 (mbinding (bndvars bndvals)
969 (if (and $numer noeval $%enumer)
970 (setq exp (maxima-substitute %e-val '$%e exp)))
971 (setq exp (if noeval
972 (resimplify exp)
973 (simplify (if predflg (mevalp exp) (meval1 exp)))))
974 (if (or (> evflg 0) $infeval)
975 (prog (exp1)
976 (setq exp (specrepcheck exp))
977 loop (do ((l evfunl (cdr l)) (exp2 exp))
978 ((null l) (setq exp1 (meval exp2)))
979 (setq exp2 (list (ncons (car l)) exp2)))
980 (dolist (item subsl)
981 (setq exp1 (maxima-substitute (meval (car item))
982 (meval (cdr item))
983 exp1)))
984 (cond ((or (and (not $infeval)
985 (= (setq evflg (1- evflg)) 0))
986 (prog2 (setq exp1 (specrepcheck exp1))
987 (alike1 exp exp1)))
988 (setq exp exp1))
989 (t (setq exp exp1) (go loop)))))
990 (if (and ratf (not $numer) (not $float))
991 (setq exp (let ($norepeat) (ratf exp)))))
992 (munlocal))
993 exp)
994 (if (not (or (atom (car l))
995 (member 'array (cdaar l) :test #'eq)
996 (member (caaar l) '(mquote msetq mlist mequal mdefine mset
997 mdefmacro $expand $local $derivlist) :test #'eq)))
998 (setq l (cons (meval (car l)) (cdr l))))
999 (cond ((or (atom (car l)) (member 'array (cdaar l) :test #'eq) (eq (caaar l) 'mquote))
1000 (or (and (symbolp (car l))
1001 (cond ((eq (car l) '$eval) (setq evflg (1+ evflg)))
1002 ((member (car l) '($noeval $rescan) :test #'eq))
1003 ((eq (car l) '$detout)
1004 (setq $doallmxops nil $doscmxops nil $detout t))
1005 ((eq (car l) '$numer) (setq $numer t $float t))
1006 ((eq (car l) '$nouns) (setq *nounsflag* t))
1007 ((eq (car l) '$pred) (setq predflg t))
1008 ((eq (car l) '$expand)
1009 (setq $expop $maxposex $expon $maxnegex))
1010 ((eq (car l) '%derivative)
1011 (setq derivflag t derivlist nil))
1012 ((get (car l) 'evflag)
1013 (setq bndvars (cons (car l) bndvars)
1014 bndvals (cons (get (car l) 'evflag) bndvals)))
1015 ((get (car l) 'evfun)
1016 (setq exp (evfunmake (car l) exp)
1017 evfunl (nconc evfunl (ncons (car l)))))))
1018 (let ((fl (meval (car l))))
1019 (cond ((symbolp fl)
1020 (cond ((eq fl '$diff)
1021 (setq l (list* nil '$del (cdr l))))
1022 ((eq fl '$risch)
1023 (setq l (list* nil '$integrate (cdr l)))))
1024 (setq *nounl* (cons ($nounify fl) *nounl*)))
1025 ((numberp fl) (improper-arg-err (car l) '$ev))
1026 ((stringp fl) (improper-arg-err (car l) '$ev))
1027 ((eq (caar fl) 'mlist)
1028 (setq l (append fl (cdr l))))
1029 ((member (caar fl)
1030 '(msetq mequal mdefine mdefmacro mset) :test #'eq)
1031 (setq l (list* nil fl (cdr l))))
1032 (t (improper-arg-err (car l) '$ev))))))
1033 ((not (member (caaar l) '(msetq mlist mequal mdefine mdefmacro
1034 $expand $local $derivlist mset) :test #'eq))
1035 (improper-arg-err (car l) '$ev))
1036 ((eq (caaar l) '$expand)
1037 (cond ((null (cdar l)) (setq $expop $maxposex $expon $maxnegex))
1038 ((null (cddar l)) (setq $expop (cadar l) $expon $maxnegex))
1039 (t (setq $expop (cadar l) $expon (caddar l)))))
1040 ((member (caaar l) '(mdefine mdefmacro) :test #'eq)
1041 (let ((fun (cadar l)) $use_fast_arrays)
1042 (if (eq (caar fun) 'mqapply) (setq fun (cadr fun)))
1043 (setq fun ($verbify (caar fun)))
1044 (setq funcl (nconc funcl (ncons (car l)))
1045 locvars (append locvars (ncons fun)))
1046 (if (rulechk fun) (setq rulefcnl (cons fun rulefcnl)))))
1047 ((eq (caaar l) '$local) (setq locvars (append locvars (cdar l))))
1048 ((eq (caaar l) '$derivlist) (setq derivflag t derivlist (cdar l)))
1049 ((and (eq (caaar l) 'mset)
1050 (setq l (cons (list '(msetq) (meval (cadar l)) (caddar l))
1051 (cdr l)))
1052 nil))
1053 ((member (caaar l) '(msetq mequal) :test #'eq)
1054 (if (and (msetqp (car l)) (msetqp (caddar l)))
1055 (setq l (nconc (|:SPREAD| (car l)) (cdr l))))
1056 (if (or noeval (not (atom (cadar l))))
1057 (setq subsl (nconc subsl (list (cons (caddar l) (cadar l))))))
1058 (if (atom (cadar l))
1059 (setq bndvars (cons (cadar l) bndvars)
1060 bndvals (cons (meval (specrepcheck (caddar l))) bndvals))))
1061 (t (setq l (append (car l) (cdr l))))))))
1063 (defmfun mevalatoms (exp)
1064 (cond ((atom exp) (meval1 exp))
1065 ((member 'array (cdar exp) :test #'eq)
1066 (let (exp1)
1067 (let ((evarrp t)) (setq exp1 (catch 'evarrp (meval1 exp))))
1068 (if (eq exp1 'notexist)
1069 (cons (car exp) (mapcar #'mevalatoms (cdr exp)))
1070 exp1)))
1071 ((eq (caar exp) 'mquote) (cadr exp))
1072 ((member (caar exp) '(msetq $define) :test #'eq)
1073 (list (car exp) (cadr exp) (mevalatoms (caddr exp))))
1074 ((or (and (eq (caar exp) '$ev)
1075 (cdr exp)
1076 (or (null (cddr exp)) (equal (cddr exp) '($eval))))
1077 (eq (caar exp) 'mprogn))
1078 (cons (car exp) (cons (mevalatoms (cadr exp)) (cddr exp))))
1079 ((member (caar exp) '($sum $product %sum %product) :test #'eq)
1080 (if msump
1081 (meval exp)
1082 (list (car exp) (cadr exp) (caddr exp)
1083 (mevalatoms (cadddr exp)) (mevalatoms (car (cddddr exp))))))
1084 ((and (eq (caar exp) '$%th) (fixnump (simplify (cadr exp))))
1085 (meval1 exp))
1086 ((prog2 (autoldchk (caar exp))
1087 (and (getl (caar exp) '(mfexpr*))
1088 (not (get (caar exp) 'evok))))
1089 exp)
1090 ((mgetl (caar exp) '(mfexprp))
1091 (cons (car exp)
1092 (do ((a (cdadr (mget (caar exp) 'mexpr)) (cdr a))
1093 (b (cdr exp) (cdr b)) (l))
1094 ((not (and a b)) (nreverse l))
1095 (cond ((mdeflistp a)
1096 (return (nreconc l (if (mquotep (cadar a))
1098 (mapcar #'mevalatoms b)))))
1099 ((mquotep (car a)) (setq l (cons (car b) l)))
1100 (t (setq l (cons (mevalatoms (car b)) l)))))))
1101 ((or (eq (caar exp) 'mmacroexpanded)
1102 (and $transrun (get (caar exp) 'translated-mmacro))
1103 (mget (caar exp) 'mmacro))
1104 (mevalatoms (mmacroexpand exp)))
1105 (t (cons (car exp) (mapcar #'mevalatoms (cdr exp))))))
1107 ;; evok properties
1108 (mapc #'(lambda (x) (putprop x t 'evok))
1109 '($map $maplist $fullmap $matrixmap $fullmapl $outermap $scanmap $apply))
1111 (defun evfunmake (fun exp)
1112 (if (msetqp exp)
1113 (list (car exp) (cadr exp) (evfunmake fun (caddr exp)))
1114 (list (ncons fun) exp)))
1116 (defun |:SPREAD| (x)
1117 (do ((val (do ((x x (caddr x))) (nil)
1118 (if (not (msetqp (caddr x))) (return (caddr x)))))
1119 (x x (caddr x)) (l))
1120 ((not (msetqp x)) l)
1121 (setq l (cons (list (car x) (cadr x) val) l))))
1123 (defmfun msetqp (x)
1124 (and (not (atom x)) (eq (caar x) 'msetq)))
1126 (defmfun mquotep (x)
1127 (and (not (atom x)) (eq (caar x) 'mquote)))
1129 (defmspec mquote (form)
1130 (cadr form))
1132 (defmfun $subvarp (x)
1133 (and (not (atom x)) (member 'array (cdar x) :test #'eq) t))
1135 (defmfun mseterr (x y)
1136 (if munbindp
1137 'munbindp
1138 (merror (intl:gettext "assignment: cannot assign ~M to ~:M") y x)))
1140 ;; assign properties
1141 (mapc #'(lambda (x) (putprop (car x) (cadr x) 'assign))
1142 '(($linel msetchk) (*read-base* msetchk) (*print-base* msetchk) (modulus msetchk)
1143 ($infolists neverset) ($trace neverset) ($ratweights msetchk)
1144 ($ratvars msetchk) ($setcheck msetchk) ($gcd msetchk)
1145 ($dotassoc msetchk) ($ratwtlvl msetchk) ($ratfac msetchk)
1146 ($all neverset) ($numer numerset) ($fortindent msetchk)
1147 ($gensumnum msetchk) ($genindex msetchk) ($fpprintprec msetchk)
1148 ($floatwidth msetchk) ($parsewindow msetchk) ($optimprefix msetchk)))
1150 (defmfun msetchk (x y)
1151 (cond ((member x '(*read-base* *print-base*) :test #'eq)
1152 (cond ((eq y 'roman))
1153 ((or (not (fixnump y)) (< y 2) (> y 36)) (mseterr x y))
1154 ((eq x '*read-base*))))
1155 ((member x '($linel $fortindent $gensumnum $fpprintprec $floatwidth
1156 $parsewindow) :test #'eq)
1157 (if (not (fixnump y)) (mseterr x y))
1158 (if (eq x '$linel)
1159 (cond ((not (and (> y 0) ; at least one char per line
1160 (< y 1000001))) ; arbitrary chosen big value
1161 (mseterr x y))
1163 (setq linel y))))
1164 (cond ((and (member x '($fortindent $gensumnum $floatwidth) :test #'eq) (< y 0))
1165 (mseterr x y))
1166 ((and (eq x '$parsewindow) (< y -1)) (mseterr x y))
1167 ((and (eq x '$fpprintprec) (or (< y 0) (= y 1))) (mseterr x y))))
1168 ((member x '($genindex $optimprefix) :test #'eq) (if (not (symbolp y)) (mseterr x y)))
1169 ((eq x '$dotassoc) (cput 'mnctimes y 'associative))
1170 ((eq x 'modulus)
1171 (cond ((null y))
1172 ((and (integerp y) (plusp y))
1173 ;; modulus must be an integer > 0. Give a warning if not
1174 ;; a prime number.
1175 (if (not (primep y))
1176 (mtell (intl:gettext "warning: assigning ~:M, a non-prime, to 'modulus'~&") y)))
1177 (t (mseterr x y))))
1178 ((eq x '$setcheck)
1179 (if (not (or (member y '($all t nil) :test #'eq) ($listp y))) (mseterr x y)))
1180 ((eq x '$gcd) (if (not (or (null y) (member y *gcdl* :test #'eq))) (mseterr x y)))
1181 ((eq x '$ratvars)
1182 (if ($listp y) (apply #'$ratvars (cdr y)) (mseterr x y)))
1183 ((eq x '$ratfac)
1184 (if (and y $ratwtlvl)
1185 (merror (intl:gettext "assignment: 'ratfac' and 'ratwtlvl' may not both be used at the same time."))))
1186 ((eq x '$ratweights)
1187 (cond ((not ($listp y)) (mseterr x y))
1188 ((null (cdr y)) (kill1 '$ratweights))
1189 (t (apply #'$ratweight (cdr y)))))
1190 ((eq x '$ratwtlvl)
1191 (if (and y (not (fixnump y))) (mseterr x y))
1192 (if (and y $ratfac)
1193 (merror (intl:gettext "assignment: 'ratfac' and 'ratwtlvl' may not both be used at the same time."))))))
1195 (defmfun numerset (assign-var y)
1196 (declare (ignore assign-var))
1197 (mset '$float y))
1199 (defmfun neverset (x assign-val)
1200 (declare (ignore assign-val))
1201 (if munbindp
1202 'munbindp
1203 (merror (intl:gettext "assignment: cannot assign to ~:M") x)))
1205 ;; Check assignment to be a positive integer including zero
1206 (defun posintegerset (x y)
1207 (if (or (not (integerp y))
1208 (not (>= y 0)))
1209 (merror
1210 (intl:gettext "assignment: '~:M must be a positive integer. Found: ~:M")
1211 x y)))
1213 (defmfun mmapev (l)
1214 (if (null (cddr l))
1215 (merror (intl:gettext "~:M: expected two or more arguments; found: ~M") (caar l) (cdr l)))
1216 (let ((op (getopr (meval (cadr l)))))
1217 (autoldchk op)
1218 (badfunchk (cadr l) op nil)
1219 (cons op (mapcar #'meval (cddr l)))))
1221 (defmspec $map (l)
1222 (apply #'map1 (mmapev l)))
1224 (defmfun map1 n
1225 (do ((i n (1- i))
1226 (argi (setarg n (format1 (arg n))) (format1 (arg (1- i))))
1227 (op (or (mapatom (arg n)) (mop (arg n))))
1228 (flag (mapatom (arg n))
1229 (or flag
1230 (setq flag (mapatom argi))
1231 (and (not maplp) (not (alike1 (mop argi) op)))))
1232 (argl nil (cons argi argl))
1233 (cdrl nil (or flag (cons (margs argi) cdrl))))
1234 ((= i 1) (if flag
1235 (cond ((not $maperror)
1236 (when $mapprint (mtell (intl:gettext "map: calling 'apply'")))
1237 (funcer (arg 1) argl))
1238 ((and (= n 2) (mapatom (arg 2)))
1239 (improper-arg-err (arg 2) '$map))
1240 (t (merror (intl:gettext "map: arguments must have same main operator; found: ~M, ~M") op (mop (first argl)))))
1241 (mcons-op-args op (apply #'mmapcar (cons (arg 1) cdrl)))))))
1243 (defmspec $maplist (l)
1244 (let ((maplp t) res)
1245 (setq res (apply #'map1 (mmapev l)))
1246 (cond ((atom res) (list '(mlist) res))
1247 ((eq (caar res) 'mlist) res)
1248 (t (cons '(mlist) (margs res))))))
1250 (defmfun mmapcar n
1251 (do ((ans nil (cons (funcer (arg 1) argl) ans))
1252 (argl nil nil))
1253 ((do ((i n (1- i)))
1254 ((= i 1) nil)
1255 (when (null (arg i))
1256 (when (or (< i n)
1257 (do ((j 2 (1+ j)))
1258 ((= j n) nil)
1259 (when (arg j) (return t))))
1260 (when $maperror
1261 (merror (intl:gettext "map: arguments must be the same length.")))
1262 (when $mapprint (mtell (intl:gettext "map: truncating one or more arguments."))))
1263 (return t))
1264 (push (car (arg i)) argl)
1265 (setarg i (cdr (arg i))))
1266 (nreverse ans))))
1268 (defun mapatom (x)
1269 (or (symbolp x) (mnump x) ($subvarp x) (stringp x)))
1271 (defmfun $mapatom (x)
1272 (if (mapatom (specrepcheck x)) t))
1274 (defmspec $fullmap (l)
1275 (setq l (mmapev l))
1276 (fmap1 (car l) (cdr l) nil))
1278 (defun fmap1 (fn argl fmapcaarl)
1279 (setq argl (mapcar #'format1 argl))
1280 (do ((op (or (mapatom (car argl)) (mop (car argl))))
1281 (fmaplvl (1- fmaplvl)) (cdr1 argl (cdr cdr1)) (argi nil nil)
1282 (cdrl nil (cons (margs (car cdr1)) cdrl)))
1283 ((null cdr1)
1284 (do ((ans nil (cons (if bottom (funcer fn carargl)
1285 (fmap1 fn carargl fmapcaarl))
1286 ans))
1287 (carargl nil nil) (cdrargl nil nil)
1288 (cdrl cdrl cdrargl) (bottom nil nil)
1289 (done (when (member nil cdrl :test #'eq)
1290 (when (dolist (e cdrl) (if e (return t)))
1291 (when $maperror
1292 (merror (intl:gettext "fullmap: arguments must have same formal structure.")))
1293 (when $mapprint
1294 (mtell (intl:gettext "fullmap: truncating one or more arguments.~%"))))
1295 t)))
1296 (done (mcons-op-args op (nreverse ans)))
1297 (do ((op (or (setq bottom (or (zerop fmaplvl) (mapatom (caar cdrl))))
1298 (mop (caar cdrl))))
1299 (eleml cdrl (cdr eleml)) (caareleml nil nil))
1300 ((null eleml)
1301 (when (and done (dolist (e cdrargl) (if e (return t))))
1302 (if $maperror
1303 (merror (intl:gettext "fullmap: arguments must have same formal structure.")))
1304 (if $mapprint (mtell (intl:gettext "fullmap: truncating one or more arguments.~%")))))
1305 (setq caareleml (caar eleml))
1306 (or bottom
1307 (setq bottom
1308 (or (mapatom caareleml)
1309 (not (alike1 op (mop caareleml)))
1310 (and fmapcaarl (not (eq (caar caareleml) fmapcaarl))))))
1311 (or done (setq done (null (cdar eleml))))
1312 (setq carargl (nconc (ncons caareleml) carargl)
1313 cdrargl (nconc cdrargl (ncons (cdar eleml)))))))
1314 (setq argi (car cdr1))
1315 (if (or (mapatom argi)
1316 (not (alike1 op (mop argi)))
1317 (and fmapcaarl (not (eq (caar argi) fmapcaarl))))
1318 (cond ($maperror (merror (intl:gettext "fullmap: arguments must have same operators.")))
1319 (t (if $mapprint (mtell (intl:gettext "fullmap: calling 'apply'.~%")))
1320 (return (funcer fn argl)))))))
1322 (defmspec $matrixmap (l)
1323 (let ((fmaplvl 2))
1324 (apply #'fmapl1 (mmapev l))))
1326 (defmspec $fullmapl (l)
1327 (apply #'fmapl1 (mmapev l)))
1329 (defmfun fmapl1 (fun &rest args)
1330 (let* ((header '(mlist))
1331 (argl (fmap1 fun
1332 (mapcar #'(lambda (z)
1333 (cond ((not (mxorlistp z))
1334 (merror (intl:gettext "fullmapl: argument must be a list or matrix; found: ~M") (or (and (consp z) (mop z)) z)))
1335 ((eq (caar z) '$matrix)
1336 (setq header '($matrix))
1337 (cons '(mlist simp) (cdr z)))
1338 (t z)))
1339 args)
1340 'mlist)))
1341 (if (dolist (e (cdr argl))
1342 (unless ($listp e) (return t)))
1343 argl
1344 (cons header (cdr argl)))))
1346 (defun $outermap (x y &rest z)
1347 (if z
1348 (apply #'outermap1 x y z)
1349 (fmapl1 x y)))
1351 (defmfun outermap1 n
1352 (let (outargs1 outargs2)
1353 (declare (special outargs1 outargs2))
1354 (cond ((mxorlistp (arg 2))
1355 (setq outargs1 (ncons (arg 1))
1356 outargs2 (listify (- 2 n)))
1357 (fmapl1 #'outermap2 (arg 2)))
1358 (t (do ((i 3 (1+ i)))
1359 ((> i n) (funcer (arg 1) (listify (- 1 n))))
1360 (when (mxorlistp (arg i))
1361 (setq outargs1 (listify (1- i))
1362 outargs2 (if (< i n) (listify (- i n))))
1363 (return (fmapl1 #'outermap2 (arg i)))))))))
1365 (defmfun outermap2 (&rest args)
1366 (declare (special outargs1 outargs2))
1367 (unless (null args)
1368 (apply #'outermap1 (append outargs1 (list (first args)) outargs2))))
1370 (defmfun funcer (fn args)
1371 (cond ((member fn '(mplus mtimes mexpt mnctimes) :test #'eq)
1372 (simplify (cons (ncons fn) args)))
1373 ((or (member fn '(outermap2 constfun) :test #'eq)
1374 (and $transrun (symbolp fn) (get fn 'translated)
1375 (not (mget fn 'local-fun)) (fboundp fn)))
1376 (apply fn (mapcar #'simplify args)))
1377 (t (mapply1 fn (mapcar #'simplify args) fn
1378 nil ;; try to get more info to pass
1379 ))))
1381 (defmspec $qput (l)
1382 (setq l (cdr l))
1383 (unless (= (length l) 3)
1384 (wna-err '$qput))
1385 ($put (car l) (cadr l) (caddr l)))
1387 (defmfun $rem (atom ind)
1388 (prop1 '$rem atom nil ind))
1390 (defmfun $put (atom val ind)
1391 (prog1
1392 (prop1 '$put atom val ind)
1393 (add2lnc atom $props)))
1395 (defun prop1 (fun atom val ind)
1396 (unless (or (symbolp atom) (stringp atom))
1397 (merror (intl:gettext "~:M: argument must be a symbol or a string; found: ~M") fun atom))
1398 (unless (or (symbolp ind) (stringp ind))
1399 (merror (intl:gettext "~:M: indicator must be a symbol or a string; found: ~M") fun ind))
1400 (unless (symbolp atom)
1401 (if (symbolp (getopr atom))
1402 (setq atom (getopr atom))
1403 (setq atom (intern atom))))
1404 (unless (symbolp ind)
1405 (setq ind (intern ind)))
1406 (let ((u (mget atom '$props)))
1407 (cond ((eq fun '$get) (and u (old-get u ind)))
1408 ((eq fun '$rem) (and u (zl-remprop u ind) '$done))
1409 ((not u) (mputprop atom (list nil ind val) '$props) val)
1410 (t (putprop u val ind)))))
1412 (defmspec $declare (l)
1413 (setq l (cdr l))
1414 (when (oddp (length l))
1415 (merror (intl:gettext "declare: number of arguments must be a multiple of 2.")))
1416 (do ((l l (cddr l)) (vars) (flag nil nil))
1417 ((null l)
1418 '$done)
1419 (cond (($listp (cadr l))
1420 (do ((l1 (cdadr l) (cdr l1))) ((if (null l1) (setq flag t)))
1421 (meval `(($declare) ,(car l) ,(car l1)))))
1422 ((nonsymchk (cadr l) '$declare))
1423 (t (setq vars (declsetup (car l) '$declare))))
1424 (cond (flag)
1425 ((member (cadr l) '($evfun $evflag $nonarray $bindtest) :test #'eq)
1426 (declare1 vars t (stripdollar (cadr l)) nil))
1427 ((eq (cadr l) '$noun)
1428 (dolist (var vars) (alias (getopr var) ($nounify var))))
1429 ((member (cadr l) '($nonscalar $scalar $mainvar) :test #'eq)
1430 (declare1 vars t (cadr l) t))
1431 ((eq (cadr l) '$alphabetic) (declare1 vars t t '$alphabetic))
1432 ((member (cadr l) opers :test #'eq)
1433 (if (member (cadr l) (cdr $features) :test #'eq) (declare1 vars t (cadr l) 'kind))
1434 (declare1 (mapcar #'getopr vars) t (cadr l) 'opers))
1435 ((member (cadr l) (cdr $features) :test #'eq) (declare1 vars t (cadr l) 'kind))
1436 ((eq (cadr l) '$feature)
1437 (dolist (var vars) (nonsymchk var '$declare) (add2lnc var $features)))
1438 (t (merror (intl:gettext "declare: unknown property ~:M") (cadr l))))))
1440 (defun declare1 (vars val prop mpropp)
1441 (dolist (var vars)
1442 (unless (or (symbolp var) (stringp var))
1443 (merror (intl:gettext "declare: argument must be a symbol or a string; found: ~M") var))
1445 (if (eq mpropp '$alphabetic)
1446 ; Explode var into characters and put each one on the *alphabet* list,
1447 ; which is used by src/nparse.lisp .
1448 (dolist (1-char (coerce var 'list))
1449 (add2lnc 1-char *alphabet*))
1450 (progn
1451 (setq var (getopr var))
1452 (cond
1453 ((eq mpropp 'kind) (declarekind var prop))
1454 ((eq mpropp 'opers)
1455 (putprop (setq var (linchk var)) t prop) (putprop var t 'opers))
1456 (mpropp
1457 (if (and (member prop '($scalar $nonscalar) :test #'eq)
1458 (mget var (if (eq prop '$scalar) '$nonscalar '$scalar)))
1459 (merror (intl:gettext "declare: inconsistent declaration ~:M") `(($declare) ,var ,prop)))
1460 (mputprop var val prop))
1461 (t (putprop var val prop)))
1462 (if (and (safe-get var 'op) (operatorp1 var)
1463 (not (member (setq var (get var 'op)) (cdr $props) :test #'eq)))
1464 (setq *mopl* (cons var *mopl*)))
1465 (add2lnc (getop var) $props)))))
1467 (defun linchk (var)
1468 (if (member var '($sum $integrate $limit $diff $transpose) :test #'eq)
1469 ($nounify var)
1470 var))
1472 (defmspec $remove (form)
1473 (i-$remove (cdr form)))
1475 (defmfun i-$remove (l)
1476 (when (oddp (length l))
1477 (merror (intl:gettext "remove: number of arguments must be a multiple of 2.")))
1478 (do ((l l (cddr l)) (vars) (flag nil nil)) ((null l) '$done)
1479 (cond (($listp (cadr l))
1480 (do ((l1 (cdadr l) (cdr l1))) ((if (null l1) (setq flag t)))
1481 (i-$remove (list (car l) (car l1)))))
1482 ((unless (or (symbolp (cadr l)) (stringp (cadr l)))
1483 (merror (intl:gettext "remove: argument must be a symbol or a string; found: ~M") (cadr l))))
1484 (t (setq vars (declsetup (car l) '$remove))))
1485 (cond (flag)
1486 ((eq (cadr l) '$value) (i-$remvalue vars))
1487 ((eq (cadr l) '$function)
1488 (remove1 (mapcar #'$verbify vars) 'mexpr t $functions t))
1489 ((eq (cadr l) '$macro)
1490 (remove1 (mapcar #'$verbify vars) 'mmacro t $macros t))
1491 ((eq (cadr l) '$array) (meval `(($remarray) ,@vars)))
1492 ((member (cadr l) '($alias $noun) :test #'eq) (remalias1 vars (eq (cadr l) '$alias)))
1493 ((eq (cadr l) '$matchdeclare) (remove1 vars 'matchdeclare t t nil))
1494 ((eq (cadr l) '$rule) (remrule (mapcar #'(lambda (v) (if (stringp v) ($verbify v) v)) vars)))
1495 ((member (cadr l) '($evfun $evflag $nonarray $bindtest
1496 $autoload $assign) :test #'eq)
1497 (remove1 vars (stripdollar (cadr l)) nil t nil))
1498 ((member (cadr l) '($mode $modedeclare) :test #'eq) (remove1 vars 'mode nil 'foo nil))
1499 ((eq (cadr l) '$atvalue) (remove1 vars 'atvalues t t nil))
1500 ((member (cadr l) '($nonscalar $scalar $mainvar $numer $atomgrad) :test #'eq)
1501 (remove1 vars (cadr l) t t nil))
1502 ((member (cadr l) opers :test #'eq) (remove1 (mapcar #'linchk vars) (cadr l) nil t nil))
1503 ((member (cadr l) (cdr $features) :test #'eq) (remove1 vars (cadr l) nil t nil))
1504 ((eq (cadr l) '$feature)
1505 (dolist (var vars)
1506 (setf $features (delete var $features :count 1 :test #'eq))))
1507 ((member (cadr l) '($alphabetic $transfun) :test #'eq)
1508 (remove1 vars (cadr l) nil t nil))
1509 ((member (cadr l) '($gradef $grad) :test #'eq) (remove1 vars 'grad nil $gradefs t))
1510 ((member (cadr l) '($dependency $depend $depends) :test #'eq)
1511 (remove1 vars 'depends t $dependencies t))
1512 ((member (cadr l) '($op $operator) :test #'eq) (remove1 vars '$op nil 'foo nil))
1513 ((member (cadr l) '($deftaylor $taylordef) :test #'eq) (remove1 vars 'sp2 nil t nil))
1514 (t (merror (intl:gettext "remove: unknown property ~:M") (cadr l))))))
1516 (defun declsetup (x fn)
1517 (cond ((atom x) (ncons x))
1518 ((eq (caar x) '$nounify) (ncons (meval x)))
1519 ((eq (caar x) 'mlist)
1520 (mapcar #'(lambda (var)
1521 (cond ((atom var) var)
1522 ((eq (caar var) '$nounify) (meval var))
1523 (t (improper-arg-err var fn))))
1524 (cdr x)))
1525 (t (improper-arg-err x fn))))
1527 (defmfun remove1 (vars prop mpropp info funp)
1528 (do ((vars vars (cdr vars)) (allflg))
1529 ((null vars))
1530 (unless (or (symbolp (car vars)) (stringp (car vars)))
1531 (merror (intl:gettext "remove: argument must be a symbol or a string; found: ~M") (car vars)))
1532 (cond
1533 ((and (eq (car vars) '$all) (null allflg))
1534 (setq vars (append vars (cond ((atom info) (cdr $props))
1535 (funp (mapcar #'caar (cdr info)))
1536 (t (cdr info))))
1537 allflg t))
1539 (if (and (stringp (car vars)) (eq prop '$op) (getopr0 (car vars)))
1540 (kill-operator (getopr0 (car vars))))
1542 (if (and (eq prop '$alphabetic) (stringp (car vars)))
1543 (dolist (1-char (coerce (car vars) 'list))
1544 (setf *alphabet* (delete 1-char *alphabet* :count 1 :test #'equal)))
1545 (let ((var (getopr (car vars)))( flag nil))
1546 (cond
1547 (mpropp (mremprop var prop)
1548 (when (member prop '(mexpr mmacro) :test #'eq)
1549 (mremprop var 'mlexprp)
1550 (mremprop var 'mfexprp)
1551 (remprop var 'lineinfo)
1552 (if (mget var 'trace)
1553 (macsyma-untrace var))))
1554 ((eq prop '$transfun)
1555 (remove-transl-fun-props var)
1556 (remove-transl-array-fun-props var))
1557 ((or (setq flag (member prop (cdr $features) :test #'eq)) (member prop opers :test #'eq))
1558 (if flag (unkind var prop))
1559 (zl-remprop var prop)
1560 (if (not (getl var (delete prop (copy-list opers) :count 1 :test #'eq)))
1561 (zl-remprop var 'opers)))
1562 (t (zl-remprop var prop)))
1563 (cond ((eq info t) (rempropchk (car vars)))
1564 ((eq info 'foo))
1565 (funp
1566 (mfunction-delete var info))
1568 (setf info (delete var info :count 1 :test #'eq))))))))))
1570 (defun remove-transl-fun-props (fun)
1571 (if (mget fun 'trace)
1572 (macsyma-untrace fun))
1573 (when (and (get fun 'translated) (not (eq $savedef '$all)))
1574 (fmakunbound fun)
1575 (zl-remprop fun 'translated-mmacro)
1576 (zl-remprop fun 'function-mode)
1577 (when (not (getl fun '(a-expr a-subr)))
1578 (zl-remprop fun 'once-translated)
1579 (zl-remprop fun 'translated))))
1581 (defun remove-transl-array-fun-props (fun)
1582 (when (and (get fun 'translated) (not (eq $savedef '$all)))
1583 (zl-remprop fun 'a-expr)
1584 (zl-remprop fun 'a-subr)
1585 (if (not (fboundp fun)) (zl-remprop fun 'translated))))
1587 (defmfun rempropchk (var)
1588 (if (and
1590 (not (symbolp var))
1591 (and
1592 (not (mgetl var '($nonscalar $scalar $mainvar $numer
1593 matchdeclare $atomgrad atvalues)))
1594 (not (getl var '(evfun evflag translated nonarray bindtest
1595 sp2 operators opers data autoload mode)))))
1596 (not (member var *builtin-$props* :test #'equal)))
1597 (delete var $props :count 1 :test #'equal)))
1599 (defmspec $remfunction (l)
1600 (setq l (cdr l))
1601 (cond ((member '$all l :test #'eq)
1602 (setq l (nconc (mapcar #'caar (cdr $functions))
1603 (mapcar #'caar (cdr $macros)))))
1604 (t (setq l (mapcar #'$verbify l))
1605 (do ((l1 l (cdr l1))) ((null l1) t)
1606 (if (not (or (assoc (ncons (car l1)) (cdr $functions) :test #'equal)
1607 (assoc (ncons (car l1)) (cdr $macros) :test #'equal)))
1608 (rplaca l1 nil)))))
1609 (remove1 l 'mexpr t $functions t)
1610 (remove1 l 'mmacro t $macros t)
1611 (cons '(mlist) l))
1613 (defmspec $remarray (l)
1614 (setq l (cdr l))
1615 (cons '(mlist)
1616 (do ((l l (cdr l)) (x) (pred)) ((null l) (nreverse x))
1617 (cond ((eq (car l) '$all) (setq l (append l (cdr $arrays))))
1618 (t (remcompary (car l)) (setq pred (mremprop (car l) 'array))
1619 (setq pred (or (mremprop (car l) 'hashar) pred))
1620 (setq pred (or (mremprop (car l) 'aexpr) pred))
1621 (setq x (cons (and pred (prog2
1622 (setf $arrays (delete (car l) $arrays :count 1 :test #'eq))
1623 (car l)))
1624 x)))))))
1626 (defun remcompary (x)
1627 (cond ((eq x (mget x 'array))
1628 (zl-remprop x 'array-mode)
1629 (zl-remprop x 'array))))
1631 (defmspec $remvalue (form)
1632 (i-$remvalue (cdr form)))
1634 (defmfun i-$remvalue (l)
1635 (cons '(mlist)
1636 (do ((l l (cdr l)) (x) (y)) ((null l) (nreverse x))
1637 (cond ((eq (car l) '$all) (setq l (append l (cdr $values))))
1638 (t (setq x (cons (cond ((atom (car l))
1639 (if (remvalue (car l) '$remvalue) (car l)))
1640 ((setq y (mgetl (caaar l) '(hashar array)))
1641 (remarrelem y (car l)) (car l)))
1642 x)))))))
1644 (defmfun remarrelem (ary form)
1645 (let ((y (car (arraydims (cadr ary)))))
1646 (arrstore form (cond ((eq y 'fixnum) 0) ((eq y 'flonum) 0.0) (t munbound)))))
1648 (defmfun remrule (l)
1649 (do ((l l (cdr l)) (u))
1650 ((null l))
1651 (cond ((eq (car l) '$all) (setq l (append l (cdr $rules))))
1652 ((get (car l) 'operators) ($remrule (car l) '$all))
1653 ((setq u (ruleof (car l))) ($remrule u (car l)))
1654 ((mget (car l) '$rule)
1655 (zl-remprop (car l) 'expr) (mremprop (car l) '$rule)
1656 (setf $rules (delete (car l) $rules :count 1 :test #'eq))))))
1658 (defmfun remalias1 (l aliasp)
1659 (do ((l l (cdr l)) (u)) ((null l))
1660 (cond ((eq (car l) '$all) (setq l (append l (cdr $aliases))))
1661 ((or aliasp (get (car l) 'noun)) (remalias (car l) t))
1662 ((setq u (get (car l) 'verb))
1663 (zl-remprop (car l) 'verb) (zl-remprop u 'noun)))))
1665 (defmfun mremprop (atom ind)
1666 (let ((props (get atom 'mprops))) (and props (zl-remprop props ind))))
1668 (defmfun mgetl (atom inds)
1669 (let ((props (get atom 'mprops))) (and props (getl props inds))))
1671 ;;; Define $matrix so that apply(matrix,...) does not need to use Lisp
1672 ;;; apply -- in GCL, apply is limited to 63 arguments.
1674 ;;; Equivalent to matrix([?rows]) := ?matrixhelper(?rows)$
1675 #+gcl (mputprop '$matrix '((lambda) ((mlist) ((mlist) rows)) ((matrixhelper) rows)) 'mexpr)
1676 #+gcl (mputprop '$matrix t 'mlexprp)
1677 #+gcl (mputprop '$matrix '$matrix 'pname)
1679 #-gcl (defun $matrix (&rest rows) (matrixhelper rows))
1681 ;; Call ONLY from $matrix
1682 (defun matrixhelper (rows)
1683 #+gcl
1684 (progn
1685 (if (not ($listp rows)) (merror "internal error: MATRIXHELPER expects a Maxima list."))
1686 (setq rows (cdr rows)))
1687 (dolist (row rows)
1688 (if (not ($listp row))
1689 (merror (intl:gettext "matrix: row must be a list; found: ~M") row)))
1690 (matcheck rows)
1691 (cons '($matrix) rows))
1693 (defmfun matcheck (l)
1694 (do ((l1 (cdr l) (cdr l1)) (n (length (car l)))) ((null l1))
1695 (if (not (= n (length (car l1))))
1696 (merror (intl:gettext "matrix: all rows must be the same length.")))))
1698 (defun harrfind (form)
1699 (prog (ary y lispsub iteml sub ncells nitems)
1700 (setq ary (symbol-array (mget (caar form) 'hashar)))
1701 (cond ((not (= (aref ary 2) (length (cdr form))))
1702 (merror (intl:gettext "evaluation: array ~:M must have ~:M indices; found: ~M")
1703 (caar form) (aref ary 2) form)))
1704 (setq sub (cdr form))
1705 (setq iteml (aref ary (setq lispsub (+ 3 (rem (hasher sub) (aref ary 0))))))
1706 a (cond ((null iteml) (go b))
1707 ((alike (caar iteml) sub) (return (cdar iteml))))
1708 (setq iteml (cdr iteml))
1709 (go a)
1710 b (cond (evarrp (throw 'evarrp 'notexist))
1711 ((null (setq y (arrfunp (caar form)))) (return (meval2 sub form))))
1712 (setq y (arrfuncall y sub form))
1713 (setq ary (symbol-array (mget (caar form) 'hashar)))
1714 (setq iteml (aref ary (setq lispsub (+ 3 (rem (hasher sub) (aref ary 0))))))
1715 (setq sub (ncons (cons sub y)))
1716 (cond (iteml (nconc iteml sub)) (t (setf (aref ary lispsub) sub)))
1717 (setf (aref ary 1) (setq nitems (1+ (aref ary 1))))
1718 (cond ((> nitems (setq ncells (aref ary 0)))
1719 (arraysize (caar form) (+ ncells ncells))))
1720 (return y)))
1722 (defun arrfind (form)
1723 (let ((sub (cdr form)) u v type)
1724 (setq v (dimcheck (caar form) sub nil))
1725 (cond (v (setq type (car (arraydims (mget (caar form) 'array))))))
1726 (cond ((and v (prog2
1727 (setq u (apply 'aref (symbol-array (mget (caar form) 'array)) sub))
1728 (cond ((eq type 'flonum) (not (= u flounbound)))
1729 ((eq type 'fixnum) (not (= u fixunbound)))
1730 (t (not (eq u munbound))))))
1732 (evarrp (throw 'evarrp 'notexist))
1733 ((or (not v) (null (setq u (arrfunp (caar form)))))
1734 (cond ((eq type 'flonum) 0.0)
1735 ((eq type 'fixnum) 0)
1736 (t (meval2 sub form))))
1737 (t (setq u (arrfuncall u sub form))
1738 (setf (apply #'aref (symbol-array (mget (caar form) 'array))
1739 sub) u)
1741 u))))
1743 (defmspec $array (x)
1744 (setq x (cdr x))
1745 (cond
1746 ((symbolp (car x))
1747 (if $use_fast_arrays
1748 (let ((type (if (symbolp (cadr x)) (cadr x) '$any))
1749 (name (car x))
1750 (diml (if (symbolp (cadr x)) (cddr x) (cdr x))))
1751 (mset name
1752 (apply '$make_array
1753 type
1754 (mapcar #'(lambda (dim)
1755 ;; let make_array catch bad vals
1756 (add 1 (meval dim)))
1757 diml))))
1758 (let ((compp (assoc (cadr x) '(($complete . t) ($integer . fixnum) ($fixnum . fixnum)
1759 ($float . flonum) ($flonum . flonum)))))
1760 (let ((fun (car x))
1761 (diml (cond (compp (setq compp (cdr compp))
1762 (cddr x))
1763 (t (cdr x))))
1764 funp
1767 (ncells 0))
1768 (when (member '$function diml :test #'eq)
1769 (setq diml (delete '$function diml :count 1 :test #'eq)
1770 funp t))
1771 (setq diml (mapcar #'meval diml))
1772 (cond ((null diml)
1773 (wna-err '$array))
1774 ((> (length diml) 5)
1775 (merror (intl:gettext "array: number of dimensions must be 5 or less; found: ~M") (length diml)))
1776 ((member nil (mapcar #'fixnump diml) :test #'eq)
1777 (merror (intl:gettext "array: all dimensions must be integers."))))
1778 (setq diml (mapcar #'1+ diml))
1779 (setq new (if compp fun (gensym)))
1780 (setf (symbol-array new)
1781 (make-array diml :initial-element (case compp
1782 (fixnum 0)
1783 (flonum 0.0)
1784 (otherwise munbound))))
1785 (when (or funp (arrfunp fun))
1786 (fillarray new (list (if (eq compp 'fixnum) fixunbound flounbound))))
1787 (cond ((null (setq old (mget fun 'hashar)))
1788 (mputprop fun new 'array))
1789 (t (unless (= (aref (symbol-array old) 2) (length diml))
1790 (merror (intl:gettext "array: array ~:M must have ~:M dimensions; found: ~M") fun (aref (symbol-array old) 2) (length diml)))
1791 (setq ncells (+ 2 (aref (symbol-array old) 0)))
1792 (do ((n 3 (1+ n)))
1793 ((> n ncells))
1794 (do ((items (aref (symbol-array old) n) (cdr items)))
1795 ((null items))
1796 (do ((x (caar items) (cdr x)) (y diml (cdr y)))
1797 ((null x)
1798 (if (and (member compp '(fixnum flonum) :test #'eq)
1799 (not (eq (ml-typep (cdar items)) compp)))
1800 (merror (intl:gettext "array: existing elements must be ~M; found: ~M") compp (cdar items)))
1801 (setf (apply #'aref (symbol-array new) (caar items))
1802 (cdar items)))
1803 (if (or (not (fixnump (car x)))
1804 (< (car x) 0)
1805 (not (< (car x) (car y))))
1806 (merror (intl:gettext "array: index must be nonnegative integer less than ~M; found: ~M") (car y) (car x))))))
1807 (mremprop fun 'hashar)
1808 (mputprop fun new 'array)))
1809 (add2lnc fun $arrays)
1810 (when (eq compp 'fixnum)
1811 (putprop fun '$fixnum 'array-mode))
1812 (when (eq compp 'flonum)
1813 (putprop fun '$float 'array-mode))
1814 fun))))
1815 (($listp (car x))
1816 (cons '(mlist) (mapcar #'(lambda (u) (meval `(($array) ,u ,@(cdr x)))) (cdar x))))
1818 (merror (intl:gettext "array: first argument must be a symbol or a list; found: ~M") (car x)))))
1821 (defmfun $show_hash_array (x)
1822 (maphash #'(lambda (k v) (format t "~%~A-->~A" k v)) x))
1824 ;; If this is T then arrays are stored in the value cell,
1825 ;; whereas if it is false they are stored in the function cell
1826 (defmvar $use_fast_arrays nil)
1828 (defmfun arrstore (l r)
1829 (let ((fun (caar l)) ary sub (lispsub 0) hashl mqapplyp)
1830 (cond ((setq ary (mget fun 'array))
1831 (dimcheck fun (setq sub (mapcar #'meval (cdr l))) t)
1832 (if (and (member (setq fun (car (arraydims ary))) '(fixnum flonum) :test #'eq)
1833 (not (eq (ml-typep r) fun)))
1834 (merror (intl:gettext "assignment: attempt to assign ~M to an array of type ~M.") r fun))
1835 (setf (apply #'aref (symbol-array ary) sub) r))
1836 ((setq ary (mget fun 'hashar))
1837 (if (not (= (aref (symbol-array ary) 2) (length (cdr l))))
1838 (merror (intl:gettext "assignment: array ~:M has dimension ~:M, but it was called by ~:M")
1839 fun (aref (symbol-array ary) 2) l))
1840 (setq sub (mapcar #'meval (cdr l)))
1841 (setq hashl (aref (symbol-array ary)
1842 (setq lispsub (+ 3 (rem (hasher sub)
1843 (aref (symbol-array ary) 0))))))
1844 (do ((hashl1 hashl (cdr hashl1)))
1845 ((null hashl1)
1846 (cond ((not (eq r munbound))
1847 (setq sub (ncons (cons sub r)))
1848 (cond ((null hashl) (setf (aref (symbol-array ary) lispsub) sub))
1849 (t (nconc hashl sub)))
1850 (setf (aref (symbol-array ary) 1) (1+ (aref (symbol-array ary) 1))))))
1851 (cond ((alike (caar hashl1) sub)
1852 (cond ((eq r munbound) (setf (aref (symbol-array ary) 1)
1853 (1- (aref (symbol-array ary) 1))))
1854 (t (nconc hashl (ncons (cons sub r)))))
1855 (setf (aref (symbol-array ary) lispsub)
1856 (delete (car hashl1) hashl :count 1 :test #'equal))
1857 (return nil))))
1858 (if (> (aref (symbol-array ary) 1) (aref (symbol-array ary) 0))
1859 (arraysize fun (* 2 (aref (symbol-array ary) 0))))
1861 ((and (eq fun 'mqapply) (or (mxorlistp (setq ary (meval (cadr l)))) (arrayp ary))
1862 (prog2
1863 (setq mqapplyp t l (cdr l))
1864 nil)))
1865 ((and (not mqapplyp)
1866 (or (not (boundp fun))
1867 (not (or (mxorlistp (setq ary (symbol-value fun)))
1868 (arrayp ary)
1869 (typep ary 'hash-table)
1870 (eq (type-of ary) 'mgenarray)))))
1871 (if (member fun '(mqapply $%) :test #'eq) (merror (intl:gettext "assignment: cannot assign to ~M") l))
1872 (if $use_fast_arrays
1873 (progn
1874 ;; (format t "ARRSTORE: use_fast_arrays=true; allocate a new value hash table for ~S~%" fun)
1875 (meval* `((mset) ,fun ,(make-equal-hash-table (cdr (mevalargs (cdr l)))))))
1876 (progn
1877 ;; (format t "ARRSTORE: use_fast_arrays=false; allocate a new property hash table for ~S~%" fun)
1878 (add2lnc fun $arrays)
1879 (setq ary (gensym))
1880 (mputprop fun ary 'hashar)
1881 (setf (symbol-array ary) (make-array 7 :initial-element nil))
1882 (setf (aref (symbol-array ary) 0) 4)
1883 (setf (aref (symbol-array ary) 1) 0)
1884 (setf (aref (symbol-array ary) 2) (length (cdr l)))))
1885 (arrstore l r))
1886 ((or (arrayp ary)
1887 (typep ary 'hash-table)
1888 (eq (type-of ary) 'mgenarray))
1889 (arrstore-extend ary (mevalargs (cdr l)) r))
1890 ((or (eq (caar ary) 'mlist) (= (length l) 2))
1891 (cond ((eq (caar ary) '$matrix)
1892 (cond ((or (not ($listp r)) (not (= (length (cadr ary)) (length r))))
1893 (merror (intl:gettext "assignment: matrix row must be a list, and same length as first row;~%found:~%~M") r))))
1894 ((not (= (length l) 2))
1895 (merror (intl:gettext "assignment: matrix row must have one index; found: ~M") (cons '(mlist) (cdr l)))))
1896 (let ((index (meval (cadr l))))
1897 (cond ((not (fixnump index))
1898 (merror (intl:gettext "assignment: matrix row index must be an integer; found: ~M") index))
1899 ((and (> index 0) (< index (length ary)))
1900 (rplaca (nthcdr (1- index) (cdr ary)) r))
1901 (t (merror (intl:gettext "assignment: matrix row index ~A out of range.") index))))
1903 (t (if (not (= (length l) 3))
1904 (merror (intl:gettext "assignment: matrix must have two indices; found: ~M") (cons '(mlist) (cdr l))))
1905 ($setelmx r (meval (cadr l)) (meval (caddr l)) ary)
1906 r))))
1908 (defun arrfunp (x)
1909 (or (and $transrun (getl x '(a-expr))) (mgetl x '(aexpr))))
1911 (defun arrfuncall (arrfun subs form)
1912 (let ((aexprp t))
1913 (case (car arrfun)
1914 (aexpr (mapply1 (cadr arrfun) subs (cadr arrfun) form))
1915 (a-expr (apply (cadr arrfun) subs))
1916 (a-subr (apply (cadr arrfun) subs)))))
1918 (defun hasher (l) ; This is not the best way to write a hasher. But,
1919 (if (null l) ; please don't change this code or you're liable to
1920 0 ; break SAVE files.
1921 (logand #o77777
1922 (let ((x (car l)))
1923 (cond (($ratp x) (merror (intl:gettext "hash function: cannot hash a special expression (CRE or Taylor).")))
1924 ((or (fixnump x) (floatp x))
1925 (+ (if (fixnump x) x (floor (+ x 5e-4)))
1926 (* 7 (hasher (cdr l)))))
1927 ((atom x) (+ (sxhash x) (hasher (cdr l))))
1928 (t (+ 1 (sxhash (caar x)) (hasher (cdr x))
1929 (hasher (cdr l)))))))))
1931 (defun arraysize (fun n)
1932 (prog (old new indx ncells cell item i y)
1933 (setq old (symbol-array (mget fun 'hashar)))
1934 (setq new (gensym))
1935 (mputprop fun new 'hashar)
1936 (setf (symbol-array new) (make-array (+ n 3) :initial-element nil))
1937 (setq new (symbol-array new))
1938 (setf (aref new 0) n)
1939 (setf (aref new 1) (aref old 1))
1940 (setf (aref new 2) (aref old 2))
1941 (setq indx 2 ncells (+ 2 (aref old 0)))
1942 a (if (> (setq indx (1+ indx)) ncells) (return t))
1943 (setq cell (aref old indx))
1944 b (if (null cell) (go a))
1945 (setq i (+ 3 (rem (hasher (car (setq item (car cell)))) n)))
1946 (if (setq y (aref new i))
1947 (nconc y (ncons item))
1948 (setf (aref new i) (ncons item)))
1949 (setq cell (cdr cell))
1950 (go b)))
1952 (defun dimcheck (ary sub fixpp)
1953 (do ((x sub (cdr x))
1954 (ret t)
1955 (y (cdr (arraydims (mget ary 'array))) (cdr y)))
1956 ((null y)
1957 (if x (merror (intl:gettext "Array ~:M has dimensions ~:M, but was called with ~:M")
1959 `((mlist) ,@(mapcar #'1- (cdr (arraydims (mget ary 'array)))))
1960 `((mlist) ,@sub))
1961 ret))
1962 (cond ((or (null x) (and (fixnump (car x)) (or (< (car x) 0) (not (< (car x) (car y))))))
1963 (setq y nil x (cons nil t)))
1964 ((not (fixnump (car x)) )
1965 (if fixpp (setq y nil x (cons nil t)) (setq ret nil))))))
1967 (defun constlam (x &aux (lam x))
1968 (if aexprp
1969 `(,(car lam) ,(cadr lam) ,@(mbinding ((mparams (cadr lam)))
1970 (mapcar #'meval (cddr lam))))
1972 lam))
1974 (defmspec $define (l)
1975 (twoargcheck l)
1976 (setq l (cdr l))
1977 (meval `((mdefine)
1978 ,(cond ((mquotep (car l)) (cadar l))
1979 ((and (not (atom (car l)))
1980 (member (caaar l) '($ev $funmake $arraymake) :test #'eq))
1981 (meval (car l)))
1982 (t (disp2 (car l))))
1983 ,(meval (cadr l)))))
1985 (defun set-lineinfo (fnname lineinfo body)
1986 (cond ((and (consp lineinfo) (eq 'src (third lineinfo)))
1987 (setf (cdddr lineinfo) (list fnname (first lineinfo)))
1988 (setf (get fnname 'lineinfo) body))
1989 (t (remprop fnname 'lineinfo))))
1991 (defmspec mdefine (l )
1992 (let ($use_fast_arrays) ;;for mdefine's we allow use the oldstyle hasharrays
1993 (twoargcheck l)
1994 (setq l (cdr l))
1995 (let ((fun (car l)) (body (cadr l)) args subs ary fnname mqdef)
1996 (cond ((or (atom fun)
1997 (and (setq mqdef (eq (caar fun) 'mqapply))
1998 (member 'array (cdar fun) :test #'eq)))
1999 (merror (intl:gettext "define: argument cannot be an atom or a subscripted memoizing function; found: ~M") fun))
2000 (mqdef (if (or (atom (cadr fun))
2001 (not (setq ary (member 'array (cdaadr fun) :test #'eq))))
2002 (merror (intl:gettext "define: expected a subscripted expression; found: ~M") (cadr fun)))
2003 (setq subs (cdadr fun) args (cddr fun) fun (cadr fun)
2004 fnname (caar fun))
2005 (if (and (not (mgetl fnname '(hashar array)))
2006 (get fnname 'specsimp))
2007 (mtell (intl:gettext "define: warning: redefining built-in subscripted function ~:M~%")
2008 fnname)))
2009 ((prog2 (setq fnname (caar fun))
2010 (or (mopp fnname) (member fnname '($all $allbut $%) :test #'eq)))
2011 (merror (intl:gettext "define: function name cannot be a built-in operator or special symbol; found: ~:@M") fnname))
2012 ((setq ary (member 'array (cdar fun) :test #'eq)) (setq subs (cdr fun)))
2014 (setq args (cdr fun))
2015 (mredef-check fnname)))
2016 (if (not ary) (remove1 (ncons fnname) 'mmacro t $macros t))
2017 (mdefchk fnname (or args (and (not mqdef) subs)) ary mqdef)
2018 (if (not (eq fnname (caar fun))) (rplaca (car fun) fnname))
2019 (cond ((not ary) (if (and evp (member fnname (car loclist) :test #'eq))
2020 (mputprop fnname t 'local-fun)
2021 (remove-transl-fun-props fnname))
2022 (add2lnc (cons (ncons fnname) args) $functions)
2023 (set-lineinfo fnname (cadar fun) body)
2024 (mputprop fnname (mdefine1 args body) 'mexpr)
2025 (if $translate (translate-function fnname)))
2026 ((prog2 (add2lnc fnname $arrays)
2027 (setq ary (mgetl fnname '(hashar array)))
2028 (remove-transl-array-fun-props fnname))
2029 (if (not (= (if (eq (car ary) 'hashar)
2030 (aref (symbol-array (cadr ary)) 2)
2031 (length (cdr (arraydims (cadr ary)))))
2032 (length subs)))
2033 (merror (intl:gettext "define: ~:M already defined with different number of subscripts.")
2034 fnname))
2035 (mdefarray fnname subs args body mqdef))
2037 (setq ary (gensym))
2038 (mputprop fnname ary 'hashar)
2039 (setf (symbol-array ary) (make-array 7 :initial-element nil))
2040 (setf (aref (symbol-array ary) 0) 4)
2041 (setf (aref (symbol-array ary) 1) 0)
2042 (setf (aref (symbol-array ary) 2) (length subs))
2043 (mdefarray fnname subs args body mqdef)))
2044 (cons '(mdefine simp) (copy-list l)))))
2046 ;; Checks to see if a user is clobbering the name of a system function.
2047 ;; Prints a warning and returns T if he is, and NIL if he isn't.
2048 (defun mredef-check (fnname)
2049 (when (and (not (mget fnname 'mexpr))
2050 (or (and (or (get fnname 'autoload)
2051 (getl-lm-fcn-prop fnname '(subr)))
2052 (not (get fnname 'translated)))
2053 (mopp fnname)))
2054 (format t (intl:gettext "define: warning: redefining the built-in ~:[function~;operator~] ~a~%")
2055 (getl fnname '(verb operators))
2056 (print-invert-case (stripdollar fnname)))
2059 (defun mdefarray (fun subs args body mqdef)
2060 (when (hash-table-p fun)
2061 ;; PRETTY SURE THIS NEXT MESSAGE IS UNREACHABLE (FUN IS ALWAYS A SYMBOL FROM WHAT I CAN TELL) !!
2062 (error "~a is already a hash table. Make it a function first" fun))
2063 (cond ((and (null args) (not mqdef)) (mputprop fun (mdefine1 subs body) 'aexpr))
2064 ((null (dolist (u subs)
2065 (unless (or (consp u) ($constantp u) (stringp u))
2066 (return t))))
2067 (arrstore (cons (ncons fun) subs) (mdefine1 args body)))
2068 (t (mdefchk fun subs t nil)
2069 (mputprop fun (mdefine1 subs (mdefine1 args body)) 'aexpr))))
2071 (defmfun mspecfunp (fun)
2072 (and (or (getl-lm-fcn-prop fun '(macro))
2073 (getl fun '(mfexpr*))
2074 (and $transrun (get fun 'translated-mmacro))
2075 (mget fun 'mmacro))
2076 (not (get fun 'evok))))
2078 (defun mdefine1 (args body)
2079 (list '(lambda) (cons '(mlist) args) body))
2081 (defun mdefchk (fun args ary mqdef)
2082 (let ((dup (find-duplicate args :test #'eq :key #'mparam)))
2083 (when dup
2084 (merror (intl:gettext "define: ~M occurs more than once in the parameter list") (mparam dup))))
2085 (do ((l args (cdr l)) (mfex) (mlex))
2086 ((null l) (and mfex (not mqdef) (mputprop fun mfex 'mfexprp))
2087 (and mlex (not mqdef) (mputprop fun mlex 'mlexprp)))
2088 (if (not (or (mdefparam (car l))
2089 (and (or (not ary) mqdef)
2090 (or (and mfexprp (mquotep (car l))
2091 (mdefparam (cadar l)) (setq mfex t))
2092 (and (mdeflistp l)
2093 (or (mdefparam (cadar l))
2094 (and mfexprp (mquotep (cadar l))
2095 (mdefparam (cadr (cadar l)))
2096 (setq mfex t)))
2097 (setq mlex t))))))
2098 (merror (intl:gettext "define: in definition of ~:M, parameter must be a symbol and must not be a system constant; found: ~M") fun (car l)))))
2100 (defun mdefparam (x)
2101 (and (symbolp x) (not (get x 'sysconst))))
2103 (defun mdeflistp (l)
2104 (and (null (cdr l)) ($listp (car l)) (cdar l) (null (cddar l))))
2106 (defmfun mopp (fun)
2107 (and (not (eq fun 'mqapply))
2108 (or (mopp1 fun)
2109 (and (get fun 'operators) (not (rulechk fun))
2110 (not (member fun rulefcnl :test #'eq)) (not (get fun 'opers))))))
2112 (defmfun mopp1 (fun)
2113 (and (setq fun (get fun 'op)) (not (member fun (cdr $props) :test #'eq))))
2115 ;; maybe should have a separate version, or a macro..
2116 (defun mapply (a b c)
2117 (mapply1 a b c nil))
2119 (defun $apply (fun arg)
2120 (unless ($listp arg)
2121 (merror (intl:gettext "apply: second argument must be a list; found: ~M") arg))
2122 (let ((fun-opr (getopr fun)))
2123 (autoldchk fun-opr)
2124 (mapply1 fun-opr (cdr arg) fun `(($apply) ,fun ,arg))))
2126 (defun autoldchk (fun)
2127 (if (and (symbolp fun)
2128 (get fun 'autoload)
2129 (not (or (fboundp fun) (mfboundp fun))))
2130 (load-function fun t)))
2132 (defmspec $dispfun (l)
2133 (setq l (cdr l))
2134 (cond ((or (cdr l) (not (eq (car l) '$all))) (dispfun1 l nil nil))
2136 `((mlist simp)
2137 ,@(apply #'append
2138 (list (cdr (dispfun1 (cdr $functions) t nil))
2139 (cdr (dispfun1
2140 (mapcan #'(lambda (x) (if (mget x 'aexpr) (ncons x)))
2141 (cdr $arrays)) nil t))
2142 (cdr (dispfun1 (cdr $macros) t nil))))))))
2144 (defun dispfun1 (l flag maexprp)
2145 `((mlist simp)
2146 ,@(loop for fun in l collect
2147 (cadr ($ldisp (consfundef (if flag (caar fun) fun) maexprp nil))))))
2149 (defmspec $fundef (x)
2150 (consfundef (fexprcheck x) nil nil))
2152 (defun consfundef (x maexprp stringp)
2153 (prog (arryp name fun)
2154 (setq arryp (and (not (atom x)) (not (eq (caar x) 'mqapply)) (member 'array (cdar x) :test #'eq)))
2155 (cond ((atom x) (setq name (if (stringp x) ($verbify x) x)
2156 fun (or (and (not maexprp) (mgetl name '(mexpr mmacro)))
2157 (mgetl name '(aexpr)))))
2158 (arryp (setq fun (meval1 (setq name (cons (list (caar x) 'array) (cdr x)))))
2159 (if (or (atom fun) (not (eq (caar fun) 'lambda))) (setq fun nil))))
2160 (cond ((not fun)
2161 (cond (stringp (return x))
2162 ((member 'edit state-pdl :test #'eq) (terpri)))
2163 (merror (intl:gettext "fundef: no such function: ~:M") x)))
2164 (return
2165 (cons (if (eq (car fun) 'mmacro) '(mdefmacro simp) '(mdefine simp))
2166 (cond (arryp (cons (cons '(mqapply) (cons name (cdadr fun))) (cddr fun)))
2167 (t (funcall #'(lambda (body)
2168 (cond ((and (eq (car fun) 'aexpr) (not (atom body))
2169 (eq (caar body) 'lambda))
2170 (list (cons '(mqapply) (cons (cons (cons name '(array))
2171 (cdr (cadadr fun)))
2172 (cdadr body)))
2173 (caddr body)))
2174 (t (list (cons (cons name (if (eq (car fun) 'aexpr) '(array)))
2175 (cdr (cadadr fun)))
2176 body))))
2177 (caddr (cadr fun)))))))))
2180 (defmfun $funmake (fun args)
2181 (if (not (or (stringp fun) (symbolp fun) ($subvarp fun)
2182 (and (not (atom fun)) (eq (caar fun) 'lambda))))
2183 (merror (intl:gettext "funmake: first argument must be a symbol, subscripted symbol, string, or lambda expression; found: ~M") fun))
2184 (if (not ($listp args)) (merror (intl:gettext "funmake: second argument must be a list; found: ~M") args))
2185 (mcons-op-args (getopr fun) (cdr args)))
2187 (defmfun mcons-op-args (op args)
2188 (if (symbolp op)
2189 (cons (ncons op) args)
2190 (list* '(mqapply) op args)))
2192 (defmfun optionp (x)
2193 (and (boundp x)
2194 (not (member x (cdr $values) :test #'eq))
2195 (not (member x (cdr $labels) :test #'eq))))
2197 (defmspec mcond (form)
2198 (setq form (cdr form))
2199 (do ((u form (cddr u)) (v))
2200 ((null u) nil)
2201 (cond ((eq (setq v (mevalp (car u))) t) (return (meval (cadr u))))
2202 (v (return (list* '(mcond) v (mapcar #'meval-atoms (cdr u))))))))
2204 (defun meval-atoms (form)
2205 (cond ((atom form) (meval1 form))
2206 ((eq (caar form) 'mquote) (cadr form))
2207 ((and (getl (caar form) '(mfexpr*))
2208 (not (member (caar form) '(mcond mand mor mnot mprogn mdo mdoin) :test #'eq)))
2209 form)
2210 (t (recur-apply #'meval-atoms form))))
2212 (defmspec mdo (form)
2213 (setq form (cdr form))
2214 (let ((mdop t) (my-var (or (car form) 'mdo)) my-step next test do-body)
2215 (setq my-step (if (caddr form) (meval (caddr form)) 1)
2216 next (or (cadddr form) (list '(mplus) my-step my-var))
2217 test (list '(mor)
2218 (cond ((null (car (cddddr form))) nil)
2219 (t (list (if (mnegp ($numfactor my-step))
2220 '(mlessp)
2221 '(mgreaterp))
2222 my-var (car (cddddr form)))))
2223 (cadr (cddddr form)))
2224 do-body (caddr (cddddr form)))
2225 (mbinding ((ncons my-var)
2226 (ncons (if (null (cadr form)) 1 (meval (cadr form)))))
2227 (do ((val) (bindl bindlist))
2228 ((is test) '$done)
2229 (cond ((null (setq val (catch 'mprog (prog2 (meval do-body) nil))))
2230 (mset my-var (meval next)))
2231 ((atom val) (merror (intl:gettext "do loop: 'go' not within 'block': ~M") val))
2232 ((not (eq bindl bindlist))
2233 (merror (intl:gettext "do loop: illegal 'return': ~M") (car val)))
2234 (t (return (car val))))))))
2236 (defmspec mdoin (form)
2237 (setq form (cdr form))
2238 (funcall #'(lambda (mdop my-var set test action)
2239 (setq set (if ($atom (setq set (format1 (meval (cadr form)))))
2240 (merror (intl:gettext "do loop: 'in' argument must be a nonatomic expression; found: ~M") set)
2241 (margs set))
2242 test (list '(mor)
2243 (if (car (cddddr form))
2244 (list '(mgreaterp) my-var (car (cddddr form))))
2245 (cadr (cddddr form)))
2246 action (caddr (cddddr form)))
2247 (cond ((atom set) '$done)
2248 (t (mbinding ((ncons my-var) (ncons (car set)))
2249 (do ((val) (bindl bindlist))
2250 ((or (atom set) (is test))
2251 '$done)
2252 (cond ((null (setq val (catch 'mprog (prog2 (meval action) nil))))
2253 (if (setq set (cdr set)) (mset my-var (car set))))
2254 ((atom val) (merror (intl:gettext "do loop: 'go' not within 'block': ~M") val))
2255 ((not (eq bindl bindlist))
2256 (merror (intl:gettext "do loop: illegal 'return': ~M") (car val)))
2257 (t (return (car val)))))))))
2258 t (or (car form) 'mdo) nil nil nil))
2260 (defmspec mprog (prog)
2261 (setq prog (cdr prog))
2262 (let (vars vals (mlocp t))
2263 (if ($listp (car prog)) (setq vars (cdar prog) prog (cdr prog)))
2264 (do ((l vars (cdr l))) ((null l) (setq vals vars))
2265 (if (not (atom (car l))) (return (setq vals t))))
2266 (if (eq vals t)
2267 (setq vals (mapcar #'(lambda (v)
2268 (cond ((atom v) v)
2269 ((eq (caar v) 'msetq) (meval (caddr v)))
2270 (t (merror
2271 (intl:gettext "block: variable list must comprise only atoms and assignment expressions; found: ~M")
2272 v))))
2273 vars)
2274 vars (mapcar #'(lambda (v) (if (atom v) v (cadr v))) vars)))
2275 (let ((dup (find-duplicate vars :test #'eq)))
2276 (when dup
2277 (merror (intl:gettext "block: ~M occurs more than once in the variable list") dup)))
2278 (setq loclist (cons nil loclist))
2279 ; Ensure that MUNLOCAL gets called so that we don't leak local
2280 ; properties if we run into an error
2281 (unwind-protect
2282 (mbinding (vars vals)
2283 (do ((prog prog (cdr prog)) (mprogp prog)
2284 (bindl bindlist) (val '$done) (retp) (x) ($%% '$%%))
2285 ((null prog) val)
2286 (cond ((atom (car prog))
2287 (if (null (cdr prog))
2288 (setq retp t val (meval (car prog)))))
2289 ((null (setq x (catch 'mprog
2290 (prog2 (setq val (setq $%% (meval (car prog))))
2291 nil)))))
2292 ((not (eq bindl bindlist))
2293 (if (not (atom x))
2294 ;; DUNNO WHAT'S "ILLEGAL" HERE
2295 (merror (intl:gettext "block: illegal 'return': ~M") (car x))
2296 ;; DUNNO WHAT'S "ILLEGAL" HERE
2297 (merror (intl:gettext "block: illegal 'go': ~M") x)))
2298 ((not (atom x)) (setq retp t val (car x)))
2299 ((not (setq prog (member x mprogp :test #'equal)))
2300 (merror (intl:gettext "block: no such tag: ~:M") x)))
2301 (if retp (setq prog '(nil)))))
2302 (munlocal))))
2304 (defmfun mreturn (&optional (x nil) &rest args)
2305 (cond
2306 ((not (null args))
2307 (merror (intl:gettext "return: too many arguments; found: ~M") `((mlist) ,x ,@args) ))
2308 ((and (not mprogp) (not mdop))
2309 (merror (intl:gettext "return: not within 'block'")))
2310 (t (throw 'mprog (ncons x)) ) ))
2312 (defmspec mgo (tag)
2313 (setq tag (fexprcheck tag))
2314 (cond ((not mprogp) (merror (intl:gettext "go: not within 'block'")))
2315 ((atom tag) (throw 'mprog tag))
2316 (t (merror (intl:gettext "go: argument must be an atom; found: ~M") tag))))
2318 (defmspec $subvar (l)
2319 (setq l (cdr l))
2320 (if (null l)
2321 (wna-err '$subvar))
2322 (meval (cons '(mqapply array) l)))
2324 (defmfun rat (x y)
2325 `((rat simp) ,x ,y))
2327 (defmfun add2lnc (item llist)
2328 (unless (memalike item (if ($listp llist) (cdr llist) llist))
2329 (unless (atom item)
2330 (setf llist (delete (assoc (car item) llist :test #'equal) llist :count 1 :test #'equal)))
2331 (nconc llist (ncons item))))
2333 (defmfun bigfloatm* (bf)
2334 (unless (member 'simp (cdar bf) :test #'eq)
2335 (setq bf (cons (list* (caar bf) 'simp (cdar bf)) (cdr bf))))
2336 (if $float ($float bf) bf))
2338 (defmfun $allbut (&rest args)
2339 (cons '($allbut) args))
2341 (defquote dsksetq (&rest l)
2342 (let ((dsksetp t))
2343 (mset (car l) (eval (cadr l)))))
2345 (defmfun dskrat (x)
2346 (orderpointer (caddar x))
2347 (mapc #'(lambda (a b) (dskrat-subst a b (cddddr (car x))) ; for TAYLOR forms
2348 (dskrat-subst a b (cdr x)))
2349 genvar (cadddr (car x)))
2350 (rplaca (cdddar x) genvar)
2351 (if (member 'trunc (car x) :test #'eq)
2352 (srconvert x) x)) ; temporary
2354 (defun dskrat-subst (x y z)
2355 (cond ((atom z) z)
2356 (t (if (eq y (car z)) (rplaca z x) (dskrat-subst x y (car z)))
2357 (dskrat-subst x y (cdr z))
2358 z)))
2360 (defmfun |''MAKE-FUN| (noun-name x)
2361 (simplifya (list (ncons noun-name) (resimplify x)) t))
2363 (macrolet ((|''MAKE| (fun noun)
2364 `(defmfun ,fun (x) (|''MAKE-FUN| ',noun x))))
2365 (|''MAKE| $log %log)
2366 (|''MAKE| $sin %sin) (|''MAKE| $cos %cos) (|''MAKE| $tan %tan)
2367 (|''MAKE| $cot %cot) (|''MAKE| $sec %sec) (|''MAKE| $csc %csc)
2368 (|''MAKE| $sinh %sinh) (|''MAKE| $cosh %cosh) (|''MAKE| $tanh %tanh)
2369 (|''MAKE| $coth %coth) (|''MAKE| $sech %sech) (|''MAKE| $csch %csch)
2370 (|''MAKE| $asin %asin) (|''MAKE| $acos %acos) (|''MAKE| $atan %atan)
2371 (|''MAKE| $acot %acot) (|''MAKE| $asec %asec) (|''MAKE| $acsc %acsc)
2372 (|''MAKE| $asinh %asinh) (|''MAKE| $acosh %acosh) (|''MAKE| $atanh %atanh)
2373 (|''MAKE| $acoth %acoth) (|''MAKE| $asech %asech) (|''MAKE| $acsch %acsch)
2374 (|''MAKE| $round %round) (|''MAKE| $truncate %truncate) (|''MAKE| $plog %plog)
2375 (|''MAKE| $signum %signum) (|''MAKE| $gamma %gamma))
2377 ;; evfun properties
2378 (mapc #'(lambda (x) (putprop x t 'evfun))
2379 '($radcan $factor $ratsimp $trigexpand $trigreduce $logcontract
2380 $rootscontract $bfloat $ratexpand $fullratsimp $rectform $polarform))
2382 ;; evflag properties
2383 (mapc #'(lambda (x) (putprop x t 'evflag))
2384 '($exponentialize $%emode $demoivre $logexpand $logarc
2385 $radexpand $keepfloat $listarith $float $ratsimpexpons $ratmx
2386 $simp $simpsum $simpproduct $algebraic $ratalgdenom $factorflag $ratfac
2387 $infeval $%enumer $programmode $lognegint $logabs $letrat
2388 $halfangles $exptisolate $isolate_wrt_times $sumexpand
2389 $cauchysum $numer_pbranch $m1pbranch $dotscrules $trigexpand))
2391 ;;; Float constants, to 2048 bits of precision.
2392 ;;; (EXP 1)
2393 (mdefprop $%e 2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320030599218174135966290435729003342952605956307381323286279434907632338298807531952510190115738341879307021540891499348841675092447614606680822648001684774118537423454424371075390777449920695517027618386062613313845830007520449338265602976067371132007093287091274437470472306969772093101416928368190255151086574637721112523897844250569536967707854499699679468644549059879316368892300987931277361782154249992295763514822082698951936680331825288693984964651058209392398294887933203625094431173012381970684161404
2394 $numer)
2395 ;;; (ATAN 0 -1)
2396 (mdefprop $%pi 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608
2397 $numer)
2398 ;;; (1+sqrt(5))/2
2399 (mdefprop $%phi 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144381497587012203408058879544547492461856953648644492410443207713449470495658467885098743394422125448770664780915884607499887124007652170575179788341662562494075890697040002812104276217711177780531531714101170466659914669798731761356006708748071013179523689427521948435305678300228785699782977834784587822891109762500302696156170025046433824377648610283831268330372
2400 $numer)
2401 ;;; Euler's constant
2402 (mdefprop $%gamma 0.57721566490153286060651209008240243104215933593992359880576723488486772677766467093694706329174674951463144724980708248096050401448654283622417399764492353625350033374293733773767394279259525824709491600873520394816567085323315177661152862119950150798479374508570574002992135478614669402960432542151905877553526733139925401296742051375413954911168510280798423487758720503843109399736137255306088933126760017247953783675927135157722610273492913940798430103417771778088154957066107501016191663340152278935867965497252036212879226555953669628176388792726801324310104765059637039473949576389065729679296010090151251959509223
2403 $numer)
2405 (mdefprop $herald_package (nil $transload t) $props)
2406 (mdefprop $load_package (nil $transload t) $props)
2408 (defprop bigfloat bigfloatm* mfexpr*)
2409 (defprop lambda constlam mfexpr*)
2410 (defprop quote cadr mfexpr*) ; Needed by MATCOM/MATRUN.
2412 (eval-when
2413 #+gcl (compile eval)
2414 #-gcl (:compile-toplevel :execute)
2416 (setq *read-base* *old-read-base*))