1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
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
) (zl-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 (defmfun 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))
383 (defmfun falsep
(pat)
385 (cond ((eq (car pat
) 'kind
)
391 (let ((isp 'unknown
))
401 (merror (intl:gettext
"declare: argument must be a symbol; found ~M") x
))
404 (do ((p (dq+) (dq+)))
408 (mark+ p
(+labs p
)))))
411 (let ((dum (semant pat
)))
413 (cntxt (ind (ncons dum
)) context
))))
415 (defmfun fact
(fun arg val
)
416 (cntxt (ind (datum (list fun arg val
))) context
))
418 (defmfun kind
(x y
&aux
#+kcl
(y y
))
419 (setq y
(datum (list 'kind x y
)))
424 (setq y
(datum (list 'par s y
)))
426 (mapc #'(lambda (lis) (addf y lis
)) s
))
432 (mapc #'(lambda (lis) (ind1 dat lis
)) (cdar dat
))
433 (mapc #'ind2
(cdar dat
))
436 (defun ind1 (dat pat
)
437 (cond ((not (nodep pat
))
438 (mapc #'(lambda (lis) (ind1 dat lis
)) pat
))
439 ((or (markp pat
) (eq 'unknown pat
)))
441 (addf dat pat
) (mark pat
))))
448 (defmfun addf
(dat nd
)
449 (push+sto
(sel nd data
) (cons dat
(sel nd data
))))
451 (defmfun maxima-remf
(dat nd
)
452 (push+sto
(sel nd data
) (fdel dat
(sel nd data
))))
454 (defun fdel (fact data
)
455 (cond ((and (eq (car fact
) (caaar data
))
456 (eq (cadr fact
) (cadaar data
))
457 (eq (caddr fact
) (caddar (car data
))))
460 (do ((ds data
(cdr ds
))
464 (cond ((and (eq (car fact
) (car d
))
465 (eq (cadr fact
) (cadr d
))
466 (eq (caddr fact
) (caddr d
)))
467 (push+sto
(sel d con data
) (delete d
(sel d con data
) :test
#'eq
))
468 (rplacd ds
(cddr ds
)) (return t
))))
471 (defun semantics (pat)
474 (list (semant pat
))))
480 (member (caar x
) '(rat bigfloat
) :test
#'eq
))))
483 (cond ((symbolp pat
) (or (zl-get pat
'var
) pat
))
484 ((db-mnump pat
) (dintnum pat
))
485 (t (mapcar #'semant pat
))))
487 (defmfun dinternp
(x)
488 (cond ((mnump x
) (dintnum x
))
490 ((assol x dobjects
))))
493 (cond ((mnump x
) (dintnum x
))
496 (t (setq dobjects
(cons (dbnode x
) dobjects
))
499 (defun dintnum (x &aux foo
)
500 (cond ((assol x
*nobjects
*))
501 ((progn (setq x
(dbnode x
)) nil
))
503 (setq *nobjects
* (list x
))
505 ((eq '$zero
(setq foo
(rgrp (car x
) (caar *nobjects
*))))
506 (let ((context 'global
))
507 (fact 'meqp x
(car *nobjects
*)))
511 (let ((context 'global
))
512 (fact 'mgrp x
(car *nobjects
*)))
516 (do ((lis *nobjects
* (cdr lis
))
519 (let ((context 'global
))
520 (fact 'mgrp
(car lis
) x
))
521 (rplacd lis
(list x
)) x
)
522 (cond ((eq '$zero
(setq foo
(rgrp (car x
) (caadr lis
))))
523 (let ((context 'global
))
524 (fact 'meqp
(cadr lis
) x
))
525 (rplacd lis
(cons x
(cdr lis
)))
528 (let ((context 'global
))
529 (fact 'mgrp
(car lis
) x
)
530 (fact 'mgrp x
(cadr lis
)))
531 (rplacd lis
(cons x
(cdr lis
)))
535 (if (atom x
) x
(car x
)))
537 (defmfun untrue
(pat)
538 (kill (car pat
) (semant (cadr pat
)) (semant (caddr pat
))))
540 (defmfun kill
(fun arg val
)
541 (kill2 fun arg val arg
)
542 (kill2 fun arg val val
))
544 (defun kill2 (fun arg val cl
)
545 (cond ((not (atom cl
)) (mapc #'(lambda (lis) (kill2 fun arg val lis
)) cl
))
547 (t (push+sto
(sel cl data
) (kill3 fun arg val
(sel cl data
))))))
549 (defun kill3 (fun arg val data
)
550 (cond ((and (eq fun
(caaar data
))
551 (eq arg
(cadaar data
))
552 (eq val
(caddar (car data
))))
555 (do ((ds data
(cdr ds
))
559 (cond ((not (and (eq fun
(car d
))
563 (t (push+sto
(sel d con data
) (delete d
(sel d con data
) :test
#'eq
))
564 (rplacd ds
(cddr ds
)) (return t
))))
567 (defmfun unkind
(x y
)
568 (setq y
(car (datum (list 'kind x y
))))
572 (defmfun remov
(fact)
573 (remov4 fact
(cadar fact
))
574 (remov4 fact
(caddar fact
)))
576 (defun remov4 (fact cl
)
577 (cond ((or (symbolp cl
) ;if CL is a symbol or
578 (and (consp cl
) ;an interned number, then we want to REMOV4 FACT
579 (mnump (car cl
)))) ;from its property list.
580 (push+sto
(sel cl data
) (delete fact
(sel cl data
) :test
#'eq
)))
581 ((or (atom cl
) (atom (car cl
)))) ;if CL is an atom (not a symbol)
582 ;or its CAR is an atom then we don't want to do
585 (mapc #'(lambda (lis) (remov4 fact lis
))
586 (cond ((atom (caar cl
)) (cdr cl
)) ;if CL's CAAR is
587 ;an atom, then CL is an expression, and
588 ;we want to REMOV4 FACT from the parts
590 ((atom (caaar cl
)) (cdar cl
)))))))
591 ;if CL's CAAAR is an atom, then CL is a
592 ;fact, and we want to REMOV4 FACT from
593 ;the parts of the fact.
595 (defmfun killframe
(cl)
596 (mapc #'remov
(sel cl data
))
597 (zl-remprop cl
'+labs
)
598 (zl-remprop cl
'-labs
)
601 (zl-remprop cl
'fact
)
604 (defmfun activate
(&rest l
)
606 (cond ((member e contexts
:test
#'eq
) nil
)
610 (defmfun deactivate
(&rest l
)
612 (cond ((not (member e contexts
:test
#'eq
))
616 (setq contexts
(delete e contexts
:test
#'eq
))))))
620 (when (> *conindex
* *connumber
*)
623 (when (> *conindex
* *connumber
*)
624 (merror (intl:gettext
"context: too many contexts.")))))
630 (cond ((not (eq (aref conmark i
) (cdr (aref conunmrk i
))))
631 (killc (aref conmark i
)))
633 (setf (aref conunmrk
*conindex
*) (aref conunmrk i
))
634 (setf (aref conmark
*conindex
*) (aref conmark i
))
635 (incf *conindex
*)))))
637 (defmfun cntxt
(dat con
)
639 (setq con
(cdr con
)))
640 (putprop con
(cons dat
(zl-get con
'data
)) 'data
)
641 (unless (eq 'global con
)
642 (putprop dat con
'con
))
645 (defmfun kcntxt
(fact con
)
647 (setq con
(cdr con
)))
648 (putprop con
(fdel fact
(zl-get con
'data
)) 'data
)
649 (unless (eq 'global con
)
650 (zl-remprop fact
'con
))
654 (cond ((not (setq f
(sel f con
))))
655 ((setq f
(zl-get f
'cmark
))
658 (defmfun contextmark
()
660 (unless (eq current con
)
667 (setq con
(cdr con
)))
668 (let ((cm (zl-get con
'cmark
)))
669 (putprop con
(if cm
(1+ cm
) 1) 'cmark
)
670 (mapc #'cmark
(zl-get con
'subc
))))
674 (setq con
(cdr con
)))
675 (let ((cm (zl-get con
'cmark
)))
676 (cond (cm (putprop con
(1- cm
) 'cmark
)))
677 (mapc #'cunmrk
(zl-get con
'subc
))))
682 (mapc #'remov
(zl-get con
'data
))
683 (zl-remprop con
'data
)
684 (zl-remprop con
'cmark
)
685 (zl-remprop con
'subc
))
692 (cond ((setq x
(dq+))
694 (if (zerop (logand (unlab lab
) (unlab (-labz x
))))
699 (if (zerop (logand (unlab lab
) (unlab (+labz x
))))
704 (defun mark+ (cl lab
)
707 (format *trace-output
* "~%MARK+: marking ~A +" cl
)
709 (mapc #'(lambda (lis) (mark+0 cl lab lis
)) (sel cl data
)))
712 (if (/= 0 (logand (unlab (+labz
(caddar dat
)))
713 (unlab (dbv (+labz
(cadar dat
)) (-labz (caar dat
))))))
714 (beg- (sel dat wn
) *world
*)))
716 (defun mark+0 (cl lab fact
)
718 (format *trace-output
* "~%MARK+0: checking ~a from ~A+" (car fact
) cl
)
720 (cond ((onpu lab fact
))
722 ((null (sel fact wn
)) (mark+1 cl lab fact
))
723 ((onp (sel fact wn
) *world
*) (mark+1 cl lab fact
))
724 ((offp (sel fact wn
) *world
*) nil
)
727 (defun mark+1 (cl lab dat
)
728 (cond ((eq (caar dat
) 'kind
)
729 (if (eq (cadar dat
) cl
) (mid (caddar dat
) lab
))) ; E1
730 ((eq (caar dat
) 'par
)
731 (if (not (eq (caddar dat
) cl
))
733 (cancel lab dat
) ; PR1
734 (mid (caddar dat
) lab
)
735 (do ((lis (cadar dat
) (cdr lis
)))
737 (if (not (eq (car lis
) cl
))
738 (mid- (car lis
) lab
))))))
740 (if (+labs
(caar dat
)) ; V1
741 (end (caddar dat
) (dbv lab
(+labs
(caar dat
)))))
742 (if (-labs (caddar dat
)) ; F4
743 (end- (caar dat
) (lpr lab
(-labs (caddar dat
))))))))
745 (defun mark- (cl lab
)
748 (format *trace-output
* "~%MARK-: marking ~A -" cl
)
750 (mapc #'(lambda (lis) (mark-0 cl lab lis
)) (sel cl data
)))
752 (defun mark-0 (cl lab fact
)
754 (format *trace-output
* "~%MARK-0: checking ~A from ~A-" (car fact
) cl
)
756 (cond ((onpu lab fact
))
758 ((null (sel fact wn
)) (mark-1 cl lab fact
))
759 ((onp (sel fact wn
) *world
*) (mark-1 cl lab fact
))
760 ((offp (sel fact wn
) *world
*) nil
)))
762 (defun mark-1 (cl lab dat
)
763 (cond ((eq (caar dat
) 'kind
)
764 (if (not (eq (cadar dat
) cl
)) (mid- (cadar dat
) lab
))) ; E4
765 ((eq (caar dat
) 'par
)
766 (if (eq (caddar dat
) cl
)
768 (cancel lab dat
) ; S4
769 (do ((lis (cadar dat
) (cdr lis
)))
771 (mid- (car lis
) lab
)))
773 (setq lab
(unlab lab
)) ; ALL4
774 (do ((lis (cadar dat
) (cdr lis
)))
776 (setq lab
(logand (unlab (-labz (car lis
))) lab
)))
777 (setq lab
(copyn lab
))
779 (mid- (caddar dat
) lab
))))
780 ((eq (caddar dat
) cl
)
781 (if (+labs
(caar dat
)) ; A2
782 (end- (cadar dat
) (dba (+labs
(caar dat
)) lab
)))
783 (if (+labs
(cadar dat
)) ; F6
784 (end- (caar dat
) (lpr (+labs
(cadar dat
)) lab
))))))
786 ;; in out in out ins in out
787 ;; ----------- ------------- ----------------
788 ;; E1 | + INV1 | + AB1 |(+) + +
789 ;; E2 | - INV2 | - AB2 |(+) - +
790 ;; E3 | + INV3 | + AB3 |(+) + -
791 ;; E4 | - INV4 | - AB4 |(+) - -
793 ;; in out in out AB6 |(-) - +
794 ;; ----------- ------------- AB7 |(-) + -
795 ;; S1 | (+) ALL1 |(+) + AB8 |(-) - -
796 ;; S2 | (-) ALL2 |(+) -
797 ;; S3 |(+) ALL3 |(-) +
798 ;; S4 |(-) ALL4 |(-) -
802 ;; in rel out in rel out in rel out
803 ;; --------------- --------------- ---------------
804 ;; V1 | (+) + A1 | + (+) F1 | + (+)
805 ;; V2 | (+) - A2 | - (+) F2 | + (-)
806 ;; V3 | (-) + A3 | + (-) F3 | - (+)
807 ;; V4 | (-) - A4 | - (-) F4 | - (-)
813 (defun uni (p1 p2 al
)
814 (cond ((dbvarp p1
) (dbunivar p1 p2 al
))
816 (cond ((dbvarp p2
) (dbunivar p2 p1 al
))
817 ((nodep p2
) (if (eq p1 p2
) al
))))
818 ((dbvarp p2
) (dbunivar p2 p1 al
))
820 ((setq al
(uni (car p1
) (car p2
) al
))
821 (uni (cdr p1
) (cdr p2
) al
))))
823 (defun dbunivar (p v al
)
824 (let ((dum (assoc p al
:test
#'eq
)))
827 (uni (cdr dum
) v al
))))