home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e051 / 3.ddi / FRANZ / VPS2.L < prev   
Encoding:
Text File  |  1980-01-01  |  85.5 KB  |  2,810 lines

  1.    (setq buc (assoc num (buckets)))
  2.     (and (not (memq name buc))
  3.          (rplacd buc (cons name (cdr buc))))
  4.     (return buc))) 
  5.  
  6. (defun buckets nil
  7.   (and (atom *buckets*) (setq *buckets* (make-nums *buckets*)))
  8.   *buckets*) 
  9.  
  10. (defun make-nums (k)
  11.   (prog (nums)
  12.         (setq nums nil)
  13.    l    (and (< k 2.) (return nums))
  14.         (setq nums (cons (ncons k) nums))
  15.         (setq k (1- k))
  16.         (go l))) 
  17.  
  18. (defun erase-literal-info (class)
  19.   (mapc (function erase-literal-info2) (get class 'att-list))
  20.   (remprop class 'att-list)) 
  21.  
  22. (defun erase-literal-info2 (att) (remprop att 'conflicts)) 
  23.  
  24.  
  25. ;;; LHS Compiler
  26.  
  27. (defun p fexpr (z) 
  28.   (finish-literalize)
  29.   (princ '*) 
  30.   (drain)
  31.   (compile-production (car z) (cdr z))) 
  32.  
  33. (defun compile-production (name matrix)
  34.   (prog (erm)
  35.         (setq *p-name* name)
  36.         (setq erm (catch (cmp-p name matrix) !error!))
  37.     (setq *p-name* nil)))
  38.  
  39. (defun peek-lex nil (car *matrix*)) 
  40.  
  41. (defun lex nil
  42.   (prog2 nil (car *matrix*) (setq *matrix* (cdr *matrix*)))) 
  43.  
  44. (defun end-of-p nil (atom *matrix*)) 
  45.  
  46. (defun rest-of-p nil *matrix*) 
  47.  
  48. (defun prepare-lex (prod) (setq *matrix* prod)) 
  49.  
  50.  
  51. (defun peek-sublex nil (car *curcond*)) 
  52.  
  53. (defun sublex nil
  54.   (prog2 nil (car *curcond*) (setq *curcond* (cdr *curcond*)))) 
  55.  
  56. (defun end-of-ce nil (atom *curcond*)) 
  57.  
  58. (defun rest-of-ce nil *curcond*) 
  59.  
  60. (defun prepare-sublex (ce) (setq *curcond* ce)) 
  61.  
  62. (defun make-bottom-node nil (setq *first-node* (list '&bus nil))) 
  63.  
  64. (defun cmp-p (name matrix)
  65.   (prog (m bakptrs)
  66.         (cond ((or (null name) (dtpr name))
  67.                (%error '|illegal production name| name))
  68.               ((equal (get name 'production) matrix)
  69.            (return nil)))
  70.         (prepare-lex matrix)
  71.         (excise-p name)
  72.         (setq bakptrs nil)
  73.         (setq *pcount* (1+ *pcount*))
  74.         (setq *feature-count* 0.)
  75.     (setq *ce-count* 0)
  76.         (setq *vars* nil)
  77.         (setq *ce-vars* nil)
  78.     (setq *rhs-bound-vars* nil)
  79.     (setq *rhs-bound-ce-vars* nil)
  80.         (setq *last-branch* nil)
  81.         (setq m (rest-of-p))
  82.    l1   (and (end-of-p) (%error '|no '-->' in production| m))
  83.         (cmp-prin)
  84.         (setq bakptrs (cons *last-branch* bakptrs))
  85.         (or (eq '--> (peek-lex)) (go l1))
  86.         (lex)
  87.     (check-rhs (rest-of-p))
  88.         (link-new-node (list '&p
  89.                              *feature-count*
  90.                              name
  91.                              (encode-dope)
  92.                              (encode-ce-dope)
  93.                              (cons 'progn (rest-of-p))))
  94.         (putprop name (cdr (nreverse bakptrs)) 'backpointers)
  95.     (putprop name matrix 'production)
  96.         (putprop name *last-node* 'topnode))) 
  97.  
  98. (defun rating-part (pnode) (cadr pnode)) 
  99.  
  100. (defun var-part (pnode) (car (cdddr pnode))) 
  101.  
  102. (defun ce-var-part (pnode) (cadr (cdddr pnode))) 
  103.  
  104. (defun rhs-part (pnode) (caddr (cdddr pnode))) 
  105.  
  106. (defun excise-p (name)
  107.   (cond ((and (symbolp name) (get name 'topnode))
  108.      (printline (list name 'is 'excised))
  109.          (setq *pcount* (1- *pcount*))
  110.          (remove-from-conflict-set name)
  111.          (kill-node (get name 'topnode))
  112.      (remprop name 'production)
  113.      (remprop name 'backpointers)
  114.          (remprop name 'topnode)))) 
  115.  
  116. (defun kill-node (node)
  117.   (prog nil
  118.    top  (and (atom node) (return nil))
  119.         (rplaca node '&old)
  120.         (setq node (cdr node))
  121.         (go top))) 
  122.  
  123. (defun cmp-prin nil
  124.   (prog nil
  125.         (setq *last-node* *first-node*)
  126.         (cond ((null *last-branch*) (cmp-posce) (cmp-nobeta))
  127.               ((eq (peek-lex) '-) (cmp-negce) (cmp-not))
  128.               (t (cmp-posce) (cmp-and))))) 
  129.  
  130. (defun cmp-negce nil (lex) (cmp-ce)) 
  131.  
  132. (defun cmp-posce nil
  133.   (setq *ce-count* (1+ *ce-count*))
  134.   (cond ((eq (peek-lex) '\{) (cmp-ce+cevar))
  135.         (t (cmp-ce)))) 
  136.  
  137. (defun cmp-ce+cevar nil
  138.   (prog (z)
  139.         (lex)
  140.         (cond ((atom (peek-lex)) (cmp-cevar) (cmp-ce))
  141.               (t (cmp-ce) (cmp-cevar)))
  142.         (setq z (lex))
  143.         (or (eq z '\}) (%error '|missing '}'| z)))) 
  144.  
  145. (defun new-subnum (k)
  146.   (or (numberp k) (%error '|tab must be a number| k))
  147.   (setq *subnum* (fix k))) 
  148.  
  149. (defun incr-subnum nil (setq *subnum* (1+ *subnum*))) 
  150.  
  151. (defun cmp-ce nil
  152.   (prog (z)
  153.         (new-subnum 0.)
  154.         (setq *cur-vars* nil)
  155.         (setq z (lex))
  156.         (and (atom z)
  157.              (%error '|atomic conditions are not allowed| z))
  158.         (prepare-sublex z)
  159.    la   (and (end-of-ce) (return nil))
  160.         (incr-subnum)
  161.         (cmp-element)
  162.         (go la))) 
  163.  
  164. (defun cmp-element nil
  165.         (and (eq (peek-sublex) '^) (cmp-tab))
  166.         (cond ((eq (peek-sublex) '\{) (cmp-product))
  167.               (t (cmp-atomic-or-any))))
  168.  
  169. (defun cmp-atomic-or-any nil
  170.         (cond ((eq (peek-sublex) '<<) (cmp-any))
  171.               (t (cmp-atomic))))
  172.  
  173. (defun cmp-any nil
  174.   (prog (a z)
  175.         (sublex)
  176.         (setq z nil)
  177.    la   (cond ((end-of-ce) (%error '|missing '>>'| a)))
  178.         (setq a (sublex))
  179.         (cond ((not (eq '>> a)) (setq z (cons a z)) (go la)))
  180.         (link-new-node (list '&any nil (current-field) z)))) 
  181.  
  182.  
  183. (defun cmp-tab nil
  184.   (prog (r)
  185.         (sublex)
  186.         (setq r (sublex))
  187.         (setq r ($litbind r))
  188.         (new-subnum r))) 
  189.  
  190. (defun $litbind (x)
  191.   (prog (r)
  192.         (cond ((and (symbolp x) (setq r (literal-binding-of x)))
  193.                (return r))
  194.               (t (return x))))) 
  195.  
  196. (defun get-bind (x)
  197.   (prog (r)
  198.         (cond ((and (symbolp x) (setq r (literal-binding-of x)))
  199.                (return r))
  200.               (t (return nil))))) 
  201.  
  202. (defun cmp-atomic nil
  203.   (prog (test x)
  204.         (setq x (peek-sublex))
  205.         (cond ((eq x '=) (setq test 'eq) (sublex))
  206.               ((eq x '<>) (setq test 'ne) (sublex))
  207.               ((eq x '<) (setq test 'lt) (sublex))
  208.               ((eq x '<=) (setq test 'le) (sublex))
  209.               ((eq x '>) (setq test 'gt) (sublex))
  210.               ((eq x '>=) (setq test 'ge) (sublex))
  211.               ((eq x '<=>) (setq test 'xx) (sublex))
  212.               (t (setq test 'eq)))
  213.         (cmp-symbol test))) 
  214.  
  215. (defun cmp-product nil
  216.   (prog (save)
  217.         (setq save (rest-of-ce))
  218.         (sublex)
  219.    la   (cond ((end-of-ce)
  220.                (cond ((member '\} save) 
  221.               (%error '|wrong contex for '}'| save))
  222.              (t (%error '|missing '}'| save))))
  223.               ((eq (peek-sublex) '\}) (sublex) (return nil)))
  224.         (cmp-atomic-or-any)
  225.         (go la))) 
  226.  
  227. (defun variablep (x) (and (symbolp x) (eq (getchar x 1.) '<))) 
  228.  
  229. (defun cmp-symbol (test)
  230.   (prog (flag)
  231.         (setq flag t)
  232.         (cond ((eq (peek-sublex) '//) (sublex) (setq flag nil)))
  233.         (cond ((and flag (variablep (peek-sublex)))
  234.                (cmp-var test))
  235.               ((numberp (peek-sublex)) (cmp-number test))
  236.               ((symbolp (peek-sublex)) (cmp-constant test))
  237.               (t (%error '|unrecognized symbol| (sublex)))))) 
  238.  
  239. (defun cmp-constant (test)
  240.   (or (memq test '(eq ne xx))
  241.       (%error '|non-numeric constant after numeric predicate| (sublex)))
  242.   (link-new-node (list (concat 't test 'a)
  243.                        nil
  244.                        (current-field)
  245.                        (sublex)))) 
  246.  
  247. (defun cmp-number (test)
  248.   (link-new-node (list (concat 't test 'n)
  249.                        nil
  250.                        (current-field)
  251.                        (sublex)))) 
  252.  
  253. (defun current-field nil (field-name *subnum*)) 
  254.  
  255. (defun field-name (num)
  256.   (cond ((= num 1.) '*c1*)
  257.         ((= num 2.) '*c2*)
  258.         ((= num 3.) '*c3*)
  259.         ((= num 4.) '*c4*)
  260.         ((= num 5.) '*c5*)
  261.         ((= num 6.) '*c6*)
  262.         ((= num 7.) '*c7*)
  263.         ((= num 8.) '*c8*)
  264.         ((= num 9.) '*c9*)
  265.         ((= num 10.) '*c10*)
  266.         ((= num 11.) '*c11*)
  267.         ((= num 12.) '*c12*)
  268.         ((= num 13.) '*c13*)
  269.         ((= num 14.) '*c14*)
  270.         ((= num 15.) '*c15*)
  271.         ((= num 16.) '*c16*)
  272.         ((= num 17.) '*c17*)
  273.         ((= num 18.) '*c18*)
  274.         ((= num 19.) '*c19*)
  275.         ((= num 20.) '*c20*)
  276.         ((= num 21.) '*c21*)
  277.         ((= num 22.) '*c22*)
  278.         ((= num 23.) '*c23*)
  279.         ((= num 24.) '*c24*)
  280.         ((= num 25.) '*c25*)
  281.         ((= num 26.) '*c26*)
  282.         ((= num 27.) '*c27*)
  283.         ((= num 28.) '*c28*)
  284.         ((= num 29.) '*c29*)
  285.         ((= num 30.) '*c30*)
  286.         ((= num 31.) '*c31*)
  287.         ((= num 32.) '*c32*)
  288.         ((= num 33.) '*c33*)
  289.         ((= num 34.) '*c34*)
  290.         ((= num 35.) '*c35*)
  291.         ((= num 36.) '*c36*)
  292.         ((= num 37.) '*c37*)
  293.         ((= num 38.) '*c38*)
  294.         ((= num 39.) '*c39*)
  295.         ((= num 40.) '*c40*)
  296.         ((= num 41.) '*c41*)
  297.         ((= num 42.) '*c42*)
  298.         ((= num 43.) '*c43*)
  299.         ((= num 44.) '*c44*)
  300.         ((= num 45.) '*c45*)
  301.         ((= num 46.) '*c46*)
  302.         ((= num 47.) '*c47*)
  303.         ((= num 48.) '*c48*)
  304.         ((= num 49.) '*c49*)
  305.         ((= num 50.) '*c50*)
  306.         ((= num 51.) '*c51*)
  307.         ((= num 52.) '*c52*)
  308.         ((= num 53.) '*c53*)
  309.         ((= num 54.) '*c54*)
  310.         ((= num 55.) '*c55*)
  311.         ((= num 56.) '*c56*)
  312.         ((= num 57.) '*c57*)
  313.         ((= num 58.) '*c58*)
  314.         ((= num 59.) '*c59*)
  315.         ((= num 60.) '*c60*)
  316.         ((= num 61.) '*c61*)
  317.         ((= num 62.) '*c62*)
  318.         ((= num 63.) '*c63*)
  319.         ((= num 64.) '*c64*)
  320.         (t (%error '|condition is too long| (rest-of-ce))))) 
  321.  
  322.  
  323. ;;; Compiling variables
  324. ;
  325. ;
  326. ;
  327. ; *cur-vars* are the variables in the condition element currently 
  328. ; being compiled.  *vars* are the variables in the earlier condition
  329. ; elements.  *ce-vars* are the condition element variables.  note
  330. ; that the interpreter will not confuse condition element and regular
  331. ; variables even if they have the same name.
  332. ;
  333. ; *cur-vars* is a list of triples: (name predicate subelement-number)
  334. ; eg:        ( (<x> eq 3)
  335. ;          (<y> ne 1)
  336. ;          . . . )
  337. ;
  338. ; *vars* is a list of triples: (name ce-number subelement-number)
  339. ; eg:        ( (<x> 3 3)
  340. ;          (<y> 1 1)
  341. ;          . . . )
  342. ;
  343. ; *ce-vars* is a list of pairs: (name ce-number)
  344. ; eg:        ( (ce1 1)
  345. ;          (<c3> 3)
  346. ;          . . . )
  347.  
  348. (defun var-dope (var) (assq var *vars*))
  349.  
  350. (defun ce-var-dope (var) (assq var *ce-vars*))
  351.  
  352. (defun cmp-var (test)
  353.   (prog (old name)
  354.         (setq name (sublex))
  355.         (setq old (assq name *cur-vars*))
  356.         (cond ((and old (eq (cadr old) 'eq))
  357.                (cmp-old-eq-var test old))
  358.               ((and old (eq test 'eq)) (cmp-new-eq-var name old))
  359.               (t (cmp-new-var name test))))) 
  360.  
  361. (defun cmp-new-var (name test)
  362.   (setq *cur-vars* (cons (list name test *subnum*) *cur-vars*))) 
  363.  
  364. (defun cmp-old-eq-var (test old)
  365.   (link-new-node (list (concat 't test 's)
  366.                        nil
  367.                        (current-field)
  368.                        (field-name (caddr old))))) 
  369.  
  370. (defun cmp-new-eq-var (name old)
  371.   (prog (pred next)
  372.         (setq *cur-vars* (delq old *cur-vars*))
  373.         (setq next (assq name *cur-vars*))
  374.         (cond (next (cmp-new-eq-var name next))
  375.               (t (cmp-new-var name 'eq)))
  376.         (setq pred (cadr old))
  377.         (link-new-node (list (concat 't pred 's)
  378.                              nil
  379.                              (field-name (caddr old))
  380.                              (current-field))))) 
  381.  
  382. (defun cmp-cevar nil
  383.   (prog (name old)
  384.         (setq name (lex))
  385.         (setq old (assq name *ce-vars*))
  386.         (and old
  387.              (%error '|condition element variable used twice| name))
  388.         (setq *ce-vars* (cons (list name 0.) *ce-vars*)))) 
  389.  
  390. (defun cmp-not nil (cmp-beta '¬)) 
  391.  
  392. (defun cmp-nobeta nil (cmp-beta nil)) 
  393.  
  394. (defun cmp-and nil (cmp-beta '&and)) 
  395.  
  396. (defun cmp-beta (kind)
  397.   (prog (tlist vdope vname vpred vpos old)
  398.         (setq tlist nil)
  399.    la   (and (atom *cur-vars*) (go lb))
  400.         (setq vdope (car *cur-vars*))
  401.         (setq *cur-vars* (cdr *cur-vars*))
  402.         (setq vname (car vdope))
  403.         (setq vpred (cadr vdope))
  404.         (setq vpos (caddr vdope))
  405.         (setq old (assq vname *vars*))
  406.         (cond (old (setq tlist (add-test tlist vdope old)))
  407.               ((neq kind '¬) (promote-var vdope)))
  408.         (go la)
  409.    lb   (and kind (build-beta kind tlist))
  410.         (or (eq kind '¬) (fudge))
  411.         (setq *last-branch* *last-node*))) 
  412.  
  413. (defun add-test (list new old)
  414.   (prog (ttype lloc rloc)
  415.     (setq *feature-count* (1+ *feature-count*))
  416.         (setq ttype (concat 't (cadr new) 'b))
  417.         (setq rloc (encode-singleton (caddr new)))
  418.         (setq lloc (encode-pair (cadr old) (caddr old)))
  419.         (return (cons ttype (cons lloc (cons rloc list)))))) 
  420.  
  421. ; the following two functions encode indices so that gelm can
  422. ; decode them as fast as possible
  423.  
  424. (defun encode-pair (a b) (+ (* 10000. (1- a)) (1- b))) 
  425.  
  426. (defun encode-singleton (a) (1- a)) 
  427.  
  428. (defun promote-var (dope)
  429.   (prog (vname vpred vpos new)
  430.         (setq vname (car dope))
  431.         (setq vpred (cadr dope))
  432.         (setq vpos (caddr dope))
  433.         (or (eq 'eq vpred)
  434.             (%error '|illegal predicate for first occurrence|
  435.                    (list vname vpred)))
  436.         (setq new (list vname 0. vpos))
  437.         (setq *vars* (cons new *vars*)))) 
  438.  
  439. (defun fudge nil
  440.   (mapc (function fudge*) *vars*)
  441.   (mapc (function fudge*) *ce-vars*)) 
  442.  
  443. (defun fudge* (z)
  444.   (prog (a) (setq a (cdr z)) (rplaca a (1+ (car a))))) 
  445.  
  446. (defun build-beta (type tests)
  447.   (prog (rpred lpred lnode lef)
  448.         (link-new-node (list '&mem nil nil (protomem)))
  449.         (setq rpred *last-node*)
  450.         (cond ((eq type '&and)
  451.                (setq lnode (list '&mem nil nil (protomem))))
  452.               (t (setq lnode (list '&two nil nil))))
  453.         (setq lpred (link-to-branch lnode))
  454.         (cond ((eq type '&and) (setq lef lpred))
  455.               (t (setq lef (protomem))))
  456.         (link-new-beta-node (list type nil lef rpred tests)))) 
  457.  
  458. (defun protomem nil (list nil)) 
  459.  
  460. (defun memory-part (mem-node) (car (cadddr mem-node))) 
  461.  
  462. (defun encode-dope nil
  463.   (prog (r all z k)
  464.         (setq r nil)
  465.         (setq all *vars*)
  466.    la   (and (atom all) (return r))
  467.         (setq z (car all))
  468.         (setq all (cdr all))
  469.         (setq k (encode-pair (cadr z) (caddr z)))
  470.         (setq r (cons (car z) (cons k r)))
  471.         (go la))) 
  472.  
  473. (defun encode-ce-dope nil
  474.   (prog (r all z k)
  475.         (setq r nil)
  476.         (setq all *ce-vars*)
  477.    la   (and (atom all) (return r))
  478.         (setq z (car all))
  479.         (setq all (cdr all))
  480.         (setq k (cadr z))
  481.         (setq r (cons (car z) (cons k r)))
  482.         (go la))) 
  483.  
  484.  
  485.  
  486. ;;; Linking the nodes
  487.  
  488. (defun link-new-node (r)
  489.   (cond ((not (member (car r) '(&p &mem &two &and ¬)))
  490.      (setq *feature-count* (1+ *feature-count*))))
  491.   (setq *virtual-cnt* (1+ *virtual-cnt*))
  492.   (setq *last-node* (link-left *last-node* r))) 
  493.  
  494. (defun link-to-branch (r)
  495.   (setq *virtual-cnt* (1+ *virtual-cnt*))
  496.   (setq *last-branch* (link-left *last-branch* r))) 
  497.  
  498. (defun link-new-beta-node (r)
  499.   (setq *virtual-cnt* (1+ *virtual-cnt*))
  500.   (setq *last-node* (link-both *last-branch* *last-node* r))
  501.   (setq *last-branch* *last-node*)) 
  502.  
  503. (defun link-left (pred succ)
  504.   (prog (a r)
  505.         (setq a (left-outs pred))
  506.         (setq r (find-equiv-node succ a))
  507.         (and r (return r))
  508.         (setq *real-cnt* (1+ *real-cnt*))
  509.         (attach-left pred succ)
  510.         (return succ))) 
  511.  
  512. (defun link-both (left right succ)
  513.   (prog (a r)
  514.         (setq a (interq (left-outs left) (right-outs right)))
  515.         (setq r (find-equiv-beta-node succ a))
  516.         (and r (return r))
  517.         (setq *real-cnt* (1+ *real-cnt*))
  518.         (attach-left left succ)
  519.         (attach-right right succ)
  520.         (return succ))) 
  521.  
  522. (defun attach-right (old new)
  523.   (rplaca (cddr old) (cons new (caddr old)))) 
  524.  
  525. (defun attach-left (old new)
  526.   (rplaca (cdr old) (cons new (cadr old)))) 
  527.  
  528. (defun right-outs (node) (caddr node)) 
  529.  
  530. (defun left-outs (node) (cadr node)) 
  531.  
  532. (defun find-equiv-node (node list)
  533.   (prog (a)
  534.         (setq a list)
  535.    l1   (cond ((atom a) (return nil))
  536.               ((equiv node (car a)) (return (car a))))
  537.         (setq a (cdr a))
  538.         (go l1))) 
  539.  
  540. (defun find-equiv-beta-node (node list)
  541.   (prog (a)
  542.         (setq a list)
  543.    l1   (cond ((atom a) (return nil))
  544.               ((beta-equiv node (car a)) (return (car a))))
  545.         (setq a (cdr a))
  546.         (go l1))) 
  547.  
  548. ; do not look at the predecessor fields of beta nodes; they have to be
  549. ; identical because of the way the candidate nodes were found
  550.  
  551. (defun equiv (a b)
  552.   (and (eq (car a) (car b))
  553.        (or (eq (car a) '&mem)
  554.            (eq (car a) '&two)
  555.            (equal (caddr a) (caddr b)))
  556.        (equal (cdddr a) (cdddr b)))) 
  557.  
  558. (defun beta-equiv (a b)
  559.   (and (eq (car a) (car b))
  560.        (equal (cddddr a) (cddddr b))
  561.        (or (eq (car a) '&and) (equal (caddr a) (caddr b))))) 
  562.  
  563. ; the equivalence tests are set up to consider the contents of
  564. ; node memories, so they are ready for the build action
  565.  
  566. ;;; Network interpreter
  567.  
  568. (defun match (flag wme)
  569.   (sendto flag (list wme) 'left (list *first-node*)))
  570.  
  571. ; note that eval-nodelist is not set up to handle building
  572. ; productions.  would have to add something like ops4's build-flag
  573.  
  574. (defun eval-nodelist (nl)
  575.   (prog nil
  576.    top  (and (not nl) (return nil))
  577.         (setq *sendtocall* nil)
  578.     (setq *last-node* (car nl))
  579.         (apply (caar nl) (cdar nl))
  580.         (setq nl (cdr nl))
  581.         (go top))) 
  582.  
  583. (defun sendto (flag data side nl)
  584.   (prog nil
  585.    top  (and (not nl) (return nil))
  586.         (setq *side* side)
  587.         (setq *flag-part* flag)
  588.         (setq *data-part* data)
  589.         (setq *sendtocall* t)
  590.     (setq *last-node* (car nl))
  591.         (apply (caar nl) (cdar nl))
  592.         (setq nl (cdr nl))
  593.         (go top))) 
  594.  
  595. ; &bus sets up the registers for the one-input nodes.  note that this
  596. (defun &bus (outs)
  597.   (prog (dp)
  598.         (setq *alpha-flag-part* *flag-part*)
  599.         (setq *alpha-data-part* *data-part*)
  600.         (setq dp (car *data-part*))
  601.         (setq *c1* (car dp))
  602.         (setq dp (cdr dp))
  603.         (setq *c2* (car dp))
  604.         (setq dp (cdr dp))
  605.         (setq *c3* (car dp))
  606.         (setq dp (cdr dp))
  607.         (setq *c4* (car dp))
  608.         (setq dp (cdr dp))
  609.         (setq *c5* (car dp))
  610.         (setq dp (cdr dp))
  611.         (setq *c6* (car dp))
  612.         (setq dp (cdr dp))
  613.         (setq *c7* (car dp))
  614.         (setq dp (cdr dp))
  615.         (setq *c8* (car dp))
  616.         (setq dp (cdr dp))
  617.         (setq *c9* (car dp))
  618.         (setq dp (cdr dp))
  619.         (setq *c10* (car dp))
  620.         (setq dp (cdr dp))
  621.         (setq *c11* (car dp))
  622.         (setq dp (cdr dp))
  623.         (setq *c12* (car dp))
  624.         (setq dp (cdr dp))
  625.         (setq *c13* (car dp))
  626.         (setq dp (cdr dp))
  627.         (setq *c14* (car dp))
  628.         (setq dp (cdr dp))
  629.         (setq *c15* (car dp))
  630.         (setq dp (cdr dp))
  631.         (setq *c16* (car dp))
  632.         (setq dp (cdr dp))
  633.         (setq *c17* (car dp))
  634.         (setq dp (cdr dp))
  635.         (setq *c18* (car dp))
  636.         (setq dp (cdr dp))
  637.         (setq *c19* (car dp))
  638.         (setq dp (cdr dp))
  639.         (setq *c20* (car dp))
  640.         (setq dp (cdr dp))
  641.         (setq *c21* (car dp))
  642.         (setq dp (cdr dp))
  643.         (setq *c22* (car dp))
  644.         (setq dp (cdr dp))
  645.         (setq *c23* (car dp))
  646.         (setq dp (cdr dp))
  647.         (setq *c24* (car dp))
  648.         (setq dp (cdr dp))
  649.         (setq *c25* (car dp))
  650.         (setq dp (cdr dp))
  651.         (setq *c26* (car dp))
  652.         (setq dp (cdr dp))
  653.         (setq *c27* (car dp))
  654.         (setq dp (cdr dp))
  655.         (setq *c28* (car dp))
  656.         (setq dp (cdr dp))
  657.         (setq *c29* (car dp))
  658.         (setq dp (cdr dp))
  659.         (setq *c30* (car dp))
  660.         (setq dp (cdr dp))
  661.         (setq *c31* (car dp))
  662.         (setq dp (cdr dp))
  663.         (setq *c32* (car dp))
  664.         (setq dp (cdr dp))
  665.         (setq *c33* (car dp))
  666.         (setq dp (cdr dp))
  667.         (setq *c34* (car dp))
  668.         (setq dp (cdr dp))
  669.         (setq *c35* (car dp))
  670.         (setq dp (cdr dp))
  671.         (setq *c36* (car dp))
  672.         (setq dp (cdr dp))
  673.         (setq *c37* (car dp))
  674.         (setq dp (cdr dp))
  675.         (setq *c38* (car dp))
  676.         (setq dp (cdr dp))
  677.         (setq *c39* (car dp))
  678.         (setq dp (cdr dp))
  679.         (setq *c40* (car dp))
  680.         (setq dp (cdr dp))
  681.         (setq *c41* (car dp))
  682.         (setq dp (cdr dp))
  683.         (setq *c42* (car dp))
  684.         (setq dp (cdr dp))
  685.         (setq *c43* (car dp))
  686.         (setq dp (cdr dp))
  687.         (setq *c44* (car dp))
  688.         (setq dp (cdr dp))
  689.         (setq *c45* (car dp))
  690.         (setq dp (cdr dp))
  691.         (setq *c46* (car dp))
  692.         (setq dp (cdr dp))
  693.         (setq *c47* (car dp))
  694.         (setq dp (cdr dp))
  695.         (setq *c48* (car dp))
  696.         (setq dp (cdr dp))
  697.         (setq *c49* (car dp))
  698.         (setq dp (cdr dp))
  699.         (setq *c50* (car dp))
  700.         (setq dp (cdr dp))
  701.         (setq *c51* (car dp))
  702.         (setq dp (cdr dp))
  703.         (setq *c52* (car dp))
  704.         (setq dp (cdr dp))
  705.         (setq *c53* (car dp))
  706.         (setq dp (cdr dp))
  707.         (setq *c54* (car dp))
  708.         (setq dp (cdr dp))
  709.         (setq *c55* (car dp))
  710.         (setq dp (cdr dp))
  711.         (setq *c56* (car dp))
  712.         (setq dp (cdr dp))
  713.         (setq *c57* (car dp))
  714.         (setq dp (cdr dp))
  715.         (setq *c58* (car dp))
  716.         (setq dp (cdr dp))
  717.         (setq *c59* (car dp))
  718.         (setq dp (cdr dp))
  719.         (setq *c60* (car dp))
  720.         (setq dp (cdr dp))
  721.         (setq *c61* (car dp))
  722.         (setq dp (cdr dp))
  723.         (setq *c62* (car dp))
  724.         (setq dp (cdr dp))
  725.         (setq *c63* (car dp))
  726.         (setq dp (cdr dp))
  727.         (setq *c64* (car dp))
  728.         (eval-nodelist outs))) 
  729.  
  730. (defun &any (outs register const-list)
  731.   (prog (z c)
  732.         (setq z (fast-symeval register))
  733.         (cond ((numberp z) (go number)))
  734.    symbol (cond ((null const-list) (return nil))
  735.                 ((eq (car const-list) z) (go ok))
  736.                 (t (setq const-list (cdr const-list)) (go symbol)))
  737.    number (cond ((null const-list) (return nil))
  738.                 ((and (numberp (setq c (car const-list)))
  739.                       (=alg c z))
  740.                  (go ok))
  741.                 (t (setq const-list (cdr const-list)) (go number)))
  742.    ok   (eval-nodelist outs))) 
  743.  
  744. (defun teqa (outs register constant)
  745.   (and (eq (fast-symeval register) constant) (eval-nodelist outs))) 
  746.  
  747. (defun tnea (outs register constant)
  748.   (and (not (eq (fast-symeval register) constant)) (eval-nodelist outs))) 
  749.  
  750. (defun txxa (outs register constant)
  751.   (and (symbolp (fast-symeval register)) (eval-nodelist outs))) 
  752.  
  753. (defun teqn (outs register constant)
  754.   (prog (z)
  755.         (setq z (fast-symeval register))
  756.         (and (numberp z)
  757.              (=alg z constant)
  758.              (eval-nodelist outs)))) 
  759.  
  760. (defun tnen (outs register constant)
  761.   (prog (z)
  762.         (setq z (fast-symeval register))
  763.         (and (or (not (numberp z))
  764.                  (not (=alg z constant)))
  765.              (eval-nodelist outs)))) 
  766.  
  767. (defun txxn (outs register constant)
  768.   (prog (z)
  769.         (setq z (fast-symeval register))
  770.         (and (numberp z) (eval-nodelist outs)))) 
  771.  
  772. (defun tltn (outs register constant)
  773.   (prog (z)
  774.         (setq z (fast-symeval register))
  775.         (and (numberp z)
  776.              (greaterp constant z)
  777.              (eval-nodelist outs)))) 
  778.  
  779. (defun tgtn (outs register constant)
  780.   (prog (z)
  781.         (setq z (fast-symeval register))
  782.         (and (numberp z)
  783.              (greaterp z constant)
  784.              (eval-nodelist outs)))) 
  785.  
  786. (defun tgen (outs register constant)
  787.   (prog (z)
  788.         (setq z (fast-symeval register))
  789.         (and (numberp z)
  790.              (not (greaterp constant z))
  791.              (eval-nodelist outs)))) 
  792.  
  793. (defun tlen (outs register constant)
  794.   (prog (z)
  795.         (setq z (fast-symeval register))
  796.         (and (numberp z)
  797.              (not (greaterp z constant))
  798.              (eval-nodelist outs)))) 
  799.  
  800. (defun teqs (outs vara varb)
  801.   (prog (a b)
  802.         (setq a (fast-symeval vara))
  803.         (setq b (fast-symeval varb))
  804.         (cond ((eq a b) (eval-nodelist outs))
  805.               ((and (numberp a)
  806.                     (numberp b)
  807.                     (=alg a b))
  808.                (eval-nodelist outs))))) 
  809.  
  810. (defun tnes (outs vara varb)
  811.   (prog (a b)
  812.         (setq a (fast-symeval vara))
  813.         (setq b (fast-symeval varb))
  814.         (cond ((eq a b) (return nil))
  815.               ((and (numberp a)
  816.                     (numberp b)
  817.                     (=alg a b))
  818.                (return nil))
  819.               (t (eval-nodelist outs))))) 
  820.  
  821. (defun txxs (outs vara varb)
  822.   (prog (a b)
  823.         (setq a (fast-symeval vara))
  824.         (setq b (fast-symeval varb))
  825.         (cond ((and (numberp a) (numberp b)) (eval-nodelist outs))
  826.               ((and (not (numberp a)) (not (numberp b)))
  827.                (eval-nodelist outs))))) 
  828.  
  829. (defun tlts (outs vara varb)
  830.   (prog (a b)
  831.         (setq a (fast-symeval vara))
  832.         (setq b (fast-symeval varb))
  833.         (and (numberp a)
  834.              (numberp b)
  835.              (greaterp b a)
  836.              (eval-nodelist outs)))) 
  837.  
  838. (defun tgts (outs vara varb)
  839.   (prog (a b)
  840.         (setq a (fast-symeval vara))
  841.         (setq b (fast-symeval varb))
  842.         (and (numberp a)
  843.              (numberp b)
  844.              (greaterp a b)
  845.              (eval-nodelist outs)))) 
  846.  
  847. (defun tges (outs vara varb)
  848.   (prog (a b)
  849.         (setq a (fast-symeval vara))
  850.         (setq b (fast-symeval varb))
  851.         (and (numberp a)
  852.              (numberp b)
  853.              (not (greaterp b a))
  854.              (eval-nodelist outs)))) 
  855.  
  856. (defun tles (outs vara varb)
  857.   (prog (a b)
  858.         (setq a (fast-symeval vara))
  859.         (setq b (fast-symeval varb))
  860.         (and (numberp a)
  861.              (numberp b)
  862.              (not (greaterp a b))
  863.              (eval-nodelist outs)))) 
  864.  
  865. (defun &two (left-outs right-outs)
  866.   (prog (fp dp)
  867.         (cond (*sendtocall*
  868.                (setq fp *flag-part*)
  869.                (setq dp *data-part*))
  870.               (t
  871.                (setq fp *alpha-flag-part*)
  872.                (setq dp *alpha-data-part*)))
  873.         (sendto fp dp 'left left-outs)
  874.         (sendto fp dp 'right right-outs))) 
  875.  
  876. (defun &mem (left-outs right-outs memory-list)
  877.   (prog (fp dp)
  878.         (cond (*sendtocall*
  879.                (setq fp *flag-part*)
  880.                (setq dp *data-part*))
  881.               (t
  882.                (setq fp *alpha-flag-part*)
  883.                (setq dp *alpha-data-part*)))
  884.         (sendto fp dp 'left left-outs)
  885.         (add-token memory-list fp dp nil)
  886.         (sendto fp dp 'right right-outs))) 
  887.  
  888. (defun &and (outs lpred rpred tests)
  889.   (prog (mem)
  890.         (cond ((eq *side* 'right) (setq mem (memory-part lpred)))
  891.               (t (setq mem (memory-part rpred))))
  892.         (cond ((not mem) (return nil))
  893.               ((eq *side* 'right) (and-right outs mem tests))
  894.               (t (and-left outs mem tests))))) 
  895.  
  896. (defun and-left (outs mem tests)
  897.   (prog (fp dp memdp tlist tst lind rind res)
  898.         (setq fp *flag-part*)
  899.         (setq dp *data-part*)
  900.    fail (and (null mem) (return nil))
  901.         (setq memdp (car mem))
  902.         (setq mem (cdr mem))
  903.         (setq tlist tests)
  904.    tloop (and (null tlist) (go succ))
  905.         (setq tst (car tlist))
  906.         (setq tlist (cdr tlist))
  907.         (setq lind (car tlist))
  908.         (setq tlist (cdr tlist))
  909.         (setq rind (car tlist))
  910.         (setq tlist (cdr tlist))
  911.         (comment the next line differs in and-left & -right)
  912.         (setq res (funcall tst (gelm memdp rind) (gelm dp lind)))
  913.         (cond (res (go tloop))
  914.               (t (go fail)))
  915.    succ (comment the next line differs in and-left & -right)
  916.         (sendto fp (cons (car memdp) dp) 'left outs)
  917.         (go fail))) 
  918.  
  919. (defun and-right (outs mem tests)
  920.   (prog (fp dp memdp tlist tst lind rind res)
  921.         (setq fp *flag-part*)
  922.         (setq dp *data-part*)
  923.    fail (and (null mem) (return nil))
  924.         (setq memdp (car mem))
  925.         (setq mem (cdr mem))
  926.         (setq tlist tests)
  927.    tloop (and (null tlist) (go succ))
  928.         (setq tst (car tlist))
  929.         (setq tlist (cdr tlist))
  930.         (setq lind (car tlist))
  931.         (setq tlist (cdr tlist))
  932.         (setq rind (car tlist))
  933.         (setq tlist (cdr tlist))
  934.         (comment the next line differs in and-left & -right)
  935.         (setq res (funcall tst (gelm dp rind) (gelm memdp lind)))
  936.         (cond (res (go tloop))
  937.               (t (go fail)))
  938.    succ (comment the next line differs in and-left & -right)
  939.         (sendto fp (cons (car dp) memdp) 'right outs)
  940.         (go fail))) 
  941.  
  942.  
  943. (defun teqb (new eqvar)
  944.   (cond ((eq new eqvar) t)
  945.         ((not (numberp new)) nil)
  946.         ((not (numberp eqvar)) nil)
  947.         ((=alg new eqvar) t)
  948.         (t nil))) 
  949.  
  950. (defun tneb (new eqvar)
  951.   (cond ((eq new eqvar) nil)
  952.         ((not (numberp new)) t)
  953.         ((not (numberp eqvar)) t)
  954.         ((=alg new eqvar) nil)
  955.         (t t))) 
  956.  
  957. (defun tltb (new eqvar)
  958.   (cond ((not (numberp new)) nil)
  959.         ((not (numberp eqvar)) nil)
  960.         ((greaterp eqvar new) t)
  961.         (t nil))) 
  962.  
  963. (defun tgtb (new eqvar)
  964.   (cond ((not (numberp new)) nil)
  965.         ((not (numberp eqvar)) nil)
  966.         ((greaterp new eqvar) t)
  967.         (t nil))) 
  968.  
  969. (defun tgeb (new eqvar)
  970.   (cond ((not (numberp new)) nil)
  971.         ((not (numberp eqvar)) nil)
  972.         ((not (greaterp eqvar new)) t)
  973.         (t nil))) 
  974.  
  975. (defun tleb (new eqvar)
  976.   (cond ((not (numberp new)) nil)
  977.         ((not (numberp eqvar)) nil)
  978.         ((not (greaterp new eqvar)) t)
  979.         (t nil))) 
  980.  
  981. (defun txxb (new eqvar)
  982.   (cond ((numberp new)
  983.          (cond ((numberp eqvar) t)
  984.                (t nil)))
  985.         (t
  986.          (cond ((numberp eqvar) nil)
  987.                (t t))))) 
  988.  
  989.  
  990. (defun &p (rating name var-dope ce-var-dope rhs)
  991.   (prog (fp dp)
  992.         (cond (*sendtocall*
  993.                (setq fp *flag-part*)
  994.                (setq dp *data-part*))
  995.               (t
  996.                (setq fp *alpha-flag-part*)
  997.                (setq dp *alpha-data-part*)))
  998.         (and (memq fp '(nil old)) (removecs name dp))
  999.         (and fp (insertcs name dp rating)))) 
  1000.  
  1001. (defun &old (a b c d e) nil) 
  1002.  
  1003. (defun ¬ (outs lmem rpred tests)
  1004.   (cond ((and (eq *side* 'right) (eq *flag-part* 'old)) nil)
  1005.         ((eq *side* 'right) (not-right outs (car lmem) tests))
  1006.         (t (not-left outs (memory-part rpred) tests lmem)))) 
  1007.  
  1008. (defun not-left (outs mem tests own-mem)
  1009.   (prog (fp dp memdp tlist tst lind rind res c)
  1010.         (setq fp *flag-part*)
  1011.         (setq dp *data-part*)
  1012.         (setq c 0.)
  1013.    fail (and (null mem) (go fin))
  1014.         (setq memdp (car mem))
  1015.         (setq mem (cdr mem))
  1016.         (setq tlist tests)
  1017.    tloop (and (null tlist) (setq c (1+ c)) (go fail))
  1018.         (setq tst (car tlist))
  1019.         (setq tlist (cdr tlist))
  1020.         (setq lind (car tlist))
  1021.         (setq tlist (cdr tlist))
  1022.         (setq rind (car tlist))
  1023.         (setq tlist (cdr tlist))
  1024.         (comment the next line differs in not-left & -right)
  1025.         (setq res (funcall tst (gelm memdp rind) (gelm dp lind)))
  1026.         (cond (res (go tloop))
  1027.               (t (go fail)))
  1028.    fin  (add-token own-mem fp dp c)
  1029.         (and (== c 0.) (sendto fp dp 'left outs)))) 
  1030.  
  1031. (defun not-right (outs mem tests)
  1032.   (prog (fp dp memdp tlist tst lind rind res newfp inc newc)
  1033.         (setq fp *flag-part*)
  1034.         (setq dp *data-part*)
  1035.         (cond ((not fp) (setq inc -1.) (setq newfp 'new))
  1036.               ((eq fp 'new) (setq inc 1.) (setq newfp nil))
  1037.               (t (return nil)))
  1038.    fail (and (null mem) (return nil))
  1039.         (setq memdp (car mem))
  1040.         (setq newc (cadr mem))
  1041.         (setq tlist tests)
  1042.    tloop (and (null tlist) (go succ))
  1043.         (setq tst (car tlist))
  1044.         (setq tlist (cdr tlist))
  1045.         (setq lind (car tlist))
  1046.         (setq tlist (cdr tlist))
  1047.         (setq rind (car tlist))
  1048.         (setq tlist (cdr tlist))
  1049.         (comment the next line differs in not-left & -right)
  1050.         (setq res (funcall tst (gelm dp rind) (gelm memdp lind)))
  1051.         (cond (res (go tloop))
  1052.               (t (setq mem (cddr mem)) (go fail)))
  1053.    succ (setq newc (+ inc newc))
  1054.         (rplaca (cdr mem) newc)
  1055.         (cond ((or (and (== inc -1.) (== newc 0.))
  1056.                    (and (== inc 1.) (== newc 1.)))
  1057.                (sendto newfp memdp 'right outs)))
  1058.         (setq mem (cddr mem))
  1059.         (go fail))) 
  1060.  
  1061.  
  1062.  
  1063. ;;; Node memories
  1064.  
  1065.  
  1066. (defun add-token (memlis flag data-part num)
  1067.   (prog (was-present)
  1068.         (cond ((eq flag 'new)
  1069.                (setq was-present nil)
  1070.                (real-add-token memlis data-part num))
  1071.               ((not flag) 
  1072.            (setq was-present (remove-old memlis data-part num)))
  1073.               ((eq flag 'old) (setq was-present t)))
  1074.         (return was-present))) 
  1075.  
  1076. (defun real-add-token (lis data-part num)
  1077.   (setq *current-token* (1+ *current-token*))
  1078.   (cond (num (rplaca lis (cons num (car lis)))))
  1079.   (rplaca lis (cons data-part (car lis)))) 
  1080.  
  1081. (defun remove-old (lis data num)
  1082.   (cond (num (remove-old-num lis data))
  1083.         (t (remove-old-no-num lis data)))) 
  1084.  
  1085. (defun remove-old-num (lis data)
  1086.   (prog (m next last)
  1087.         (setq m (car lis))
  1088.         (cond ((atom m) (return nil))
  1089.               ((top-levels-eq data (car m))
  1090.                (setq *current-token* (1- *current-token*))
  1091.                (rplaca lis (cddr m))
  1092.                (return (car m))))
  1093.         (setq next m)
  1094.    loop (setq last next)
  1095.         (setq next (cddr next))
  1096.         (cond ((atom next) (return nil))
  1097.               ((top-levels-eq data (car next))
  1098.                (rplacd (cdr last) (cddr next))
  1099.                (setq *current-token* (1- *current-token*))
  1100.                (return (car next)))
  1101.               (t (go loop))))) 
  1102.  
  1103. (defun remove-old-no-num (lis data)
  1104.   (prog (m next last)
  1105.         (setq m (car lis))
  1106.         (cond ((atom m) (return nil))
  1107.               ((top-levels-eq data (car m))
  1108.                (setq *current-token* (1- *current-token*))
  1109.                (rplaca lis (cdr m))
  1110.                (return (car m))))
  1111.         (setq next m)
  1112.    loop (setq last next)
  1113.         (setq next (cdr next))
  1114.         (cond ((atom next) (return nil))
  1115.               ((top-levels-eq data (car next))
  1116.                (rplacd last (cdr next))
  1117.                (setq *current-token* (1- *current-token*))
  1118.                (return (car next)))
  1119.               (t (go loop))))) 
  1120.  
  1121.  
  1122.  
  1123. ;;; Conflict Resolution
  1124. ;
  1125. ;
  1126. ; each conflict set element is a list of the following form:
  1127. ; ((p-name . data-part) (sorted wm-recency) special-case-number)
  1128.  
  1129. (defun removecs (name data)
  1130.   (prog (cr-data inst cs)
  1131.         (setq cr-data (cons name data))
  1132.     (setq cs *conflict-set*)
  1133.   l:    (cond ((null cs) 
  1134.                (record-refract name data)
  1135.                (return nil)))
  1136.     (setq inst (car cs))
  1137.     (setq cs (cdr cs))
  1138.     (and (not (top-levels-eq (car inst) cr-data)) (go l:))
  1139.         (setq *conflict-set* (delq inst *conflict-set*))))
  1140.  
  1141. (defun insertcs (name data rating)
  1142.   (prog (instan)
  1143.     (and (refracted name data) (return nil))
  1144.     (setq instan (list (cons name data) (order-tags data) rating))
  1145.     (and (atom *conflict-set*) (setq *conflict-set* nil))
  1146.     (return (setq *conflict-set* (cons instan *conflict-set*))))) 
  1147.  
  1148. (defun order-tags (dat)
  1149.   (prog (tags)
  1150.         (setq tags nil)
  1151.    l1:  (and (atom dat) (go l2:))
  1152.         (setq tags (cons (creation-time (car dat)) tags))
  1153.         (setq dat (cdr dat))
  1154.         (go l1:)
  1155.    l2:  (cond ((eq *strategy* 'mea)
  1156.                (return (cons (car tags) (dsort (cdr tags)))))
  1157.               (t (return (dsort tags)))))) 
  1158.  
  1159. ; destructively sort x into descending order
  1160.  
  1161. (defun dsort (x)
  1162.   (prog (sorted cur next cval nval)
  1163.         (and (atom (cdr x)) (return x))
  1164.    loop (setq sorted t)
  1165.         (setq cur x)
  1166.         (setq next (cdr x))
  1167.    chek (setq cval (car cur))
  1168.         (setq nval (car next))
  1169.         (cond ((> nval cval)
  1170.                (setq sorted nil)
  1171.                (rplaca cur nval)
  1172.                (rplaca next cval)))
  1173.         (setq cur next)
  1174.         (setq next (cdr cur))
  1175.         (cond ((not (null next)) (go chek))
  1176.               (sorted (return x))
  1177.               (t (go loop))))) 
  1178.  
  1179. (defun conflict-resolution nil
  1180.   (prog (best len)
  1181.         (setq len (length *conflict-set*))
  1182.         (cond ((> len *max-cs*) (setq *max-cs* len)))
  1183.         (setq *total-cs* (+ *total-cs* len))
  1184.         (cond (*conflict-set*
  1185.                (setq best (best-of *conflict-set*))
  1186.                (setq *conflict-set* (delq best *conflict-set*))
  1187.                (return (pname-instantiation best)))
  1188.               (t (return nil))))) 
  1189.  
  1190. (defun best-of (set) (best-of* (car set) (cdr set))) 
  1191.  
  1192. (defun best-of* (best rem)
  1193.   (cond ((not rem) best)
  1194.         ((conflict-set-compare best (car rem))
  1195.          (best-of* best (cdr rem)))
  1196.         (t (best-of* (car rem) (cdr rem))))) 
  1197.  
  1198. (defun remove-from-conflict-set (name)
  1199.   (prog (cs entry)
  1200.    l1   (setq cs *conflict-set*)
  1201.    l2   (cond ((atom cs) (return nil)))
  1202.         (setq entry (car cs))
  1203.         (setq cs (cdr cs))
  1204.         (cond ((eq name (caar entry))
  1205.                (setq *conflict-set* (delq entry *conflict-set*))
  1206.                (go l1))
  1207.               (t (go l2))))) 
  1208.  
  1209. (defun pname-instantiation (conflict-elem) (car conflict-elem)) 
  1210.  
  1211. (defun order-part (conflict-elem) (cdr conflict-elem)) 
  1212.  
  1213. (defun instantiation (conflict-elem)
  1214.   (cdr (pname-instantiation conflict-elem))) 
  1215.  
  1216.  
  1217. (defun conflict-set-compare (x y)
  1218.   (prog (x-order y-order xl yl xv yv)
  1219.         (setq x-order (order-part x))
  1220.         (setq y-order (order-part y))
  1221.         (setq xl (car x-order))
  1222.         (setq yl (car y-order))
  1223.    data (cond ((and (null xl) (null yl)) (go ps))
  1224.               ((null yl) (return t))
  1225.               ((null xl) (return nil)))
  1226.         (setq xv (car xl))
  1227.         (setq yv (car yl))
  1228.         (cond ((> xv yv) (return t))
  1229.               ((> yv xv) (return nil)))
  1230.         (setq xl (cdr xl))
  1231.         (setq yl (cdr yl))
  1232.         (go data)
  1233.    ps   (setq xl (cdr x-order))
  1234.         (setq yl (cdr y-order))
  1235.    psl  (cond ((null xl) (return t)))
  1236.         (setq xv (car xl))
  1237.         (setq yv (car yl))
  1238.         (cond ((> xv yv) (return t))
  1239.               ((> yv xv) (return nil)))
  1240.         (setq xl (cdr xl))
  1241.         (setq yl (cdr yl))
  1242.         (go psl))) 
  1243.  
  1244.  
  1245. (defun conflict-set nil
  1246.   (prog (cnts cs p z best)
  1247.         (setq cnts nil)
  1248.         (setq cs *conflict-set*)
  1249.    l1:  (and (atom cs) (go l2:))
  1250.         (setq p (caaar cs))
  1251.         (setq cs (cdr cs))
  1252.         (setq z (assq p cnts))
  1253.         (cond ((null z) (setq cnts (cons (cons p 1.) cnts)))
  1254.               (t (rplacd z (1+ (cdr z)))))
  1255.         (go l1:)
  1256.    l2:  (cond ((atom cnts)
  1257.                (setq best (best-of *conflict-set*))
  1258.                (terpri)
  1259.                (return (list (caar best) 'dominates))))
  1260.         (terpri)
  1261.         (princ (caar cnts))
  1262.         (cond ((> (cdar cnts) 1.)
  1263.                (princ '|    (|)
  1264.                (princ (cdar cnts))
  1265.                (princ '| occurrences)|)))
  1266.         (setq cnts (cdr cnts))
  1267.         (go l2:))) 
  1268.     
  1269.  
  1270.  
  1271. ;;; WM maintaining functions
  1272. ;
  1273. ; The order of operations in the following two functions is critical.
  1274. ; add-to-wm order: (1) change wm (2) record change (3) match 
  1275. ; remove-from-wm order: (1) record change (2) match (3) change wm
  1276. ; (back will not restore state properly unless wm changes are recorded
  1277. ; before the cs changes that they cause)  (match will give errors if 
  1278. ; the thing matched is not in wm at the time)
  1279.  
  1280.  
  1281. (defun add-to-wm (wme override)
  1282.   (prog (fa z part timetag port)
  1283.     (setq *critical* t)
  1284.     (setq *current-wm* (1+ *current-wm*))
  1285.     (and (> *current-wm* *max-wm*) (setq *max-wm* *current-wm*))
  1286.     (setq *action-count* (1+ *action-count*))
  1287.     (setq fa (wm-hash wme))
  1288.     (or (memq fa *wmpart-list*)
  1289.         (setq *wmpart-list* (cons fa *wmpart-list*)))
  1290.     (setq part (get fa 'wmpart*))
  1291.     (cond (override (setq timetag override))
  1292.           (t (setq timetag *action-count*)))
  1293.     (setq z (cons wme timetag))
  1294.     (putprop fa (cons z part) 'wmpart*)
  1295.     (record-change '=>wm *action-count* wme)
  1296.     (match 'new wme)
  1297.     (setq *critical* nil)
  1298.     (cond ((and *in-rhs* *wtrace*)
  1299.            (setq port (trace-file))
  1300.            (terpri port)
  1301.            (princ '|=>wm: | port)
  1302.            (ppelm wme port))))) 
  1303.  
  1304. ; remove-from-wm uses eq, not equal to determine if wme is present
  1305.  
  1306. (defun remove-from-wm (wme)
  1307.   (prog (fa z part timetag port)
  1308.     (setq fa (wm-hash wme))
  1309.     (setq part (get fa 'wmpart*))
  1310.     (setq z (assq wme part))
  1311.     (or z (return nil))
  1312.     (setq timetag (cdr z))
  1313.     (cond ((and *wtrace* *in-rhs*)
  1314.            (setq port (trace-file))
  1315.            (terpri port)
  1316.            (princ '|<=wm: | port)
  1317.            (ppelm wme port)))
  1318.     (setq *action-count* (1+ *action-count*))
  1319.     (setq *critical* t)
  1320.     (setq *current-wm* (1- *current-wm*))
  1321.     (record-change '<=wm timetag wme)
  1322.     (match nil wme)
  1323.     (putprop fa (delq z part) 'wmpart*)
  1324.     (setq *critical* nil))) 
  1325.  
  1326. ; mapwm maps down the elements of wm, applying fn to each element
  1327. ; each element is of form (datum . creation-time)
  1328.  
  1329. (defun mapwm (fn)
  1330.   (prog (wmpl part)
  1331.         (setq wmpl *wmpart-list*)
  1332.    lab1 (cond ((atom wmpl) (return nil)))
  1333.         (setq part (get (car wmpl) 'wmpart*))
  1334.         (setq wmpl (cdr wmpl))
  1335.         (mapc fn part)
  1336.         (go lab1))) 
  1337.  
  1338. (defun wm fexpr (a) 
  1339.   (mapc (function (lambda (z) (terpri) (ppelm z t))) 
  1340.     (get-wm a))
  1341.   nil) 
  1342.  
  1343. (defun get-wm (z)
  1344.   (setq *wm-filter* z)
  1345.   (setq *wm* nil)
  1346.   (mapwm (function get-wm2))
  1347.   (prog2 nil *wm* (setq *wm* nil))) 
  1348.  
  1349. (defun get-wm2 (elem) 
  1350.  (cond ((or (null *wm-filter*) (member (cdr elem) *wm-filter*))
  1351.     (setq *wm* (cons (car elem) *wm*)]
  1352.  
  1353. (defun wm-hash (x)
  1354.   (cond ((not x) '<default>)
  1355.         ((not (car x)) (wm-hash (cdr x)))
  1356.         ((symbolp (car x)) (car x))
  1357.         (t (wm-hash (cdr x))))) 
  1358.  
  1359. (defun creation-time (wme)
  1360.   (cdr (assq wme (get (wm-hash wme) 'wmpart*)))) 
  1361.  
  1362. (defun refresh nil
  1363.   (prog nil
  1364.     (setq *old-wm* nil)
  1365.     (mapwm (function refresh-collect))
  1366.     (mapc (function refresh-del) *old-wm*)
  1367.     (mapc (function refresh-add) *old-wm*)
  1368.     (setq *old-wm* nil))) 
  1369.  
  1370. (defun refresh-collect (x) (setq *old-wm* (cons x *old-wm*))) 
  1371.  
  1372. (defun refresh-del (x) (remove-from-wm (car x))) 
  1373.  
  1374. (defun refresh-add (x) (add-to-wm (car x) (cdr x))) 
  1375.  
  1376. (defun trace-file ()
  1377.   (prog (port)
  1378.         (setq port t)
  1379.     (cond (*trace-file*
  1380.            (setq port ($ofile *trace-file*))
  1381.            (cond ((null port)
  1382.                   (%warn '|trace: file has been closed| *trace-file*)
  1383.               (setq port t)))))
  1384.         (return port)))
  1385.  
  1386.  
  1387. ;;; Basic functions for RHS evaluation
  1388.  
  1389. (defun eval-rhs (pname data)
  1390.   (prog (node port)
  1391.     (cond (*ptrace*
  1392.            (setq port (trace-file))
  1393.            (terpri port)
  1394.            (princ *cycle-count* port)
  1395.            (princ '|. | port)
  1396.            (princ pname port)
  1397.            (time-tag-print data port)))
  1398.     (setq *data-matched* data)
  1399.     (setq *p-name* pname)
  1400.     (setq *last* nil)
  1401.     (setq node (get pname 'topnode))
  1402.     (init-var-mem (var-part node))
  1403.     (init-ce-var-mem (ce-var-part node))
  1404.     (begin-record pname data)
  1405.     (setq *in-rhs* t)
  1406.     (eval (rhs-part node))
  1407.     (setq *in-rhs* nil)
  1408.     (end-record))) 
  1409.  
  1410. (defun time-tag-print (data port)
  1411.   (cond ((not (null data))
  1412.          (time-tag-print (cdr data) port)
  1413.          (princ '| | port)
  1414.          (princ (creation-time (car data)) port))))
  1415.  
  1416. (defun init-var-mem (vlist)
  1417.   (prog (v ind r)
  1418.         (setq *variable-memory* nil)
  1419.    top  (and (atom vlist) (return nil))
  1420.         (setq v (car vlist))
  1421.         (setq ind (cadr vlist))
  1422.         (setq vlist (cddr vlist))
  1423.         (setq r (gelm *data-matched* ind))
  1424.         (setq *variable-memory* (cons (cons v r) *variable-memory*))
  1425.         (go top))) 
  1426.  
  1427. (defun init-ce-var-mem (vlist)
  1428.   (prog (v ind r)
  1429.         (setq *ce-variable-memory* nil)
  1430.    top  (and (atom vlist) (return nil))
  1431.         (setq v (car vlist))
  1432.         (setq ind (cadr vlist))
  1433.         (setq vlist (cddr vlist))
  1434.         (setq r (ce-gelm *data-matched* ind))
  1435.         (setq *ce-variable-memory*
  1436.               (cons (cons v r) *ce-variable-memory*))
  1437.         (go top))) 
  1438.  
  1439. (defun make-ce-var-bind (var elem)
  1440.   (setq *ce-variable-memory*
  1441.         (cons (cons var elem) *ce-variable-memory*))) 
  1442.  
  1443. (defun make-var-bind (var elem)
  1444.   (setq *variable-memory* (cons (cons var elem) *variable-memory*))) 
  1445.  
  1446. (defun $varbind (x)
  1447.   (prog (r)
  1448.     (and (not *in-rhs*) (return x))
  1449.         (setq r (assq x *variable-memory*))
  1450.         (cond (r (return (cdr r)))
  1451.               (t (return x))))) 
  1452.  
  1453. (defun get-ce-var-bind (x)
  1454.   (prog (r)
  1455.         (cond ((numberp x) (return (get-num-ce x))))
  1456.         (setq r (assq x *ce-variable-memory*))
  1457.         (cond (r (return (cdr r)))
  1458.               (t (return nil))))) 
  1459.  
  1460. (defun get-num-ce (x)
  1461.   (prog (r l d)
  1462.         (setq r *data-matched*)
  1463.         (setq l (length r))
  1464.         (setq d (- l x))
  1465.         (and (> 0. d) (return nil))
  1466.    la   (cond ((null r) (return nil))
  1467.               ((> 1. d) (return (car r))))
  1468.         (setq d (1- d))
  1469.         (setq r (cdr r))
  1470.         (go la))) 
  1471.  
  1472.  
  1473. (defun build-collect (z)
  1474.   (prog (r)
  1475.    la   (and (atom z) (return nil))
  1476.         (setq r (car z))
  1477.         (setq z (cdr z))
  1478.         (cond ((dtpr r)
  1479.                ($value '\()
  1480.                (build-collect r)
  1481.                ($value '\)))
  1482.               ((eq r '\\) ($change (car z)) (setq z (cdr z)))
  1483.               (t ($value r)))
  1484.         (go la))) 
  1485.  
  1486. (defun unflat (x) (setq *rest* x) (unflat*)) 
  1487.  
  1488. (defun unflat* nil
  1489.   (prog (c)
  1490.         (cond ((atom *rest*) (return nil)))
  1491.         (setq c (car *rest*))
  1492.         (setq *rest* (cdr *rest*))
  1493.         (cond ((eq c '\() (return (cons (unflat*) (unflat*))))
  1494.               ((eq c '\)) (return nil))
  1495.               (t (return (cons c (unflat*))))))) 
  1496.  
  1497.  
  1498. (defun $change (x)
  1499.   (prog nil
  1500.         (cond ((dtpr x) (eval-function x))
  1501.               (t ($value ($varbind x)))))) 
  1502.  
  1503. (defun eval-args (z)
  1504.   (prog (r)
  1505.         (rhs-tab 1.)
  1506.    la   (and (atom z) (return nil))
  1507.         (setq r (car z))
  1508.         (setq z (cdr z))
  1509.         (cond ((eq r '^)
  1510.                (rhs-tab (car z))
  1511.                (setq r (cadr z))
  1512.                (setq z (cddr z))))
  1513.         (cond ((eq r '//) ($value (car z)) (setq z (cdr z)))
  1514.               (t ($change r)))
  1515.         (go la))) 
  1516.  
  1517.  
  1518. (defun eval-function (form)
  1519.   (cond ((not *in-rhs*)
  1520.      (%warn '|functions cannot be used at top level| (car form)))
  1521.     (t (eval form))))
  1522.  
  1523.  
  1524. ;;; Functions to manipulate the result array
  1525.  
  1526.  
  1527. (defun $reset nil
  1528.   (setq *max-index* 0.)
  1529.   (setq *next-index* 1.)) 
  1530.  
  1531. ; rhs-tab implements the tab ('^') function in the rhs.  it has
  1532. ; four responsibilities:
  1533. ;    - to move the array pointers
  1534. ;    - to watch for tabbing off the left end of the array
  1535. ;      (ie, to watch for pointers less than 1)
  1536. ;    - to watch for tabbing off the right end of the array
  1537. ;    - to write nil in all the slots that are skipped
  1538. ; the last is necessary if the result array is not to be cleared
  1539. ; after each use; if rhs-tab did not do this, $reset
  1540. ; would be much slower.
  1541.  
  1542. (defun rhs-tab (z) ($tab ($varbind z)))
  1543.  
  1544. (defun $tab (z)
  1545.   (prog (edge next)
  1546.         (setq next ($litbind z))
  1547.         (and (floatp next) (setq next (fix next)))
  1548.         (cond ((or (not (numberp next)) 
  1549.            (> next *size-result-array*)
  1550.            (> 1. next))
  1551.                (%warn '|illegal index after ^| next)
  1552.                (return *next-index*)))
  1553.         (setq edge (- next 1.))
  1554.         (cond ((> *max-index* edge) (go ok)))
  1555.    clear (cond ((== *max-index* edge) (go ok)))
  1556.         (putvector *result-array* edge nil)
  1557.         (setq edge (1- edge))
  1558.         (go clear)
  1559.    ok   (setq *next-index* next)
  1560.         (return next))) 
  1561.  
  1562. (defun $value (v)
  1563.   (cond ((> *next-index* *size-result-array*)
  1564.          (%warn '|index too large| *next-index*))
  1565.         (t
  1566.          (and (> *next-index* *max-index*)
  1567.               (setq *max-index* *next-index*))
  1568.          (putvector *result-array* *next-index* v)
  1569.          (setq *next-index* (1+ *next-index*))))) 
  1570.  
  1571. (defun use-result-array nil
  1572.   (prog (k r)
  1573.         (setq k *max-index*)
  1574.         (setq r nil)
  1575.    top  (and (== k 0.) (return r))
  1576.         (setq r (cons (getvector *result-array* k) r))
  1577.         (setq k (1- k))
  1578.         (go top))) 
  1579.  
  1580. (defun $assert nil
  1581.   (setq *last* (use-result-array))
  1582.   (add-to-wm *last* nil))
  1583.  
  1584. (defun $parametercount nil *max-index*)
  1585.  
  1586. (defun $parameter (k)
  1587.   (cond ((or (not (numberp k)) (> k *size-result-array*) (< k 1.))
  1588.      (%warn '|illegal parameter number | k)
  1589.          nil)
  1590.         ((> k *max-index*) nil)
  1591.     (t (getvector *result-array* k))))
  1592.  
  1593.  
  1594. ;;; RHS actions
  1595.  
  1596. (defun make fexpr (z)
  1597.   (prog nil
  1598.         ($reset)
  1599.         (eval-args z)
  1600.         ($assert))) 
  1601.  
  1602. (defun modify fexpr (z)
  1603.   (prog (old)
  1604.     (cond ((not *in-rhs*)
  1605.            (%warn '|cannot be called at top level| 'modify)
  1606.            (return nil)))
  1607.         (setq old (get-ce-var-bind (car z)))
  1608.         (cond ((null old)
  1609.                (%warn '|modify: first argument must be an element variable|
  1610.                         (car z))
  1611.                (return nil)))
  1612.         (remove-from-wm old)
  1613.         (setq z (cdr z))
  1614.         ($reset)
  1615.    copy (and (atom old) (go fin))
  1616.         ($change (car old))
  1617.         (setq old (cdr old))
  1618.         (go copy)
  1619.    fin  (eval-args z)
  1620.         ($assert))) 
  1621.  
  1622. (defun bind fexpr (z)
  1623.   (prog (val)
  1624.     (cond ((not *in-rhs*)
  1625.            (%warn '|cannot be called at top level| 'bind)
  1626.            (return nil)))
  1627.     (cond ((< (length z) 1.)
  1628.            (%warn '|bind: wrong number of arguments to| z)
  1629.            (return nil))
  1630.           ((not (symbolp (car z)))
  1631.            (%warn '|bind: illegal argument| (car z))
  1632.            (return nil))
  1633.           ((= (length z) 1.) (setq val (gensym)))
  1634.           (t ($reset)
  1635.              (eval-args (cdr z))
  1636.              (setq val ($parameter 1.))))
  1637.     (make-var-bind (car z) val))) 
  1638.  
  1639. (defun cbind fexpr (z)
  1640.   (cond ((not *in-rhs*)
  1641.      (%warn '|cannot be called at top level| 'cbind))
  1642.     ((not (= (length z) 1.))
  1643.      (%warn '|cbind: wrong number of arguments| z))
  1644.     ((not (symbolp (car z)))
  1645.      (%warn '|cbind: illegal argument| (car z)))
  1646.     ((null *last*)
  1647.      (%warn '|cbind: nothing added yet| (car z)))
  1648.     (t (make-ce-var-bind (car z) *last*)))) 
  1649.  
  1650. (defun remove fexpr (z)
  1651.   (prog (old)
  1652.     (and (not *in-rhs*)(return (top-level-remove z)))
  1653.    top  (and (atom z) (return nil))
  1654.         (setq old (get-ce-var-bind (car z)))
  1655.         (cond ((null old)
  1656.                (%warn '|remove: argument not an element variable| (car z))
  1657.                (return nil)))
  1658.         (remove-from-wm old)
  1659.         (setq z (cdr z))
  1660.         (go top))) 
  1661.  
  1662. (defun call fexpr (z)
  1663.   (prog (f)
  1664.     (setq f (car z))
  1665.         ($reset)
  1666.         (eval-args (cdr z))
  1667.         (funcall f))) 
  1668.  
  1669. (defun write fexpr (z)
  1670.   (prog (port max k x needspace)
  1671.     (cond ((not *in-rhs*)
  1672.            (%warn '|cannot be called at top level| 'write)
  1673.            (return nil)))
  1674.     ($reset)
  1675.     (eval-args z)
  1676.     (setq k 1.)
  1677.     (setq max ($parametercount))
  1678.     (cond ((< max 1.)
  1679.            (%warn '|write: nothing to print| z)
  1680.            (return nil)))
  1681.     (setq port (default-write-file))
  1682.     (setq x ($parameter 1.))
  1683.     (cond ((and (symbolp x) ($ofile x)) 
  1684.            (setq port ($ofile x))
  1685.            (setq k 2.)))
  1686.         (setq needspace t)
  1687.    la   (and (> k max) (return nil))
  1688.     (setq x ($parameter k))
  1689.     (cond ((eq x '|=== C R L F ===|)
  1690.            (setq needspace nil)
  1691.                (terpri port))
  1692.               ((eq x '|=== R J U S T ===|)
  1693.            (setq k (+ 2 k))
  1694.            (do-rjust ($parameter (1- k)) ($parameter k) port))
  1695.           ((eq x '|=== T A B T O ===|)
  1696.            (setq needspace nil)
  1697.            (setq k (1+ k))
  1698.            (do-tabto ($parameter k) port))
  1699.           (t 
  1700.            (and needspace (princ '| | port))
  1701.            (setq needspace t)
  1702.            (princ x port)))
  1703.     (setq k (1+ k))
  1704.     (go la))) 
  1705.     
  1706. (defun default-write-file ()
  1707.   (prog (port)
  1708.     (setq port t)
  1709.     (cond (*write-file*
  1710.            (setq port ($ofile *write-file*))
  1711.            (cond ((null port) 
  1712.               (%warn '|write: file has been closed| *write-file*)
  1713.               (setq port t)))))
  1714.         (return port)))
  1715.  
  1716. (defun do-rjust (width value port)
  1717.   (prog (size)
  1718.     (cond ((eq value '|=== T A B T O ===|)
  1719.            (%warn '|rjust cannot precede this function| 'tabto)
  1720.                (return nil))
  1721.           ((eq value '|=== C R L F ===|)
  1722.            (%warn '|rjust cannot precede this function| 'crlf)
  1723.                (return nil))
  1724.           ((eq value '|=== R J U S T ===|)
  1725.            (%warn '|rjust cannot precede this function| 'rjust)
  1726.                (return nil)))
  1727.         (setq size (flatc value (1+ width)))
  1728.     (cond ((> size width)
  1729.            (princ '| | port)
  1730.            (princ value port)
  1731.            (return nil)))
  1732.         (do k (- width size) (1- k) (not (> k 0)) (princ '| | port))
  1733.     (princ value port)))
  1734.  
  1735. (defun do-tabto (col port)
  1736.   (prog (pos)
  1737.     (setq pos (1+ (nwritn port)))
  1738.     (cond ((> pos col)
  1739.            (terpri port)
  1740.            (setq pos 1)))
  1741.     (do k (- col pos) (1- k) (not (> k 0)) (princ '| | port))
  1742.     (return nil)))
  1743.  
  1744.  
  1745. (defun halt nil 
  1746.   (cond ((not *in-rhs*)
  1747.      (%warn '|cannot be called at top level| 'halt))
  1748.     (t (setq *halt-flag* t)))) 
  1749.  
  1750. (defun build fexpr (z)
  1751.   (prog (r)
  1752.     (cond ((not *in-rhs*)
  1753.            (%warn '|cannot be called at top level| 'build)
  1754.            (return nil)))
  1755.         ($reset)
  1756.         (build-collect z)
  1757.         (setq r (unflat (use-result-array)))
  1758.         (and *build-trace* (funcall *build-trace* r))
  1759.         (compile-production (car r) (cdr r)))) 
  1760.  
  1761. (defun openfile fexpr (z)
  1762.   (prog (file mode id)
  1763.     ($reset)
  1764.     (eval-args z)
  1765.     (cond ((not (equal ($parametercount) 3.))
  1766.            (%warn '|openfile: wrong number of arguments| z)
  1767.            (return nil)))
  1768.     (setq id ($parameter 1))
  1769.     (setq file ($parameter 2))
  1770.     (setq mode ($parameter 3))
  1771.     (cond ((not (symbolp id))
  1772.            (%warn '|openfile: file id must be a symbolic atom| id)
  1773.            (return nil))
  1774.               ((null id)
  1775.                (%warn '|openfile: 'nil' is reserved for the terminal| nil)
  1776.                (return nil))
  1777.           ((or ($ifile id)($ofile id))
  1778.            (%warn '|openfile: name already in use| id)
  1779.            (return nil)))
  1780.     (cond ((eq mode 'in) (putprop id (infile file) 'inputfile))
  1781.           ((eq mode 'out) (putprop id (outfile file) 'outputfile))
  1782.           (t (%warn '|openfile: illegal mode| mode)
  1783.          (return nil)))
  1784.     (return nil)))
  1785.  
  1786. (defun $ifile (x) 
  1787.   (cond ((symbolp x) (get x 'inputfile))
  1788.         (t nil)))
  1789.  
  1790. (defun $ofile (x) 
  1791.   (cond ((symbolp x) (get x 'outputfile))
  1792.         (t nil)))
  1793.  
  1794.  
  1795. (defun closefile fexpr (z)
  1796.   ($reset)
  1797.   (eval-args z)
  1798.   (mapc (function closefile2) (use-result-array)))
  1799.  
  1800. (defun closefile2 (file)
  1801.   (prog (port)
  1802.     (cond ((not (symbolp file))
  1803.            (%warn '|closefile: illegal file identifier| file))
  1804.           ((setq port ($ifile file))
  1805.            (close port)
  1806.            (remprop file 'inputfile))
  1807.           ((setq port ($ofile file))
  1808.            (close port)
  1809.            (remprop file 'outputfile)))
  1810.     (return nil)))
  1811.  
  1812. (defun default fexpr (z)
  1813.   (prog (file use)
  1814.     ($reset)
  1815.     (eval-args z)
  1816.     (cond ((not (equal ($parametercount) 2.))
  1817.            (%warn '|default: wrong number of arguments| z)
  1818.            (return nil)))
  1819.     (setq file ($parameter 1))
  1820.     (setq use ($parameter 2))
  1821.     (cond ((not (symbolp file))
  1822.            (%warn '|default: illegal file identifier| file)
  1823.            (return nil))
  1824.           ((not (memq use '(write accept trace)))
  1825.            (%warn '|default: illegal use for a file| use)
  1826.            (return nil))
  1827.           ((and (memq use '(write trace)) 
  1828.             (not (null file))
  1829.             (not ($ofile file)))
  1830.            (%warn '|default: file has not been opened for output| file)
  1831.            (return nil))
  1832.           ((and (eq use 'accept) 
  1833.             (not (null file))
  1834.             (not ($ifile file)))
  1835.            (%warn '|default: file has not been opened for input| file)
  1836.            (return nil))
  1837.           ((eq use 'write) (setq *write-file* file))
  1838.           ((eq use 'accept) (setq *accept-file* file))
  1839.           ((eq use 'trace) (setq *trace-file* file)))
  1840.     (return nil)))
  1841.  
  1842.  
  1843.  
  1844. ;;; RHS Functions
  1845.  
  1846. (defun accept fexpr (z)
  1847.   (prog (port arg)
  1848.     (cond ((> (length z) 1.)
  1849.            (%warn '|accept: wrong number of arguments| z)
  1850.            (return nil)))
  1851.     (setq port t)
  1852.     (cond (*accept-file*
  1853.            (setq port ($ifile *accept-file*))
  1854.            (cond ((null port) 
  1855.               (%warn '|accept: file has been closed| *accept-file*)
  1856.               (return nil)))))
  1857.     (cond ((= (length z) 1)
  1858.            (setq arg ($varbind (car z)))
  1859.            (cond ((not (symbolp arg))
  1860.                   (%warn '|accept: illegal file name| arg)
  1861.               (return nil)))
  1862.            (setq port ($ifile arg))
  1863.            (cond ((null port) 
  1864.               (%warn '|accept: file not open for input| arg)
  1865.               (return nil)))))
  1866.         (cond ((= (tyipeek port) -1.)
  1867.            ($value 'end-of-file)
  1868.            (return nil)))
  1869.     (flat-value (read port)))) 
  1870.  
  1871. (defun flat-value (x)
  1872.   (cond ((atom x) ($value x))
  1873.         (t (mapc (function flat-value) x)))) 
  1874.  
  1875. (defun span-chars (x prt)
  1876.   (do ch (tyipeek prt) (tyipeek prt) (not (member ch x)) (readc prt)))
  1877.  
  1878. (defun acceptline fexpr (z)
  1879.   (prog (c def arg port)
  1880.     (setq port t)
  1881.     (setq def z)
  1882.     (cond (*accept-file*
  1883.            (setq port ($ifile *accept-file*))
  1884.            (cond ((null port) 
  1885.               (%warn '|acceptline: file has been closed| 
  1886.                      *accept-file*)
  1887.               (return nil)))))
  1888.     (cond ((> (length def) 0)
  1889.            (setq arg ($varbind (car def)))
  1890.            (cond ((and (symbolp arg) ($ifile arg))
  1891.                   (setq port ($ifile arg))
  1892.               (setq def (cdr def))))))
  1893.         (span-chars '(9. 41.) port)
  1894.     (setq c (tyi port))
  1895.     (cond ((memq (tyipeek port) '(-1. 10.))
  1896.            (mapc (function $change) def)
  1897.            (return nil)))
  1898.    l:    (flat-value (read port))
  1899.         (span-chars '(9. 41.) port)
  1900.     (cond ((not (memq (tyipeek port) '(-1. 10.))) (go l:)))))
  1901.  
  1902. (defun substr fexpr (l)
  1903.   (prog (k elm start end)
  1904.         (cond ((not (= (length l) 3.))
  1905.                (%warn '|substr: wrong number of arguments| l)
  1906.                (return nil)))
  1907.         (setq elm (get-ce-var-bind (car l)))
  1908.         (cond ((null elm)
  1909.                (%warn '|first argument to substr must be a ce var|
  1910.                         l)
  1911.                (return nil)))
  1912.         (setq start ($varbind (cadr l)))
  1913.     (setq start ($litbind start))
  1914.         (cond ((not (numberp start))
  1915.                (%warn '|second argument to substr must be a number|
  1916.                         l)
  1917.                (return nil)))
  1918.     (comment |if a variable is bound to INF, the following|
  1919.          |will get the binding and treat it as INF is|
  1920.          |always treated.  that may not be good|)
  1921.         (setq end ($varbind (caddr l)))
  1922.         (cond ((eq end 'inf) (setq end (length elm))))
  1923.     (setq end ($litbind end))
  1924.         (cond ((not (numberp end))
  1925.                (%warn '|third argument to substr must be a number|
  1926.                         l)
  1927.                (return nil)))
  1928.         (comment |this loop does not check for the end of elm|
  1929.                  |instead it relies on cdr of nil being nil|
  1930.                  |this may not work in all versions of lisp|)
  1931.         (setq k 1.)
  1932.    la   (cond ((> k end) (return nil))
  1933.               ((not (< k start)) ($value (car elm))))
  1934.         (setq elm (cdr elm))
  1935.         (setq k (1+ k))
  1936.         (go la))) 
  1937.  
  1938.  
  1939. (defun compute fexpr (z) ($value (ari z))) 
  1940.  
  1941. ; arith is the obsolete form of compute
  1942. (defun arith fexpr (z) ($value (ari z))) 
  1943.  
  1944. (defun ari (x)
  1945.   (cond ((atom x)
  1946.          (%warn '|bad syntax in arithmetic expression | x)
  1947.      0.)
  1948.         ((atom (cdr x)) (ari-unit (car x)))
  1949.         ((eq (cadr x) '+)
  1950.          (plus (ari-unit (car x)) (ari (cddr x))))
  1951.         ((eq (cadr x) '-)
  1952.          (difference (ari-unit (car x)) (ari (cddr x))))
  1953.         ((eq (cadr x) '*)
  1954.          (times (ari-unit (car x)) (ari (cddr x))))
  1955.         ((eq (cadr x) '//)
  1956.          (quotient (ari-unit (car x)) (ari (cddr x))))
  1957.         ((eq (cadr x) '\\)
  1958.          (mod (fix (ari-unit (car x))) (fix (ari (cddr x)))))
  1959.         (t (%warn '|bad syntax in arithmetic expression | x) 0.))) 
  1960.  
  1961. (defun ari-unit (a)
  1962.   (prog (r)
  1963.         (cond ((dtpr a) (setq r (ari a)))
  1964.               (t (setq r ($varbind a))))
  1965.         (cond ((not (numberp r))
  1966.                (%warn '|bad value in arithmetic expression| a)
  1967.                (return 0.))
  1968.               (t (return r))))) 
  1969.  
  1970. (defun genatom nil ($value (gensym))) 
  1971.  
  1972. (defun litval fexpr (z)
  1973.   (prog (r)
  1974.     (cond ((not (= (length z) 1.))
  1975.            (%warn '|litval: wrong number of arguments| z)
  1976.            ($value 0) 
  1977.            (return nil))
  1978.           ((numberp (car z)) ($value (car z)) (return nil)))
  1979.     (setq r ($litbind ($varbind (car z))))
  1980.     (cond ((numberp r) ($value r) (return nil)))
  1981.     (%warn '|litval: argument has no literal binding| (car z))
  1982.     ($value 0)))
  1983.  
  1984.  
  1985. (defun rjust fexpr (z)
  1986.   (prog (val)
  1987.         (cond ((not (= (length z) 1.))
  1988.            (%warn '|rjust: wrong number of arguments| z)
  1989.                (return nil)))
  1990.         (setq val ($varbind (car z)))
  1991.     (cond ((or (not (numberp val)) (< val 1.) (> val 127.))
  1992.            (%warn '|rjust: illegal value for field width| val)
  1993.            (return nil)))
  1994.         ($value '|=== R J U S T ===|)
  1995.     ($value val)))
  1996.  
  1997. (defun crlf fexpr (z)
  1998.         (cond  (z (%warn '|crlf: does not take arguments| z))
  1999.            (t ($value '|=== C R L F ===|))))
  2000.  
  2001. (defun tabto fexpr (z)
  2002.   (prog (val)
  2003.         (cond ((not (= (length z) 1.))
  2004.            (%warn '|tabto: wrong number of arguments| z)
  2005.            (return nil)))
  2006.         (setq val ($varbind (car z)))
  2007.     (cond ((or (not (numberp val)) (< val 1.) (> val 127.))
  2008.            (%warn '|tabto: illegal column number| z)
  2009.            (return nil)))
  2010.         ($value '|=== T A B T O ===|)
  2011.     ($value val)))
  2012.  
  2013.  
  2014.  
  2015. ;;; Printing WM
  2016.  
  2017. (defun ppwm fexpr (avlist)
  2018.   (prog (next a)
  2019.         (setq *filters* nil)
  2020.         (setq next 1.)
  2021.    l:   (and (atom avlist) (go print))
  2022.         (setq a (car avlist))
  2023.         (setq avlist (cdr avlist))
  2024.         (cond ((eq a '^)
  2025.                (setq next (car avlist))
  2026.                (setq avlist (cdr avlist))
  2027.                (setq next ($litbind next))
  2028.                (and (floatp next) (setq next (fix next)))
  2029.                (cond ((or (not (numberp next))
  2030.                           (> next *size-result-array*)
  2031.                           (> 1. next))
  2032.                       (%warn '|illegal index after ^| next)
  2033.                       (return nil))))
  2034.               ((variablep a)
  2035.                (%warn '|ppwm does not take variables| a)
  2036.                (return nil))
  2037.               (t (setq *filters* (cons next (cons a *filters*)))
  2038.                  (setq next (1+ next))))
  2039.         (go l:)
  2040.    print (mapwm (function ppwm2))
  2041.         (terpri)
  2042.         (return nil))) 
  2043.  
  2044. (defun ppwm2 (elm-tag)
  2045.   (cond ((filter (car elm-tag)) (terpri) (ppelm (car elm-tag) t)))) 
  2046.  
  2047. (defun filter (elm)
  2048.   (prog (fl indx val)
  2049.         (setq fl *filters*)
  2050.    top  (and (atom fl) (return t))
  2051.         (setq indx (car fl))
  2052.         (setq val (cadr fl))
  2053.         (setq fl (cddr fl))
  2054.         (and (ident (nth (1- indx) elm) val) (go top))
  2055.         (return nil))) 
  2056.  
  2057. (defun ident (x y)
  2058.   (cond ((eq x y) t)
  2059.         ((not (numberp x)) nil)
  2060.         ((not (numberp y)) nil)
  2061.         ((=alg x y) t)
  2062.         (t nil))) 
  2063.  
  2064. ; the new ppelm is designed especially to handle literalize format
  2065. ; however, it will do as well as the old ppelm on other formats
  2066.  
  2067. (defun ppelm (elm port)
  2068.   (prog (ppdat sep val att mode lastpos)
  2069.     (princ (creation-time elm) port)
  2070.     (princ '|:  | port)
  2071.         (setq mode 'vector)
  2072.     (setq ppdat (get (car elm) 'ppdat))
  2073.     (and ppdat (setq mode 'a-v))
  2074.     (setq sep '|(|)
  2075.         (setq lastpos 0)
  2076.     (do
  2077.      ((curpos 1 (1+ curpos)) (vlist elm (cdr vlist)))
  2078.      ((atom vlist) nil)
  2079.      (setq val (car vlist))
  2080.      (setq att (assoc curpos ppdat))
  2081.      (cond (att (setq att (cdr att)))
  2082.            (t (setq att curpos)))
  2083.          (and (symbolp att) (is-vector-attribute att) (setq mode 'vector))
  2084.      (cond ((or (not (null val)) (eq mode 'vector))
  2085.         (princ sep port)
  2086.         (ppval val att lastpos port)
  2087.         (setq sep '|    |)
  2088.         (setq lastpos curpos))))
  2089.     (princ '|)| port)))
  2090.  
  2091. (defun ppval (val att lastpos port)
  2092.   (cond ((not (equal att (1+ lastpos)))
  2093.          (princ '^ port)
  2094.          (princ att port)
  2095.          (princ '| | port)))
  2096.   (princ val port))
  2097.  
  2098.  
  2099.  
  2100. ;;; printing production memory
  2101.  
  2102. (defun pm fexpr (z) (mapc (function pprule) z) (terpri) nil)
  2103.  
  2104. (defun pprule (name)
  2105.   (prog (matrix next lab)
  2106.         (and (not (symbolp name)) (return nil))
  2107.         (setq matrix (get name 'production))
  2108.     (and (null matrix) (return nil))
  2109.     (terpri)
  2110.     (princ '|(p |)
  2111.     (princ name)
  2112.    top    (and (atom matrix) (go fin))
  2113.         (setq next (car matrix))
  2114.     (setq matrix (cdr matrix))
  2115.     (setq lab nil)
  2116.     (terpri)
  2117.     (cond ((eq next '-)
  2118.            (princ '|  - |)
  2119.            (setq next (car matrix))
  2120.            (setq matrix (cdr matrix)))
  2121.           ((eq next '-->)
  2122.            (princ '|  |))
  2123.           ((and (eq next '{) (atom (car matrix)))
  2124.            (princ '|   {|)
  2125.            (setq lab (car matrix))
  2126.            (setq next (cadr matrix))
  2127.            (setq matrix (cdddr matrix)))
  2128.           ((eq next '{)
  2129.            (princ '|   {|)
  2130.            (setq lab (cadr matrix))
  2131.            (setq next (car matrix))
  2132.            (setq matrix (cdddr matrix)))
  2133.           (t (princ '|    |)))
  2134.         (ppline next)
  2135.     (cond (lab (princ '| |) (princ lab) (princ '})))
  2136.     (go top)
  2137.     fin    (princ '|)|)))
  2138.  
  2139. (defun ppline (line)
  2140.   (prog ()
  2141.     (cond ((atom line) (princ line))
  2142.           (t
  2143.            (princ '|(|)
  2144.            (setq *ppline* line)
  2145.            (ppline2)
  2146.            (princ '|)|)))
  2147.         (return nil)))
  2148.  
  2149. (defun ppline2 ()
  2150.   (prog (needspace)
  2151.         (setq needspace nil)
  2152.    top  (and (atom *ppline*) (return nil))
  2153.         (and needspace (princ '| |))
  2154.         (cond ((eq (car *ppline*) '^) (ppattval))
  2155.           (t (pponlyval)))
  2156.         (setq needspace t)
  2157.         (go top)))
  2158.  
  2159. (defun ppattval ()
  2160.   (prog (att val)
  2161.         (setq att (cadr *ppline*))
  2162.     (setq *ppline* (cddr *ppline*))
  2163.     (setq val (getval))
  2164.     (cond ((> (+ (nwritn) (flatc att) (flatc val)) 76.)
  2165.            (terpri)
  2166.            (princ '|        |)))
  2167.         (princ '^)
  2168.     (princ att)
  2169.     (mapc (function (lambda (z) (princ '| |) (princ z))) val)))
  2170.  
  2171. (defun pponlyval ()
  2172.   (prog (val needspace)
  2173.     (setq val (getval))
  2174.     (setq needspace nil)
  2175.     (cond ((> (+ (nwritn) (flatc val)) 76.)
  2176.            (setq needspace nil)
  2177.            (terpri)
  2178.            (princ '|        |)))
  2179.     top    (and (atom val) (return nil))
  2180.         (and needspace (princ '| |))
  2181.     (setq needspace t)
  2182.     (princ (car val))
  2183.     (setq val (cdr val))
  2184.     (go top)))
  2185.  
  2186. (defun getval ()
  2187.   (prog (res v1)
  2188.         (setq v1 (car *ppline*))
  2189.     (setq *ppline* (cdr *ppline*))
  2190.     (cond ((memq v1 '(= <> < <= => > <=>))
  2191.            (setq res (cons v1 (getval))))
  2192.           ((eq v1 '{)
  2193.            (setq res (cons v1 (getupto '}))))
  2194.           ((eq v1 '<<)
  2195.            (setq res (cons v1 (getupto '>>))))
  2196.           ((eq v1 '//)
  2197.            (setq res (list v1 (car *ppline*)))
  2198.            (setq *ppline* (cdr *ppline*)))
  2199.           (t (setq res (list v1))))
  2200.         (return res)))
  2201.  
  2202. (defun getupto (end)
  2203.   (prog (v)
  2204.         (and (atom *ppline*) (return nil))
  2205.     (setq v (car *ppline*))
  2206.     (setq *ppline* (cdr *ppline*))
  2207.     (cond ((eq v end) (return (list v)))
  2208.           (t (return (cons v (getupto end))))))) 
  2209.  
  2210.  
  2211.  
  2212.  
  2213.  
  2214.  
  2215. ;;; backing up
  2216.  
  2217.  
  2218.  
  2219. (defun record-index-plus (k)
  2220.   (setq *record-index* (+ k *record-index*))
  2221.   (cond ((< *record-index* 0.)
  2222.          (setq *record-index* *max-record-index*))
  2223.         ((> *record-index* *max-record-index*)
  2224.          (setq *record-index* 0.)))) 
  2225.  
  2226. ; the following routine initializes the record.  putting nil in the
  2227. ; first slot indicates that that the record does not go back further
  2228. ; than that.  (when the system backs up, it writes nil over the used
  2229. ; records so that it will recognize which records it has used.  thus
  2230. ; the system is set up anyway never to back over a nil.)
  2231.  
  2232. (defun initialize-record nil
  2233.   (setq *record-index* 0.)
  2234.   (setq *recording* nil)
  2235.   (setq *max-record-index* 31.)
  2236.   (putvector *record-array* 0. nil)) 
  2237.  
  2238. ; *max-record-index* holds the maximum legal index for record-array
  2239. ; so it and the following must be changed at the same time
  2240.  
  2241. (defun begin-record (p data)
  2242.   (setq *recording* t)
  2243.   (setq *record* (list '=>refract p data))) 
  2244.  
  2245. (defun end-record nil
  2246.   (cond (*recording*
  2247.          (setq *record*
  2248.                (cons *cycle-count* (cons *p-name* *record*)))
  2249.          (record-index-plus 1.)
  2250.          (putvector *record-array* *record-index* *record*)
  2251.          (setq *record* nil)
  2252.          (setq *recording* nil)))) 
  2253.  
  2254. (defun record-change (direct time elm)
  2255.   (cond (*recording*
  2256.          (setq *record*
  2257.                (cons direct (cons time (cons elm *record*))))))) 
  2258.  
  2259. ; to maintain refraction information, need keep only one piece of information:
  2260. ; need to record all unsuccessful attempts to delete things from the conflict
  2261. ; set.  unsuccessful deletes are caused by attempting to delete refracted
  2262. ; instantiations.  when backing up, have to avoid putting things back into the
  2263. ; conflict set if they were not deleted when running forward
  2264.  
  2265. (defun record-refract (rule data)
  2266.   (and *recording*
  2267.        (setq *record* (cons '<=refract (cons rule (cons data *record*)]
  2268.  
  2269. (defun refracted (rule data)
  2270.   (prog (z)
  2271.         (and (null *refracts*) (return nil))
  2272.     (setq z (cons rule data))
  2273.     (return (member z *refracts*))))
  2274.  
  2275. (defun back (k)
  2276.   (prog (r)
  2277.    l:   (and (< k 1.) (return nil))
  2278.         (setq r (getvector *record-array* *record-index*))
  2279.         (and (null r) (return '|nothing more stored|))
  2280.         (putvector *record-array* *record-index* nil)
  2281.         (record-index-plus -1.)
  2282.         (undo-record r)
  2283.         (setq k (1- k))
  2284.         (go l:))) 
  2285.  
  2286. (defun undo-record (r)
  2287.   (prog (save act a b rate)
  2288.         (comment *recording* must be off during back up)
  2289.         (setq save *recording*)
  2290.         (setq *refracts* nil)
  2291.         (setq *recording* nil)
  2292.         (and *ptrace* (back-print (list 'undo: (car r) (cadr r))))
  2293.         (setq r (cddr r))
  2294.    top  (and (atom r) (go fin))
  2295.         (setq act (car r))
  2296.         (setq a (cadr r))
  2297.         (setq b (caddr r))
  2298.         (setq r (cdddr r))
  2299.         (and *wtrace* (back-print (list 'undo: act a)))
  2300.         (cond ((eq act '<=wm) (add-to-wm b a))
  2301.               ((eq act '=>wm) (remove-from-wm b))
  2302.               ((eq act '<=refract)
  2303.                (setq *refracts* (cons (cons a b) *refracts*)))
  2304.               ((and (eq act '=>refract) (still-present b))
  2305.            (setq *refracts* (delete (cons a b) *refracts*))
  2306.                (setq rate (rating-part (get a 'topnode)))
  2307.                (removecs a b)
  2308.                (insertcs a b rate))
  2309.               (t (%warn '|back: cannot undo action| (list act a))))
  2310.         (go top)
  2311.    fin  (setq *recording* save)
  2312.         (setq *refracts* nil)
  2313.         (return nil))) 
  2314.  
  2315. ; still-present makes sure that the user has not deleted something
  2316. ; from wm which occurs in the instantiation about to be restored; it
  2317. ; makes the check by determining whether each wme still has a time tag.
  2318.  
  2319. (defun still-present (data)
  2320.   (prog nil
  2321.    l:   (cond ((atom data) (return t))
  2322.               ((creation-time (car data))
  2323.                (setq data (cdr data))
  2324.                (go l:))
  2325.               (t (return nil))))) 
  2326.  
  2327.  
  2328. (defun back-print (x) 
  2329.   (prog (port)
  2330.         (setq port (trace-file))
  2331.         (terpri port)
  2332.     (print x port)))
  2333.  
  2334.  
  2335.  
  2336.  
  2337. ;;; Functions to show how close rules are to firing
  2338.  
  2339. (defun matches fexpr (rule-list)
  2340.   (mapc (function matches2) rule-list)
  2341.   (terpri)) 
  2342.  
  2343. (defun matches2 (p)
  2344.   (cond ((atom p)
  2345.          (terpri)
  2346.          (terpri)
  2347.          (princ p)
  2348.          (matches3 (get p 'backpointers) 2. (ncons 1.))))) 
  2349.  
  2350. (defun matches3 (nodes ce part)
  2351.   (cond ((not (null nodes))
  2352.          (terpri)
  2353.          (princ '| ** matches for |)
  2354.          (princ part)
  2355.          (princ '| ** |)
  2356.          (mapc (function write-elms) (find-left-mem (car nodes)))
  2357.          (terpri)
  2358.          (princ '| ** matches for |)
  2359.          (princ (ncons ce))
  2360.          (princ '| ** |)
  2361.          (mapc (function write-elms) (find-right-mem (car nodes)))
  2362.          (matches3 (cdr nodes) (1+ ce) (cons ce part))))) 
  2363.  
  2364. (defun write-elms (wme-or-count)
  2365.   (cond ((dtpr wme-or-count)
  2366.      (terpri)
  2367.      (mapc (function write-elms2) wme-or-count)))) 
  2368.  
  2369. (defun write-elms2 (x)
  2370.   (princ '|  |)
  2371.   (princ (creation-time x)))
  2372.  
  2373. (defun find-left-mem (node)
  2374.   (cond ((eq (car node) '&and) (memory-part (caddr node)))
  2375.         (t (car (caddr node))))) 
  2376.  
  2377. (defun find-right-mem (node) (memory-part (cadddr node))) 
  2378.  
  2379.  
  2380. ;;; Check the RHSs of productions 
  2381.  
  2382.  
  2383. (defun check-rhs (rhs) (mapc (function check-action) rhs))
  2384.  
  2385. (defun check-action (x)
  2386.   (prog (a)
  2387.     (cond ((atom x)
  2388.            (%warn '|atomic action| x)
  2389.        (return nil)))
  2390.     (setq a (setq *action-type* (car x)))
  2391.     (cond ((eq a 'bind) (check-bind x))
  2392.           ((eq a 'cbind) (check-cbind x))
  2393.           ((eq a 'make) (check-make x))
  2394.           ((eq a 'modify) (check-modify x))
  2395.           ((eq a 'remove) (check-remove x))
  2396.           ((eq a 'write) (check-write x))
  2397.           ((eq a 'call) (check-call x))
  2398.           ((eq a 'halt) (check-halt x))
  2399.           ((eq a 'openfile) (check-openfile x))
  2400.           ((eq a 'closefile) (check-closefile x))
  2401.           ((eq a 'default) (check-default x))
  2402.           ((eq a 'build) (check-build x))
  2403.           (t (%warn '|undefined rhs action| a))))) 
  2404.  
  2405. (defun check-build (z)
  2406.   (and (null (cdr z)) (%warn '|needs arguments| z))
  2407.   (check-build-collect (cdr z)))
  2408.  
  2409. (defun check-build-collect (args)
  2410.   (prog (r)
  2411.     top    (and (null args) (return nil))
  2412.     (setq r (car args))
  2413.     (setq args (cdr args))
  2414.     (cond ((dtpr r) (check-build-collect r))
  2415.           ((eq r '\\)
  2416.            (and (null args) (%warn '|nothing to evaluate| r))
  2417.            (check-rhs-value (car args))
  2418.            (setq args (cdr args))))
  2419.     (go top)))
  2420.  
  2421. (defun check-remove (z) 
  2422.   (and (null (cdr z)) (%warn '|needs arguments| z))
  2423.   (mapc (function check-rhs-ce-var) (cdr z))) 
  2424.  
  2425. (defun check-make (z)
  2426.   (and (null (cdr z)) (%warn '|needs arguments| z))
  2427.   (check-change& (cdr z))) 
  2428.  
  2429. (defun check-openfile (z)
  2430.   (and (null (cdr z)) (%warn '|needs arguments| z))
  2431.   (check-change& (cdr z))) 
  2432.  
  2433. (defun check-closefile (z)
  2434.   (and (null (cdr z)) (%warn '|needs arguments| z))
  2435.   (check-change& (cdr z))) 
  2436.  
  2437. (defun check-default (z)
  2438.   (and (null (cdr z)) (%warn '|needs arguments| z))
  2439.   (check-change& (cdr z))) 
  2440.  
  2441. (defun check-modify (z)
  2442.   (and (null (cdr z)) (%warn '|needs arguments| z))
  2443.   (check-rhs-ce-var (cadr z))
  2444.   (and (null (cddr z)) (%warn '|no changes to make| z))
  2445.   (check-change& (cddr z))) 
  2446.  
  2447. (defun check-write (z)
  2448.   (and (null (cdr z)) (%warn '|needs arguments| z))
  2449.   (check-change& (cdr z))) 
  2450.  
  2451. (defun check-call (z)
  2452.   (prog (f)
  2453.     (and (null (cdr z)) (%warn '|needs arguments| z))
  2454.     (setq f (cadr z))
  2455.     (and (variablep f)
  2456.          (%warn '|function name must be a constant| z))
  2457.     (or (symbolp f)
  2458.         (%warn '|function name must be a symbolic atom| f))
  2459.     (or (externalp f)
  2460.         (%warn '|function name not declared external| f))
  2461.     (check-change& (cddr z)))) 
  2462.  
  2463. (defun check-halt (z)
  2464.   (or (null (cdr z)) (%warn '|does not take arguments| z))) 
  2465.  
  2466. (defun check-cbind (z)
  2467.   (prog (v)
  2468.     (or (= (length z) 2.) (%warn '|takes only one argument| z))
  2469.     (setq v (cadr z))
  2470.     (or (variablep v) (%warn '|takes variable as argument| z))
  2471.     (note-ce-variable v))) 
  2472.  
  2473. (defun check-bind (z)
  2474.   (prog (v)
  2475.     (or (> (length z) 1.) (%warn '|needs arguments| z))
  2476.     (setq v (cadr z))
  2477.     (or (variablep v) (%warn '|takes variable as argument| z))
  2478.     (note-variable v)
  2479.     (check-change& (cddr z)))) 
  2480.  
  2481.  
  2482. (defun check-change& (z)
  2483.   (prog (r tab-flag)
  2484.         (setq tab-flag nil)
  2485.    la   (and (atom z) (return nil))
  2486.         (setq r (car z))
  2487.         (setq z (cdr z))
  2488.         (cond ((eq r '^)
  2489.                (and tab-flag
  2490.                     (%warn '|no value before this tab| (car z)))
  2491.                (setq tab-flag t)
  2492.                (check-tab-index (car z))
  2493.                (setq z (cdr z)))
  2494.               ((eq r '//) (setq tab-flag nil) (setq z (cdr z)))
  2495.               (t (setq tab-flag nil) (check-rhs-value r)))
  2496.         (go la))) 
  2497.  
  2498. (defun check-rhs-ce-var (v)
  2499.   (cond ((and (not (numberp v)) (not (ce-bound? v)))
  2500.          (%warn '|unbound element variable| v))
  2501.         ((and (numberp v) (or (< v 1.) (> v *ce-count*)))
  2502.          (%warn '|numeric element designator out of bounds| v)))) 
  2503.  
  2504. (defun check-rhs-value (x)
  2505.   (cond ((dtpr x) (check-rhs-function x))
  2506.         (t (check-rhs-atomic x)))) 
  2507.  
  2508. (defun check-rhs-atomic (x)
  2509.   (and (variablep x) 
  2510.        (not (bound? x)) 
  2511.        (%warn '|unbound variable| x)))
  2512.  
  2513. (defun check-rhs-function (x)
  2514.   (prog (a)
  2515.     (setq a (car x))
  2516.     (cond ((eq a 'compute) (check-compute x))
  2517.           ((eq a 'arith) (check-compute x))
  2518.           ((eq a 'substr) (check-substr x))
  2519.           ((eq a 'accept) (check-accept x))
  2520.           ((eq a 'acceptline) (check-acceptline x))
  2521.           ((eq a 'crlf) (check-crlf x))
  2522.           ((eq a 'genatom) (check-genatom x))
  2523.       ((eq a 'litval) (check-litval x))
  2524.           ((eq a 'tabto) (check-tabto x))
  2525.       ((eq a 'rjust) (check-rjust x))
  2526.       ((not (externalp a))
  2527.        (%warn '"rhs function not declared external" a)))))
  2528.  
  2529. (defun check-litval (x) 
  2530.   (or (= (length x) 2) (%warn '|wrong number of arguments| x))
  2531.   (check-rhs-atomic (cadr x)))
  2532.  
  2533. (defun check-accept (x)
  2534.   (cond ((= (length x) 1) nil)
  2535.         ((= (length x) 2) (check-rhs-atomic (cadr x)))
  2536.     (t (%warn '|too many arguments| x))))
  2537.  
  2538. (defun check-acceptline (x)
  2539.   (mapc (function check-rhs-atomic) (cdr x)))
  2540.  
  2541. (defun check-crlf (x) 
  2542.   (check-0-args x)) 
  2543.  
  2544. (defun check-genatom (x) (check-0-args x)) 
  2545.  
  2546. (defun check-tabto (x)
  2547.   (or (= (length x) 2) (%warn '|wrong number of arguments| x))
  2548.   (check-print-control (cadr x)))
  2549.  
  2550. (defun check-rjust (x)
  2551.   (or (= (length x) 2) (%warn '|wrong number of arguments| x))
  2552.   (check-print-control (cadr x)))
  2553.  
  2554. (defun check-0-args (x)
  2555.   (or (= (length x) 1.) (%warn '|should not have arguments| x))) 
  2556.  
  2557. (defun check-substr (x)
  2558.   (or (= (length x) 4.) (%warn '|wrong number of arguments| x))
  2559.   (check-rhs-ce-var (cadr x))
  2560.   (check-substr-index (caddr x))
  2561.   (check-last-substr-index (cadddr x))) 
  2562.  
  2563. (defun check-compute (x) (check-arithmetic (cdr x))) 
  2564.  
  2565. (defun check-arithmetic (l)
  2566.   (cond ((atom l)
  2567.          (%warn '|syntax error in arithmetic expression| l))
  2568.         ((atom (cdr l)) (check-term (car l)))
  2569.         ((not (memq (cadr l) '(+ - * // \\)))
  2570.          (%warn '|unknown operator| l))
  2571.         (t (check-term (car l)) (check-arithmetic (cddr l))))) 
  2572.  
  2573. (defun check-term (x)
  2574.   (cond ((dtpr x) (check-arithmetic x))
  2575.         (t (check-rhs-atomic x)))) 
  2576.  
  2577. (defun check-last-substr-index (x)
  2578.   (or (eq x 'inf) (check-substr-index x))) 
  2579.  
  2580. (defun check-substr-index (x)
  2581.   (prog (v)
  2582.     (cond ((bound? x) (return x)))
  2583.     (setq v ($litbind x))
  2584.     (cond ((not (numberp v))
  2585.            (%warn '|unbound symbol used as index in substr| x))
  2586.           ((or (< v 1.) (> v 127.))
  2587.            (%warn '|index out of bounds in tab| x))))) 
  2588.  
  2589. (defun check-print-control (x)
  2590.   (prog ()
  2591.     (cond ((bound? x) (return x)))
  2592.     (cond ((or (not (numberp x)) (< x 1.) (> x 127.))
  2593.            (%warn '|illegal value for printer control| x))))) 
  2594.  
  2595. (defun check-tab-index (x)
  2596.   (prog (v)
  2597.     (cond ((bound? x) (return x)))
  2598.     (setq v ($litbind x))
  2599.     (cond ((not (numberp v))
  2600.            (%warn '|unbound symbol occurs after ^| x))
  2601.           ((or (< v 1.) (> v 127.))
  2602.            (%warn '|index out of bounds after ^| x))))) 
  2603.  
  2604. (defun note-variable (var)
  2605.   (setq *rhs-bound-vars* (cons var *rhs-bound-vars*)))
  2606.  
  2607. (defun bound? (var)
  2608.   (or (memq var *rhs-bound-vars*)
  2609.       (var-dope var)))
  2610.  
  2611. (defun note-ce-variable (ce-var)
  2612.   (setq *rhs-bound-ce-vars* (cons ce-var *rhs-bound-ce-vars*)))
  2613.  
  2614. (defun ce-bound? (ce-var)
  2615.   (or (memq ce-var *rhs-bound-ce-vars*)
  2616.       (ce-var-dope ce-var)))
  2617.  
  2618. ;;; Top level routines
  2619.  
  2620. (defun process-changes (adds dels)
  2621.   (prog (x)
  2622.    process-deletes (and (atom dels) (go process-adds))
  2623.         (setq x (car dels))
  2624.         (setq dels (cdr dels))
  2625.         (remove-from-wm x)
  2626.         (go process-deletes)
  2627.    process-adds (and (atom adds) (return nil))
  2628.         (setq x (car adds))
  2629.         (setq adds (cdr adds))
  2630.         (add-to-wm x nil)
  2631.         (go process-adds))) 
  2632.  
  2633. (defun main nil
  2634.   (prog (instance r)
  2635.         (setq *halt-flag* nil)
  2636.         (setq *break-flag* nil)
  2637.         (setq instance nil)
  2638.    dil  (setq *phase* 'conflict-resolution)
  2639.         (cond (*halt-flag*
  2640.                (setq r '|end -- explicit halt|)
  2641.                (go finis))
  2642.           ((zerop *remaining-cycles*)
  2643.            (setq r '***break***)
  2644.            (setq *break-flag* t)
  2645.            (go finis))
  2646.               (*break-flag* (setq r '***break***) (go finis)))
  2647.     (setq *remaining-cycles* (1- *remaining-cycles*))
  2648.         (setq instance (conflict-resolution))
  2649.         (cond ((not instance)
  2650.                (setq r '|end -- no production true|)
  2651.                (go finis)))
  2652.         (setq *phase* (car instance))
  2653.         (accum-stats)
  2654.         (eval-rhs (car instance) (cdr instance))
  2655.         (check-limits)
  2656.     (and (broken (car instance)) (setq *break-flag* t))
  2657.         (go dil)
  2658.   finis (setq *p-name* nil)
  2659.         (return r))) 
  2660.  
  2661. (defun do-continue (wmi)
  2662.     (cond (*critical*
  2663.            (terpri)
  2664.            (princ '|warning: network may be inconsistent|)))
  2665.     (process-changes wmi nil)
  2666.     (print-times (main))) 
  2667.  
  2668. (defun accum-stats nil
  2669.   (setq *cycle-count* (1+ *cycle-count*))
  2670.   (setq *total-token* (+ *total-token* *current-token*))
  2671.   (cond ((> *current-token* *max-token*)
  2672.          (setq *max-token* *current-token*)))
  2673.   (setq *total-wm* (+ *total-wm* *current-wm*))
  2674.   (cond ((> *current-wm* *max-wm*) (setq *max-wm* *current-wm*)))) 
  2675.  
  2676.  
  2677. (defun print-times (mess)
  2678.   (prog (cc ac)
  2679.         (cond (*break-flag* (terpri) (return mess)))
  2680.         (setq cc (plus (float *cycle-count*) 1.0e-20))
  2681.         (setq ac (plus (float *action-count*) 1.0e-20))
  2682.         (terpri)
  2683.         (princ mess)
  2684.         (pm-size)
  2685.         (printlinec (list *cycle-count*
  2686.                           'firings
  2687.                           (list *action-count* 'rhs 'actions)))
  2688.         (terpri)
  2689.         (printlinec (list (round (quotient (float *total-wm*) cc))
  2690.                           'mean 'working 'memory 'size
  2691.                           (list *max-wm* 'maximum)))
  2692.         (terpri)
  2693.         (printlinec (list (round (quotient (float *total-cs*) cc))
  2694.                           'mean 'conflict 'set 'size
  2695.                           (list *max-cs* 'maximum)))
  2696.         (terpri)
  2697.         (printlinec (list (round (quotient (float *total-token*) cc))
  2698.                           'mean 'token 'memory 'size
  2699.                           (list *max-token* 'maximum)))
  2700.         (terpri))) 
  2701.  
  2702. (defun pm-size nil
  2703.   (terpri)
  2704.   (printlinec (list *pcount*
  2705.                     'productions
  2706.                     (list *real-cnt* '// *virtual-cnt* 'nodes)))
  2707.   (terpri)) 
  2708.  
  2709. (defun check-limits nil
  2710.   (cond ((> (length *conflict-set*) *limit-cs*)
  2711.          (terpri)
  2712.          (terpri)
  2713.          (printlinec (list '|conflict set size exceeded the limit of|
  2714.                            *limit-cs*
  2715.                            '|after|
  2716.                            *p-name*))
  2717.          (setq *halt-flag* t)))
  2718.   (cond ((> *current-token* *limit-token*)
  2719.          (terpri)
  2720.          (terpri)
  2721.          (printlinec (list '|token memory size exceeded the limit of|
  2722.                            *limit-token*
  2723.                            '|after|
  2724.                            *p-name*))
  2725.          (setq *halt-flag* t)))) 
  2726.  
  2727.  
  2728. (defun top-level-remove (z)
  2729.   (cond ((equal z '(*)) (process-changes nil (get-wm nil)))
  2730.         (t (process-changes nil (get-wm z))))) 
  2731.  
  2732. (defun excise fexpr (z) (mapc (function excise-p) z))
  2733.  
  2734. (defun run fexpr (z)
  2735.   (cond ((atom z) (setq *remaining-cycles* 1000000.) (do-continue nil))
  2736.         ((and (atom (cdr z)) (numberp (car z)) (> (car z) 0.))
  2737.          (setq *remaining-cycles* (car z))
  2738.          (do-continue nil))
  2739.         (t 'what?))) 
  2740.  
  2741. (defun strategy fexpr (z)
  2742.   (cond ((atom z) *strategy*)
  2743.         ((equal z '(lex)) (setq *strategy* 'lex))
  2744.         ((equal z '(mea)) (setq *strategy* 'mea))
  2745.         (t 'what?))) 
  2746.  
  2747. (defun cs fexpr (z)
  2748.   (cond ((atom z) (conflict-set))
  2749.         (t 'what?))) 
  2750.  
  2751. (defun watch fexpr (z)
  2752.   (cond ((equal z '(0.))
  2753.          (setq *wtrace* nil)
  2754.          (setq *ptrace* nil)
  2755.          0.)
  2756.         ((equal z '(1.)) (setq *wtrace* nil) (setq *ptrace* t) 1.)
  2757.         ((equal z '(2.)) (setq *wtrace* t) (setq *ptrace* t) 2.)
  2758.         ((equal z '(3.))
  2759.          (setq *wtrace* t)
  2760.          (setq *ptrace* t)
  2761.          '(2. -- conflict set trace not supported))
  2762.         ((and (atom z) (null *ptrace*)) 0.)
  2763.         ((and (atom z) (null *wtrace*)) 1.)
  2764.         ((atom z) 2.)
  2765.         (t 'what?))) 
  2766.  
  2767. (defun external fexpr (z) (catch (external2 z) !error!))
  2768.  
  2769. (defun external2 (z) (mapc (function external3) z))
  2770.  
  2771. (defun external3 (x) 
  2772.   (cond ((symbolp x) (putprop x t 'external-routine))
  2773.     (t (%error '|not a legal function name| x))))
  2774.  
  2775. (defun externalp (x)
  2776.   (cond ((symbolp x) (get x 'external-routine))
  2777.     (t (%warn '|not a legal function name| x) nil)))
  2778.  
  2779. (defun pbreak fexpr (z)
  2780.   (cond ((atom z) (terpri) *brkpts*)
  2781.     (t (mapc (function pbreak2) z) nil)))
  2782.  
  2783. (defun pbreak2 (rule)
  2784.   (cond ((not (symbolp rule)) (%warn '|illegal name| rule))
  2785.     ((not (get rule 'topnode)) (%warn '|not a production| rule))
  2786.     ((memq rule *brkpts*) (setq *brkpts* (rematm rule *brkpts*)))
  2787.     (t (setq *brkpts* (cons rule *brkpts*)))))
  2788.  
  2789. (defun rematm (atm list)
  2790.   (cond ((atom list) list)
  2791.     ((eq atm (car list)) (rematm atm (cdr list)))
  2792.     (t (cons (car list) (rematm atm (cdr list))))))
  2793.  
  2794. (defun broken (rule) (memq rule *brkpts*))
  2795.  
  2796.  (setq *c48* (car dp))
  2797.         (setq dp (cdr dp))
  2798.         (setq *c49* (car dp))
  2799.         (setq dp (This disk contains three subdirectories, namely MAC, COMMON and FRANZ. They
  2800. contain source files for MacLispOpos5, CommonLispOps5 and FranzLispOps5 
  2801. respectively. But note that two files (MAB.L and TRY.L) in COMMON should be
  2802. copied to FRANZ. That is, FRANZ is supposed to consist of
  2803.  
  2804.         files.l
  2805.         vps2.l
  2806.         mab.l
  2807.         trys.l
  2808.  
  2809. --Zhang Guojun, 8-16-85, Carnegie-Mellon University⌐┤Φ Θµâ√ u┤╗ÿ╚║.╜Φ∙Θ╙╛ÿ╚Φ:Θ╦ï╪â√u
  2810. S┤Φαδ&É[S║æ╜Φ, ┤╗ÿ╚║J╜Φ╩δÉè╠╡X╛ÿ╚Φ▄⌠├ΘÅ[├è╠╡π9╕╖╞┐╜å├¼< uå├δ<9w$<0r å├≈÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷