1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; (c) Copyright 1982 Massachusetts Institute of Technology ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15 (load-macsyma-macros mrgmac
)
17 ;; This file uses its own special syntax which is set up here. The function
18 ;; which does it is defined in LIBMAX;MRGMAC. It sets up <, >, and : for
19 ;; structure manipulation. A major bug with this package is that the code is
20 ;; almost completely uncommented. Someone with nothing better to do should go
21 ;; through it, figure out how it works, and write it down.
25 (defvar context
'global
)
27 (defvar current
'global
)
33 (defvar *nobjects
* nil
)
34 (defvar *dbcheck
* nil
)
39 (defvar *connumber
* 50)
41 (defconstant +lab-high-bit
+ most-negative-fixnum
)
43 ;; One less than the number of bits in a fixnum.
44 (defconstant +labnumber
+ (1- (integer-length +lab-high-bit
+)))
46 ;; A cell with the high bit turned on.
47 (defvar *lab-high-lab
* (list +lab-high-bit
+))
49 ;; Variables that are set by (clear)
69 ;; Macro for indirecting through the contents of a cell.
71 (defmacro unlab
(cell)
77 (defmacro iorm
(cell n
)
78 `(rplaca ,cell
(logior (car ,cell
) (car ,n
))))
80 (defmacro xorm
(cell n
)
81 `(rplaca ,cell
(logxor (car ,cell
) (car ,n
))))
83 (defprop global
1 cmark
)
85 (defvar conunmrk
(make-array (1+ *connumber
*) :initial-element nil
))
86 (defvar conmark
(make-array (1+ *connumber
*) :initial-element nil
))
92 (and (symbolp x
) (get x
'mark
)))
94 (defun zl-remprop (sym indicator
)
96 (remprop sym indicator
)
98 (remf (cdr sym
) indicator
))))
101 (zl-remprop x
'mark
))
106 (t (mapc #'marks x
))))
110 ((or (atom x
) (numberp (car x
))) (unmrk x
))
111 (t (mapc #'unmrks x
))))
114 (atom (selector +labs
) (selector -labs
) (selector data
))
118 (atom (selector =labs
) (selector nlabs
) (selector data
) (selector in
))
122 (atom (selector =labs
) (selector nlabs
) (selector data
) (selector un
))
126 (atom (selector ulabs
) (selector con
) (selector wn
))
130 (atom (selector cmark fixnum
0) (selector subc
) (selector data
)))
152 (defmacro subp
(&rest x
)
153 (setq x
(mapcar #'(lambda (form) `(unlab ,form
)) x
))
154 `(= ,(car x
) (logand ,@x
)))
157 (if (symbolp x
) x
(list x
)))
160 (or (atom x
) (mnump (car x
))))
169 (cond ((do ((l *lprs
* (cdr l
)))
171 (if (and (labeq m
(caaar l
)) (labeq n
(cdaar l
)))
173 ((= (decf *lprindex
*) *labindex
*)
176 (push (cons (cons m n
) (ash 1 *lprindex
*)) *lprs
*)
180 (= (logior x
+lab-high-bit
+) (logior y
+lab-high-bit
+)))
184 ((= *lprindex
* (incf *labindex
*))
186 (t (push (cons nd
(lab *labindex
*)) *labs
*)
187 (beg nd
(lab *labindex
*))
191 (do ((l *lprs
* (cdr l
))
194 (unless (or (zerop (logand r
(cdar l
))) (zerop (logand x
(caaar l
))))
195 (setq y
(logior (cdaar l
) y
)))))
198 (do ((l *lprs
* (cdr l
))
201 (unless (or (zerop (logand r
(cdar l
))) (zerop (logand (cdaar l
) y
)))
202 (setq x
(logior x
(caaar l
))))))
207 (format t
" ~,,' ,3:B" (logandc1 +lab-high-bit
+ x
))))
210 (subp lab
(+labz cl
)))
213 (subp lab
(-labz cl
)))
215 (defun onpu (lab fact
)
216 (subp lab
(ulabz fact
)))
218 (defun visiblep (dat)
219 (and (not (ulabs dat
)) (cntp dat
)))
221 (defun cancel (lab dat
)
222 (cond ((setq *db
* (ulabs dat
))
226 (setq lab
(unlab lab
))
227 (putprop dat
(copyn lab
) 'ulabs
))))
229 (defun queue+p
(nd lab
)
230 (cond ((null (setq *db
* (+labs nd
)))
232 (setq lab
(unlab lab
))
233 (putprop nd
(copyn (logior +lab-high-bit
+ lab
)) '+labs
))
236 ((subp *lab-high-lab
* *db
*)
240 (iorm *db
* (copyn (logior +lab-high-bit
+ (unlab lab
)))))))
243 (setq lab
(copyn lab
))
251 (defun queue-p (nd lab
)
252 (cond ((null (setq *db
* (-labs nd
)))
254 (setq lab
(unlab lab
))
255 (putprop nd
(copyn (logior +lab-high-bit
+ lab
)) '-labs
))
258 ((subp *lab-high-lab
* *db
*)
262 (iorm *db
* (copyn (logior +lab-high-bit
+ (unlab lab
)))))))
265 (setq lab
(copyn lab
))
271 (setq -s
(cons nd -s
)))))
280 (rplacd +sm
(cons nd
(cdr +sm
)))
282 (setq +sl
(cdr +sl
)))
283 (setq +sm
(cdr +sm
))))))
292 (rplacd -sm
(cons nd
(cdr -sm
)))
294 (setq -sl
(cdr -sl
)))
295 (setq -sm
(cdr -sm
))))))
304 (rplacd +sl
(ncons nd
))
305 (setq +sl
(cdr +sl
))))))
314 (rplacd -sl
(ncons nd
))
315 (setq -sl
(cdr -sl
))))))
320 (xorm (zl-get (car +s
) '+labs
) *lab-high-lab
*)
322 (cond ((not (eq +s
+sm
))
335 (xorm (-labs (car -s
)) *lab-high-lab
*)
337 (cond ((not (eq -s -sm
))
349 (format *trace-output
* "~%CLEAR: clearing ~A" *marks
*))
350 (mapc #'(lambda (sym) (push+sto
(sel sym
+labs
) nil
)) +labs
)
351 (mapc #'(lambda (sym) (push+sto
(sel sym -labs
) nil
)) -labs
)
352 (mapc #'(lambda (sym) (zl-remprop sym
'ulabs
)) ulabs
)
362 *lprindex
* +labnumber
+
371 (cond ((atom pat
) pat
)
372 ((prog2 (setq pat
(mapcar #'semant pat
)) nil
))
373 ((eq (car pat
) 'kind
)
380 (beg (car pat
) (lpr 1 2))
385 (cond ((eq (car pat
) 'kind
)
391 (let ((isp 'unknown
) #+ccl
(err t
))
397 #+ccl
(setq err nil
))
403 ;; Return NIL for all non-symbols.
408 (do ((p (dq+) (dq+)))
412 (mark+ p
(+labs p
))))))
415 (let ((dum (semant pat
)))
417 (cntxt (ind (ncons dum
)) context
))))
419 (defun fact (fun arg val
)
420 (cntxt (ind (datum (list fun arg val
))) context
))
423 (setq y
(datum (list 'kind x y
)))
428 (setq y
(datum (list 'par s y
)))
430 (mapc #'(lambda (lis) (addf y lis
)) s
))
436 (mapc #'(lambda (lis) (ind1 dat lis
)) (cdar dat
))
437 (mapc #'ind2
(cdar dat
))
440 (defun ind1 (dat pat
)
441 (cond ((not (nodep pat
))
442 (mapc #'(lambda (lis) (ind1 dat lis
)) pat
))
443 ((or (markp pat
) (eq 'unknown pat
)))
445 (addf dat pat
) (mark pat
))))
453 (push+sto
(sel nd data
) (cons dat
(sel nd data
))))
455 (defun maxima-remf (dat nd
)
456 (push+sto
(sel nd data
) (fdel dat
(sel nd data
))))
458 (defun fdel (fact data
)
459 (cond ((and (eq (car fact
) (caaar data
))
460 (eq (cadr fact
) (cadaar data
))
461 (eq (caddr fact
) (caddar (car data
))))
464 (do ((ds data
(cdr ds
))
468 (cond ((and (eq (car fact
) (car d
))
469 (eq (cadr fact
) (cadr d
))
470 (eq (caddr fact
) (caddr d
)))
471 (push+sto
(sel d con data
) (delete d
(sel d con data
) :test
#'eq
))
472 (rplacd ds
(cddr ds
)) (return t
))))
475 (defun semantics (pat)
478 (list (semant pat
))))
484 (member (caar x
) '(rat bigfloat
) :test
#'eq
))))
487 (cond ((symbolp pat
) (or (get pat
'var
) pat
))
488 ((db-mnump pat
) (dintnum pat
))
489 (t (mapcar #'semant pat
))))
492 (cond ((mnump x
) (dintnum x
))
494 ((assol x dobjects
))))
497 (cond ((mnump x
) (dintnum x
))
500 (t (setq dobjects
(cons (dbnode x
) dobjects
))
503 (defun dintnum (x &aux foo
)
504 (cond ((assol x
*nobjects
*))
505 ((progn (setq x
(dbnode x
)) nil
))
507 (setq *nobjects
* (list x
))
509 ((eq '$zero
(setq foo
(rgrp (car x
) (caar *nobjects
*))))
510 (let ((context 'global
))
511 (fact 'meqp x
(car *nobjects
*)))
515 (let ((context 'global
))
516 (fact 'mgrp x
(car *nobjects
*)))
520 (do ((lis *nobjects
* (cdr lis
))
523 (let ((context 'global
))
524 (fact 'mgrp
(car lis
) x
))
525 (rplacd lis
(list x
)) x
)
526 (cond ((eq '$zero
(setq foo
(rgrp (car x
) (caadr lis
))))
527 (let ((context 'global
))
528 (fact 'meqp
(cadr lis
) x
))
529 (rplacd lis
(cons x
(cdr lis
)))
532 (let ((context 'global
))
533 (fact 'mgrp
(car lis
) x
)
534 (fact 'mgrp x
(cadr lis
)))
535 (rplacd lis
(cons x
(cdr lis
)))
539 (if (atom x
) x
(car x
)))
542 (kill (car pat
) (semant (cadr pat
)) (semant (caddr pat
))))
544 (defun kill (fun arg val
)
545 (kill2 fun arg val arg
)
546 (kill2 fun arg val val
))
548 (defun kill2 (fun arg val cl
)
549 (cond ((not (atom cl
)) (mapc #'(lambda (lis) (kill2 fun arg val lis
)) cl
))
551 (t (push+sto
(sel cl data
) (kill3 fun arg val
(sel cl data
))))))
553 (defun kill3 (fun arg val data
)
554 (cond ((and (eq fun
(caaar data
))
555 (eq arg
(cadaar data
))
556 (eq val
(caddar (car data
))))
559 (do ((ds data
(cdr ds
))
563 (cond ((not (and (eq fun
(car d
))
567 (t (push+sto
(sel d con data
) (delete d
(sel d con data
) :test
#'eq
))
568 (rplacd ds
(cddr ds
)) (return t
))))
572 (setq y
(car (datum (list 'kind x y
))))
577 (remov4 fact
(cadar fact
))
578 (remov4 fact
(caddar fact
)))
580 (defun remov4 (fact cl
)
581 (cond ((or (symbolp cl
) ;if CL is a symbol or
582 (and (consp cl
) ;an interned number, then we want to REMOV4 FACT
583 (mnump (car cl
)))) ;from its property list.
584 (push+sto
(sel cl data
) (delete fact
(sel cl data
) :test
#'eq
)))
585 ((or (atom cl
) (atom (car cl
)))) ;if CL is an atom (not a symbol)
586 ;or its CAR is an atom then we don't want to do
589 (mapc #'(lambda (lis) (remov4 fact lis
))
590 (cond ((atom (caar cl
)) (cdr cl
)) ;if CL's CAAR is
591 ;an atom, then CL is an expression, and
592 ;we want to REMOV4 FACT from the parts
594 ((atom (caaar cl
)) (cdar cl
)))))))
595 ;if CL's CAAAR is an atom, then CL is a
596 ;fact, and we want to REMOV4 FACT from
597 ;the parts of the fact.
599 (defun killframe (cl)
600 (mapc #'remov
(sel cl data
))
601 (zl-remprop cl
'+labs
)
602 (zl-remprop cl
'-labs
)
605 (zl-remprop cl
'fact
)
608 (defun activate (&rest l
)
610 (cond ((member e contexts
:test
#'eq
) nil
)
614 (defun deactivate (&rest l
)
616 (cond ((not (member e contexts
:test
#'eq
))
620 (setq contexts
(delete e contexts
:test
#'eq
))))))
624 (when (> *conindex
* *connumber
*)
627 (when (> *conindex
* *connumber
*)
628 (merror (intl:gettext
"context: too many contexts.")))))
634 (cond ((not (eq (aref conmark i
) (cdr (aref conunmrk i
))))
635 (killc (aref conmark i
)))
637 (setf (aref conunmrk
*conindex
*) (aref conunmrk i
))
638 (setf (aref conmark
*conindex
*) (aref conmark i
))
639 (incf *conindex
*)))))
641 (defun cntxt (dat con
)
643 (setq con
(cdr con
)))
644 (putprop con
(cons dat
(zl-get con
'data
)) 'data
)
645 (unless (eq 'global con
)
646 (putprop dat con
'con
))
649 (defun kcntxt (fact con
)
651 (setq con
(cdr con
)))
652 (putprop con
(fdel fact
(zl-get con
'data
)) 'data
)
653 (unless (eq 'global con
)
654 (zl-remprop fact
'con
))
658 (cond ((not (setq f
(sel f con
))))
659 ((setq f
(zl-get f
'cmark
))
662 (defun contextmark ()
664 (unless (eq current con
)
671 (setq con
(cdr con
)))
672 (let ((cm (zl-get con
'cmark
)))
673 (putprop con
(if cm
(1+ cm
) 1) 'cmark
)
674 (mapc #'cmark
(zl-get con
'subc
))))
678 (setq con
(cdr con
)))
679 (let ((cm (zl-get con
'cmark
)))
680 (cond (cm (putprop con
(1- cm
) 'cmark
)))
681 (mapc #'cunmrk
(zl-get con
'subc
))))
686 (mapc #'remov
(zl-get con
'data
))
687 (zl-remprop con
'data
)
688 (zl-remprop con
'cmark
)
689 (zl-remprop con
'subc
))
696 (cond ((setq x
(dq+))
698 (if (zerop (logand (unlab lab
) (unlab (-labz x
))))
703 (if (zerop (logand (unlab lab
) (unlab (+labz x
))))
708 (defun mark+ (cl lab
)
711 (format *trace-output
* "~%MARK+: marking ~A +" cl
)
713 (mapc #'(lambda (lis) (mark+0 cl lab lis
)) (sel cl data
)))
716 (if (/= 0 (logand (unlab (+labz
(caddar dat
)))
717 (unlab (dbv (+labz
(cadar dat
)) (-labz (caar dat
))))))
718 (beg- (sel dat wn
) *world
*)))
720 (defun mark+0 (cl lab fact
)
722 (format *trace-output
* "~%MARK+0: checking ~a from ~A+" (car fact
) cl
)
724 (cond ((onpu lab fact
))
726 ((null (sel fact wn
)) (mark+1 cl lab fact
))
727 ((onp (sel fact wn
) *world
*) (mark+1 cl lab fact
))
728 ((offp (sel fact wn
) *world
*) nil
)
731 (defun mark+1 (cl lab dat
)
732 (cond ((eq (caar dat
) 'kind
)
733 (if (eq (cadar dat
) cl
) (mid (caddar dat
) lab
))) ; E1
734 ((eq (caar dat
) 'par
)
735 (if (not (eq (caddar dat
) cl
))
737 (cancel lab dat
) ; PR1
738 (mid (caddar dat
) lab
)
739 (do ((lis (cadar dat
) (cdr lis
)))
741 (if (not (eq (car lis
) cl
))
742 (mid- (car lis
) lab
))))))
744 (if (+labs
(caar dat
)) ; V1
745 (end (caddar dat
) (dbv lab
(+labs
(caar dat
)))))
746 (if (-labs (caddar dat
)) ; F4
747 (end- (caar dat
) (lpr lab
(-labs (caddar dat
))))))))
749 (defun mark- (cl lab
)
752 (format *trace-output
* "~%MARK-: marking ~A -" cl
)
754 (mapc #'(lambda (lis) (mark-0 cl lab lis
)) (sel cl data
)))
756 (defun mark-0 (cl lab fact
)
758 (format *trace-output
* "~%MARK-0: checking ~A from ~A-" (car fact
) cl
)
760 (cond ((onpu lab fact
))
762 ((null (sel fact wn
)) (mark-1 cl lab fact
))
763 ((onp (sel fact wn
) *world
*) (mark-1 cl lab fact
))
764 ((offp (sel fact wn
) *world
*) nil
)))
766 (defun mark-1 (cl lab dat
)
767 (cond ((eq (caar dat
) 'kind
)
768 (if (not (eq (cadar dat
) cl
)) (mid- (cadar dat
) lab
))) ; E4
769 ((eq (caar dat
) 'par
)
770 (if (eq (caddar dat
) cl
)
772 (cancel lab dat
) ; S4
773 (do ((lis (cadar dat
) (cdr lis
)))
775 (mid- (car lis
) lab
)))
777 (setq lab
(unlab lab
)) ; ALL4
778 (do ((lis (cadar dat
) (cdr lis
)))
780 (setq lab
(logand (unlab (-labz (car lis
))) lab
)))
781 (setq lab
(copyn lab
))
783 (mid- (caddar dat
) lab
))))
784 ((eq (caddar dat
) cl
)
785 (if (+labs
(caar dat
)) ; A2
786 (end- (cadar dat
) (dba (+labs
(caar dat
)) lab
)))
787 (if (+labs
(cadar dat
)) ; F6
788 (end- (caar dat
) (lpr (+labs
(cadar dat
)) lab
))))))
790 ;; in out in out ins in out
791 ;; ----------- ------------- ----------------
792 ;; E1 | + INV1 | + AB1 |(+) + +
793 ;; E2 | - INV2 | - AB2 |(+) - +
794 ;; E3 | + INV3 | + AB3 |(+) + -
795 ;; E4 | - INV4 | - AB4 |(+) - -
797 ;; in out in out AB6 |(-) - +
798 ;; ----------- ------------- AB7 |(-) + -
799 ;; S1 | (+) ALL1 |(+) + AB8 |(-) - -
800 ;; S2 | (-) ALL2 |(+) -
801 ;; S3 |(+) ALL3 |(-) +
802 ;; S4 |(-) ALL4 |(-) -
806 ;; in rel out in rel out in rel out
807 ;; --------------- --------------- ---------------
808 ;; V1 | (+) + A1 | + (+) F1 | + (+)
809 ;; V2 | (+) - A2 | - (+) F2 | + (-)
810 ;; V3 | (-) + A3 | + (-) F3 | - (+)
811 ;; V4 | (-) - A4 | - (-) F4 | - (-)
817 (defun uni (p1 p2 al
)
818 (cond ((dbvarp p1
) (dbunivar p1 p2 al
))
820 (cond ((dbvarp p2
) (dbunivar p2 p1 al
))
821 ((nodep p2
) (if (eq p1 p2
) al
))))
822 ((dbvarp p2
) (dbunivar p2 p1 al
))
824 ((setq al
(uni (car p1
) (car p2
) al
))
825 (uni (cdr p1
) (cdr p2
) al
))))
827 (defun dbunivar (p v al
)
828 (let ((dum (assoc p al
:test
#'eq
)))
831 (uni (cdr dum
) v al
))))