Merge branch 'rtoy-mathjax-for-lapack'
[maxima.git] / src / dskfn.lisp
blobd1bfb99eb36adc2d0dc8ecf5333fe0344999767f
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
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 dskfn)
15 (declare-top (special aaaaa errset indlist
16 savefile
17 context))
19 (defvar indlist '(evfun evflag bindtest nonarray sp2 sp2subs opers
20 special autoload assign mode))
22 (defun infolstchk (x)
23 (let ((iteml (cond ((not (and x (or (member (car x) '($all $contexts) :test #'eq)
24 (member (car x) (cdr $infolists) :test #'eq))))
26 ((eq (car x) '$all)
27 (infolstchk (append (cdr $infolists)
28 '($linenum $ratvars *ratweights
29 tellratlist *alphabet* $dontfactor $features $contexts))))
30 ((eq (car x) '$labels) (reverse (cdr $labels)))
31 ((member (car x) '($functions $macros $gradefs $dependencies $structures) :test #'eq)
32 (mapcar #'caar (cdr (symbol-value (car x)))))
33 ((eq (car x) '$contexts) (delete '$global (reverse (cdr $contexts)) :count 1 :test #'eq))
34 (t (cdr (symbol-value (car x)))))))
35 (if (eq iteml t)
37 (append (or iteml '(nil)) (cdr x)))))
39 (defmacro with-maxima-io-syntax (&rest forms)
40 `(let ((*readtable* (copy-readtable nil)) (*print-readably* t) *print-gensym*
41 (*print-circle* nil) (*print-level* nil) (*print-length* nil) (*print-base* 10.) (*print-radix* t)
42 #-gcl (*print-pprint-dispatch* (copy-pprint-dispatch)))
43 (progn
44 #-(or scl allegro)
45 (setf (readtable-case *readtable*) :invert)
46 #+(or scl allegro)
47 (unless #+scl (eq ext:*case-mode* :lower)
48 #+allegro (eq excl:*current-case-mode* :case-sensitive-lower)
49 (setf (readtable-case *readtable*) :invert))
50 #-gcl(set-pprint-dispatch '(cons (member maxima::defmtrfun))
51 #'pprint-defmtrfun))
52 ,@forms))
54 (defmspec $save (form)
55 (when (= (length (rest form)) 0)
56 (merror (intl:gettext "save: no file name specified.")))
57 (let ((fname (meval (second form))))
58 (unless (stringp fname)
59 (merror (intl:gettext "save: first argument must be a string; found: ~M") fname))
60 (with-maxima-io-syntax ; $save stores Lisp expressions.
61 (dsksetup (cdr form) '$save fname))))
63 (defvar *dsksetup-errset-value* t)
65 (defun dsksetup (x fn fname)
66 (let (list maxima-error (errset *dsksetup-errset-value*))
67 (setq savefile
68 (if (or (eq $file_output_append '$true) (eq $file_output_append t))
69 (open fname :direction :output :if-exists :append :if-does-not-exist :create)
70 (open fname :direction :output :if-exists :supersede :if-does-not-exist :create)))
71 (princ ";;; -*- Mode: LISP; package:maxima; syntax:common-lisp; -*- " savefile)
72 (terpri savefile)
73 (princ "(in-package :maxima)" savefile)
74 ;; Check arguments. First argument was checked above.
75 ;; May want to relax requirement that all atoms be symbols.
76 (dolist (u (cdr x))
77 (cond ((atom u) (if (not (symbolp u)) (improper-arg-err u fn)))
78 ((listargp u))
79 ((or (not (eq (caar u) 'mequal)) (not (symbolp (cadr u))))
80 (improper-arg-err u fn))))
81 (setq list (ncons (car x))
82 x (cdr x))
83 (if (null (errset (dskstore x list)))
84 (setq maxima-error t))
85 (close savefile)
86 (namestring (truename savefile))))
88 (defun dskstore (x list)
89 (do ((x x (cdr x))
90 (val)
91 (rename)
92 (item)
93 (alrdystrd)
94 (nitemfl nil nil))
95 ((null x))
96 (cond ((setq val (listargp (car x)))
97 (setq x (nconc (getlabels (car val) (cdr val) nil) (cdr x))))
98 ((setq val (assoc (car x) '(($inlabels . $inchar) ($outlabels . $outchar)
99 ($linelabels . $linechar)) :test #'eq))
100 (setq x (nconc (getlabels* (eval (cdr val)) nil) (cdr x)))))
101 (if (not (atom (car x)))
102 (setq rename (cadar x) item (getopr (caddar x)))
103 (setq x (infolstchk x) item (setq rename (and x (getopr (car x))))))
104 (cond ((not (symbolp item))
105 (setq nitemfl item)
106 (setq item (let ((nitem (gensym))) (setf (symbol-value nitem) (meval item)) nitem)))
107 ((eq item '$ratweights) (setq item '*ratweights))
108 ((eq item '$tellrats) (setq item 'tellratlist))
109 ((eq item '$alphabet) (setq item '*alphabet*)))
110 (cond
111 ((null x) (return nil))
112 ((null (car x)))
113 ((and (setq val (assoc item alrdystrd :test #'eq)) (eq rename (cdr val))))
114 ((null (setq alrdystrd (cons (cons item rename) alrdystrd))))
115 ((and (or (not (boundp item))
116 (and (eq item '$ratvars) (null varlist))
117 (prog2 (setq val (symbol-value item))
118 (or (and (eq item '$dontfactor)
119 (null (cdr val)))
120 (and (member item '(tellratlist *alphabet* *ratweights) :test #'eq) (null val))
121 (and (eq item '$features) (alike (cdr val) featurel))
122 (and (eq item '$default_let_rule_package)
123 (eq item val)))))
124 (or ;; This clause has been reformulated to cut out a test with
125 ;; dsksavep and unstorep, but to respect the side effects.
126 (null (setq val (safe-get item 'mprops)))
127 (equal val '(nil))
128 nil)
129 (not (getl item '(operators reversealias grad noun verb expr op data)))
130 (not (member item (cdr $props) :test #'eq))
131 (or (not (member item (cdr $contexts) :test #'eq))
132 (not (eq item '$initial))
133 (let ((context '$initial)) (null (cdr ($facts '$initial)))))))
134 (t (when (boundp item)
135 (setq val (symbol-value item))
136 (if (eq item '$context) (setq x (list* nil val (cdr x))))
137 (dskatom item rename val)
138 (if (not (optionp rename)) (infostore item 'value rename)))
139 (when (setq val (and (member item (cdr $aliases) :test #'eq) (get item 'reversealias)))
140 (dskdefprop rename val 'reversealias)
141 (pradd2lnc rename '$aliases)
142 (dskdefprop val rename 'alias)
143 (and greatorder (not (assoc 'greatorder alrdystrd :test #'eq))
144 (setq x (list* nil 'greatorder (cdr x))))
145 (and lessorder (not (assoc 'lessorder alrdystrd :test #'eq))
146 (setq x (list* nil 'lessorder (cdr x))))
147 (setq x (list* nil val (cdr x))))
148 (cond ((setq val (get item 'noun))
149 (setq x (list* nil val (cdr x)))
150 (dskdefprop rename val 'noun))
151 ((setq val (get item 'verb))
152 (setq x (list* nil val (cdr x)))
153 (dskdefprop rename val 'verb)))
154 (when (mget item '$rule)
155 (if (setq val (ruleof item))
156 (setq x (list* nil val (cdr x))))
157 (pradd2lnc (getop rename) '$rules))
158 (when (and (setq val (cadr (getl-lm-fcn-prop item '(expr))))
159 (or (mget item '$rule) (get item 'translated)))
160 (if (mget item 'trace)
161 (let (val1)
162 (remprop item 'expr)
163 (if (setq val1 (get item 'expr))
164 (dskdefprop rename val1 'expr))
165 (setf (symbol-plist item) (list* 'expr val (symbol-plist item))))
166 (dskdefprop rename val 'expr))
167 (propschk item rename 'translated))
168 (when (setq val (get item 'operators))
169 (dskdefprop rename val 'operators)
170 (when (setq val (get item 'rules))
171 (dskdefprop rename val 'rules)
172 (setq x (cons nil (append val (cdr x)))))
173 (if (member item (cdr $props) :test #'eq) (pradd2lnc rename '$props))
174 (setq val (mget item 'oldrules))
175 (and val (setq x (cons nil (nconc (cdr (reverse val)) (cdr x))))))
176 (if (member item (cdr $features) :test #'eq) (pradd2lnc rename '$features))
177 (when (member (getop item) (cdr $props) :test #'eq)
178 (dolist (ind indlist) (propschk item rename ind))
179 (dolist (oper opers) (propschk item rename oper)))
180 (when (and (setq val (get item 'op)) (member val (cdr $props) :test #'eq))
181 (dskdefprop item val 'op)
182 (dskdefprop val item 'opr)
183 (pradd2lnc val '$props)
184 (if (setq val (extopchk item val))
185 (setq x (list* nil val (cdr x)))))
186 (when (and (setq val (get item 'grad)) (assoc (ncons item) $gradefs :test #'equal))
187 (dskdefprop rename val 'grad)
188 (pradd2lnc (cons (ncons rename) (car val)) '$gradefs))
189 (when (and (get item 'data)
190 (not (member item (cdr $contexts) :test #'eq))
191 (setq val (cdr ($facts item))))
192 (fasprin `(restore-facts (quote ,val)))
193 (if (member item (cdr $props) :test #'eq) (pradd2lnc item '$props)))
194 (when (and (member item (cdr $contexts) :test #'eq)
195 (let ((context item)) (setq val (cdr ($facts item)))))
196 (fasprint t `(dsksetq $context (quote ,item)))
197 (if (member item (cdr $activecontexts) :test #'eq)
198 (fasprint t `($activate (quote ,item))))
199 (fasprint t `(restore-facts (quote ,val))))
200 (mpropschk item rename)
201 (if (not (get item 'verb))
202 (nconc list (ncons (or nitemfl (getop item)))))))))
204 (defun dskatom (item rename val)
205 (cond ((eq item '$ratvars)
206 (fasprint t `(setq varlist (append varlist (quote ,varlist))))
207 (fasprint t '(setq $ratvars (cons '(mlist simp) varlist)))
208 (pradd2lnc '$ratvars '$myoptions))
209 ((eq item '$dontfactor)
210 (fasprin `(setq ,item (nconc (quote ,val) (cdr ,item))))
211 (pradd2lnc item '$myoptions))
212 ((eq item 'tellratlist)
213 (fasprin `(setq tellratlist (nconc (quote ,val) tellratlist)))
214 (pradd2lnc 'tellratlist '$myoptions))
215 ((eq item '*alphabet*)
216 (fasprin `(setq *alphabet* (nconc (quote ,val) *alphabet*))))
217 ((eq item '*ratweights)
218 (fasprin `(apply (function $ratweight) (quote ,(dot2l val)))))
219 ((eq item '$features)
220 (dolist (var (cdr $features))
221 (if (not (member var featurel :test #'eq)) (pradd2lnc var '$features))))
222 ((and (eq item '$linenum) (eq item rename))
223 (fasprint t `(setq $linenum ,val)))
224 (($ratp val) (fasprint t `(dsksetq ,rename (dskrat (quote ,val)))))
226 (cond
227 ;; Hash tables ("fast arrays") aren't printable in all Lisp implementations.
228 ((hash-table-p val)
229 (fasprint t `(dsksetq ,rename (fill-hash (make-hash-table :test 'equal) ',(list-hash-pairs val)))))
230 ;; If there are other unprintable values, this is a place to handle them.
232 (fasprint t (list 'dsksetq rename (list 'quote val))))))))
234 (defun fill-hash (h kv-list)
235 (dolist (kv kv-list)
236 (setf (gethash (first kv) h) (second kv)))
239 (defun list-hash-pairs (h)
240 (loop for value being the hash-values of h using (hash-key key)
241 collect (list key value)))
243 (defun mpropschk (item rename)
244 (do ((props (cdr (or (get item 'mprops) '(nil))) (cddr props)) (val))
245 ((null props))
246 (cond ((or (member (car props) '(trace trace-type trace-level trace-oldfun) :test #'eq)
247 ;; This clause has been reformulated to cut out a mfile-test,
248 ;; but to respect the side effect of assigning a value to val.
249 (and (setq val (cadr props)) nil)
250 (and (eq (car props) 't-mfexpr)
251 (not (get item 'translated)))))
252 ((not (member (car props) '(hashar array) :test #'eq))
253 (fasprin (list 'mdefprop rename val (car props)))
254 (if (not (member (car props) '(mlexprp mfexprp t-mfexpr) :test #'eq))
255 (infostore item (car props)
256 (cond ((member (car props) '(mexpr mmacro) :test #'eq)
257 (let ((val1 (get item 'function-mode)))
258 (if val1 (dskdefprop rename
259 val1
260 'function-mode)))
261 (cons (ncons rename) (cdadr val)))
262 ((eq (car props) 'depends)
263 (cons (ncons rename) val))
264 (t rename)))))
265 (t (dskary item (list 'quote rename) val (car props))
266 (infostore item (car props) rename)))))
268 (defun dskary (item rename val ind)
269 ;; Some small forms ordinarily non-EQ for fasdump must be output
270 ;; in proper sequence with the big mungeables.
271 ;; For this reason only they are output as EQ-forms.
272 (let ((ary (cond ((and (eq ind 'array) (get item 'array)) rename)
273 ;; This code handles "complete" arrays.
274 (t (fasprint t '(defparameter aaaaa (gensym))) 'aaaaa)))
275 (dims (arraydims val))
276 val1)
277 (if (eq ind 'hashar) (fasprint t `(remcompary ,rename)))
278 (fasprint t `(mremprop ,rename (quote ,(if (eq ind 'array) 'hashar 'array))))
279 (fasprint t `(mputprop ,rename ,ary (quote ,ind)))
280 (fasprint t `(*array ,ary (quote ,(car dims)) ,.(cdr dims)))
281 (fasprint t `(fillarray ,ary (quote ,(listarray val))))
282 (if (setq val1 (get item 'array-mode))
283 (fasprint t `(defprop ,(cadr rename) ,val1 array-mode)))))
285 (defun extopchk (item val)
286 (when (or (get item 'nud) (get item 'led) (get item 'lbp))
287 (fasprin `(define-symbol (quote ,val)))
288 (if (member val *mopl* :test #'eq)
289 (fasprin `(setq *mopl* (cons (quote ,val) *mopl*))))
290 (when (setq val (get item 'dimension))
291 (dskdefprop item val 'dimension)
292 (dskdefprop item (get item 'dissym) 'dissym)
293 (dskdefprop item (get item 'grind) 'grind))
294 (if (setq val (get item 'lbp)) (dskdefprop item val 'lbp))
295 (if (setq val (get item 'rbp)) (dskdefprop item val 'rbp))
296 (if (setq val (get item 'nud)) (dskdefprop item val 'nud))
297 (if (setq val (get item 'led)) (dskdefprop item val 'led))
298 (when (setq val (get item 'verb))
299 (dskdefprop val (get val 'dimension) 'dimension)
300 (dskdefprop val (get val 'dissym) 'dissym))
301 (when (setq val (get item 'match))
302 (dskdefprop item val 'match) val)))
304 (defun propschk (item rename ind)
305 (let ((val (get item ind)))
306 (when val (dskdefprop rename val ind)
307 (pradd2lnc (getop rename) '$props))))
309 (defun fasprin (form)
310 (fasprint nil form))
312 (defun fasprint (eqfl form)
313 (declare (ignore eqfl))
314 (print form savefile))
316 (defun infostore (item flag rename)
317 (let ((prop (cond ((eq flag 'value)
318 (if (member rename (cdr $labels) :test #'eq) '$labels '$values))
319 ((eq flag 'mexpr) '$functions)
320 ((eq flag 'mmacro) '$macros)
321 ((member flag '(array hashar) :test #'eq) '$arrays)
322 ((eq flag 'depends) '$dependencies)
323 (t '$props))))
324 (cond ((eq prop '$labels)
325 (fasprin `(addlabel (quote ,rename)))
326 (if (get item 'nodisp) (dskdefprop rename t 'nodisp)))
327 (t (pradd2lnc rename prop)))))
329 (defun pradd2lnc (item prop)
330 (if (or (null $packagefile) (not (member prop (cdr $infolists) :test #'eq))
331 (and (eq prop '$props) (getopr0 item)))
332 (fasprin `(add2lnc (quote ,item) ,prop))))
334 (defun dskdefprop (name val ind)
335 (declare (special *opr-table*))
336 (fasprin
337 (cond
338 ((and (member ind '(expr fexpr macro) :test #'eq) (eq (car val) 'lambda))
339 (list* 'defun name (if (eq ind 'expr) (cdr val) (cons ind (cdr val)))))
340 ((eq ind 'opr)
341 (if (symbolp name)
342 (list 'defprop name val ind)
343 `(setf (gethash ,name *opr-table*) ',val)))
345 (list 'defprop name val ind)))))