home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e051 / 3.ddi / COMMON / OPS.LSP < prev    next >
Encoding:
Lisp/Scheme  |  1980-01-01  |  111.8 KB  |  4,055 lines

  1. truncate  k 10000.))        ;use multiple-value-setq???
  2.     (setq sub (- k (* ce 10000.)))        ;@@@ ^
  3.     
  4.     celoop (and (eq ce 0.) (go ph2))
  5.     (setq x (cdr x))
  6.     (and (eq ce 1.) (go ph2))
  7.     (setq x (cdr x))
  8.     (and (eq ce 2.) (go ph2))
  9.     (setq x (cdr x))
  10.     (and (eq ce 3.) (go ph2))
  11.     (setq x (cdr x))
  12.     (and (eq ce 4.) (go ph2))
  13.     (setq ce (- ce 4.))
  14.     (go celoop)
  15.     ph2  (setq x (car x))
  16.     subloop (and (eq sub 0.) (go finis))
  17.     (setq x (cdr x))
  18.     (and (eq sub 1.) (go finis))
  19.     (setq x (cdr x))
  20.     (and (eq sub 2.) (go finis))
  21.     (setq x (cdr x))
  22.     (and (eq sub 3.) (go finis))
  23.     (setq x (cdr x))
  24.     (and (eq sub 4.) (go finis))
  25.     (setq x (cdr x))
  26.     (and (eq sub 5.) (go finis))
  27.     (setq x (cdr x))
  28.     (and (eq sub 6.) (go finis))
  29.     (setq x (cdr x))
  30.     (and (eq sub 7.) (go finis))
  31.     (setq x (cdr x))
  32.     (and (eq sub 8.) (go finis))
  33.     (setq sub (- sub 8.))
  34.     (go subloop)
  35.     finis (return (car x))) ) ;  )      ;end prog,< locally >, defun
  36.  
  37.  
  38. ;;; Utility functions
  39.  
  40.  
  41.        
  42. ;;; intersect two lists using eq for the equality test
  43. (defun interq (x y)
  44.   (cond ((atom x) nil)
  45.     ((member (car x) y) (cons (car x) (interq (cdr x) y)))
  46.     (t (interq (cdr x) y)))) 
  47.  
  48.  
  49. (eval-when (compile load eval)
  50.   (set-macro-character #\{ #'(lambda (s c)
  51.                    (declare (ignore s c))
  52.                    '\{))   ;5/5/83
  53.   (set-macro-character #\} #'(lambda (s c)
  54.                    (declare (ignore s c))
  55.                    '\}))   ;5/5/83
  56.   (set-macro-character #\^ #'(lambda (s c)
  57.                    (declare (ignore s c))
  58.                    '\^))   ;5/5/83
  59.   )
  60.  
  61.  
  62. (defun i-g-v nil
  63.   (prog (x)
  64.     ;    (sstatus translink t)   ;5/24/83
  65.     ;@@@ moved set-macros out
  66.     ;### kluge
  67.     (setq *buckets* 127.)        ; regular OPS5 allows 64 named slots
  68.     (setq *accept-file* nil)
  69.     (setq *write-file* nil)
  70.     (setq *trace-file* nil)
  71.     (setq *class-list* nil)
  72.     (setq *brkpts* nil)
  73.     (setq *strategy* 'lex)
  74.     (setq *in-rhs* nil)
  75.     (setq *ptrace* t)
  76.     (setq *wtrace* nil)
  77.     (setq *recording* nil)
  78.     (setq *refracts* nil)
  79.     (setq *real-cnt* (setq *virtual-cnt* 0.))
  80.     (setq *max-cs* (setq *total-cs* 0.))
  81.     (setq *limit-token* 1000000.)
  82.     (setq *limit-cs* 1000000.)
  83.     (setq *critical* nil)
  84.     (setq *build-trace* nil)
  85.     (setq *wmpart-list* nil)
  86.     (setq *size-result-array* 255.)
  87.     ;255 /256 set by gdw
  88.     (setq *result-array* (make-array 256 :initial-element ()));jgk
  89.     (setq *record-array* (make-array 256 :initial-element ()));jgk
  90.     (setq x 0)
  91.     loop    (putvector *result-array* x nil)
  92.     (setq x (1+ x))                ;"plus" changed to "+" by gdw
  93.     (and (not (> x *size-result-array*)) (go loop))
  94.     (make-bottom-node)
  95.     (setq *pcount* 0.)
  96.     (initialize-record)
  97.     (setq *cycle-count* (setq *action-count* 0.))
  98.     (setq *total-token*
  99.       (setq *max-token* (setq *current-token* 0.)))
  100.     (setq *total-cs* (setq *max-cs* 0.))
  101.     (setq *total-wm* (setq *max-wm* (setq *current-wm* 0.)))
  102.     (setq *conflict-set* nil)
  103.     (setq *wmpart-list* nil)
  104.     (setq *p-name* nil)
  105.     (setq *remaining-cycles* 1000000)))
  106.  
  107. ; if the size of result-array changes, change the line in i-g-v which
  108. ; sets the value of *size-result-array*
  109.  
  110. (defun %warn (what where)
  111.   (prog nil
  112.     (terpri)
  113.     (princ '?)
  114.     (and *p-name* (princ *p-name*))
  115.     (princ '|..|)
  116.     (princ where)
  117.     (princ '|..|)
  118.     (princ what)
  119.     (return where))) 
  120.  
  121. (defun %error (what where)
  122.   (%warn what where)
  123.   (throw '!error! '!error!))     ;jgk quoted arguments
  124.  
  125. ;@@@(defun round (x) (fix (+ 0.5 x)))         ;"plus" changed to "+" by gdw
  126. ;@@@ removed; calls converted to native clisp (round)
  127.  
  128. (defun top-levels-eq (la lb)
  129.   (prog nil
  130.     lx   (cond ((eq la lb) (return t))
  131.            ((null la) (return nil))
  132.            ((null lb) (return nil))
  133.            ((not (eq (car la) (car lb))) (return nil)))
  134.     (setq la (cdr la))
  135.     (setq lb (cdr lb))
  136.     (go lx))) 
  137.  
  138.  
  139. ;;; LITERAL and LITERALIZE
  140.  
  141. (defun ops-literal (z)
  142.   (prog (atm val old)
  143.     top  (and (atom z) (return 'bound))
  144.     (or (eq (cadr z) '=) (return (%warn '|wrong format| z)))
  145.     (setq atm (car z))
  146.     (setq val (caddr z))
  147.     (setq z (cdddr z))
  148.     (cond ((not (numberp val))
  149.        (%warn '|can bind only to numbers| val))
  150.       ((or (not (symbolp atm)) (variablep atm))
  151.        (%warn '|can bind only constant atoms| atm))
  152.       ((and (setq old (literal-binding-of atm)) (not (equal old val)))
  153.        (%warn '|attempt to rebind attribute| atm))
  154.       (t (putprop atm val 'ops-bind)))
  155.     (go top))) 
  156.  
  157.  
  158. (defun ops-literalize (l)
  159.   (prog (class-name atts)
  160.     (setq class-name (car l))
  161.     (cond ((have-compiled-production)
  162.        (%warn '|literalize called after p| class-name)
  163.        (return nil))
  164.       ((get class-name 'att-list)
  165.        (%warn '|attempt to redefine class| class-name)
  166.        (return nil)))
  167.     (setq *class-list* (cons class-name *class-list*))
  168.     (setq atts (remove-duplicates (cdr l)))        ; ??? should this
  169.     ; warn of dup atts?
  170.     (test-attribute-names atts)
  171.     (mark-conflicts atts atts)
  172.     (putprop class-name atts 'att-list))) 
  173.  
  174. (defun ops-vector-attribute (l)
  175.   (cond ((have-compiled-production)
  176.      (%warn '|vector-attribute called after p| l))
  177.     (t 
  178.      (test-attribute-names l)
  179.      (mapc (function vector-attribute2) l)))) 
  180.  
  181. (defun vector-attribute2 (att) (putprop att t 'vector-attribute))
  182.  
  183. (defun is-vector-attribute (att) (get att 'vector-attribute))
  184.  
  185. (defun test-attribute-names (l)
  186.   (mapc (function test-attribute-names2) l)) 
  187.  
  188. (defun test-attribute-names2 (atm)
  189.   (cond ((or (not (symbolp atm)) (variablep atm))
  190.      (%warn '|can bind only constant atoms| atm)))) 
  191.  
  192. (defun finish-literalize nil
  193.   (cond ((not (null *class-list*))
  194.      (mapc (function note-user-assigns) *class-list*)
  195.      (mapc (function assign-scalars) *class-list*)
  196.      (mapc (function assign-vectors) *class-list*)
  197.      (mapc (function put-ppdat) *class-list*)
  198.      (mapc (function erase-literal-info) *class-list*)
  199.      (setq *class-list* nil)
  200.      (setq *buckets* nil)))) 
  201.  
  202. (defun have-compiled-production nil (not (zerop *pcount*))) 
  203.        
  204. (defun put-ppdat (class)
  205.   (prog (al att ppdat)
  206.     (setq ppdat nil)
  207.     (setq al (get class 'att-list))
  208.     top  (cond ((not (atom al))
  209.         (setq att (car al))
  210.         (setq al (cdr al))
  211.         (setq ppdat
  212.               (cons (cons (literal-binding-of att) att)
  213.                 ppdat))
  214.         (go top)))
  215.     (putprop class ppdat 'ppdat))) 
  216.  
  217. ; note-user-assigns and note-user-vector-assigns are needed only when
  218. ; literal and literalize are both used in a program.  They make sure that
  219. ; the assignments that are made explicitly with literal do not cause problems
  220. ; for the literalized classes.
  221.  
  222. (defun note-user-assigns (class)
  223.   (mapc (function note-user-assigns2) (get class 'att-list)))
  224.  
  225. (defun note-user-assigns2 (att)
  226.   (prog (num conf buck clash)
  227.     (setq num (literal-binding-of att))
  228.     (and (null num) (return nil))
  229.     (setq conf (get att 'conflicts))
  230.     (setq buck (store-binding att num))
  231.     (setq clash (find-common-atom buck conf))
  232.     (and clash
  233.      (%warn '|attributes in a class assigned the same number|
  234.         (cons att clash)))
  235.     (return nil)))
  236.  
  237. (defun note-user-vector-assigns (att given needed)
  238.   (and (> needed given)
  239.        (%warn '|vector attribute assigned too small a value in literal| att)))
  240.  
  241. (defun assign-scalars (class)
  242.   (mapc (function assign-scalars2) (get class 'att-list))) 
  243.  
  244. (defun assign-scalars2 (att)
  245.   (prog (tlist num bucket conf)
  246.     (and (literal-binding-of att) (return nil))
  247.     (and (is-vector-attribute att) (return nil))
  248.     (setq tlist (buckets))
  249.     (setq conf (get att 'conflicts))
  250.     top  (cond ((atom tlist)
  251.         (%warn '|could not generate a binding| att)
  252.         (store-binding att -1.)
  253.         (return nil)))
  254.     (setq num (caar tlist))
  255.     (setq bucket (cdar tlist))
  256.     (setq tlist (cdr tlist))
  257.     (cond ((disjoint bucket conf) (store-binding att num))
  258.       (t (go top))))) 
  259.  
  260. (defun assign-vectors (class)
  261.   (mapc (function assign-vectors2) (get class 'att-list))) 
  262.  
  263. (defun assign-vectors2 (att)
  264.   (prog (big conf new old need)
  265.     (and (not (is-vector-attribute att)) (return nil))
  266.     (setq big 1.)
  267.     (setq conf (get att 'conflicts))
  268.     top  (cond ((not (atom conf))
  269.         (setq new (car conf))
  270.         (setq conf (cdr conf))
  271.         (cond ((is-vector-attribute new)
  272.                (%warn '|class has two vector attributes|
  273.                   (list att new)))
  274.               (t (setq big (max (literal-binding-of new) big))))
  275.         (go top)))
  276.     (setq need (1+ big))            ;"plus" changed to "+" by gdw
  277.     (setq old (literal-binding-of att))
  278.     (cond (old (note-user-vector-assigns att old need))
  279.       (t (store-binding att need)))
  280.     (return nil)))
  281.  
  282. (defun disjoint (la lb) (not (find-common-atom la lb))) 
  283.  
  284. (defun find-common-atom (la lb)
  285.   (prog nil
  286.     top  (cond ((null la) (return nil))
  287.            ((member (car la) lb) (return (car la)))
  288.            (t (setq la (cdr la)) (go top))))) 
  289.  
  290. (defun mark-conflicts (rem all)
  291.   (cond ((not (null rem))
  292.      (mark-conflicts2 (car rem) all)
  293.      (mark-conflicts (cdr rem) all)))) 
  294.  
  295. (defun mark-conflicts2 (atm lst)
  296.   (prog (l)
  297.     (setq l lst)
  298.     top  (and (atom l) (return nil))
  299.     (conflict atm (car l))
  300.     (setq l (cdr l))
  301.     (go top))) 
  302.  
  303. (defun conflict (a b)
  304.   (prog (old)
  305.     (setq old (get a 'conflicts))
  306.     (and (not (eq a b))
  307.      (not (member b old))
  308.      (putprop a (cons b old) 'conflicts)))) 
  309.  
  310. ;@@@ use intrinsic 
  311. ;(defun remove-duplicates  (lst)
  312.    ;  (cond ((atom lst) nil)
  313.         ;        ((member (car lst) (cdr lst)) (remove-duplicates (cdr lst)))
  314.         ;        (t (cons (car lst) (remove-duplicates (cdr lst)))))) 
  315.  
  316. (defun literal-binding-of (name) (get name 'ops-bind)) 
  317.  
  318. (defun store-binding (name lit)
  319.   (putprop name lit 'ops-bind)
  320.   (add-bucket name lit)) 
  321.  
  322. (defun add-bucket (name num)
  323.   (prog (buc)
  324.     (setq buc (assoc num (buckets)))
  325.     (and (not (member name buc))
  326.      (rplacd buc (cons name (cdr buc))))
  327.     (return buc))) 
  328.  
  329. (defun buckets nil
  330.   (and (atom *buckets*) (setq *buckets* (make-nums *buckets*)))
  331.   *buckets*) 
  332.  
  333. (defmacro ncons (x) `(cons ,x nil))
  334.  
  335. (defun make-nums (k)
  336.   (prog (nums)
  337.     (setq nums nil)
  338.     l    (and (< k 2.) (return nums))
  339.     (setq nums (cons (ncons k) nums))
  340.     (setq k (1- k))
  341.     (go l))) 
  342.  
  343. (defun erase-literal-info (class)
  344.   (mapc (function erase-literal-info2) (get class 'att-list))
  345.   (remprop class 'att-list)) 
  346.  
  347. (defun erase-literal-info2 (att) (remprop att 'conflicts)) 
  348.  
  349.  
  350. ;;; LHS Compiler
  351.  
  352. (defun ops-p (z) 
  353.   (finish-literalize)
  354.   (princ '*) 
  355.   ;(drain) commented out temporarily
  356.   (force-output)            ;@@@ clisp drain?
  357.   (compile-production (car z) (cdr z))) 
  358.  
  359. (defun compile-production (name matrix) ;jgk inverted args to catch 
  360.   (prog (erm)                ;and quoted tag
  361.     (setq *p-name* name)
  362.     (setq erm (catch '!error! (cmp-p name matrix)))
  363.     (setq *p-name* nil)))
  364.  
  365. (defun peek-lex nil (car *matrix*)) 
  366.  
  367. (defun lex nil
  368.   (prog2 nil (car *matrix*) (setq *matrix* (cdr *matrix*)))) 
  369.  
  370. (defun end-of-p nil (atom *matrix*)) 
  371.  
  372. (defun rest-of-p nil *matrix*) 
  373.  
  374. (defun prepare-lex (prod) (setq *matrix* prod)) 
  375.  
  376.  
  377. (defun peek-sublex nil (car *curcond*)) 
  378.  
  379. (defun sublex nil
  380.   (prog2 nil (car *curcond*) (setq *curcond* (cdr *curcond*)))) 
  381.  
  382. (defun end-of-ce nil (atom *curcond*)) 
  383.  
  384. (defun rest-of-ce nil *curcond*) 
  385.  
  386. (defun prepare-sublex (ce) (setq *curcond* ce)) 
  387.  
  388. (defun make-bottom-node nil (setq *first-node* (list '&bus nil))) 
  389.  
  390. (defun cmp-p (name matrix)
  391.   (prog (m bakptrs)
  392.     (cond ((or (null name) (consp  name))    ;dtpr\consp gdw
  393.        (%error '|illegal production name| name))
  394.       ((equal (get name 'production) matrix)
  395.        (return nil)))
  396.     (prepare-lex matrix)
  397.     (excise-p name)
  398.     (setq bakptrs nil)
  399.     (setq *pcount* (1+ *pcount*))        ;"plus" changed to "+" by gdw
  400.     (setq *feature-count* 0.)
  401.     (setq *ce-count* 0)
  402.     (setq *vars* nil)
  403.     (setq *ce-vars* nil)
  404.     (setq *rhs-bound-vars* nil)
  405.     (setq *rhs-bound-ce-vars* nil)
  406.     (setq *last-branch* nil)
  407.     (setq m (rest-of-p))
  408.     l1   (and (end-of-p) (%error '|no '-->' in production| m))
  409.     (cmp-prin)
  410.     (setq bakptrs (cons *last-branch* bakptrs))
  411.     (or (eq '--> (peek-lex)) (go l1))
  412.     (lex)
  413.     (check-rhs (rest-of-p))
  414.     (link-new-node (list '&p
  415.              *feature-count*
  416.              name
  417.              (encode-dope)
  418.              (encode-ce-dope)
  419.              (cons 'progn (rest-of-p))))
  420.     (putprop name (cdr (nreverse bakptrs)) 'backpointers)
  421.     (putprop name matrix 'production)
  422.     (putprop name *last-node* 'topnode))) 
  423.  
  424. (defun rating-part (pnode) (cadr pnode)) 
  425.  
  426. (defun var-part (pnode) (car (cdddr pnode))) 
  427.  
  428. (defun ce-var-part (pnode) (cadr (cdddr pnode))) 
  429.  
  430. (defun rhs-part (pnode) (caddr (cdddr pnode))) 
  431.  
  432. (defun excise-p (name)
  433.   (cond ((and (symbolp name) (get name 'topnode))
  434.      (format t "~S is excised~%" name)
  435.      (setq *pcount* (1- *pcount*))
  436.      (remove-from-conflict-set name)
  437.      (kill-node (get name 'topnode))
  438.      (remprop name 'production)
  439.      (remprop name 'backpointers)
  440.      (remprop name 'topnode)))) 
  441.  
  442. (defun kill-node (node)
  443.   (prog nil
  444.     top  (and (atom node) (return nil))
  445.     (rplaca node '&old)
  446.     (setq node (cdr node))
  447.     (go top))) 
  448.  
  449. (defun cmp-prin nil
  450.   (prog nil
  451.     (setq *last-node* *first-node*)
  452.     (cond ((null *last-branch*) (cmp-posce) (cmp-nobeta))
  453.       ((eq (peek-lex) '-) (cmp-negce) (cmp-not))
  454.       (t (cmp-posce) (cmp-and))))) 
  455.  
  456. (defun cmp-negce nil (lex) (cmp-ce)) 
  457.  
  458. (defun cmp-posce nil
  459.   (setq *ce-count* (1+ *ce-count*))        ;"plus" changed to "+" by gdw
  460.   (cond ((eq (peek-lex) '\{) (cmp-ce+cevar))    ;"plus" changed to "+" by gdw
  461.     (t (cmp-ce)))) 
  462.  
  463. (defun cmp-ce+cevar nil
  464.   (prog (z)
  465.     (lex)
  466.     (cond ((atom (peek-lex)) (cmp-cevar) (cmp-ce))
  467.       (t (cmp-ce) (cmp-cevar)))
  468.     (setq z (lex))
  469.     (or (eq z '\}) (%error '|missing '}'| z)))) 
  470.  
  471. (defun new-subnum (k)
  472.   (or (numberp k) (%error '|tab must be a number| k))
  473.   (setq *subnum* (fix k))) 
  474.  
  475. (defun incr-subnum nil (setq *subnum* (1+ *subnum*))) 
  476.  
  477. (defun cmp-ce nil
  478.   (prog (z)
  479.     (new-subnum 0.)
  480.     (setq *cur-vars* nil)
  481.     (setq z (lex))
  482.     (and (atom z)
  483.      (%error '|atomic conditions are not allowed| z))
  484.     (prepare-sublex z)
  485.     la   (and (end-of-ce) (return nil))
  486.     (incr-subnum)
  487.     (cmp-element)
  488.     (go la))) 
  489.  
  490. (defun cmp-element nil
  491.   (and (eq (peek-sublex) '^) (cmp-tab))
  492.   (cond ((eq (peek-sublex) '\{) (cmp-product))
  493.     (t (cmp-atomic-or-any))))
  494.  
  495. (defun cmp-atomic-or-any nil
  496.   (cond ((eq (peek-sublex) '<<) (cmp-any))
  497.     (t (cmp-atomic))))
  498.  
  499. (defun cmp-any nil
  500.   (prog (a z)
  501.     (sublex)
  502.     (setq z nil)
  503.     la   (cond ((end-of-ce) (%error '|missing '>>'| a)))
  504.     (setq a (sublex))
  505.     (cond ((not (eq '>> a)) (setq z (cons a z)) (go la)))
  506.     (link-new-node (list '&any nil (current-field) z)))) 
  507.  
  508.  
  509. (defun cmp-tab nil
  510.   (prog (r)
  511.     (sublex)
  512.     (setq r (sublex))
  513.     (setq r ($litbind r))
  514.     (new-subnum r))) 
  515.  
  516. (defun $litbind (x)
  517.   (prog (r)
  518.     (cond ((and (symbolp x) (setq r (literal-binding-of x)))
  519.        (return r))
  520.       (t (return x))))) 
  521.  
  522. (defun get-bind (x)
  523.   (prog (r)
  524.     (cond ((and (symbolp x) (setq r (literal-binding-of x)))
  525.        (return r))
  526.       (t (return nil))))) 
  527.  
  528. (defun cmp-atomic nil
  529.   (prog (test x)
  530.     (setq x (peek-sublex))
  531.     (cond ((eq x '= ) (setq test 'eq) (sublex))
  532.       ((eq x '<>) (setq test 'ne) (sublex))
  533.       ((eq x '<) (setq test 'lt) (sublex))
  534.       ((eq x '<=) (setq test 'le) (sublex))
  535.       ((eq x '>) (setq test 'gt) (sublex))
  536.       ((eq x '>=) (setq test 'ge) (sublex))
  537.       ((eq x '<=>) (setq test 'xx) (sublex))
  538.       (t (setq test 'eq)))
  539.     (cmp-symbol test))) 
  540.  
  541. (defun cmp-product nil
  542.   (prog (save)
  543.     (setq save (rest-of-ce))
  544.     (sublex)
  545.     la   (cond ((end-of-ce)
  546.         (cond ((member '\} save :test #'equal) 
  547.                (%error '|wrong contex for '}'| save))
  548.               (t (%error '|missing '}'| save))))
  549.            ((eq (peek-sublex) '\}) (sublex) (return nil)))
  550.     (cmp-atomic-or-any)
  551.     (go la))) 
  552.  
  553.  
  554. ;here's my kluge to solve symbol-print-name
  555. (defun symbol-print-name (x)
  556.   (symbol-name x))
  557.  
  558. ;(defun variablep (x) (and (symbolp x) ;changed from getchar form
  559. ;              (eq (char (symbol-print-name x) 0.) 
  560. ;                  (char (symbol-print-name '<) 0.))))
  561. ;@@@ revision suggested by sf/inc. by gdw
  562. (defun variablep (x)
  563.   (and (symbolp x)
  564.        (char= (char (symbol-name x) 0) #\< )))
  565.  
  566. (defun cmp-symbol (test)
  567.   (prog (flag)
  568.     (setq flag t)
  569.     (cond ((eq (peek-sublex) '//) (sublex) (setq flag nil)))
  570.     (cond ((and flag (variablep (peek-sublex)))
  571.        (cmp-var test))
  572.       ((numberp (peek-sublex)) (cmp-number test))
  573.       ((symbolp (peek-sublex)) (cmp-constant test))
  574.       (t (%error '|unrecognized symbol| (sublex)))))) 
  575.  
  576. (defun cmp-constant (test)   ;jgk inserted concatenate form
  577.   (or (member test '(eq ne xx))
  578.       (%error '|non-numeric constant after numeric predicate| (sublex)))
  579.   (link-new-node (list (intern (concatenate 'string
  580.                         (symbol-print-name 't)
  581.                         (symbol-print-name  test)
  582.                         (symbol-print-name 'a)))
  583.                nil
  584.                (current-field)
  585.                (sublex)))) 
  586.  
  587. (defun cmp-number (test)   ;jgk inserted concatenate form
  588.   (link-new-node 
  589.     (list 
  590.       (intern 
  591.     (concatenate 'string
  592.       (symbol-print-name 't)
  593.       (symbol-print-name  test)
  594.           (symbol-print-name 'n)))
  595.       nil                ;outs -- nodelist to traverse
  596.       (current-field)            ;register 
  597.       (sublex))))            ;constant
  598.  
  599. ; %%% things to change to convert set of *cN* to *c* array:
  600. ; %%% current-field field-name *subnum*  eval-nodelist ?? wm-hash 
  601. ; %%% send-to, &bus
  602.  
  603. (defun current-field nil (field-name *subnum*)) 
  604.  
  605. (defun field-name (num)
  606.   (cond ((= num 1.) '*c1*)
  607.     ((= num 2.) '*c2*)
  608.     ((= num 3.) '*c3*)
  609.     ((= num 4.) '*c4*)
  610.     ((= num 5.) '*c5*)
  611.     ((= num 6.) '*c6*)
  612.     ((= num 7.) '*c7*)
  613.     ((= num 8.) '*c8*)
  614.     ((= num 9.) '*c9*)
  615.     ((= num 10.) '*c10*)
  616.     ((= num 11.) '*c11*)
  617.     ((= num 12.) '*c12*)
  618.     ((= num 13.) '*c13*)
  619.     ((= num 14.) '*c14*)
  620.     ((= num 15.) '*c15*)
  621.     ((= num 16.) '*c16*)
  622.     ((= num 17.) '*c17*)
  623.     ((= num 18.) '*c18*)
  624.     ((= num 19.) '*c19*)
  625.     ((= num 20.) '*c20*)
  626.     ((= num 21.) '*c21*)
  627.     ((= num 22.) '*c22*)
  628.     ((= num 23.) '*c23*)
  629.     ((= num 24.) '*c24*)
  630.     ((= num 25.) '*c25*)
  631.     ((= num 26.) '*c26*)
  632.     ((= num 27.) '*c27*)
  633.     ((= num 28.) '*c28*)
  634.     ((= num 29.) '*c29*)
  635.     ((= num 30.) '*c30*)
  636.     ((= num 31.) '*c31*)
  637.     ((= num 32.) '*c32*)
  638.     ((= num 33.) '*c33*)
  639.     ((= num 34.) '*c34*)
  640.     ((= num 35.) '*c35*)
  641.     ((= num 36.) '*c36*)
  642.     ((= num 37.) '*c37*)
  643.     ((= num 38.) '*c38*)
  644.     ((= num 39.) '*c39*)
  645.     ((= num 40.) '*c40*)
  646.     ((= num 41.) '*c41*)
  647.     ((= num 42.) '*c42*)
  648.     ((= num 43.) '*c43*)
  649.     ((= num 44.) '*c44*)
  650.     ((= num 45.) '*c45*)
  651.     ((= num 46.) '*c46*)
  652.     ((= num 47.) '*c47*)
  653.     ((= num 48.) '*c48*)
  654.     ((= num 49.) '*c49*)
  655.     ((= num 50.) '*c50*)
  656.     ((= num 51.) '*c51*)
  657.     ((= num 52.) '*c52*)
  658.     ((= num 53.) '*c53*)
  659.     ((= num 54.) '*c54*)
  660.     ((= num 55.) '*c55*)
  661.     ((= num 56.) '*c56*)
  662.     ((= num 57.) '*c57*)
  663.     ((= num 58.) '*c58*)
  664.     ((= num 59.) '*c59*)
  665.     ((= num 60.) '*c60*)
  666.     ((= num 61.) '*c61*)
  667.     ((= num 62.) '*c62*)
  668.     ((= num 63.) '*c63*)
  669.     ((= num 64.) '*c64*)
  670.     ((= num 65.) '*c65* ) 
  671.     ((= num 66.) '*c66* ) 
  672.     ((= num 67.) '*c67*)
  673.     ((= num 68.) '*c68*)
  674.     ((= num 69.) '*c69*)
  675.     ((= num 70.) '*c70*)
  676.     ((= num 71.) '*c71*)
  677.     ((= num 72.) '*c72*)
  678.     ((= num 73.) '*c73*)
  679.     ((= num 74.) '*c74*)
  680.     ((= num 75.) '*c75*)
  681.     ((= num 76.) '*c76*)
  682.     ((= num 77.) '*c77*)
  683.     ((= num 78.) '*c78*)
  684.     ((= num 79.) '*c79*)
  685.     ((= num 80.) '*c80*)
  686.     ((= num 81.) '*c81*)
  687.     ((= num 82.) '*c82*)
  688.     ((= num 83.) '*c83*)
  689.     ((= num 84.) '*c84*)
  690.     ((= num 85.) '*c85*)
  691.     ((= num 86.) '*c86*)
  692.     ((= num 87.) '*c87*)
  693.     ((= num 88.) '*c88*)
  694.     ((= num 89.) '*c89*)
  695.     ((= num 90.) '*c90*)
  696.     ((= num 91.) '*c91*)
  697.     ((= num 92.) '*c92*)
  698.     ((= num 93.) '*c93*)
  699.     ((= num 94.) '*c94*)
  700.     ((= num 95.) '*c95*)
  701.     ((= num 96.) '*c96*)
  702.     ((= num 97.) '*c97*)
  703.     ((= num 98.) '*c98*)
  704.     ((= num 99.) '*c99*)
  705.     ((= num 100.) '*c100*)
  706.     ((= num 101.) '*c101*)
  707.     ((= num 102.) '*c102*)
  708.     ((= num 103.) '*c103*)
  709.     ((= num 104.) '*c104*)
  710.     ((= num 105.) '*c105*)
  711.     ((= num 106.) '*c106*)
  712.     ((= num 107.) '*c107*)
  713.     ((= num 108.) '*c108*)
  714.     ((= num 109.) '*c109*)
  715.     ((= num 110.) '*c110*)
  716.     ((= num 111.) '*c111*)
  717.     ((= num 112.) '*c112*)
  718.     ((= num 113.) '*c113*)
  719.     ((= num 114.) '*c114*)
  720.     ((= num 115.) '*c115*)
  721.     ((= num 116.) '*c116*)
  722.     ((= num 117.) '*c117*)
  723.     ((= num 118.) '*c118*)
  724.     ((= num 119.) '*c119*)
  725.     ((= num 120.) '*c120*)
  726.     ((= num 121.) '*c121*)
  727.     ((= num 122.) '*c122*)
  728.     ((= num 123.) '*c123*)
  729.     ((= num 124.) '*c124*)
  730.     ((= num 125.) '*c125*)
  731.     ((= num 126.) '*c126*)
  732.     ((= num 127.) '*c127*)
  733.     ;    ((= num 128.) '*c128*)
  734.     
  735.     (t (%error '|condition is too long| (rest-of-ce))))) 
  736.  
  737.  
  738. ;;; Compiling variables
  739. ;
  740. ;
  741. ;
  742. ; *cur-vars* are the variables in the condition element currently 
  743. ; being compiled.  *vars* are the variables in the earlier condition
  744. ; elements.  *ce-vars* are the condition element variables.  note
  745. ; that the interpreter will not confuse condition element and regular
  746. ; variables even if they have the same name.
  747. ;
  748. ; *cur-vars* is a list of triples: (name predicate subelement-number)
  749. ; eg:        ( (<x> eq 3)
  750.           ;          (<y> ne 1)
  751.           ;          . . . )
  752. ;
  753. ; *vars* is a list of triples: (name ce-number subelement-number)
  754. ; eg:        ( (<x> 3 3)
  755.           ;          (<y> 1 1)
  756.           ;          . . . )
  757. ;
  758. ; *ce-vars* is a list of pairs: (name ce-number)
  759. ; eg:        ( (ce1 1)
  760.           ;          (<c3> 3)
  761.           ;          . . . )
  762. ;@@@ defun' assq (should be replaced) ###
  763. (defun assq (i l)(assoc i l))
  764.  
  765. (defun var-dope (var) (assq var *vars*))
  766.  
  767. (defun ce-var-dope (var) (assq var *ce-vars*))
  768.  
  769. (defun cmp-var (test)
  770.   (prog (old name)
  771.     (setq name (sublex))
  772.     (setq old (assq name *cur-vars*))
  773.     (cond ((and old (eq (cadr old) 'eq))
  774.        (cmp-old-eq-var test old))
  775.       ((and old (eq test 'eq)) (cmp-new-eq-var name old))
  776.       (t (cmp-new-var name test))))) 
  777.  
  778. (defun cmp-new-var (name test)
  779.   (setq *cur-vars* (cons (list name test *subnum*) *cur-vars*))) 
  780.  
  781. (defun cmp-old-eq-var (test old)  ; jgk inserted concatenate form
  782.   (link-new-node (list (intern (concatenate 'string
  783.                         (symbol-print-name 't)
  784.                         (symbol-print-name  test)
  785.                         (symbol-print-name 's)))
  786.                nil
  787.                (current-field)
  788.                (field-name (caddr old))))) 
  789.  
  790. ;@@@ added defunb of delq
  791. (defun delq (i l)
  792.   (delete i l :test #'eq))
  793.  
  794. ;
  795. ; Spdelete "special delete" is a function which deletes every occurence
  796. ; of element from list. This function was defined because common lisp's
  797. ; delete function only deletes top level elements from a list, not lists
  798. ; from lists. 
  799. ;
  800. (defun spdelete (element list)
  801.   
  802.   (cond ((null list) nil)
  803.     ((equal element (car list)) (spdelete element (cdr list)))
  804.     (t (cons (car list) (spdelete element (cdr list))))))
  805.  
  806.  
  807.  
  808. (defun cmp-new-eq-var (name old)  ;jgk inserted concatenate form
  809.   (prog (pred next)
  810.     (setq *cur-vars* (delq old *cur-vars*))
  811.     (setq next (assq name *cur-vars*))
  812.     (cond (next (cmp-new-eq-var name next))
  813.       (t (cmp-new-var name 'eq)))
  814.     (setq pred (cadr old))
  815.     (link-new-node (list (intern (concatenate 'string
  816.                           (symbol-print-name 't)
  817.                           (symbol-print-name  pred)
  818.                           (symbol-print-name 's)))
  819.              nil
  820.              (field-name (caddr old))
  821.              (current-field))))) 
  822.  
  823. (defun cmp-cevar nil
  824.   (prog (name old)
  825.     (setq name (lex))
  826.     (setq old (assq name *ce-vars*))
  827.     (and old
  828.      (%error '|condition element variable used twice| name))
  829.     (setq *ce-vars* (cons (list name 0.) *ce-vars*)))) 
  830.  
  831. (defun cmp-not nil (cmp-beta '¬)) 
  832.  
  833. (defun cmp-nobeta nil (cmp-beta nil)) 
  834.  
  835. (defun cmp-and nil (cmp-beta '&and)) 
  836.  
  837. (defun cmp-beta (kind)
  838.   (prog (tlist vdope vname #|vpred vpos|# old)
  839.     (setq tlist nil)
  840.     la   (and (atom *cur-vars*) (go lb))
  841.     (setq vdope (car *cur-vars*))
  842.     (setq *cur-vars* (cdr *cur-vars*))
  843.     (setq vname (car vdope))
  844.     ;;  (setq vpred (cadr vdope))    Dario - commented out (unused)
  845.     ;;  (setq vpos (caddr vdope))
  846.     (setq old (assq vname *vars*))
  847.     (cond (old (setq tlist (add-test tlist vdope old)))
  848.       ((not (eq kind '¬)) (promote-var vdope)))
  849.     (go la)
  850.     lb   (and kind (build-beta kind tlist))
  851.     (or (eq kind '¬) (fudge))
  852.     (setq *last-branch* *last-node*))) 
  853.  
  854. (defun add-test (list new old) ; jgk inserted concatenate form
  855.   (prog (ttype lloc rloc)
  856.     (setq *feature-count* (1+ *feature-count*))
  857.     (setq ttype (intern (concatenate 'string (symbol-print-name 't)
  858.                      (symbol-print-name (cadr new))
  859.                      (symbol-print-name 'b))))
  860.     (setq rloc (encode-singleton (caddr new)))
  861.     (setq lloc (encode-pair (cadr old) (caddr old)))
  862.     (return (cons ttype (cons lloc (cons rloc list)))))) 
  863.  
  864. ; the following two functions encode indices so that gelm can
  865. ; decode them as fast as possible
  866.  
  867. ;; %%% change to use clisp hash fns ??
  868. (defun encode-pair (a b) (+ (* 10000. (1- a)) (1- b))) 
  869. ;"plus" changed to "+" by gdw
  870.  
  871. (defun encode-singleton (a) (1- a)) 
  872.  
  873. (defun promote-var (dope)
  874.   (prog (vname vpred vpos new)
  875.     (setq vname (car dope))
  876.     (setq vpred (cadr dope))
  877.     (setq vpos (caddr dope))
  878.     (or (eq 'eq vpred)
  879.     (%error '|illegal predicate for first occurrence|
  880.         (list vname vpred)))
  881.     (setq new (list vname 0. vpos))
  882.     (setq *vars* (cons new *vars*)))) 
  883.  
  884. (defun fudge nil
  885.   (mapc (function fudge*) *vars*)
  886.   (mapc (function fudge*) *ce-vars*)) 
  887.  
  888. (defun fudge* (z)
  889.   (prog (a) (setq a (cdr z)) (rplaca a (1+ (car a))))) 
  890.  
  891. (defun build-beta (type tests)
  892.   (prog (rpred lpred lnode lef)
  893.     (link-new-node (list '&mem nil nil (protomem)))
  894.     (setq rpred *last-node*)
  895.     (cond ((eq type '&and)
  896.        (setq lnode (list '&mem nil nil (protomem))))
  897.       (t (setq lnode (list '&two nil nil))))
  898.     (setq lpred (link-to-branch lnode))
  899.     (cond ((eq type '&and) (setq lef lpred))
  900.       (t (setq lef (protomem))))
  901.     (link-new-beta-node (list type nil lef rpred tests)))) 
  902.  
  903. (defun protomem nil (list nil)) 
  904.  
  905. (defun memory-part (mem-node) (car (cadddr mem-node))) 
  906.  
  907. (defun encode-dope nil
  908.   (prog (r all z k)
  909.     (setq r nil)
  910.     (setq all *vars*)
  911.     la   (and (atom all) (return r))
  912.     (setq z (car all))
  913.     (setq all (cdr all))
  914.     (setq k (encode-pair (cadr z) (caddr z)))
  915.     (setq r (cons (car z) (cons k r)))
  916.     (go la))) 
  917.  
  918. (defun encode-ce-dope nil
  919.   (prog (r all z k)
  920.     (setq r nil)
  921.     (setq all *ce-vars*)
  922.     la   (and (atom all) (return r))
  923.     (setq z (car all))
  924.     (setq all (cdr all))
  925.     (setq k (cadr z))
  926.     (setq r (cons (car z) (cons k r)))
  927.     (go la))) 
  928.  
  929.  
  930.  
  931. ;;; Linking the nodes
  932.  
  933. (defun link-new-node (r)
  934.   (cond ((not (member (car r) '(&p &mem &two &and ¬) :test #'equal))
  935.      (setq *feature-count* (1+ *feature-count*))))
  936.   (setq *virtual-cnt* (1+ *virtual-cnt*))
  937.   (setq *last-node* (link-left *last-node* r))) 
  938.  
  939. (defun link-to-branch (r)
  940.   (setq *virtual-cnt* (1+ *virtual-cnt*))
  941.   (setq *last-branch* (link-left *last-branch* r))) 
  942.  
  943. (defun link-new-beta-node (r)
  944.   (setq *virtual-cnt* (1+ *virtual-cnt*))
  945.   (setq *last-node* (link-both *last-branch* *last-node* r))
  946.   (setq *last-branch* *last-node*)) 
  947.  
  948. (defun link-left (pred succ)
  949.   (prog (a r)
  950.     (setq a (left-outs pred))
  951.     (setq r (find-equiv-node succ a))
  952.     (and r (return r))
  953.     (setq *real-cnt* (1+ *real-cnt*))
  954.     (attach-left pred succ)
  955.     (return succ))) 
  956.  
  957. (defun link-both (left right succ)
  958.   (prog (a r)
  959.     (setq a (interq (left-outs left) (right-outs right)))
  960.     (setq r (find-equiv-beta-node succ a))
  961.     (and r (return r))
  962.     (setq *real-cnt* (1+ *real-cnt*))
  963.     (attach-left left succ)
  964.     (attach-right right succ)
  965.     (return succ))) 
  966.  
  967. (defun attach-right (old new)
  968.   (rplaca (cddr old) (cons new (caddr old)))) 
  969.  
  970. (defun attach-left (old new)
  971.   (rplaca (cdr old) (cons new (cadr old)))) 
  972.  
  973. (defun right-outs (node) (caddr node)) 
  974.  
  975. (defun left-outs (node) (cadr node)) 
  976.  
  977. (defun find-equiv-node (node list)
  978.   (prog (a)
  979.     (setq a list)
  980.     l1   (cond ((atom a) (return nil))
  981.            ((equiv node (car a)) (return (car a))))
  982.     (setq a (cdr a))
  983.     (go l1))) 
  984.  
  985. (defun find-equiv-beta-node (node list)
  986.   (prog (a)
  987.     (setq a list)
  988.     l1   (cond ((atom a) (return nil))
  989.            ((beta-equiv node (car a)) (return (car a))))
  990.     (setq a (cdr a))
  991.     (go l1))) 
  992.  
  993. ; do not look at the predecessor fields of beta nodes; they have to be
  994. ; identical because of the way the candidate nodes were found
  995.  
  996. (defun equiv (a b)
  997.   (and (eq (car a) (car b))
  998.        (or (eq (car a) '&mem)
  999.        (eq (car a) '&two)
  1000.        (equal (caddr a) (caddr b)))
  1001.        (equal (cdddr a) (cdddr b)))) 
  1002.  
  1003. (defun beta-equiv (a b)
  1004.   (and (eq (car a) (car b))
  1005.        (equal (cddddr a) (cddddr b))
  1006.        (or (eq (car a) '&and) (equal (caddr a) (caddr b))))) 
  1007.  
  1008. ; the equivalence tests are set up to consider the contents of
  1009. ; node memories, so they are ready for the build action
  1010.  
  1011. ;;; Network interpreter
  1012.  
  1013. (defun match (flag wme)
  1014.   (sendto flag (list wme) 'left (list *first-node*)))
  1015.  
  1016. ; note that eval-nodelist is not set up to handle building
  1017. ; productions.  would have to add something like ops4's build-flag
  1018.  
  1019. (defun eval-nodelist (nl)
  1020.   (prog nil
  1021.     top  (and (not nl) (return nil))
  1022.     (setq *sendtocall* nil)
  1023.     (setq *last-node* (car nl))
  1024.     (apply (caar nl) (cdar nl))        ;; %%% here's the apply cdar nl must 
  1025.                         ;; be the *cN* item, caar nl is test
  1026.     (setq nl (cdr nl))
  1027.     (go top))) 
  1028.  
  1029. (defun sendto (flag data side nl)
  1030.   (prog nil
  1031.     top  (and (not nl) (return nil))
  1032.     (setq *side* side)
  1033.     (setq *flag-part* flag)
  1034.     (setq *data-part* data)
  1035.     (setq *sendtocall* t)
  1036.     (setq *last-node* (car nl))
  1037.     (apply (caar nl) (cdar nl))        ;; %%% ditto
  1038.     (setq nl (cdr nl))
  1039.     (go top))) 
  1040.  
  1041. ; &bus sets up the registers for the one-input nodes.  note that this
  1042. ; %%% map dp into *c* array ??
  1043. (defun &bus (outs)
  1044.   (prog (dp)
  1045.     (setq *alpha-flag-part* *flag-part*)
  1046.     (setq *alpha-data-part* *data-part*)
  1047.     (setq dp (car *data-part*))
  1048.     (setq *c1* (car dp))
  1049.     (setq dp (cdr dp))
  1050.     (setq *c2* (car dp))
  1051.     (setq dp (cdr dp))
  1052.     (setq *c3* (car dp))
  1053.     (setq dp (cdr dp))
  1054.     (setq *c4* (car dp))
  1055.     (setq dp (cdr dp))
  1056.     (setq *c5* (car dp))
  1057.     (setq dp (cdr dp))
  1058.     (setq *c6* (car dp))
  1059.     (setq dp (cdr dp))
  1060.     (setq *c7* (car dp))
  1061.     (setq dp (cdr dp))
  1062.     (setq *c8* (car dp))
  1063.     (setq dp (cdr dp))
  1064.     (setq *c9* (car dp))
  1065.     (setq dp (cdr dp))
  1066.     (setq *c10* (car dp))
  1067.     (setq dp (cdr dp))
  1068.     (setq *c11* (car dp))
  1069.     (setq dp (cdr dp))
  1070.     (setq *c12* (car dp))
  1071.     (setq dp (cdr dp))
  1072.     (setq *c13* (car dp))
  1073.     (setq dp (cdr dp))
  1074.     (setq *c14* (car dp))
  1075.     (setq dp (cdr dp))
  1076.     (setq *c15* (car dp))
  1077.     (setq dp (cdr dp))
  1078.     (setq *c16* (car dp))
  1079.     (setq dp (cdr dp))
  1080.     (setq *c17* (car dp))
  1081.     (setq dp (cdr dp))
  1082.     (setq *c18* (car dp))
  1083.     (setq dp (cdr dp))
  1084.     (setq *c19* (car dp))
  1085.     (setq dp (cdr dp))
  1086.     (setq *c20* (car dp))
  1087.     (setq dp (cdr dp))
  1088.     (setq *c21* (car dp))
  1089.     (setq dp (cdr dp))
  1090.     (setq *c22* (car dp))
  1091.     (setq dp (cdr dp))
  1092.     (setq *c23* (car dp))
  1093.     (setq dp (cdr dp))
  1094.     (setq *c24* (car dp))
  1095.     (setq dp (cdr dp))
  1096.     (setq *c25* (car dp))
  1097.     (setq dp (cdr dp))
  1098.     (setq *c26* (car dp))
  1099.     (setq dp (cdr dp))
  1100.     (setq *c27* (car dp))
  1101.     (setq dp (cdr dp))
  1102.     (setq *c28* (car dp))
  1103.     (setq dp (cdr dp))
  1104.     (setq *c29* (car dp))
  1105.     (setq dp (cdr dp))
  1106.     (setq *c30* (car dp))
  1107.     (setq dp (cdr dp))
  1108.     (setq *c31* (car dp))
  1109.     (setq dp (cdr dp))
  1110.     (setq *c32* (car dp))
  1111.     (setq dp (cdr dp))
  1112.     (setq *c33* (car dp))
  1113.     (setq dp (cdr dp))
  1114.     (setq *c34* (car dp))
  1115.     (setq dp (cdr dp))
  1116.     (setq *c35* (car dp))
  1117.     (setq dp (cdr dp))
  1118.     (setq *c36* (car dp))
  1119.     (setq dp (cdr dp))
  1120.     (setq *c37* (car dp))
  1121.     (setq dp (cdr dp))
  1122.     (setq *c38* (car dp))
  1123.     (setq dp (cdr dp))
  1124.     (setq *c39* (car dp))
  1125.     (setq dp (cdr dp))
  1126.     (setq *c40* (car dp))
  1127.     (setq dp (cdr dp))
  1128.     (setq *c41* (car dp))
  1129.     (setq dp (cdr dp))
  1130.     (setq *c42* (car dp))
  1131.     (setq dp (cdr dp))
  1132.     (setq *c43* (car dp))
  1133.     (setq dp (cdr dp))
  1134.     (setq *c44* (car dp))
  1135.     (setq dp (cdr dp))
  1136.     (setq *c45* (car dp))
  1137.     (setq dp (cdr dp))
  1138.     (setq *c46* (car dp))
  1139.     (setq dp (cdr dp))
  1140.     (setq *c47* (car dp))
  1141.     (setq dp (cdr dp))
  1142.     (setq *c48* (car dp))
  1143.     (setq dp (cdr dp))
  1144.     (setq *c49* (car dp))
  1145.     (setq dp (cdr dp))
  1146.     (setq *c50* (car dp))
  1147.     (setq dp (cdr dp))
  1148.     (setq *c51* (car dp))
  1149.     (setq dp (cdr dp))
  1150.     (setq *c52* (car dp))
  1151.     (setq dp (cdr dp))
  1152.     (setq *c53* (car dp))
  1153.     (setq dp (cdr dp))
  1154.     (setq *c54* (car dp))
  1155.     (setq dp (cdr dp))
  1156.     (setq *c55* (car dp))
  1157.     (setq dp (cdr dp))
  1158.     (setq *c56* (car dp))
  1159.     (setq dp (cdr dp))
  1160.     (setq *c57* (car dp))
  1161.     (setq dp (cdr dp))
  1162.     (setq *c58* (car dp))
  1163.     (setq dp (cdr dp))
  1164.     (setq *c59* (car dp))
  1165.     (setq dp (cdr dp))
  1166.     (setq *c60* (car dp))
  1167.     (setq dp (cdr dp))
  1168.     (setq *c61* (car dp))
  1169.     (setq dp (cdr dp))
  1170.     (setq *c62* (car dp))
  1171.     (setq dp (cdr dp))
  1172.     (setq *c63* (car dp))
  1173.     (setq dp (cdr dp))
  1174.     (setq *c64* (car dp))
  1175.     ;-------- added for 127 atr
  1176.     (setq dp (cdr dp))
  1177.     (setq *c65* (car dp))
  1178.     (setq dp (cdr dp))
  1179.     (setq *c66* (car dp))
  1180.     (setq dp (cdr dp))
  1181.     (setq *c67* (car dp))
  1182.     (setq dp (cdr dp))
  1183.     (setq *c68* (car dp))
  1184.     (setq dp (cdr dp))
  1185.     (setq *c69*(car dp))
  1186.     (setq dp (cdr dp))
  1187.     (setq *c70* (car dp))
  1188.     (setq dp (cdr dp))
  1189.     (setq *c71* (car dp))
  1190.     (setq dp (cdr dp))
  1191.     (setq *c72* (car dp))
  1192.     (setq dp (cdr dp))
  1193.     (setq *c73* (car dp))
  1194.     (setq dp (cdr dp))
  1195.     (setq *c74* (car dp))
  1196.     (setq dp (cdr dp))
  1197.     (setq *c75* (car dp))
  1198.     (setq dp (cdr dp))
  1199.     (setq *c76* (car dp))
  1200.     (setq dp (cdr dp))
  1201.     (setq *c77* (car dp))
  1202.     (setq dp (cdr dp))
  1203.     (setq *c78* (car dp))
  1204.     (setq dp (cdr dp))
  1205.     (setq *c79*(car dp))
  1206.     (setq dp (cdr dp))
  1207.     (setq *c80* (car dp))
  1208.     (setq dp (cdr dp))
  1209.     (setq *c81* (car dp))
  1210.     (setq dp (cdr dp))
  1211.     (setq *c82* (car dp))
  1212.     (setq dp (cdr dp))
  1213.     (setq *c83* (car dp))
  1214.     (setq dp (cdr dp))
  1215.     (setq *c84* (car dp))
  1216.     (setq dp (cdr dp))
  1217.     (setq *c85* (car dp))
  1218.     (setq dp (cdr dp))
  1219.     (setq *c86* (car dp))
  1220.     (setq dp (cdr dp))
  1221.     (setq *c87* (car dp))
  1222.     (setq dp (cdr dp))
  1223.     (setq *c88* (car dp))
  1224.     (setq dp (cdr dp))
  1225.     (setq *c89*(car dp))
  1226.     (setq dp (cdr dp))
  1227.     (setq *c90* (car dp))
  1228.     (setq dp (cdr dp))
  1229.     (setq *c91* (car dp))
  1230.     (setq dp (cdr dp))
  1231.     (setq *c92* (car dp))
  1232.     (setq dp (cdr dp))
  1233.     (setq *c93* (car dp))
  1234.     (setq dp (cdr dp))
  1235.     (setq *c94* (car dp))
  1236.     (setq dp (cdr dp))
  1237.     (setq *c95* (car dp))
  1238.     (setq dp (cdr dp))
  1239.     (setq *c96* (car dp))
  1240.     (setq dp (cdr dp))
  1241.     (setq *c97* (car dp))
  1242.     (setq dp (cdr dp))
  1243.     (setq *c98* (car dp))
  1244.     (setq dp (cdr dp))
  1245.     (setq *c99*(car dp))
  1246.     (setq dp (cdr dp))
  1247.     (setq *c100* (car dp))
  1248.     (setq dp (cdr dp))
  1249.     (setq *c101* (car dp))
  1250.     (setq dp (cdr dp))
  1251.     (setq *c102* (car dp))
  1252.     (setq dp (cdr dp))
  1253.     (setq *c103* (car dp))
  1254.     (setq dp (cdr dp))
  1255.     (setq *c104* (car dp))
  1256.     (setq dp (cdr dp))
  1257.     (setq *c105* (car dp))
  1258.     (setq dp (cdr dp))
  1259.     (setq *c106* (car dp))
  1260.     (setq dp (cdr dp))
  1261.     (setq *c107* (car dp))
  1262.     (setq dp (cdr dp))
  1263.     (setq *c108* (car dp))
  1264.     (setq dp (cdr dp))
  1265.     (setq *c109*(car dp))
  1266.     (setq dp (cdr dp))
  1267.     (setq *c110* (car dp))
  1268.     (setq dp (cdr dp))
  1269.     (setq *c111* (car dp))
  1270.     (setq dp (cdr dp))
  1271.     (setq *c112* (car dp))
  1272.     (setq dp (cdr dp))
  1273.     (setq *c113* (car dp))
  1274.     (setq dp (cdr dp))
  1275.     (setq *c114* (car dp))
  1276.     (setq dp (cdr dp))
  1277.     (setq *c115* (car dp))
  1278.     (setq dp (cdr dp))
  1279.     (setq *c116* (car dp))
  1280.     (setq dp (cdr dp))
  1281.     (setq *c117* (car dp))
  1282.     (setq dp (cdr dp))
  1283.     (setq *c118* (car dp))
  1284.     (setq dp (cdr dp))
  1285.     (setq *c119*(car dp))
  1286.     (setq dp (cdr dp))
  1287.     (setq *c120* (car dp))
  1288.     (setq dp (cdr dp))
  1289.     (setq *c121* (car dp))
  1290.     (setq dp (cdr dp))
  1291.     (setq *c122* (car dp))
  1292.     (setq dp (cdr dp))
  1293.     (setq *c123* (car dp))
  1294.     (setq dp (cdr dp))
  1295.     (setq *c124* (car dp))
  1296.     (setq dp (cdr dp))
  1297.     (setq *c125* (car dp))
  1298.     (setq dp (cdr dp))
  1299.     (setq *c126* (car dp))
  1300.     (setq dp (cdr dp))
  1301.     (setq *c127* (car dp))
  1302.     ;(setq dp (cdr dp))
  1303.     ;(setq *c128* (car dp))
  1304.     ;--------
  1305.     (eval-nodelist outs))) 
  1306.  
  1307. (defun &any (outs register const-list)
  1308.   (prog (z c)
  1309.     (setq z (symbol-value register))
  1310.     (cond ((numberp z) (go number)))
  1311.     symbol (cond ((null const-list) (return nil))
  1312.          ((eq (car const-list) z) (go ok))
  1313.          (t (setq const-list (cdr const-list)) (go symbol)))
  1314.     number (cond ((null const-list) (return nil))
  1315.          ((and (numberp (setq c (car const-list)))
  1316.                (=alg c z))
  1317.           (go ok))
  1318.          (t (setq const-list (cdr const-list)) (go number)))
  1319.     ok   (eval-nodelist outs))) 
  1320.  
  1321. (defun teqa (outs register constant)
  1322.   (and (eq (symbol-value register) constant) (eval-nodelist outs))) 
  1323.  
  1324. (defun tnea (outs register constant)
  1325.   (and (not (eq (symbol-value register) constant)) (eval-nodelist outs))) 
  1326.  
  1327. (defun txxa (outs register constant)
  1328.   (declare (ignore constant))
  1329.   (and (symbolp (symbol-value register)) (eval-nodelist outs))) 
  1330.  
  1331. (defun teqn (outs register constant)
  1332.   (prog (z)
  1333.     (setq z (symbol-value register))
  1334.     (and (numberp z)
  1335.      (=alg z constant)
  1336.      (eval-nodelist outs)))) 
  1337.  
  1338. (defun tnen (outs register constant)
  1339.   (prog (z)
  1340.     (setq z (symbol-value register))
  1341.     (and (or (not (numberp z))
  1342.          (not (=alg z constant)))
  1343.      (eval-nodelist outs)))) 
  1344.  
  1345. (defun txxn (outs register constant)
  1346.   (declare (ignore constant))
  1347.   (prog (z)
  1348.     (setq z (symbol-value register))
  1349.     (and (numberp z) (eval-nodelist outs)))) 
  1350.  
  1351. (defun tltn (outs register constant)
  1352.   (prog (z)
  1353.     (setq z (symbol-value register))
  1354.     (and (numberp z)
  1355.      (> constant z)
  1356.      (eval-nodelist outs)))) 
  1357.  
  1358. (defun tgtn (outs register constant)
  1359.   (prog (z)
  1360.     (setq z (symbol-value register))
  1361.     (and (numberp z)
  1362.      (> z constant)
  1363.      (eval-nodelist outs)))) 
  1364.  
  1365. (defun tgen (outs register constant)
  1366.   (prog (z)
  1367.     (setq z (symbol-value register))
  1368.     (and (numberp z)
  1369.      (not (> constant z))
  1370.      (eval-nodelist outs)))) 
  1371.  
  1372. (defun tlen (outs register constant)
  1373.   (prog (z)
  1374.     (setq z (symbol-value register))
  1375.     (and (numberp z)
  1376.      (not (> z constant))
  1377.      (eval-nodelist outs)))) 
  1378.  
  1379. (defun teqs (outs vara varb)
  1380.   (prog (a b)
  1381.     (setq a (symbol-value vara))
  1382.     (setq b (symbol-value varb))
  1383.     (cond ((eq a b) (eval-nodelist outs))
  1384.       ((and (numberp a)
  1385.         (numberp b)
  1386.         (=alg a b))
  1387.        (eval-nodelist outs))))) 
  1388.  
  1389. (defun tnes (outs vara varb)
  1390.   (prog (a b)
  1391.     (setq a (symbol-value vara))
  1392.     (setq b (symbol-value varb))
  1393.     (cond ((eq a b) (return nil))
  1394.       ((and (numberp a)
  1395.         (numberp b)
  1396.         (=alg a b))
  1397.        (return nil))
  1398.       (t (eval-nodelist outs))))) 
  1399.  
  1400. (defun txxs (outs vara varb)
  1401.   (prog (a b)
  1402.     (setq a (symbol-value vara))
  1403.     (setq b (symbol-value varb))
  1404.     (cond ((and (numberp a) (numberp b)) (eval-nodelist outs))
  1405.       ((and (not (numberp a)) (not (numberp b)))
  1406.        (eval-nodelist outs))))) 
  1407.  
  1408. (defun tlts (outs vara varb)
  1409.   (prog (a b)
  1410.     (setq a (symbol-value vara))
  1411.     (setq b (symbol-value varb))
  1412.     (and (numberp a)
  1413.      (numberp b)
  1414.      (> b a)
  1415.      (eval-nodelist outs)))) 
  1416.  
  1417. (defun tgts (outs vara varb)
  1418.   (prog (a b)
  1419.     (setq a (symbol-value vara))
  1420.     (setq b (symbol-value varb))
  1421.     (and (numberp a)
  1422.      (numberp b)
  1423.      (> a b)
  1424.      (eval-nodelist outs)))) 
  1425.  
  1426. (defun tges (outs vara varb)
  1427.   (prog (a b)
  1428.     (setq a (symbol-value vara))
  1429.     (setq b (symbol-value varb))
  1430.     (and (numberp a)
  1431.      (numberp b)
  1432.      (not (> b a))
  1433.      (eval-nodelist outs)))) 
  1434.  
  1435. (defun tles (outs vara varb)
  1436.   (prog (a b)
  1437.     (setq a (symbol-value vara))
  1438.     (setq b (symbol-value varb))
  1439.     (and (numberp a)
  1440.      (numberp b)
  1441.      (not (> a b))
  1442.      (eval-nodelist outs)))) 
  1443.  
  1444. (defun &two (left-outs right-outs)
  1445.   (prog (fp dp)
  1446.     (cond (*sendtocall*
  1447.        (setq fp *flag-part*)
  1448.        (setq dp *data-part*))
  1449.       (t
  1450.        (setq fp *alpha-flag-part*)
  1451.        (setq dp *alpha-data-part*)))
  1452.     (sendto fp dp 'left left-outs)
  1453.     (sendto fp dp 'right right-outs))) 
  1454.  
  1455. (defun &mem (left-outs right-outs memory-list)
  1456.   (prog (fp dp)
  1457.     (cond (*sendtocall*
  1458.        (setq fp *flag-part*)
  1459.        (setq dp *data-part*))
  1460.       (t
  1461.        (setq fp *alpha-flag-part*)
  1462.        (setq dp *alpha-data-part*)))
  1463.     (sendto fp dp 'left left-outs)
  1464.     (add-token memory-list fp dp nil)
  1465.     (sendto fp dp 'right right-outs))) 
  1466.  
  1467. (defun &and (outs lpred rpred tests)
  1468.   (prog (mem)
  1469.     (cond ((eq *side* 'right) (setq mem (memory-part lpred)))
  1470.       (t (setq mem (memory-part rpred))))
  1471.     (cond ((not mem) (return nil))
  1472.       ((eq *side* 'right) (and-right outs mem tests))
  1473.       (t (and-left outs mem tests))))) 
  1474.  
  1475. (defun and-left (outs mem tests)
  1476.   (prog (fp dp memdp tlist tst lind rind res)
  1477.     (setq fp *flag-part*)
  1478.     (setq dp *data-part*)
  1479.     fail (and (null mem) (return nil))
  1480.     (setq memdp (car mem))
  1481.     (setq mem (cdr mem))
  1482.     (setq tlist tests)
  1483.     tloop (and (null tlist) (go succ))
  1484.     (setq tst (car tlist))
  1485.     (setq tlist (cdr tlist))
  1486.     (setq lind (car tlist))
  1487.     (setq tlist (cdr tlist))
  1488.     (setq rind (car tlist))
  1489.     (setq tlist (cdr tlist))
  1490.     ;###        (comment the next line differs in and-left & -right)
  1491.     (setq res (funcall tst (gelm memdp rind) (gelm dp lind)))
  1492.     (cond (res (go tloop))
  1493.       (t (go fail)))
  1494.     succ 
  1495.     ;###    (comment the next line differs in and-left & -right)
  1496.     (sendto fp (cons (car memdp) dp) 'left outs)
  1497.     (go fail))) 
  1498.  
  1499. (defun and-right (outs mem tests)
  1500.   (prog (fp dp memdp tlist tst lind rind res)
  1501.     (setq fp *flag-part*)
  1502.     (setq dp *data-part*)
  1503.     fail (and (null mem) (return nil))
  1504.     (setq memdp (car mem))
  1505.     (setq mem (cdr mem))
  1506.     (setq tlist tests)
  1507.     tloop (and (null tlist) (go succ))
  1508.     (setq tst (car tlist))
  1509.     (setq tlist (cdr tlist))
  1510.     (setq lind (car tlist))
  1511.     (setq tlist (cdr tlist))
  1512.     (setq rind (car tlist))
  1513.     (setq tlist (cdr tlist))
  1514.     ;###        (comment the next line differs in and-left & -right)
  1515.     (setq res (funcall tst (gelm dp rind) (gelm memdp lind)))
  1516.     (cond (res (go tloop))
  1517.       (t (go fail)))
  1518.     succ 
  1519.     ;###        (comment the next line differs in and-left & -right)
  1520.     (sendto fp (cons (car dp) memdp) 'right outs)
  1521.     (go fail))) 
  1522.  
  1523.  
  1524. (defun teqb (new eqvar)
  1525.   (cond ((eq new eqvar) t)
  1526.     ((not (numberp new)) nil)
  1527.     ((not (numberp eqvar)) nil)
  1528.     ((=alg new eqvar) t)
  1529.     (t nil))) 
  1530.  
  1531. (defun tneb (new eqvar)
  1532.   (cond ((eq new eqvar) nil)
  1533.     ((not (numberp new)) t)
  1534.     ((not (numberp eqvar)) t)
  1535.     ((=alg new eqvar) nil)
  1536.     (t t))) 
  1537.  
  1538. (defun tltb (new eqvar)
  1539.   (cond ((not (numberp new)) nil)
  1540.     ((not (numberp eqvar)) nil)
  1541.     ((> eqvar new) t)
  1542.     (t nil))) 
  1543.  
  1544. (defun tgtb (new eqvar)
  1545.   (cond ((not (numberp new)) nil)
  1546.     ((not (numberp eqvar)) nil)
  1547.     ((> new eqvar) t)
  1548.     (t nil))) 
  1549.  
  1550. (defun tgeb (new eqvar)
  1551.   (cond ((not (numberp new)) nil)
  1552.     ((not (numberp eqvar)) nil)
  1553.     ((not (> eqvar new)) t)
  1554.     (t nil))) 
  1555.  
  1556. (defun tleb (new eqvar)
  1557.   (cond ((not (numberp new)) nil)
  1558.     ((not (numberp eqvar)) nil)
  1559.     ((not (> new eqvar)) t)
  1560.     (t nil))) 
  1561.  
  1562. (defun txxb (new eqvar)
  1563.   (cond ((numberp new)
  1564.      (cond ((numberp eqvar) t)
  1565.            (t nil)))
  1566.     (t
  1567.      (cond ((numberp eqvar) nil)
  1568.            (t t))))) 
  1569.  
  1570.  
  1571. (defun &p (rating name var-dope ce-var-dope rhs)
  1572.   (declare (ignore var-dope ce-var-dope rhs))
  1573.   (prog (fp dp)
  1574.     (cond (*sendtocall*
  1575.        (setq fp *flag-part*)
  1576.        (setq dp *data-part*))
  1577.       (t
  1578.        (setq fp *alpha-flag-part*)
  1579.        (setq dp *alpha-data-part*)))
  1580.     (and (member fp '(nil old)) (removecs name dp))
  1581.     (and fp (insertcs name dp rating)))) 
  1582.  
  1583. (defun &old (a b c d e)
  1584.   (declare (ignore a b c d e))
  1585.   nil) 
  1586.  
  1587. (defun ¬ (outs lmem rpred tests)
  1588.   (cond ((and (eq *side* 'right) (eq *flag-part* 'old)) nil)
  1589.     ((eq *side* 'right) (not-right outs (car lmem) tests))
  1590.     (t (not-left outs (memory-part rpred) tests lmem)))) 
  1591.  
  1592. (defun not-left (outs mem tests own-mem)
  1593.   (prog (fp dp memdp tlist tst lind rind res c)
  1594.     (setq fp *flag-part*)
  1595.     (setq dp *data-part*)
  1596.     (setq c 0.)
  1597.     fail (and (null mem) (go fin))
  1598.     (setq memdp (car mem))
  1599.     (setq mem (cdr mem))
  1600.     (setq tlist tests)
  1601.     tloop (and (null tlist) (setq c (1+ c)) (go fail))
  1602.     (setq tst (car tlist))
  1603.     (setq tlist (cdr tlist))
  1604.     (setq lind (car tlist))
  1605.     (setq tlist (cdr tlist))
  1606.     (setq rind (car tlist))
  1607.     (setq tlist (cdr tlist))
  1608.     ;###        (comment the next line differs in not-left & -right)
  1609.     (setq res (funcall tst (gelm memdp rind) (gelm dp lind)))
  1610.     (cond (res (go tloop))
  1611.       (t (go fail)))
  1612.     fin  (add-token own-mem fp dp c)
  1613.     (and (== c 0.) (sendto fp dp 'left outs)))) 
  1614.  
  1615. (defun not-right (outs mem tests)
  1616.   (prog (fp dp memdp tlist tst lind rind res newfp inc newc)
  1617.     (setq fp *flag-part*)
  1618.     (setq dp *data-part*)
  1619.     (cond ((not fp) (setq inc -1.) (setq newfp 'new))
  1620.       ((eq fp 'new) (setq inc 1.) (setq newfp nil))
  1621.       (t (return nil)))
  1622.     fail (and (null mem) (return nil))
  1623.     (setq memdp (car mem))
  1624.     (setq newc (cadr mem))
  1625.     (setq tlist tests)
  1626.     tloop (and (null tlist) (go succ))
  1627.     (setq tst (car tlist))
  1628.     (setq tlist (cdr tlist))
  1629.     (setq lind (car tlist))
  1630.     (setq tlist (cdr tlist))
  1631.     (setq rind (car tlist))
  1632.     (setq tlist (cdr tlist))
  1633.     ;###        (comment the next line differs in not-left & -right)
  1634.     (setq res (funcall tst (gelm dp rind) (gelm memdp lind)))
  1635.     (cond (res (go tloop))
  1636.       (t (setq mem (cddr mem)) (go fail)))
  1637.     succ (setq newc (+ inc newc))        ;"plus" changed to "+" by gdw
  1638.     (rplaca (cdr mem) newc)
  1639.     (cond ((or (and (== inc -1.) (== newc 0.))
  1640.            (and (== inc 1.) (== newc 1.)))
  1641.        (sendto newfp memdp 'right outs)))
  1642.     (setq mem (cddr mem))
  1643.     (go fail))) 
  1644.  
  1645.  
  1646.  
  1647. ;;; Node memories
  1648.  
  1649.  
  1650. (defun add-token (memlis flag data-part num)
  1651.   (prog (was-present)
  1652.     (cond ((eq flag 'new)
  1653.        (setq was-present nil)
  1654.        (real-add-token memlis data-part num))
  1655.       ((not flag) 
  1656.        (setq was-present (remove-old memlis data-part num)))
  1657.       ((eq flag 'old) (setq was-present t)))
  1658.     (return was-present))) 
  1659.  
  1660. (defun real-add-token (lis data-part num)
  1661.   (setq *current-token* (1+ *current-token*))
  1662.   (cond (num (rplaca lis (cons num (car lis)))))
  1663.   (rplaca lis (cons data-part (car lis)))) 
  1664.  
  1665. (defun remove-old (lis data num)
  1666.   (cond (num (remove-old-num lis data))
  1667.     (t (remove-old-no-num lis data)))) 
  1668.  
  1669. (defun remove-old-num (lis data)
  1670.   (prog (m next last)
  1671.     (setq m (car lis))
  1672.     (cond ((atom m) (return nil))
  1673.       ((top-levels-eq data (car m))
  1674.        (setq *current-token* (1- *current-token*))
  1675.        (rplaca lis (cddr m))
  1676.        (return (car m))))
  1677.     (setq next m)
  1678.     loop (setq last next)
  1679.     (setq next (cddr next))
  1680.     (cond ((atom next) (return nil))
  1681.       ((top-levels-eq data (car next))
  1682.        (rplacd (cdr last) (cddr next))
  1683.        (setq *current-token* (1- *current-token*))
  1684.        (return (car next)))
  1685.       (t (go loop))))) 
  1686.  
  1687. (defun remove-old-no-num (lis data)
  1688.   (prog (m next last)
  1689.     (setq m (car lis))
  1690.     (cond ((atom m) (return nil))
  1691.       ((top-levels-eq data (car m))
  1692.        (setq *current-token* (1- *current-token*))
  1693.        (rplaca lis (cdr m))
  1694.        (return (car m))))
  1695.     (setq next m)
  1696.     loop (setq last next)
  1697.     (setq next (cdr next))
  1698.     (cond ((atom next) (return nil))
  1699.       ((top-levels-eq data (car next))
  1700.        (rplacd last (cdr next))
  1701.        (setq *current-token* (1- *current-token*))
  1702.        (return (car next)))
  1703.       (t (go loop))))) 
  1704.  
  1705.  
  1706.  
  1707. ;;; Conflict Resolution
  1708. ;
  1709. ;
  1710. ; each conflict set element is a list of the following form:
  1711. ; ((p-name . data-part) (sorted wm-recency) special-case-number)
  1712.  
  1713. (defun removecs (name data)
  1714.   (prog (cr-data inst cs)
  1715.     (setq cr-data (cons name data))
  1716.     (setq cs *conflict-set*)
  1717.     loop    (cond ((null cs) 
  1718.                (record-refract name data)
  1719.                (return nil)))
  1720.     (setq inst (car cs))
  1721.     (setq cs (cdr cs))
  1722.     (and (not (top-levels-eq (car inst) cr-data)) (go loop))
  1723.     (setq *conflict-set* (delq inst *conflict-set*))))
  1724.  
  1725. (defun insertcs (name data rating)
  1726.   (prog (instan)
  1727.     (and (refracted name data) (return nil))
  1728.     (setq instan (list (cons name data) (order-tags data) rating))
  1729.     (and (atom *conflict-set*) (setq *conflict-set* nil))
  1730.     (return (setq *conflict-set* (cons instan *conflict-set*))))) 
  1731.  
  1732. (defun order-tags (dat)
  1733.   (prog (tags)
  1734.     (setq tags nil)
  1735.     l1p  (and (atom dat) (go l2p))
  1736.     (setq tags (cons (creation-time (car dat)) tags))
  1737.     (setq dat (cdr dat))
  1738.     (go l1p)
  1739.     l2p  (cond ((eq *strategy* 'mea)
  1740.         (return (cons (car tags) (dsort (cdr tags)))))
  1741.            (t (return (dsort tags)))))) 
  1742.  
  1743. ; destructively sort x into descending order
  1744.  
  1745. (defun dsort (x)
  1746.   (prog (sorted cur next cval nval)
  1747.     (and (atom (cdr x)) (return x))
  1748.     loop (setq sorted t)
  1749.     (setq cur x)
  1750.     (setq next (cdr x))
  1751.     chek (setq cval (car cur))
  1752.     (setq nval (car next))
  1753.     (cond ((> nval cval)
  1754.        (setq sorted nil)
  1755.        (rplaca cur nval)
  1756.        (rplaca next cval)))
  1757.     (setq cur next)
  1758.     (setq next (cdr cur))
  1759.     (cond ((not (null next)) (go chek))
  1760.       (sorted (return x))
  1761.       (t (go loop))))) 
  1762.  
  1763. (defun conflict-resolution nil
  1764.   (prog (best len)
  1765.     (setq len (length *conflict-set*))
  1766.     (cond ((> len *max-cs*) (setq *max-cs* len)))
  1767.     (setq *total-cs* (+ *total-cs* len))    ;"plus" changed to "+" by gdw
  1768.     (cond (*conflict-set*
  1769.        (setq best (best-of *conflict-set*))
  1770.        (setq *conflict-set* (delq best *conflict-set*))
  1771.        (return (pname-instantiation best)))
  1772.       (t (return nil))))) 
  1773.  
  1774. (defun best-of (set) (best-of* (car set) (cdr set))) 
  1775.  
  1776. (defun best-of* (best rem)
  1777.   (cond ((not rem) best)
  1778.     ((conflict-set-compare best (car rem))
  1779.      (best-of* best (cdr rem)))
  1780.     (t (best-of* (car rem) (cdr rem))))) 
  1781.  
  1782. (defun remove-from-conflict-set (name)
  1783.   (prog (cs entry)
  1784.     l1   (setq cs *conflict-set*)
  1785.     l2   (cond ((atom cs) (return nil)))
  1786.     (setq entry (car cs))
  1787.     (setq cs (cdr cs))
  1788.     (cond ((eq name (caar entry))
  1789.        (setq *conflict-set* (delq entry *conflict-set*))
  1790.        (go l1))
  1791.       (t (go l2))))) 
  1792.  
  1793. (defun pname-instantiation (conflict-elem) (car conflict-elem)) 
  1794.  
  1795. (defun order-part (conflict-elem) (cdr conflict-elem)) 
  1796.  
  1797. (defun instantiation (conflict-elem)
  1798.   (cdr (pname-instantiation conflict-elem))) 
  1799.  
  1800.  
  1801. (defun conflict-set-compare (x y)
  1802.   (prog (x-order y-order xl yl xv yv)
  1803.     (setq x-order (order-part x))
  1804.     (setq y-order (order-part y))
  1805.     (setq xl (car x-order))
  1806.     (setq yl (car y-order))
  1807.     data (cond ((and (null xl) (null yl)) (go ps))
  1808.            ((null yl) (return t))
  1809.            ((null xl) (return nil)))
  1810.     (setq xv (car xl))
  1811.     (setq yv (car yl))
  1812.     (cond ((> xv yv) (return t))
  1813.       ((> yv xv) (return nil)))
  1814.     (setq xl (cdr xl))
  1815.     (setq yl (cdr yl))
  1816.     (go data)
  1817.     ps   (setq xl (cdr x-order))
  1818.     (setq yl (cdr y-order))
  1819.     psl  (cond ((null xl) (return t)))
  1820.     (setq xv (car xl))
  1821.     (setq yv (car yl))
  1822.     (cond ((> xv yv) (return t))
  1823.       ((> yv xv) (return nil)))
  1824.     (setq xl (cdr xl))
  1825.     (setq yl (cdr yl))
  1826.     (go psl))) 
  1827.  
  1828.  
  1829. (defun conflict-set nil
  1830.   (prog (cnts cs p z best)
  1831.     (setq cnts nil)
  1832.     (setq cs *conflict-set*)
  1833.     l1p  (and (atom cs) (go l2p))
  1834.     (setq p (caaar cs))
  1835.     (setq cs (cdr cs))
  1836.     (setq z (assq p cnts))
  1837.     (cond ((null z) (setq cnts (cons (cons p 1.) cnts)))
  1838.       (t (rplacd z (1+ (cdr z)))))
  1839.     (go l1p)
  1840.     l2p  (cond ((atom cnts)
  1841.         (setq best (best-of *conflict-set*))
  1842.         (terpri)
  1843.         (return (list (caar best) 'dominates))))
  1844.     (terpri)
  1845.     (princ (caar cnts))
  1846.     (cond ((> (cdar cnts) 1.)
  1847.        (princ '|    (|)
  1848.           (princ (cdar cnts))
  1849.           (princ '| occurrences)|)))
  1850.     (setq cnts (cdr cnts))
  1851.     (go l2p))) 
  1852.  
  1853.  
  1854.  
  1855. ;;; WM maintaining functions
  1856. ;
  1857. ; The order of operations in the following two functions is critical.
  1858. ; add-to-wm order: (1) change wm (2) record change (3) match 
  1859. ; remove-from-wm order: (1) record change (2) match (3) change wm
  1860. ; (back will not restore state properly unless wm changes are recorded
  1861.     ; before the cs changes that they cause)  (match will give errors if 
  1862.                              ; the thing matched is not in wm at the time)
  1863.  
  1864.  
  1865. (defun add-to-wm (wme override)
  1866.   (prog (fa z part timetag port)
  1867.     (setq *critical* t)
  1868.     (setq *current-wm* (1+ *current-wm*))
  1869.     (and (> *current-wm* *max-wm*) (setq *max-wm* *current-wm*))
  1870.     (setq *action-count* (1+ *action-count*))
  1871.     (setq fa (wm-hash wme))
  1872.     (or (member fa *wmpart-list*)
  1873.     (setq *wmpart-list* (cons fa *wmpart-list*)))
  1874.     (setq part (get fa 'wmpart*))
  1875.     (cond (override (setq timetag override))
  1876.       (t (setq timetag *action-count*)))
  1877.     (setq z (cons wme timetag))
  1878.     (putprop fa (cons z part) 'wmpart*)
  1879.     (record-change '=>wm *action-count* wme)
  1880.     (match 'new wme)
  1881.     (setq *critical* nil)
  1882.     (cond ((and *in-rhs* *wtrace*)
  1883.        (setq port (trace-file))
  1884.        (terpri port)
  1885.        (princ '|=>wm: | port)
  1886.        (ppelm wme port))))) 
  1887.  
  1888. ; remove-from-wm uses eq, not equal to determine if wme is present
  1889.  
  1890. (defun remove-from-wm (wme)
  1891.   (prog (fa z part timetag port)
  1892.     (setq fa (wm-hash wme))
  1893.     (setq part (get fa 'wmpart*))
  1894.     (setq z (assq wme part))
  1895.     (or z (return nil))
  1896.     (setq timetag (cdr z))
  1897.     (cond ((and *wtrace* *in-rhs*)
  1898.        (setq port (trace-file))
  1899.        (terpri port)
  1900.        (princ '|<=wm: | port)
  1901.        (ppelm wme port)))
  1902.     (setq *action-count* (1+ *action-count*))
  1903.     (setq *critical* t)
  1904.     (setq *current-wm* (1- *current-wm*))
  1905.     (record-change '<=wm timetag wme)
  1906.     (match nil wme)
  1907.     (putprop fa (delq z part) 'wmpart*)
  1908.     (setq *critical* nil))) 
  1909.  
  1910. ; mapwm maps down the elements of wm, applying fn to each element
  1911. ; each element is of form (datum . creation-time)
  1912.  
  1913. (defun mapwm (fn)
  1914.   (prog (wmpl part)
  1915.     (setq wmpl *wmpart-list*)
  1916.     lab1 (cond ((atom wmpl) (return nil)))
  1917.     (setq part (get (car wmpl) 'wmpart*))
  1918.     (setq wmpl (cdr wmpl))
  1919.     (mapc fn part)
  1920.     (go lab1))) 
  1921.  
  1922. (defun ops-wm (a) 
  1923.   (mapc (function (lambda (z) (terpri) (ppelm z *standard-output*))) 
  1924.     (get-wm a))
  1925.   nil) 
  1926.  
  1927. (defun get-wm (z)
  1928.   (setq *wm-filter* z)
  1929.   (setq *wm* nil)
  1930.   (mapwm (function get-wm2))
  1931.   (prog2 nil *wm* (setq *wm* nil))) 
  1932.  
  1933. (defun get-wm2 (elem) 
  1934.   ; (cond ((or (null *wm-filter*) (member (cdr elem) *wm-filter*) :test #'equal)))
  1935.   (cond ((or (null *wm-filter*) (member (cdr elem) *wm-filter*)) ;test #'equal)
  1936.     (setq *wm* (cons (car elem) *wm*)))))
  1937.  
  1938. (defun wm-hash (x)
  1939.   (cond ((not x) '<default>)
  1940.     ((not (car x)) (wm-hash (cdr x)))
  1941.     ((symbolp (car x)) (car x))
  1942.     (t (wm-hash (cdr x))))) 
  1943.  
  1944. (defun creation-time (wme)
  1945.   (cdr (assq wme (get (wm-hash wme) 'wmpart*)))) 
  1946.  
  1947. (defun refresh nil
  1948.   (prog nil
  1949.     (setq *old-wm* nil)
  1950.     (mapwm (function refresh-collect))
  1951.     (mapc (function refresh-del) *old-wm*)
  1952.     (mapc (function refresh-add) *old-wm*)
  1953.     (setq *old-wm* nil))) 
  1954.  
  1955. (defun refresh-collect (x) (setq *old-wm* (cons x *old-wm*))) 
  1956.  
  1957. (defun refresh-del (x) (remove-from-wm (car x))) 
  1958.  
  1959. (defun refresh-add (x) (add-to-wm (car x) (cdr x))) 
  1960.  
  1961. (defun trace-file ()
  1962.   (prog (port)
  1963.     (setq port *standard-output*)
  1964.     (cond (*trace-file*
  1965.        (setq port ($ofile *trace-file*))
  1966.        (cond ((null port)
  1967.           (%warn '|trace: file has been closed| *trace-file*)
  1968.           (setq port *standard-output*)))))
  1969.     (return port)))
  1970.  
  1971.  
  1972. ;;; Basic functions for RHS evaluation
  1973.  
  1974. (defun eval-rhs (pname data)
  1975.   (prog (node port)
  1976.     (cond (*ptrace*
  1977.        (setq port (trace-file))
  1978.        (terpri port)
  1979.        (princ *cycle-count* port)
  1980.        (princ '|. | port)
  1981.        (princ pname port)
  1982.        (time-tag-print data port)))
  1983.     (setq *data-matched* data)
  1984.     (setq *p-name* pname)
  1985.     (setq *last* nil)
  1986.     (setq node (get pname 'topnode))
  1987.     (init-var-mem (var-part node))
  1988.     (init-ce-var-mem (ce-var-part node))
  1989.     (begin-record pname data)
  1990.     (setq *in-rhs* t)
  1991.     (eval (rhs-part node))
  1992.     (setq *in-rhs* nil)
  1993.     (end-record))) 
  1994.  
  1995. (defun time-tag-print (data port)
  1996.   (cond ((not (null data))
  1997.      (time-tag-print (cdr data) port)
  1998.      (princ '| | port)
  1999.      (princ (creation-time (car data)) port))))
  2000.  
  2001. (defun init-var-mem (vlist)
  2002.   (prog (v ind r)
  2003.     (setq *variable-memory* nil)
  2004.     top  (and (atom vlist) (return nil))
  2005.     (setq v (car vlist))
  2006.     (setq ind (cadr vlist))
  2007.     (setq vlist (cddr vlist))
  2008.     (setq r (gelm *data-matched* ind))
  2009.     (setq *variable-memory* (cons (cons v r) *variable-memory*))
  2010.     (go top))) 
  2011.  
  2012. (defun init-ce-var-mem (vlist)
  2013.   (prog (v ind r)
  2014.     (setq *ce-variable-memory* nil)
  2015.     top  (and (atom vlist) (return nil))
  2016.     (setq v (car vlist))
  2017.     (setq ind (cadr vlist))
  2018.     (setq vlist (cddr vlist))
  2019.     (setq r (ce-gelm *data-matched* ind))
  2020.     (setq *ce-variable-memory*
  2021.       (cons (cons v r) *ce-variable-memory*))
  2022.     (go top))) 
  2023.  
  2024. (defun make-ce-var-bind (var elem)
  2025.   (setq *ce-variable-memory*
  2026.     (cons (cons var elem) *ce-variable-memory*))) 
  2027.  
  2028. (defun make-var-bind (var elem)
  2029.   (setq *variable-memory* (cons (cons var elem) *variable-memory*))) 
  2030.  
  2031. (defun $varbind (x)
  2032.   (prog (r)
  2033.     (and (not *in-rhs*) (return x))
  2034.     (setq r (assq x *variable-memory*))
  2035.     (cond (r (return (cdr r)))
  2036.       (t (return x))))) 
  2037.  
  2038. (defun get-ce-var-bind (x)
  2039.   (prog (r)
  2040.     (cond ((numberp x) (return (get-num-ce x))))
  2041.     (setq r (assq x *ce-variable-memory*))
  2042.     (cond (r (return (cdr r)))
  2043.       (t (return nil))))) 
  2044.  
  2045. (defun get-num-ce (x)
  2046.   (prog (r l d)
  2047.     (setq r *data-matched*)
  2048.     (setq l (length r))
  2049.     (setq d (- l x))
  2050.     (and (> 0. d) (return nil))
  2051.     la   (cond ((null r) (return nil))
  2052.            ((> 1. d) (return (car r))))
  2053.     (setq d (1- d))
  2054.     (setq r (cdr r))
  2055.     (go la))) 
  2056.  
  2057.  
  2058. (defun build-collect (z)
  2059.   (prog (r)
  2060.     la   (and (atom z) (return nil))
  2061.     (setq r (car z))
  2062.     (setq z (cdr z))
  2063.     (cond ((consp  r)    ;dtpr\consp gdw
  2064.        ($value '\()
  2065.            (build-collect r)
  2066.            ($value '\)))
  2067.       ((eq r '\\) ($change (car z)) (setq z (cdr z)))
  2068.       (t ($value r)))
  2069.     (go la))) 
  2070.  
  2071. (defun unflat (x) (setq *rest* x) (unflat*)) 
  2072.  
  2073. (defun unflat* nil
  2074.   (prog (c)
  2075.     (cond ((atom *rest*) (return nil)))
  2076.     (setq c (car *rest*))
  2077.     (setq *rest* (cdr *rest*))
  2078.     (cond ((eq c '\() (return (cons (unflat*) (unflat*))))
  2079.        ((eq c '\)) (return nil))
  2080.       (t (return (cons c (unflat*))))))) 
  2081.  
  2082.  
  2083. (defun $change (x)
  2084.   (prog nil
  2085.     (cond ((consp  x) (eval-function x))    ;dtpr\consp gdw
  2086.       (t ($value ($varbind x)))))) 
  2087.  
  2088. (defun eval-args (z)
  2089.   (prog (r)
  2090.     (rhs-tab 1.)
  2091.     la   (and (atom z) (return nil))
  2092.     (setq r (car z))
  2093.     (setq z (cdr z))
  2094.     (cond ((EQ R '^)
  2095.        (RHS-tab (car z))
  2096.        (setq r (cadr z))
  2097.        (setq z (cddr z))))
  2098.     (cond ((eq r '//) ($value (car z)) (setq z (cdr z)))
  2099.       (t ($change r)))
  2100.     (go la))) 
  2101.  
  2102.  
  2103. (defun eval-function (form)
  2104.   (cond ((not *in-rhs*)
  2105.      (%warn '|functions cannot be used at top level| (car form)))
  2106.     (t (eval form))))
  2107.  
  2108.  
  2109. ;;; Functions to manipulate the result array
  2110.  
  2111.  
  2112. (defun $reset nil
  2113.   (setq *max-index* 0.)
  2114.   (setq *next-index* 1.)) 
  2115.  
  2116. ; rhs-tab implements the tab ('^') function in the rhs.  it has
  2117. ; four responsibilities:
  2118. ;    - to move the array pointers
  2119. ;    - to watch for tabbing off the left end of the array
  2120. ;      (ie, to watch for pointers less than 1)
  2121. ;    - to watch for tabbing off the right end of the array
  2122. ;    - to write nil in all the slots that are skipped
  2123. ; the last is necessary if the result array is not to be cleared
  2124. ; after each use; if rhs-tab did not do this, $reset
  2125. ; would be much slower.
  2126.  
  2127. (defun rhs-tab (z) ($tab ($varbind z)))
  2128.  
  2129. (defun $tab (z)
  2130.   (prog (edge next)
  2131.     (setq next ($litbind z))
  2132.     (and (floatp next) (setq next (fix next)))
  2133.     (cond ((or (not (numberp next)) 
  2134.            (> next *size-result-array*)
  2135.            (> 1. next))                ; ( '| |)
  2136.        (%warn '|illegal index after ^| next)
  2137.        (return *next-index*)))
  2138.     (setq edge (- next 1.))
  2139.     (cond ((> *max-index* edge) (go ok)))
  2140.     clear (cond ((== *max-index* edge) (go ok)))
  2141.     (putvector *result-array* edge nil)
  2142.     (setq edge (1- edge))
  2143.     (go clear)
  2144.     ok   (setq *next-index* next)
  2145.     (return next))) 
  2146.  
  2147. (defun $value (v)
  2148.   (cond ((> *next-index* *size-result-array*)
  2149.      (%warn '|index too large| *next-index*))
  2150.     (t
  2151.      (and (> *next-index* *max-index*)
  2152.           (setq *max-index* *next-index*))
  2153.      (putvector *result-array* *next-index* v)
  2154.      (setq *next-index* (1+ *next-index*))))) 
  2155.  
  2156.  
  2157. (defun use-result-array nil
  2158.   (prog (k r)
  2159.     (setq k *max-index*)
  2160.     (setq r nil)
  2161.     top  (and (== k 0.) (return r))
  2162.     (setq r (cons (getvector *result-array* k) r))
  2163.     (setq k (1- k))
  2164.     (go top))) 
  2165.  
  2166. (defun $assert nil
  2167.   (setq *last* (use-result-array))
  2168.   (add-to-wm *last* nil))
  2169.  
  2170. (defun $parametercount nil *max-index*)
  2171.  
  2172. (defun $parameter (k)
  2173.   (cond ((or (not (numberp k)) (> k *size-result-array*) (< k 1.))
  2174.      (%warn '|illegal parameter number | k)
  2175.      nil)
  2176.     ((> k *max-index*) nil)
  2177.     (t (getvector *result-array* k))))
  2178.  
  2179.  
  2180. ;;; RHS actions
  2181.  
  2182. (defun ops-make (z)
  2183.   (prog nil
  2184.     ($reset)
  2185.     (eval-args z)
  2186.     ($assert))) 
  2187.  
  2188. (defun ops-modify (z)
  2189.   (prog (old)
  2190.     (cond ((not *in-rhs*)
  2191.        (%warn '|cannot be called at top level| 'modify)
  2192.        (return nil)))
  2193.     (setq old (get-ce-var-bind (car z)))
  2194.     (cond ((null old)
  2195.        (%warn '|modify: first argument must be an element variable|
  2196.           (car z))
  2197.        (return nil)))
  2198.     (remove-from-wm old)
  2199.     (setq z (cdr z))
  2200.     ($reset)
  2201.     copy (and (atom old) (go fin))
  2202.     ($change (car old))
  2203.     (setq old (cdr old))
  2204.     (go copy)
  2205.     fin  (eval-args z)
  2206.     ($assert))) 
  2207.  
  2208. (defun ops-bind (z)
  2209.   (prog (val)
  2210.     (cond ((not *in-rhs*)
  2211.        (%warn '|cannot be called at top level| 'bind)
  2212.        (return nil)))
  2213.     (cond ((< (length z) 1.)
  2214.        (%warn '|bind: wrong number of arguments to| z)
  2215.        (return nil))
  2216.       ((not (symbolp (car z)))
  2217.        (%warn '|bind: illegal argument| (car z))
  2218.        (return nil))
  2219.       ((= (length z) 1.) (setq val (gensym)))
  2220.       (t ($reset)
  2221.          (eval-args (cdr z))
  2222.          (setq val ($parameter 1.))))
  2223.     (make-var-bind (car z) val))) 
  2224.  
  2225. (defun ops-cbind (z)
  2226.   (cond ((not *in-rhs*)
  2227.      (%warn '|cannot be called at top level| 'cbind))
  2228.     ((not (= (length z) 1.))
  2229.      (%warn '|cbind: wrong number of arguments| z))
  2230.     ((not (symbolp (car z)))
  2231.      (%warn '|cbind: illegal argument| (car z)))
  2232.     ((null *last*)
  2233.      (%warn '|cbind: nothing added yet| (car z)))
  2234.     (t (make-ce-var-bind (car z) *last*)))) 
  2235.  
  2236. (defun ops-remove (z)
  2237.   (prog (old)
  2238.     (and (not *in-rhs*)(return (top-level-remove z)))
  2239.     top  (and (atom z) (return nil))
  2240.     (setq old (get-ce-var-bind (car z)))
  2241.     (cond ((null old)
  2242.        (%warn '|remove: argument not an element variable| (car z))
  2243.        (return nil)))
  2244.     (remove-from-wm old)
  2245.     (setq z (cdr z))
  2246.     (go top))) 
  2247.  
  2248.  
  2249.  
  2250. (defun ops-call (z)
  2251.   (prog (f)
  2252.     (setq f (car z))
  2253.     ($reset)
  2254.     (eval-args (cdr z))
  2255.     (funcall f))) 
  2256.  
  2257.  
  2258.  
  2259. ;;; Dario Giuse - rewrote the (write) function to follow OPS-5 specifications.
  2260. ;;;
  2261. (defmacro append-string (x)
  2262.   `(setq wrstring (concatenate 'simple-string wrstring ,x)))
  2263.  
  2264. (defun ops-write (z)
  2265.   (prog (port max lastcol k x)
  2266.     (declare (ignore lastcol))
  2267.     (cond ((not *in-rhs*)
  2268.        (%warn '|cannot be called at top level| 'write)
  2269.        (return nil)))
  2270.     ($reset)
  2271.     (eval-args z)
  2272.     (setq lastcol 0)
  2273.     (setq max ($parametercount))
  2274.     (cond ((< max 1)
  2275.        (%warn '|write: nothing to print| z)
  2276.        (return nil)))
  2277.     (setq x ($parameter 1))
  2278.     (cond ((and (symbolp x) ($ofile x)) 
  2279.        (setq port ($ofile x))
  2280. ; @@@ bug fix - this was a SET: GDW -- Wed Jul 18 18:36:07 1984
  2281.        (setq k 2))
  2282.       (t
  2283.        (setq port (default-write-file))
  2284.        (setq k 1)))
  2285.     ;; Analyze and output all the parameters (write) was passed.
  2286.     (do* ((wrstring "")
  2287.       (x ($parameter k) ($parameter k))
  2288.       field-width)
  2289.      ((> k max)
  2290.       (format port wrstring)
  2291.       (force-output))     ; Dario Giuse - added to force output
  2292.       (incf k)
  2293.       (case x
  2294.     ('|=== C R L F ===|
  2295.      (format port "~A~%" wrstring)     ; Flush the previous line
  2296.      (setq wrstring ""))
  2297.     ('|=== R J U S T ===|
  2298.      (setq field-width ($parameter k))           ; Number following (tabto)
  2299.      (incf k)
  2300.      (setq x (format nil "~A" ($parameter k)))   ; Next field to print
  2301.      (when (<= (length x) field-width)
  2302.        ;; Right-justify field
  2303.        (append-string (format nil "~V@A" field-width x))
  2304.        (incf k)))   ; Skip next field, since we printed it already
  2305.     ('|=== T A B T O ===|
  2306.      (setq x ($parameter k))         ; Position to tab to
  2307.      (incf k)
  2308.      (when (< x (length wrstring))
  2309.        ;; Flush line, start a new one
  2310.        (format port "~A~%" wrstring)
  2311.        (setq wrstring ""))
  2312.      (append-string (format nil "~V,@T" (- x (length wrstring) 1))))
  2313.     (t
  2314.      (append-string (format nil "~A " x)))))))
  2315.  
  2316.  
  2317.  
  2318.  
  2319. (defun default-write-file ()
  2320.   (prog (port)
  2321.     (setq port *standard-output*)
  2322.     (cond (*write-file*
  2323.        (setq port ($ofile *write-file*))
  2324.        (cond ((null port) 
  2325.           (%warn '|write: file has been closed| *write-file*)
  2326.           (setq port *standard-output*)))))
  2327.     (return port)))
  2328.  
  2329. (defun do-rjust (width value port)
  2330.   (prog (size)
  2331.     (cond ((eq value '|=== T A B T O ===|)
  2332.        (%warn '|rjust cannot precede this function| 'tabto)
  2333.        (return nil))
  2334.       ((eq value '|=== C R L F ===|)
  2335.        (%warn '|rjust cannot precede this function| 'crlf)
  2336.        (return nil))
  2337.       ((eq value '|=== R J U S T ===|)
  2338.        (%warn '|rjust cannot precede this function| 'rjust)
  2339.        (return nil)))
  2340.     ;original->        (setq size (flatc value (1+ width)))
  2341.     (setq size (min value (1+ width)))  ;### KLUGE
  2342.     (cond ((> size width)
  2343.        (princ '| | port)
  2344.        (princ value port)
  2345.        (return nil)))
  2346.     ;###        (do k (- width size) (1- k) (not (> k 0)) (princ '| | port))
  2347.     ;^^^KLUGE @@@do
  2348.     (princ value port)))
  2349.  
  2350. (defun do-tabto (col port)
  2351.   (prog (pos)
  2352.     ;### KLUGE: FLUSHES STREAM & SETS POS TO 0
  2353.     ;OIRGINAL->    (setq pos (1+ (nwritn port)))    ;hmm-takes 1 arg @@@ port
  2354.     (finish-output port);kluge
  2355.     (setq pos 0);kluge
  2356.     (cond ((> pos col)
  2357.        (terpri port)
  2358.        (setq pos 1)))
  2359.     ;###(do k (- col pos) (1- k) (not (> k 0)) (princ '| | port))
  2360.     ;^^^KLUGE @@@do
  2361.     (return nil)))
  2362.  
  2363.  
  2364. (defun halt nil 
  2365.   (cond ((not *in-rhs*)
  2366.      (%warn '|cannot be called at top level| 'halt))
  2367.     (t (setq *halt-flag* t)))) 
  2368.  
  2369. (defun ops-build (z)
  2370.   (prog (r)
  2371.     (cond ((not *in-rhs*)
  2372.        (%warn '|cannot be called at top level| 'build)
  2373.        (return nil)))
  2374.     ($reset)
  2375.     (build-collect z)
  2376.     (setq r (unflat (use-result-array)))
  2377.     (and *build-trace* (funcall *build-trace* r))
  2378.     (compile-production (car r) (cdr r)))) 
  2379.  
  2380. (defun ops-openfile (z)
  2381.   (prog (file mode id)
  2382.     ($reset)
  2383.     (eval-args z)
  2384.     (cond ((not (equal ($parametercount) 3.))
  2385.        (%warn '|openfile: wrong number of arguments| z)
  2386.        (return nil)))
  2387.     (setq id ($parameter 1))
  2388.     (setq file ($parameter 2))
  2389.     (setq mode ($parameter 3))
  2390.     (cond ((not (symbolp id))
  2391.        (%warn '|openfile: file id must be a symbolic atom| id)
  2392.        (return nil))
  2393.       ((null id)
  2394.        (%warn '|openfile: 'nil' is reserved for the terminal| nil)
  2395.        (return nil))
  2396.       ((or ($ifile id)($ofile id))
  2397.        (%warn '|openfile: name already in use| id)
  2398.        (return nil)))
  2399.     ;@@@    (cond ((eq mode 'in) (putprop id (infile file) 'inputfile))
  2400.               ;@@@          ((eq mode 'out) (putprop id (outfile file) 'outputfile))
  2401.               ; dec 7 83 gdw added setq : is putprop needed ? )
  2402.     (cond ((eq mode 'in) (putprop id (setq id (infile file)) 'inputfile))
  2403.       ((eq mode 'out) (putprop id (setq id (outfile file)) 'outputfile))
  2404.       (t (%warn '|openfile: illegal mode| mode)
  2405.          (return nil)))
  2406.     (return nil)))
  2407.  
  2408. (defun $ifile (x) 
  2409.   (cond ((symbolp x) (get x 'inputfile))
  2410.     (t nil)))
  2411.  
  2412. (defun $ofile (x) 
  2413.   (cond ((symbolp x) (get x 'outputfile))
  2414.     (t nil)))
  2415.  
  2416.  
  2417. (defun ops-closefile (z)
  2418.   ($reset)
  2419.   (eval-args z)
  2420.   (mapc (function closefile2) (use-result-array)))
  2421.  
  2422. (defun closefile2 (file)
  2423.   (prog (port)
  2424.     (cond ((not (symbolp file))
  2425.        (%warn '|closefile: illegal file identifier| file))
  2426.       ((setq port ($ifile file))
  2427.        (close port)
  2428.        (remprop file 'inputfile))
  2429.       ((setq port ($ofile file))
  2430.        (close port)
  2431.        (remprop file 'outputfile)))
  2432.     (return nil)))
  2433.  
  2434.  
  2435. (defun ops-default (z)
  2436.   (prog (file use)
  2437.     ($reset)
  2438.     (eval-args z)
  2439.     (cond ((not (equal ($parametercount) 2.))
  2440.        (%warn '|default: wrong number of arguments| z)
  2441.        (return nil)))
  2442.     (setq file ($parameter 1))
  2443.     (setq use ($parameter 2))
  2444.     (cond ((not (symbolp file))
  2445.        (%warn '|default: illegal file identifier| file)
  2446.        (return nil))
  2447.       ((not (member use '(write accept trace) :test #'equal))
  2448.        (%warn '|default: illegal use for a file| use)
  2449.        (return nil))
  2450.       ((and (member use '(write trace) :test #'equal)
  2451.         (not (null file))
  2452.         (not ($ofile file)))
  2453.        (%warn '|default: file has not been opened for output| file)
  2454.        (return nil))
  2455.       ((and (equal use 'accept) 
  2456.         (not (null file))
  2457.         (not ($ifile file)))
  2458.        (%warn '|default: file has not been opened for input| file)
  2459.        (return nil))
  2460.       ((equal use 'write) (setq *write-file* file))
  2461.       ((equal use 'accept) (setq *accept-file* file))
  2462.       ((equal use 'trace) (setq *trace-file* file)))
  2463.     (return nil)))
  2464.  
  2465.  
  2466.  
  2467. ;;; ------------------------------------------------------------ RHS Functions
  2468.  
  2469. (defun flat-value (x)
  2470.   (cond ((atom x) ($value x))
  2471.     (t (mapc (function flat-value) x)))) 
  2472.  
  2473.  
  2474. (defun ops-accept (z)
  2475.   (prog (port arg)
  2476.     (cond ((> (length z) 1.)
  2477.        (%warn '|accept: wrong number of arguments| z)
  2478.        (return nil)))
  2479.     (setq port *standard-input*)
  2480.     (cond (*accept-file*
  2481.        (setq port ($ifile *accept-file*))
  2482.        (cond ((null port) 
  2483.           (%warn '|accept: file has been closed| *accept-file*)
  2484.           (return nil)))))
  2485.     (cond ((= (length z) 1)
  2486.        (setq arg ($varbind (car z)))
  2487.        (cond ((not (symbolp arg))
  2488.           (%warn '|accept: illegal file name| arg)
  2489.           (return nil)))
  2490.        (setq port ($ifile arg))
  2491.        (cond ((null port) 
  2492.           (%warn '|accept: file not open for input| arg)
  2493.           (return nil)))))
  2494.     (cond ((equal (peek-char t port nil "eof" ) "eof" )
  2495.        ($value 'end-of-file)
  2496.        (return nil)))
  2497.     (flat-value (read port)))) 
  2498.  
  2499.  
  2500.  
  2501. ;;; Dario Giuse - completely changed the algorithm. It now uses one read-line
  2502. ;;; and the read-from-string.
  2503. ;;;
  2504. (defun ops-acceptline (z)
  2505.   (let ((port *standard-input*)
  2506.     (def z))
  2507.     (cond (*accept-file*
  2508.        (setq port ($ifile *accept-file*))
  2509.        (cond ((null port) 
  2510.           (%warn '|acceptline: file has been closed| 
  2511.              *accept-file*)
  2512.           (return-from ops-acceptline nil)))))
  2513.     (cond ((> (length def) 0)
  2514.        (let ((arg ($varbind (car def))))
  2515.          (cond ((and (symbolp arg) ($ifile arg))
  2516.             (setq port ($ifile arg))
  2517.             (setq def (cdr def)))))))
  2518.     (let ((line (read-line port nil 'eof)))
  2519.       (declare (simple-string line))
  2520.       ;; Strip meaningless characters from start and end of string.
  2521.       (setq line (string-trim '(#\( #\) #\, #\tab #\space) line))
  2522.       (when (equal line "")
  2523.     (mapc (function $change) def)
  2524.     (return-from ops-acceptline nil))
  2525.       (setq line (concatenate 'simple-string "(" line ")"))
  2526.       ;; Read all items from the line
  2527.       (flat-value (read-from-string line)))))
  2528.  
  2529.  
  2530.  
  2531.  
  2532. (defun ops-substr (l)
  2533.   (prog (k elm start end)
  2534.     (cond ((not (= (length l) 3.))
  2535.        (%warn '|substr: wrong number of arguments| l)
  2536.        (return nil)))
  2537.     (setq elm (get-ce-var-bind (car l)))
  2538.     (cond ((null elm)
  2539.        (%warn '|first argument to substr must be a ce var|
  2540.           l)
  2541.        (return nil)))
  2542.     (setq start ($varbind (cadr l)))
  2543.     (setq start ($litbind start))
  2544.     (cond ((not (numberp start))
  2545.        (%warn '|second argument to substr must be a number|
  2546.           l)
  2547.        (return nil)))
  2548.     ;###    (comment |if a variable is bound to INF, the following|
  2549.              ;     |will get the binding and treat it as INF is|
  2550.              ;     |always treated.  that may not be good|)
  2551.     (setq end ($varbind (caddr l)))
  2552.     (cond ((eq end 'inf) (setq end (length elm))))
  2553.     (setq end ($litbind end))
  2554.     (cond ((not (numberp end))
  2555.        (%warn '|third argument to substr must be a number|
  2556.           l)
  2557.        (return nil)))
  2558.     ;###    (comment |this loop does not check for the end of elm|
  2559.              ;         |instead it relies on cdr of nil being nil|
  2560.              ;         |this may not work in all versions of lisp|)
  2561.     (setq k 1.)
  2562.     la   (cond ((> k end) (return nil))
  2563.            ((not (< k start)) ($value (car elm))))
  2564.     (setq elm (cdr elm))
  2565.     (setq k (1+ k))
  2566.     (go la))) 
  2567.  
  2568.  
  2569. (defun ops-compute (z) ($value (ari z))) 
  2570.  
  2571. ; arith is the obsolete form of compute
  2572. (defun ops-arith (z) ($value (ari z))) 
  2573.  
  2574. (defun ari (x)
  2575.   (cond ((atom x)
  2576.      (%warn '|bad syntax in arithmetic expression | x)
  2577.      0.)
  2578.     ((atom (cdr x)) (ari-unit (car x)))
  2579.     ((eq (cadr x) '+)
  2580.      (+ (ari-unit (car x)) (ari (cddr x))))
  2581.     ;"plus" changed to "+" by gdw
  2582.     ((eq (cadr x) '-)
  2583.      (- (ari-unit (car x)) (ari (cddr x))))
  2584.     ((eq (cadr x) '*)
  2585.      (* (ari-unit (car x)) (ari (cddr x))))
  2586.     ((eq (cadr x) '//)
  2587.      (floor (ari-unit (car x)) (ari (cddr x))))   ;@@@ quotient? /
  2588.     ;@@@ kluge only works for integers
  2589.     ;@@@ changed to floor by jcp (from round)
  2590.     ((eq (cadr x) '\\)
  2591.      (mod (fix (ari-unit (car x))) (fix (ari (cddr x)))))
  2592.     (t (%warn '|bad syntax in arithmetic expression | x) 0.))) 
  2593.  
  2594. (defun ari-unit (a)
  2595.   (prog (r)
  2596.     (cond ((consp  a) (setq r (ari a)))    ;dtpr\consp gdw
  2597.       (t (setq r ($varbind a))))
  2598.     (cond ((not (numberp r))
  2599.        (%warn '|bad value in arithmetic expression| a)
  2600.        (return 0.))
  2601.       (t (return r))))) 
  2602.  
  2603. (defun genatom nil ($value (gensym))) 
  2604.  
  2605. (defun ops-litval (z)
  2606.   (prog (r)
  2607.     (cond ((not (= (length z) 1.))
  2608.        (%warn '|litval: wrong number of arguments| z)
  2609.        ($value 0) 
  2610.        (return nil))
  2611.       ((numberp (car z)) ($value (car z)) (return nil)))
  2612.     (setq r ($litbind ($varbind (car z))))
  2613.     (cond ((numberp r) ($value r) (return nil)))
  2614.     (%warn '|litval: argument has no literal binding| (car z))
  2615.     ($value 0)))
  2616.  
  2617.  
  2618. (defun ops-rjust (z)
  2619.   (prog (val)
  2620.     (cond ((not (= (length z) 1.))
  2621.        (%warn '|rjust: wrong number of arguments| z)
  2622.        (return nil)))
  2623.     (setq val ($varbind (car z)))
  2624.     (cond ((or (not (numberp val)) (< val 1.) (> val 127.))
  2625.        (%warn '|rjust: illegal value for field width| val)
  2626.        (return nil)))
  2627.     ($value '|=== R J U S T ===|)
  2628.     ($value val)))
  2629.  
  2630.  
  2631. (defun ops-crlf (z)
  2632.   (cond  (z (%warn '|crlf: does not take arguments| z))
  2633.      (t ($value '|=== C R L F ===|))))
  2634.  
  2635.  
  2636. (defun ops-tabto (z)
  2637.   (prog (val)
  2638.     (cond ((not (= (length z) 1.))
  2639.        (%warn '|tabto: wrong number of arguments| z)
  2640.        (return nil)))
  2641.     (setq val ($varbind (car z)))
  2642.     (cond ((or (not (numberp val)) (< val 1.) (> val 127.))
  2643.        (%warn '|tabto: illegal column number| z)
  2644.        (return nil)))
  2645.     ($value '|=== T A B T O ===|)
  2646.     ($value val)))
  2647.  
  2648.  
  2649.  
  2650. ;;; Printing WM
  2651.  
  2652. (defun ops-ppwm (avlist)
  2653.   (prog (next a)
  2654.     (setq *filters* nil)
  2655.     (setq next 1.)
  2656.     loop   (and (atom avlist) (go print))
  2657.     (setq a (car avlist))
  2658.     (setq avlist (cdr avlist))
  2659.     ;this must be expecting (ppwm class ^ attr ^ attr2 ...) not ^attr
  2660.     (cond ((eq a '^)
  2661.        (setq next (car avlist))
  2662.        (setq avlist (cdr avlist))
  2663.        (setq next ($litbind next))
  2664.        (and (floatp next) (setq next (fix next)))
  2665.        (cond ((or (not (numberp next))
  2666.               (> next *size-result-array*)
  2667.               (> 1. next))
  2668.           (%warn '|illegal index after ^| next)
  2669.           (return nil))))
  2670.       ((variablep a)
  2671.        (%warn '|ppwm does not take variables| a)
  2672.        (return nil))
  2673.       (t (setq *filters* (cons next (cons a *filters*)))
  2674.          (setq next (1+ next))))
  2675.     (go loop)
  2676.     print (mapwm (function ppwm2))
  2677.     (terpri)
  2678.     (return nil))) 
  2679.  
  2680. (defun ppwm2 (elm-tag)
  2681.   (cond ((filter (car elm-tag))
  2682.      (terpri) (ppelm (car elm-tag) (default-write-file))))) 
  2683.  
  2684. (defun filter (elm)
  2685.   (prog (fl indx val)
  2686.     (setq fl *filters*)
  2687.     top  (and (atom fl) (return t))
  2688.     (setq indx (car fl))
  2689.     (setq val (cadr fl))
  2690.     (setq fl (cddr fl))
  2691.     (and (ident (nth (1- indx) elm) val) (go top))
  2692.     (return nil))) 
  2693.  
  2694. (defun ident (x y)
  2695.   (cond ((eq x y) t)
  2696.     ((not (numberp x)) nil)
  2697.     ((not (numberp y)) nil)
  2698.     ((=alg x y) t)
  2699.     (t nil))) 
  2700.  
  2701. ; the new ppelm is designed especially to handle literalize format
  2702. ; however, it will do as well as the old ppelm on other formats
  2703.  
  2704. (defun ppelm (elm port)
  2705.   (prog (ppdat sep val att mode lastpos)
  2706.     (princ (creation-time elm) port)
  2707.     (princ '|:  | port)
  2708.     (setq mode 'vector)
  2709.     (setq ppdat (get (car elm) 'ppdat))
  2710.     (and ppdat (setq mode 'a-v))
  2711.     (setq sep "(")                ; ")" 
  2712.     (setq lastpos 0)
  2713.     (do ((curpos 1 (1+ curpos)) (vlist elm (cdr vlist)))
  2714.     ((atom vlist) nil)                    ; terminate
  2715.       (setq val (car vlist))                ; tagbody begin
  2716.       (setq att (assoc curpos ppdat))    ;should ret (curpos attr-name) 
  2717.       (cond (att (setq att (cdr att)))    ; att = (attr-name) ??
  2718.         (t (setq att curpos)))
  2719.       (and (symbolp att) (is-vector-attribute att) (setq mode 'vector))
  2720.       (cond ((or (not (null val)) (eq mode 'vector))
  2721.          (princ sep port)
  2722.          (ppval val att lastpos port)
  2723.          (setq sep '|    |)
  2724.          (setq lastpos curpos))))
  2725.     (princ '|)| port)))
  2726.  
  2727. (defun ppval (val att lastpos port)
  2728.   ;  (break "in ppval")        
  2729.   (cond ((not (equal att (1+ lastpos)))        ; ok, if we got an att 
  2730.      (princ '^ port)
  2731.      (princ att port)
  2732.      (princ '| | port)))
  2733.   (princ val port))
  2734.  
  2735.  
  2736.  
  2737. ;;; printing production memory
  2738.  
  2739. (defun ops-pm (z) (mapc (function pprule) z) (terpri) nil)
  2740.  
  2741. (defun pprule (name)
  2742.   (prog (matrix next lab)
  2743.     (and (not (symbolp name)) (return nil))
  2744.     (setq matrix (get name 'production))
  2745.     (and (null matrix) (return nil))
  2746.     (terpri)
  2747.     (princ '|(p |)      ;)
  2748.     (princ name)
  2749.     top    (and (atom matrix) (go fin))
  2750.     (setq next (car matrix))
  2751.     (setq matrix (cdr matrix))
  2752.     (setq lab nil)
  2753.     (terpri)
  2754.     (cond ((eq next '-)
  2755.        (princ '|  - |)
  2756.        (setq next (car matrix))
  2757.        (setq matrix (cdr matrix)))
  2758.       ((eq next '-->)
  2759.        (princ '|  |))
  2760.       ((and (eq next '{) (atom (car matrix)))
  2761.        (princ '|   {|)
  2762.        (setq lab (car matrix))
  2763.        (setq next (cadr matrix))
  2764.        (setq matrix (cdddr matrix)))
  2765.       ((eq next '{)
  2766.        (princ '|   {|)
  2767.        (setq lab (cadr matrix))
  2768.        (setq next (car matrix))
  2769.        (setq matrix (cdddr matrix)))
  2770.       (t (princ '|    |)))
  2771.     (ppline next)
  2772.     (cond (lab (princ '| |) (princ lab) (princ '})))
  2773.     (go top)
  2774.     fin    (princ '|)|)))
  2775.  
  2776. (defun ppline (line)
  2777.   (prog ()
  2778.     (cond ((atom line) (princ line))
  2779.       (t
  2780.        (princ '|(|)      ;)
  2781.        (setq *ppline* line)
  2782.        (ppline2)
  2783.        ;(
  2784.        (princ '|)|)))
  2785.     (return nil)))
  2786.  
  2787. (defun ppline2 ()
  2788.   (prog (needspace)
  2789.     (setq needspace nil)
  2790.     top  (and (atom *ppline*) (return nil))
  2791.     (and needspace (princ '| |))
  2792.     (cond ((eq (car *ppline*) '^) (ppattval))
  2793.       (t (pponlyval)))
  2794.     (setq needspace t)
  2795.     (go top)))
  2796.  
  2797. (defun ppattval ()
  2798.   (prog (att val)
  2799.     (setq att (cadr *ppline*))
  2800.     (setq *ppline* (cddr *ppline*))
  2801.     (setq val (getval))
  2802.     ;###    (cond ((> (+ (nwritn) (flatc att) (flatc val)) 76.)))
  2803.     ;@@@ nwritn no arg
  2804.     ;                        ;"plus" changed to "+" by gdw
  2805.     ;           (terpri)
  2806.     ;           (princ '|        |)
  2807.     (princ '^)
  2808.     (princ att)
  2809.     (mapc (function (lambda (z) (princ '| |) (princ z))) val)))
  2810.  
  2811. (defun pponlyval ()
  2812.   (prog (val needspace)
  2813.     (setq val (getval))
  2814.     (setq needspace nil)
  2815.     ;###    (cond ((> (+ (nwritn) (flatc val)) 76.)))
  2816.     ;"plus" changed to "+" by gdw
  2817.     ;           (setq needspace nil)        ;^nwritn no arg @@@
  2818.     ;           (terpri)
  2819.     ;           (princ '|        |)
  2820.     top    (and (atom val) (return nil))
  2821.     (and needspace (princ '| |))
  2822.     (setq needspace t)
  2823.     (princ (car val))
  2824.     (setq val (cdr val))
  2825.     (go top)))
  2826.  
  2827. (defun getval ()
  2828.   (prog (res v1)
  2829.     (setq v1 (car *ppline*))
  2830.     (setq *ppline* (cdr *ppline*))
  2831.     (cond ((member v1 '(= <> < <= => > <=>))
  2832.        (setq res (cons v1 (getval))))
  2833.       ((eq v1 '{)
  2834.        (setq res (cons v1 (getupto '}))))
  2835.       ((eq v1 '<<)
  2836.        (setq res (cons v1 (getupto '>>))))
  2837.       ((eq v1 '//)
  2838.        (setq res (list v1 (car *ppline*)))
  2839.        (setq *ppline* (cdr *ppline*)))
  2840.       (t (setq res (list v1))))
  2841.     (return res)))
  2842.  
  2843. (defun getupto (end)
  2844.   (prog (v)
  2845.     (and (atom *ppline*) (return nil))
  2846.     (setq v (car *ppline*))
  2847.     (setq *ppline* (cdr *ppline*))
  2848.     (cond ((eq v end) (return (list v)))
  2849.       (t (return (cons v (getupto end))))))) 
  2850.  
  2851.  
  2852.  
  2853.  
  2854.  
  2855.  
  2856. ;;; backing up
  2857.  
  2858.  
  2859.  
  2860. (defun record-index-plus (k)
  2861.   (setq *record-index* (+ k *record-index*))    ;"plus" changed to "+" by gdw
  2862.   (cond ((< *record-index* 0.)
  2863.      (setq *record-index* *max-record-index*))
  2864.     ((> *record-index* *max-record-index*)
  2865.      (setq *record-index* 0.)))) 
  2866.  
  2867. ; the following routine initializes the record.  putting nil in the
  2868. ; first slot indicates that that the record does not go back further
  2869. ; than that.  (when the system backs up, it writes nil over the used
  2870.         ; records so that it will recognize which records it has used.  thus
  2871.         ; the system is set up anyway never to back over a nil.)
  2872.  
  2873. (defun initialize-record nil
  2874.   (setq *record-index* 0.)
  2875.   (setq *recording* nil)
  2876.   (setq *max-record-index* 31.)
  2877.   (putvector *record-array* 0. nil)) 
  2878.  
  2879. ; *max-record-index* holds the maximum legal index for record-array
  2880. ; so it and the following must be changed at the same time
  2881.  
  2882. (defun begin-record (p data)
  2883.   (setq *recording* t)
  2884.   (setq *record* (list '=>refract p data))) 
  2885.  
  2886. (defun end-record nil
  2887.   (cond (*recording*
  2888.      (setq *record*
  2889.            (cons *cycle-count* (cons *p-name* *record*)))
  2890.      (record-index-plus 1.)
  2891.      (putvector *record-array* *record-index* *record*)
  2892.      (setq *record* nil)
  2893.      (setq *recording* nil)))) 
  2894.  
  2895. (defun record-change (direct time elm)
  2896.   (cond (*recording*
  2897.      (setq *record*
  2898.            (cons direct (cons time (cons elm *record*))))))) 
  2899.  
  2900. ; to maintain refraction information, need keep only one piece of information:
  2901. ; need to record all unsuccessful attempts to delete things from the conflict
  2902. ; set.  unsuccessful deletes are caused by attempting to delete refracted
  2903. ; instantiations.  when backing up, have to avoid putting things back into the
  2904. ; conflict set if they were not deleted when running forward
  2905.  
  2906. (defun record-refract (rule data)
  2907.   (and *recording*
  2908.        (setq *record* (cons '<=refract (cons rule (cons data *record*))))))
  2909.  
  2910. (defun refracted (rule data)
  2911.   (prog (z)
  2912.     (and (null *refracts*) (return nil))
  2913.     (setq z (cons rule data))
  2914.     (return (member z *refracts* :test #'equal))))
  2915.  
  2916. (defun back (k)
  2917.   (prog (r)
  2918.     loop   (and (< k 1.) (return nil))
  2919.     (setq r (getvector *record-array* *record-index*))    ; (('))
  2920.     (and (null r) (return '|nothing more stored|))
  2921.     (putvector *record-array* *record-index* nil)
  2922.     (record-index-plus -1.)
  2923.     (undo-record r)
  2924.     (setq k (1- k))
  2925.     (go loop)))
  2926.  
  2927.  
  2928. ;; replaced per jcp
  2929. ;;; Commented out
  2930. #|
  2931. (defun undo-record (r)
  2932.   (prog (save act a b rate)
  2933.     ;###    (comment *recording* must be off during back up)
  2934.     (setq save *recording*)
  2935.     (setq *refracts* nil)
  2936.     (setq *recording* nil)
  2937.     (and *ptrace* (back-print (list '|undo:| (car r) (cadr r))))
  2938.     (setq r (cddr r))
  2939.     top  (and (atom r) (go fin))
  2940.     (setq act (car r))
  2941.     (setq a (cadr r))
  2942.     (setq b (caddr r))
  2943.     (setq r (cdddr r))
  2944.     (and *wtrace* (back-print (list '|undo:| act a)))
  2945.     (cond ((eq act '<=wm) (add-to-wm b a))
  2946.       ((eq act '=>wm) (remove-from-wm b))
  2947.       ((eq act '<=refract)
  2948.        (setq *refracts* (cons (cons a b) *refracts*)))
  2949.       ((and (eq act '=>refract) (still-present b))
  2950.        (setq *refracts* (delete (cons a b) *refracts*))
  2951.        (setq rate (rating-part (get a 'topnode)))
  2952.        (removecs a b)
  2953.        (insertcs a b rate))
  2954.       (t (%warn '|back: cannot undo action| (list act a))))
  2955.     (go top)
  2956.     fin  (setq *recording* save)
  2957.     (setq *refracts* nil)
  2958.     (return nil)))
  2959. ;;; End commented out
  2960. |#
  2961.  
  2962.  
  2963. (defun undo-record (r)
  2964.   (prog (save act a b rate)
  2965.     ;###    (comment *recording* must be off during back up)
  2966.     (setq save *recording*)
  2967.     (setq *refracts* nil)
  2968.     (setq *recording* nil)
  2969.     (and *ptrace* (back-print (list '|undo:| (car r) (cadr r))))
  2970.     (setq r (cddr r))
  2971.     top  (and (atom r) (go fin))
  2972.     (setq act (car r))
  2973.     (setq a (cadr r))
  2974.     (setq b (caddr r))
  2975.     (setq r (cdddr r))
  2976.     (and *wtrace* (back-print (list '|undo:| act a)))
  2977.     (cond ((eq act '<=wm) (add-to-wm b a))
  2978.       ((eq act '=>wm) (remove-from-wm b))
  2979.       ((eq act '<=refract)
  2980.        (setq *refracts* (cons (cons a b) *refracts*)))
  2981.       ((and (eq act '=>refract) (still-present b))
  2982.        (setq *refracts* (spdelete (cons a b) *refracts*))
  2983.        (setq rate (rating-part (get a 'topnode)))
  2984.        (removecs a b)
  2985.        (insertcs a b rate))
  2986.       (t (%warn '|back: cannot undo action| (list act a))))
  2987.     (go top)
  2988.     fin  (setq *recording* save)
  2989.     (setq *refracts* nil)
  2990.     (return nil))) 
  2991.  
  2992.  
  2993.  
  2994. ; still-present makes sure that the user has not deleted something
  2995. ; from wm which occurs in the instantiation about to be restored; it
  2996. ; makes the check by determining whether each wme still has a time tag.
  2997.  
  2998. (defun still-present (data)
  2999.   (prog nil
  3000.     loop
  3001.     (cond ((atom data) (return t))
  3002.       ((creation-time (car data))
  3003.        (setq data (cdr data))
  3004.        (go loop))
  3005.       (t (return nil))))) 
  3006.  
  3007.  
  3008. (defun back-print (x) 
  3009.   (prog (port)
  3010.     (setq port (trace-file))
  3011.     (terpri port)
  3012.     (print x port)))
  3013.  
  3014.  
  3015.  
  3016.  
  3017. ;;; Functions to show how close rules are to firing
  3018.  
  3019. (defun ops-matches (rule-list)
  3020.   (mapc (function matches2) rule-list)
  3021.   (terpri)) 
  3022.  
  3023. (defun matches2 (p)
  3024.   (cond ((atom p)
  3025.      (terpri)
  3026.      (terpri)
  3027.      (princ p)
  3028.      (matches3 (get p 'backpointers) 2. (ncons 1.))))) 
  3029.  
  3030. (defun matches3 (nodes ce part)
  3031.   (cond ((not (null nodes))
  3032.      (terpri)
  3033.      (princ '| ** matches for |)
  3034.      (princ part)
  3035.      (princ '| ** |)
  3036.      (mapc (function write-elms) (find-left-mem (car nodes)))
  3037.      (terpri)
  3038.      (princ '| ** matches for |)
  3039.      (princ (ncons ce))
  3040.      (princ '| ** |)
  3041.      (mapc (function write-elms) (find-right-mem (car nodes)))
  3042.      (matches3 (cdr nodes) (1+ ce) (cons ce part))))) 
  3043.  
  3044. (defun write-elms (wme-or-count)
  3045.   (cond ((consp  wme-or-count)    ;dtpr\consp gdw
  3046.      (terpri)
  3047.      (mapc (function write-elms2) wme-or-count)))) 
  3048.  
  3049. (defun write-elms2 (x)
  3050.   (princ '|  |)
  3051.   (princ (creation-time x)))
  3052.  
  3053. (defun find-left-mem (node)
  3054.   (cond ((eq (car node) '&and) (memory-part (caddr node)))
  3055.     (t (car (caddr node))))) 
  3056.  
  3057. (defun find-right-mem (node) (memory-part (cadddr node))) 
  3058.  
  3059.  
  3060. ;;; Check the RHSs of productions 
  3061.  
  3062.  
  3063. (defun check-rhs (rhs) (mapc (function check-action) rhs))
  3064.  
  3065. (defun check-action (x)
  3066.   (prog (a)
  3067.     (cond ((atom x)
  3068.        (%warn '|atomic action| x)
  3069.        (return nil)))
  3070.     (setq a (setq *action-type* (car x)))
  3071.     (case a
  3072.       (bind (check-bind x))
  3073.       (cbind (check-cbind x))
  3074.       (make (check-make x))
  3075.       (modify (check-modify x))
  3076.       (remove (check-remove x))
  3077.       (write (check-write x))    
  3078.       (call (check-call x))        
  3079.       (halt (check-halt x))
  3080.       (openfile (check-openfile x))
  3081.       (closefile (check-closefile x))
  3082.       (default (check-default x))
  3083.       (build (check-build x))
  3084.       (t (%warn '|undefined rhs action| a)))))
  3085.  
  3086.  
  3087. ;(defun chg-to-write (x)
  3088. ;    (setq x (cons 'write (cdr x))))
  3089.  
  3090. (defun check-build (z)
  3091.   (and (null (cdr z)) (%warn '|needs arguments| z))
  3092.   (check-build-collect (cdr z)))
  3093.  
  3094. (defun check-build-collect (args)
  3095.   (prog (r)
  3096.     top    (and (null args) (return nil))
  3097.     (setq r (car args))
  3098.     (setq args (cdr args))
  3099.     (cond ((consp  r) (check-build-collect r))    ;dtpr\consp gdw
  3100.       ((eq r '\\)
  3101.        (and (null args) (%warn '|nothing to evaluate| r))
  3102.        (check-rhs-value (car args))
  3103.        (setq args (cdr args))))
  3104.     (go top)))
  3105.  
  3106. (defun check-remove (z)                 ;@@@ kluge by gdw
  3107.   (and (null (cdr z)) (%warn '|needs arguments| z))
  3108.   (mapc (function check-rhs-ce-var) (cdr z))) 
  3109.  
  3110. ;(defun check-remove (z)                     ;original
  3111.    ; (and (null (cdr z)) (%warn '|needs arguments| z))
  3112.    ;(mapc (function check-rhs-ce-var) (cdr z))) 
  3113.  
  3114. (defun check-make (z)
  3115.   (and (null (cdr z)) (%warn '|needs arguments| z))
  3116.   (check-change& (cdr z))) 
  3117.  
  3118. (defun check-openfile (z)
  3119.   (and (null (cdr z)) (%warn '|needs arguments| z))
  3120.   (check-change& (cdr z))) 
  3121.  
  3122. (defun check-closefile (z)
  3123.   (and (null (cdr z)) (%warn '|needs arguments| z))
  3124.   (check-change& (cdr z))) 
  3125.  
  3126. (defun check-default (z)
  3127.   (and (null (cdr z)) (%warn '|needs arguments| z))
  3128.   (check-change& (cdr z))) 
  3129.  
  3130. (defun check-modify (z)
  3131.   (and (null (cdr z)) (%warn '|needs arguments| z))
  3132.   (check-rhs-ce-var (cadr z))
  3133.   (and (null (cddr z)) (%warn '|no changes to make| z))
  3134.   (check-change& (cddr z))) 
  3135.  
  3136. (defun check-write (z)                ;note this works w/write
  3137.   (and (null (cdr z)) (%warn '|needs arguments| z))
  3138.   (check-change& (cdr z))) 
  3139.  
  3140. (defun check-call (z)
  3141.   (prog (f)
  3142.     (and (null (cdr z)) (%warn '|needs arguments| z))
  3143.     (setq f (cadr z))
  3144.     (and (variablep f)
  3145.      (%warn '|function name must be a constant| z))
  3146.     (or (symbolp f)
  3147.     (%warn '|function name must be a symbolic atom| f))
  3148.     (or (externalp f)
  3149.     (%warn '|function name not declared external| f))
  3150.     (check-change& (cddr z)))) 
  3151.  
  3152. (defun check-halt (z)
  3153.   (or (null (cdr z)) (%warn '|does not take arguments| z))) 
  3154.  
  3155. (defun check-cbind (z)
  3156.   (prog (v)
  3157.     (or (= (length z) 2.) (%warn '|takes only one argument| z))
  3158.     (setq v (cadr z))
  3159.     (or (variablep v) (%warn '|takes variable as argument| z))
  3160.     (note-ce-variable v))) 
  3161.  
  3162. (defun check-bind (z)
  3163.   (prog (v)
  3164.     (or (> (length z) 1.) (%warn '|needs arguments| z))
  3165.     (setq v (cadr z))
  3166.     (or (variablep v) (%warn '|takes variable as argument| z))
  3167.     (note-variable v)
  3168.     (check-change& (cddr z)))) 
  3169.  
  3170.  
  3171. (defun check-change& (z)
  3172.   (prog (r tab-flag)
  3173.     (setq tab-flag nil)
  3174.     la   (and (atom z) (return nil))
  3175.     (setq r (car z))
  3176.     (setq z (cdr z))
  3177.     (cond ((eq r '^)
  3178.        (and tab-flag
  3179.         (%warn '|no value before this tab| (car z)))
  3180.        (setq tab-flag t)
  3181.        (check-tab-index (car z))
  3182.        (setq z (cdr z)))
  3183.       ((eq r '//) (setq tab-flag nil) (setq z (cdr z)))
  3184.       (t (setq tab-flag nil) (check-rhs-value r)))
  3185.     (go la))) 
  3186.  
  3187. (defun check-rhs-ce-var (v)
  3188.   (cond ((and (not (numberp v)) (not (ce-bound? v)))
  3189.      (%warn '|unbound element variable| v))
  3190.     ((and (numberp v) (or (< v 1.) (> v *ce-count*)))
  3191.      (%warn '|numeric element designator out of bounds| v)))) 
  3192.  
  3193. (defun check-rhs-value (x)
  3194.   (cond ((consp  x) (check-rhs-function x))    ;dtpr\consp gdw
  3195.     (t (check-rhs-atomic x)))) 
  3196.  
  3197. (defun check-rhs-atomic (x)
  3198.   (and (variablep x) 
  3199.        (not (bound? x)) 
  3200.        (%warn '|unbound variable| x)))
  3201.  
  3202. (defun check-rhs-function (x)
  3203.   (prog (a)
  3204.     (setq a (car x))
  3205.     (cond ((eq a 'compute) (check-compute x))
  3206.       ((eq a 'arith) (check-compute x))
  3207.       ((eq a 'substr) (check-substr x))
  3208.       ((eq a 'accept) (check-accept x))
  3209.       ((eq a 'acceptline) (check-acceptline x))
  3210.       ((eq a 'crlf) (check-crlf x))
  3211.       ((eq a 'genatom) (check-genatom x))
  3212.       ((eq a 'litval) (check-litval x))
  3213.       ((eq a 'tabto) (check-tabto x))
  3214.       ((eq a 'rjust) (check-rjust x))
  3215.       ((not (externalp a))
  3216.        (%warn '"rhs function not declared external" a)))))
  3217.  
  3218. (defun check-litval (x) 
  3219.   (or (= (length x) 2) (%warn '|wrong number of arguments| x))
  3220.   (check-rhs-atomic (cadr x)))
  3221.  
  3222. (defun check-accept (x)
  3223.   (cond ((= (length x) 1) nil)
  3224.     ((= (length x) 2) (check-rhs-atomic (cadr x)))
  3225.     (t (%warn '|too many arguments| x))))
  3226.  
  3227. (defun check-acceptline (x)
  3228.   (mapc (function check-rhs-atomic) (cdr x)))
  3229.  
  3230. (defun check-crlf (x) 
  3231.   (check-0-args x)) 
  3232.  
  3233. (defun check-genatom (x) (check-0-args x)) 
  3234.  
  3235. (defun check-tabto (x)
  3236.   (or (= (length x) 2) (%warn '|wrong number of arguments| x))
  3237.   (check-print-control (cadr x)))
  3238.  
  3239. (defun check-rjust (x)
  3240.   (or (= (length x) 2) (%warn '|wrong number of arguments| x))
  3241.   (check-print-control (cadr x)))
  3242.  
  3243. (defun check-0-args (x)
  3244.   (or (= (length x) 1.) (%warn '|should not have arguments| x))) 
  3245.  
  3246. (defun check-substr (x)
  3247.   (or (= (length x) 4.) (%warn '|wrong number of arguments| x))
  3248.   (check-rhs-ce-var (cadr x))
  3249.   (check-substr-index (caddr x))
  3250.   (check-last-substr-index (cadddr x))) 
  3251.  
  3252. (defun check-compute (x) (check-arithmetic (cdr x))) 
  3253.  
  3254. (defun check-arithmetic (l)
  3255.   (cond ((atom l)
  3256.      (%warn '|syntax error in arithmetic expression| l))
  3257.     ((atom (cdr l)) (check-term (car l)))
  3258.     ((not (member (cadr l) '(+ - * // \\)))    ;"plus" changed to "+" by gdw
  3259.      (%warn '|unknown operator| l))
  3260.     (t (check-term (car l)) (check-arithmetic (cddr l))))) 
  3261.  
  3262. (defun check-term (x)
  3263.   (cond ((consp  x) (check-arithmetic x))    ;dtpr\consp gdw
  3264.     (t (check-rhs-atomic x)))) 
  3265.  
  3266. (defun check-last-substr-index (x)
  3267.   (or (eq x 'inf) (check-substr-index x))) 
  3268.  
  3269. (defun check-substr-index (x)
  3270.   (prog (v)
  3271.     (cond ((bound? x) (return x)))
  3272.     (setq v ($litbind x))
  3273.     (cond ((not (numberp v))
  3274.        (%warn '|unbound symbol used as index in substr| x))
  3275.       ((or (< v 1.) (> v 127.))
  3276.        (%warn '|index out of bounds in tab| x))))) 
  3277.  
  3278. (defun check-print-control (x)
  3279.   (prog ()
  3280.     (cond ((bound? x) (return x)))
  3281.     (cond ((or (not (numberp x)) (< x 1.) (> x 127.))
  3282.        (%warn '|illegal value for printer control| x))))) 
  3283.  
  3284. (defun check-tab-index (x)
  3285.   (prog (v)
  3286.     (cond ((bound? x) (return x)))
  3287.     (setq v ($litbind x))
  3288.     (cond ((not (numberp v))
  3289.        (%warn '|unbound symbol occurs after ^| x))
  3290.       ((or (< v 1.) (> v 127.))
  3291.        (%warn '|index out of bounds after ^| x))))) 
  3292.  
  3293. (defun note-variable (var)
  3294.   (setq *rhs-bound-vars* (cons var *rhs-bound-vars*)))
  3295.  
  3296. (defun bound? (var)
  3297.   (or (member var *rhs-bound-vars*)
  3298.       (var-dope var)))
  3299.  
  3300. (defun note-ce-variable (ce-var)
  3301.   (setq *rhs-bound-ce-vars* (cons ce-var *rhs-bound-ce-vars*)))
  3302.  
  3303. (defun ce-bound? (ce-var)
  3304.   (or (member ce-var *rhs-bound-ce-vars*)
  3305.       (ce-var-dope ce-var)))
  3306.  
  3307. ;;; Top level routines
  3308.  
  3309. (defun process-changes (adds dels)
  3310.   (prog (x)
  3311.     process-deletes (and (atom dels) (go process-adds))
  3312.     (setq x (car dels))
  3313.     (setq dels (cdr dels))
  3314.     (remove-from-wm x)
  3315.     (go process-deletes)
  3316.     process-adds (and (atom adds) (return nil))
  3317.     (setq x (car adds))
  3318.     (setq adds (cdr adds))
  3319.     (add-to-wm x nil)
  3320.     (go process-adds))) 
  3321.  
  3322. (defun main nil
  3323.   (prog (instance r)
  3324.     (setq *halt-flag* nil)
  3325.     (setq *break-flag* nil)
  3326.     (setq instance nil)
  3327.     dil  (setq *phase* 'conflict-resolution)
  3328.     (cond (*halt-flag*
  3329.        (setq r '|end -- explicit halt|)
  3330.        (go finis))
  3331.       ((zerop *remaining-cycles*)
  3332.        (setq r '***break***)
  3333.        (setq *break-flag* t)
  3334.        (go finis))
  3335.       (*break-flag* (setq r '***break***) (go finis)))
  3336.     (setq *remaining-cycles* (1- *remaining-cycles*))
  3337.     (setq instance (conflict-resolution))
  3338.     (cond ((not instance)
  3339.        (setq r '|end -- no production true|)
  3340.        (go finis)))
  3341.     (setq *phase* (car instance))
  3342.     (accum-stats)
  3343.     (eval-rhs (car instance) (cdr instance))
  3344.     (check-limits)
  3345.     (and (broken (car instance)) (setq *break-flag* t))
  3346.     (go dil)
  3347.     finis (setq *p-name* nil)
  3348.     (return r))) 
  3349.  
  3350. (defun do-continue (wmi)
  3351.   (cond (*critical*
  3352.      (terpri)
  3353.      (princ '|warning: network may be inconsistent|)))
  3354.   (process-changes wmi nil)
  3355.   (print-times (main))) 
  3356.  
  3357. (defun accum-stats nil
  3358.   (setq *cycle-count* (1+ *cycle-count*))
  3359.   (setq *total-token* (+ *total-token* *current-token*))
  3360.   ;"plus" changed to "+" by gdw
  3361.   (cond ((> *current-token* *max-token*)
  3362.      (setq *max-token* *current-token*)))
  3363.   (setq *total-wm* (+ *total-wm* *current-wm*))    ;"plus" changed to "+" by gdw
  3364.   (cond ((> *current-wm* *max-wm*) (setq *max-wm* *current-wm*)))) 
  3365.  
  3366.  
  3367.  
  3368.  
  3369. (defun print-times (mess)
  3370.   (prog (cc)
  3371.     (cond (*break-flag* (terpri) (return mess)))
  3372.     (setq cc (+ (float *cycle-count*) 1.0e-20))
  3373.     (terpri)
  3374.     (princ mess)
  3375.     (terpri)
  3376.     (format t "~3D productions (~D // ~D nodes)~%"
  3377.         *pcount* *real-cnt* *virtual-cnt*)
  3378.     (format t "~3D firings (~D rhs actions)~%"
  3379.         *cycle-count* *action-count*)
  3380.     (format t "~3D mean working memory size (~D maximum)~%"
  3381.         (round (float *total-wm*) cc) *max-wm*)
  3382.     (format t "~3D mean conflict set size (~D maximum)~%"
  3383.         (round (float *total-cs*) cc) *max-cs*)
  3384.     (format t "~3D mean token memory size (~D maximum)~%"
  3385.         (round (float *total-token*) cc)
  3386.         *max-token*)))
  3387.  
  3388.  
  3389.  
  3390. (defun check-limits nil
  3391.   (cond ((> (length *conflict-set*) *limit-cs*)
  3392.      (format t "~%~%conflict set size exceeded the limit of ~D after ~D~%"
  3393.          *limit-cs* *p-name*)
  3394.      (setq *halt-flag* t)))
  3395.   (cond ((> *current-token* *limit-token*)
  3396.      (format t "~%~%token memory size exceeded the limit of ~D after ~D~%"
  3397.          *limit-token* *p-name*)
  3398.      (setq *halt-flag* t))))
  3399.  
  3400.  
  3401. (defun top-level-remove (z)
  3402.   (cond ((equal z '(*)) (process-changes nil (get-wm nil)))
  3403.     (t (process-changes nil (get-wm z))))) 
  3404.  
  3405. (defun ops-excise (z) (mapc (function excise-p) z))
  3406.  
  3407. (defun ops-run (z)
  3408.   (cond ((atom z) (setq *remaining-cycles* 1000000.) (do-continue nil))
  3409.     ((and (atom (cdr z)) (numberp (car z)) (> (car z) 0.))
  3410.      (setq *remaining-cycles* (car z))
  3411.      (do-continue nil))
  3412.     (t 'what?))) 
  3413.  
  3414. (defun ops-strategy (z)
  3415.   (cond ((atom z) *strategy*)
  3416.     ((equal z '(lex)) (setq *strategy* 'lex))
  3417.     ((equal z '(mea)) (setq *strategy* 'mea))
  3418.     (t 'what?))) 
  3419.  
  3420. (defun ops-cs (z)
  3421.   (cond ((atom z) (conflict-set))
  3422.     (t 'what?))) 
  3423.  
  3424. (defun ops-watch (z)
  3425.   (cond ((equal z '(0.))
  3426.      (setq *wtrace* nil)
  3427.      (setq *ptrace* nil)
  3428.      0.)
  3429.     ((equal z '(1.)) (setq *wtrace* nil) (setq *ptrace* t) 1.)
  3430.     ((equal z '(2.)) (setq *wtrace* t) (setq *ptrace* t) 2.)
  3431.     ((equal z '(3.))
  3432.      (setq *wtrace* t)
  3433.      (setq *ptrace* t)
  3434.      '(2. -- conflict set trace not supported))
  3435.     ((and (atom z) (null *ptrace*)) 0.)
  3436.     ((and (atom z) (null *wtrace*)) 1.)
  3437.     ((atom z) 2.)
  3438.     (t 'what?))) 
  3439.  
  3440. (defun ops-external (z) (catch '!error! (external2 z))) ;jgk inverted args
  3441. ;& quoted tag
  3442. (defun external2 (z) (mapc (function external3) z))
  3443.  
  3444. (defun external3 (x) 
  3445.   (cond ((symbolp x) (putprop x t 'external-routine))
  3446.     (t (%error '|not a legal function name| x))))
  3447.  
  3448. (defun externalp (x)
  3449.   ;  (cond ((symbolp x) (get x 'external-routine))     ;) @@@
  3450.   ;ok, I'm eliminating this temporarily @@@@
  3451.   (cond ((symbolp x) t)
  3452.     (t (%warn '|not a legal function name| x) nil)))
  3453.  
  3454. (defun ops-pbreak (z)
  3455.   (cond ((atom z) (terpri) *brkpts*)
  3456.     (t (mapc (function pbreak2) z) nil)))
  3457.  
  3458. (defun pbreak2 (rule)
  3459.   (cond ((not (symbolp rule)) (%warn '|illegal name| rule))
  3460.     ((not (get rule 'topnode)) (%warn '|not a production| rule))
  3461.     ((member rule *brkpts*) (setq *brkpts* (rematm rule *brkpts*)))
  3462.     (t (setq *brkpts* (cons rule *brkpts*)))))
  3463.  
  3464. (defun rematm (atm list)
  3465.   (cond ((atom list) list)
  3466.     ((eq atm (car list)) (rematm atm (cdr list)))
  3467.     (t (cons (car list) (rematm atm (cdr list))))))
  3468.  
  3469. (defun broken (rule) (member rule *brkpts*))
  3470.  
  3471. ;(defun dtpr  (x) (consp x))    ;dtpr\consp gdw
  3472.  
  3473. (defun fix (x)(floor x))
  3474.  
  3475.  
  3476. ;@@@   this is a mistake: it must either go before = is called for 
  3477. ;non-numeric args, or such calls replaced with eq, equal, etc.
  3478. ;(defun = 
  3479. ;(x y) (equal x y))
  3480.  
  3481.  
  3482. (defun infile (f_name)
  3483.   (open f_name :direction :input))
  3484.  
  3485. (defun sequencep (x) (typep x 'sequence))
  3486.  
  3487. (defun outfile (f_name)
  3488.   (open f_name :direction :output :if-exists :new-version))
  3489.  
  3490.  
  3491.  
  3492. ;;; Added automatic (i-g-v) on load - Dario Giuse
  3493. (i-g-v)
  3494.  
  3495.  
  3496.  
  3497.  
  3498. ;This is unnecessary in Spice Lisp  - Dario Giuse.
  3499.  
  3500. ; break mechanism:
  3501. #+vms (defun setbreak nil 
  3502.       (terpri)
  3503.       (princ #\^G)
  3504.       (if *break-flag*
  3505.         (princ " --- *BREAK-FLAG* ALREADY SET ---")
  3506.         (princ " --- ^C Acknowledged: *break-flag* set ---"))
  3507.       (setq *break-flag* t)
  3508.       (terpri))
  3509.  
  3510. #+vms (defun vms-keyboard-bindings nil
  3511.     (unbind-keyboard-function #\^C)
  3512.     (defun throw-to-previous-command-level nil
  3513.       (throw-to-command-level :previous))
  3514.     (bind-keyboard-function #\^Z #'throw-to-previous-command-level)
  3515.     (SETQ *break-flag* NIL)
  3516.     (setq *break-character* #\^C)
  3517.     (bind-keyboard-function *break-character* #'setbreak)
  3518.     )
  3519. #+vms #,(vms-keyboard-bindings)
  3520.  
  3521.     
  3522.  
  3523. setq *c99*(car dp))
  3524.     (setq dp (cdr dp))
  3525.     (setq *c100* (car dp))
  3526.     (setq dp (cdr dp))
  3527.     (setq *c101* (car dp))
  3528.     (setq dp (cdr dp))
  3529.     (setq *c102* (car dp))
  3530.     (setq dp (cdr dp;;;      FILE: sys$user:[gdw.opsl]opsenv.lsp;1                    ;;;
  3531. ;;;      LastEditDate = Tue Dec  4 00:16:03 1984      GDW            ;;;
  3532. ;;;                                         ;;;
  3533.  
  3534. ; OPSENV.LSP - contains functions to define DAYTIME and functions to
  3535. ; provide a nice ops/emacs interface. Supercedes and combines testops.lsp
  3536. ; and daytime.lsp
  3537.  
  3538. (proclaim '(special *time-adjust*))
  3539. ;(setq *time-adjust* 1073655424) ;done by experimentation
  3540. (setq *time-adjust* 0) ;done by experimentation
  3541.  
  3542. (defun daytime nil
  3543.    (setq time-list 
  3544.        (multiple-value-list 
  3545.        (decode-universal-time 
  3546.            (- (get-universal-time) *time-adjust*))))
  3547.    (concatenate 'string 
  3548.        (princ-to-string (nth 2 time-list))         ;hr
  3549.        ":" (pad (princ-to-string (nth 1 time-list)))     ;min
  3550.        ":" (pad (princ-to-string (nth 0 time-list)))   ;sec
  3551.        "  "(day-string (nth 6 time-list))        ;day of week
  3552.        (month-string (1- (nth 4 time-list))) " "     ;month
  3553.        (princ-to-string (nth 3 time-list)) " "     ;date
  3554.        (princ-to-string (nth 5 time-list))         ;year
  3555.    ))
  3556. ;;;date: produces a string containing an english-language time & date.
  3557. ;
  3558. ;also included: get-date, set-time; vars *rev* & *time-adjust* are setq'd
  3559. ;set-time allows eroneous common-lisp times to be corrected
  3560.  
  3561.  
  3562. (defun pad (x)
  3563.     (cond ( (> (length x) 1) x)
  3564.           (t (concatenate 'string "0" x))))
  3565.  
  3566. (defun day-string (x)
  3567.     (nth x '(" Sun" " Mon" " Tue" " Wed" " Thu" " Fri" " Sat")))
  3568.  
  3569. (defun month-string (x)
  3570.     (nth x '(" Jan" " Feb" " Mar" " Apr" " May" " Jun"
  3571.          " Jul" " Aug" " Sep" " Oct" " Nov" " Dec")))
  3572.  
  3573. (defun set-time nil
  3574.    (prog (adj)   
  3575.        (setq *time-adjust* 1073655424)     ;done by experimentation
  3576. lp&   (princ "amount to adjust by: ") 
  3577.     (setq adj (read))
  3578.     (cond ( (not (typep adj 'number))(return *time-adjust*)))
  3579.        (setq *time-adjust* (- *time-adjust* adj))
  3580. ;   (SETQ DTIME (get-universal-time))
  3581. ;   (setq atime 
  3582. ;    (multiple-value-list (decode-universal-time (- dtime *time-adjust*))))
  3583.    (terpri)
  3584.    (terpri)(princ (daytime))(terpri)
  3585.    (princ "adjustment: ")(princ *time-adjust*)(terpri)(terpri)
  3586. (go lp&)
  3587.    ))
  3588.  
  3589. (setq *rev* (daytime))
  3590.  
  3591. (defun get_date nil ($value (daytime))) 
  3592.  
  3593. ;;;    FILE: sys$user:[gdw.clisp]testops.lsp
  3594. ;;;    LastEditDate = Wed May  9 05:05:01 1984 --  George Wood  [gdw]
  3595. ;;; 
  3596. ;; test (no prompt) for use w submit, etc
  3597. (setq *print-pretty* t)
  3598. (terpri)
  3599. (terpri)
  3600. (princ 
  3601. "    *    TEST.L: test aids for vax lisp     *    enter (??) for info")
  3602. (terpri)
  3603. (terpri)
  3604. (setq *load-verbose* nil)
  3605. ;     maybe these should be 'autoloaded'
  3606.  
  3607. ;load the pretty-printing macro
  3608.  
  3609. ;;;; (load "[gdw.clisp]ppdef.cl")  ;;; OBSOLETE
  3610. (DEFMACRO PPDEF (FN &OPTIONAL STR) `(PPRINT-DEFINITION ',FN ',STR))
  3611.  
  3612.  
  3613. ;;;
  3614.  
  3615. ;;;
  3616.  
  3617. (setq *load-verbose* t)
  3618.  
  3619. ;inquire -- get user input
  3620.  
  3621. (defun inquire (x &optional y)
  3622. "
  3623.    ( inquire <prompt> &optional <default> )
  3624.  
  3625.     returns input line or default value if <CR> is entered.
  3626.     WARNING: value returned is ALWAYS a string.
  3627.  
  3628. "
  3629.    (prog (tmp val)
  3630.       (princ x)
  3631.       (if y (progn (princ " [")(princ y)(princ "]: "))(princ ": "))
  3632.       (setq tmp (read-line))
  3633.       (if (equal "" tmp) (return y)(return tmp))))
  3634.  
  3635. ; set the *source* for loading with (TV)
  3636.  
  3637. (defun ns nil (setq *source* (inquire "source file to be loaded")))
  3638.  
  3639. ; functions to load a source file
  3640.  
  3641. (defun tt nil (load *source*))
  3642. (defun tv nil (load *source* ':verbose ':print))
  3643.  
  3644. ; (K) SPAWN (or attach to existing) a process
  3645. ;    get ready
  3646.  
  3647. (setq *exproc* nil)
  3648. (setq *test_lisp* "TEST_LISP")
  3649.  
  3650. (defun setk nil 
  3651.     (setq *exproc* (inquire "New process name: "))
  3652.     (cond     ((not (equal "" *exproc*))
  3653.             (setq *test_lisp* *exproc*)
  3654.             (setq *exproc* nil))
  3655.         (t (setq *exproc* t))))
  3656.  
  3657.  
  3658.  
  3659. (defun k nil
  3660.     (cond (*exproc* (attach *test_lisp* ))
  3661.           (t (setq *exproc* t)
  3662.          (spawn ':process-name *test_lisp*)))
  3663.     (terpri)
  3664.     (princ "  *returned to lisp*  ")
  3665.     (terpri)
  3666.     (princ "> "))
  3667.  
  3668.  
  3669. (defun drb nil
  3670.     (dribble (inquire "dribble to file: ")))
  3671.  
  3672. (defun openinf nil
  3673.     (setq inf (open (inquire "input filename: ") :direction :input)))
  3674. (defun openoutf nil
  3675.     (setq outf (open (inquire "output filename: ")
  3676.          :direction :output :if-exists :new-version)))
  3677.  
  3678. (terpri)
  3679.  
  3680. (defun set-debug-levels (x)
  3681.     (terpri)
  3682.     (princ "set-debug-levels ")
  3683.     (princ x)
  3684.     (terpri)
  3685.     (setq *debug-print-level* x)
  3686.     (setq *debug-print-length* x))
  3687.  
  3688. ; ?? - a (very) little help. reminder, mostly
  3689.  
  3690. (defun ??  ()
  3691. (terpri)(terpri)
  3692. (princ "    CONTROL CHARACTERS & associated functions --")
  3693. (terpri)(terpri)
  3694. (princ 
  3695. "^C = ops break. ")
  3696. (terpri)
  3697. (princ 
  3698. "^X,(TV) = load *source*, verbose.        * (NS) = set new source")
  3699. (terpri)
  3700. (princ 
  3701. "^K,(K) = attach to process *test_lisp*        - <default =  `TEST_LISP'>")
  3702. (terpri)
  3703. (princ 
  3704. "^E,(EM) = attach to existing EMACS process     [ EMACS$<your-term> ]")
  3705. (terpri)
  3706. (princ 
  3707. "^G = enter lisp DEBUG. <use ? for debug help>    * (SETK) = set *test_lisp*")
  3708. (terpri)
  3709. (princ  
  3710. "^B = call lisp BREAK fn <use (CONTINUE) to resume from break at entry point>")
  3711. (terpri)
  3712. (PRINC 
  3713. "^Z = LISP Throw to previous command LEVEL - CANNOT RESUME FROM BREAK POINT!!")
  3714. (terpri)
  3715. (princ 
  3716. "^Y = DCL BREAK. to resume lisp from this, type CONTINUE before starting job")
  3717. (terpri)
  3718. (terpri)
  3719. (princ "    USEFUL FUNCTIONS --    ")
  3720. (terpri)
  3721. (terpri)
  3722. (princ 
  3723. "(APPROPOS <string>) = info about <string>'s occurrences in lisp symbols")
  3724. (terpri)
  3725. (princ
  3726. "(DESCRIBE '<function-name>) =  prints function's lisp documentation, if any")
  3727. (terpri)
  3728. (princ 
  3729. "(DIR) = DIRECTORY    (PPDEF <symbol>) = pretty print <symbol>'s definition"
  3730. )
  3731. (terpri)
  3732. (princ 
  3733.  "(OPENINF) = open INF    (OPENOUTF) = open OUTF        (DRB) = open dribble")
  3734. (terpri)
  3735. (princ 
  3736. "(SET-DEBUG-LEVELS) sets debug-pprint-level, debug-print-length <default= 3>")
  3737. (terpri)
  3738. )
  3739.  
  3740. (set-debug-levels 3)
  3741.  
  3742. (bind-keyboard-function #\^X #'tv)
  3743. (bind-keyboard-function #\^B #'break)
  3744. (bind-keyboard-function #\^G #'debug)
  3745. (bind-keyboard-function #\^K #'k)
  3746.  
  3747. (DEFUN TERM NIL (CADR (GET-PROCESS-INFORMATION NIL :TERMINAL)))
  3748.  
  3749. (defvar *EMACS-PROCESS-NAME* (CONCATENATE 'STRING "EMACS$" 
  3750.                    (SYSTEM:REMOVE #\: (TERM))))
  3751.  
  3752. (DEFVAR *emacs-lisp-file*
  3753.       (inquire "emacs-lisp-file" "SYS$LOGIN:LEDIT.TMP"))
  3754.       
  3755.  
  3756. ;(DEFUN EM NIL (ATTACH (CONCATENATE 'STRING  "EMACS$"
  3757. ;    (SYSTEM:REMOVE #\: (TERM)))))
  3758. (DEFUN EM NIL 
  3759.     (ATTACH *EMACS-PROCESS-NAME*)
  3760.     (if 
  3761.       (probe-file *emacs-lisp-file*)
  3762.       (unwind-protect 
  3763.       (load *emacs-lisp-file* :verbose t)
  3764.       (delete-file* *emacs-lisp-file*))))
  3765.  
  3766. (DEFUN EMSTART NIL 
  3767.     (SPAWN *EMACS-PROCESS-NAME* 
  3768.       :COMMAND-STRING "EMACS/NAME=*EMACS-PROCESS-NAME*")
  3769.     (if 
  3770.       (probe-file *emacs-lisp-file*)
  3771.       (unwind-protect 
  3772.       (load *emacs-lisp-file* :verbose t)
  3773.       (delete-file* *emacs-lisp-file*))))
  3774.  
  3775. (bind-keyboard-function #\^E #'EM)
  3776.  
  3777. (DEFMACRO WHILE (CONDITION ACTION)
  3778. " ( while <condition> <action> )
  3779.  
  3780.     as long as <condition> is not nil <action> is repeated.
  3781.     action is not executed if <condition> is initially nil.
  3782.     action should affect condition, natch, if you want out.
  3783.     <action> should be a single function-call or prog. This
  3784.     is a kluge hack, & carries NO warantee. Returns T if
  3785.     action is ever executed, nil otherwise.
  3786. "
  3787.     `(PROG NIL
  3788.       (IF (NOT ,CONDITION)(RETURN NIL))
  3789.     LOOP
  3790.     ,ACTION
  3791.     (IF (NOT ,CONDITION)(RETURN T)(GO LOOP))))
  3792.  
  3793. (DEFUN DELETE-FILE* (FILE)
  3794.     (WHILE (PROBE-FILE FILE) 
  3795.       (PROG (FILENAME)
  3796.     (PRINC "\;  DELETING ")
  3797.     (PRINC 
  3798.       (SETQ FILENAME (NAMESTRING (PROBE-FILE FILE))))
  3799.     (TERPRI)
  3800.     (PRINC
  3801.       (IF (DELETE-FILE FILENAME) T "\;  ERROR WHILE DELETING"))
  3802.     (TERPRI))))
  3803.  
  3804. (terpri)
  3805. ;;; #p foo will cause the definition of foo to be Pretty printed. FROM LEDIT
  3806. (set-dispatch-macro-character #\# #\p
  3807.     #'(lambda (stream subchar arg)
  3808.     (declare (ignore subchar arg))
  3809.     `(PPrint-definition ',(read stream t nil t))))
  3810.  
  3811. ;;; #? foo will cause foo to be DESCRIBEd.    FROM LEDIT
  3812. ;;; #?? foo will cause foo to APROPOSed.
  3813. (set-dispatch-macro-character #\# #\?
  3814.     #'(lambda (stream subchar arg)
  3815.     (declare (ignore subchar arg))
  3816.     (let ((following-char (peek-char nil stream)));; change arg to t soon.
  3817.       (cond 
  3818.         ((char= following-char #\?)
  3819.           (read-char stream)
  3820.           (list 'apropos (list 'quote (read stream t nil t))))
  3821.         (t (list 'describe (list 'quote (read stream t nil t))))))))
  3822.     
  3823.  
  3824. (DEFMACRO DCL (&REST CMD)
  3825.   `(SPAWN :COMMAND-STRING
  3826.           (APPLY #'CONCATENATE
  3827.                  (CONS 'STRING 
  3828.          (CONS "$ " (MAPCAR #'space-STRING ',CMD))))))
  3829.  
  3830. (defun space-string (x)
  3831.     (concatenate 'string (string x) " "))
  3832.  
  3833. (defmacro dir (&REST ARG)
  3834.   `(SPAWN :COMMAND-STRING
  3835.           (APPLY #'CONCATENATE
  3836.                  (CONS 'STRING 
  3837.          (CONS "$ DIR " (MAPCAR #'space-STRING ',ARG))))))
  3838.  
  3839. ()
  3840. o ok)))
  3841.     (putvector *result-array* edge nil)
  3842.     (setq edge (1- edge))
  3843.     (go clear)
  3844.     ok   (setq *next-index* next)
  3845.     (return next))) 
  3846.  
  3847. (defun $value (v)
  3848.   (cond ((> *next-index* *size-result-array*)
  3849.      (%warn '|index too large| *next-index*))
  3850.     (t
  3851.      (and (> *next-index* *max-index*)
  3852.           (setq *max-index* *next-index*))
  3853.      (putvector *result-array* *next-index* v)
  3854.      (setq *next-index* (1+ *next-index*))))) 
  3855.  
  3856.  
  3857. (defun use-result-array nil
  3858.  
  3859. (literalize monkey
  3860.     at
  3861.     on
  3862.     holds)
  3863.  
  3864. (literalize object
  3865.     name
  3866.     at
  3867.     weight
  3868.     on)
  3869.  
  3870. (literalize goal
  3871.     status
  3872.     type
  3873.     object
  3874.     to)
  3875.  
  3876. (p mb1
  3877.     (goal ^status active ^type holds ^object <w>)
  3878.     (object ^name <w> ^at <p> ^on ceiling)
  3879.     -->
  3880.     (make goal ^status active ^type move ^object ladder ^to <p>))
  3881.  
  3882.  
  3883. (p mb2
  3884.     (goal ^status active ^type holds ^object <w>)
  3885.     (object ^name <w> ^at <p> ^on ceiling)
  3886.     (object ^name ladder ^at <p>)
  3887.     -->
  3888.     (make goal ^status active ^type on ^object ladder))
  3889.  
  3890.  
  3891. (p mb3
  3892.     (goal ^status active ^type holds ^object <w>)
  3893.     (object ^name <w> ^at <p> ^on ceiling)
  3894.     (object ^name ladder ^at <p>)
  3895.     (monkey ^on ladder)
  3896.     -->
  3897.     (make goal ^status active ^type holds ^object nil))
  3898.  
  3899.  
  3900. (p mb4
  3901.     (goal ^status active ^type holds ^object <w>)
  3902.     (object ^name <w> ^at <p> ^on ceiling)
  3903.     (object ^name ladder ^at <p>)
  3904.     (monkey ^on ladder ^holds nil)
  3905.     -->
  3906.     (write (crlf) grab <w>)
  3907.     (modify 4 ^holds <w>)
  3908.     (modify 1 ^status satified))
  3909.  
  3910.  
  3911.  
  3912. (p mb5
  3913.     (goal ^status active ^type holds ^object <w>)
  3914.     (object ^name <w> ^at <p> ^on floor)
  3915.     -->
  3916.     (make goal ^status active ^type walk-to ^object <p>))
  3917.  
  3918.  
  3919. (p mb6
  3920.     (goal ^status active ^type holds ^object <w>)
  3921.     (object ^name <w> ^at <p> ^on floor)
  3922.     (monkey ^at <p>)
  3923.     -->
  3924.     (make goal ^status active ^type holds ^object nil))
  3925.  
  3926.  
  3927. (p mb7
  3928.     (goal ^status active ^type holds ^object <w>)
  3929.     (object ^name <w> ^at <p> ^on floor)
  3930.     (monkey ^at <p> ^holds nil)
  3931.     -->
  3932.     (write (crlf) grab <w>)
  3933.     (modify 3 ^holds <w>)
  3934.     (modify 1 ^status satisfied))
  3935.  
  3936. (p mb8
  3937.     (goal ^status active ^type move ^object <o> ^to <p>)
  3938.     (object ^name <o> ^weight light ^at <> <p>)
  3939.     -->
  3940.     (make goal ^status active ^type holds ^object <o>))
  3941.  
  3942.  
  3943. (p mb9
  3944.     (goal ^status active ^type move ^object <o> ^to <p>)
  3945.     (object ^name <o> ^weight light ^at <> <p>)
  3946.     (monkey ^holds <o>)
  3947.     -->
  3948.     (make goal ^status active ^type walk-to ^object <p>))
  3949.  
  3950.  
  3951. (p mb10
  3952.     (goal ^status active ^type move ^object <o> ^to <p>)
  3953.     (object ^name <o> ^weight light ^at <p>)
  3954.     -->
  3955.     (modify 1 ^status satisfied))
  3956.  
  3957.  
  3958. (p mb11
  3959.     (goal ^status active ^type walk-to ^object <p>)
  3960.     -->
  3961.     (make goal ^status active ^type on ^object floor))
  3962.  
  3963. (p mb12
  3964.     (goal ^status active ^type walk-to ^object <p>)
  3965.     (monkey ^on floor ^at {<c> <> <p>} ^holds nil)
  3966.     -->
  3967.     (write (crlf) walk to <p>)
  3968.     (modify 2 ^at <p>)
  3969.     (modify 1 ^status satisfied))
  3970.  
  3971.  
  3972. (p mb13
  3973.     (goal ^status active ^type walk-to ^object <p>)
  3974.     (monkey ^on floor ^at {<c> <> <p>} ^holds <w> <> nil)
  3975.     (object ^name <w>)
  3976.     -->
  3977.     (write (crlf) walk to <p>)
  3978.     (modify 2 ^at <p>)
  3979.     (modify 3 ^at <p>)
  3980.     (modify 1 ^status satisfied))
  3981.  
  3982. (p mb14
  3983.     (goal ^status active ^type on ^object floor)
  3984.     (monkey ^on {<x> <> floor})
  3985.     -->
  3986.     (write (crlf) jump onto the floor)
  3987.     (modify 2 ^on floor)
  3988.     (modify 1 ^status satisfied))
  3989.  
  3990. (p mb15
  3991.     (goal ^status active ^type on ^object <o>)
  3992.     (object ^name <o> ^at <p>)
  3993.     -->
  3994.     (make goal ^status active ^type walk-to ^object <p>))
  3995.  
  3996.  
  3997.  
  3998. (p mb16
  3999.     (goal ^status active ^type on ^object <o>)
  4000.     (object ^name <o> ^at <p>)
  4001.     (monkey ^at <p>)
  4002.     -->
  4003.     (make goal ^status active ^type holds ^object nil))
  4004.  
  4005.  
  4006. (p mb17
  4007.     (goal ^status active ^type on ^object <o>)
  4008.     (object ^name <o> ^at <p>)
  4009.     (monkey ^at <p> ^holds nil)
  4010.     -->
  4011.     (write (crlf) climb onto <o>)
  4012.     (modify 3 ^on <o>)
  4013.     (modify 1 ^status satisfied))
  4014.  
  4015. (p mb18
  4016.     (goal ^status active ^type holds ^object nil)
  4017.     (monkey ^holds {<x> <> nil})
  4018.     -->
  4019.     (write (crlf) drop <x>)
  4020.     (modify 2 ^holds nil)
  4021.     (modify 1 ^status satisfied))
  4022.  
  4023. (p mb19
  4024.     (goal ^status active)
  4025.     -->
  4026.     (modify 1 ^status not-processed))
  4027.  
  4028. (p t1
  4029.     (start 1)
  4030.     -->
  4031.     (make monkey ^at 5-7 ^on couch)
  4032.     (make object ^name couch ^at 5-7 ^weight heavy)
  4033.     (make object ^name bananas ^on ceiling ^at 2-2)
  4034.     (make object ^name ladder ^on floor ^at 9-5 ^weight light)
  4035.     (make goal ^status active ^type holds ^object bananas))
  4036.  
  4037. e to file: ")))
  4038.  
  4039. (defun openinf nil
  4040.     (setq inf (open (inquire "input filename: ") :direction :input)))
  4041. (defun openoutf nil
  4042.     (setq outf (open (inquire "output filename: "
  4043. (literalize try att)
  4044.  
  4045. (p try
  4046.     (try ^att 1)
  4047.     -->
  4048.     (write (crlf) ops5 installed (crlf)))
  4049.  
  4050. (make try ^att 1)
  4051.  
  4052. (run)
  4053. OPSENV  LSP '
  4054. !≥+"MAB     L   ╙s'
  4055. √RTRY5    L  Y!