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