Codebase list cafeobj / 03f86a1
Start refactoring. NOTE: does compile but does not work many cases. tswd 9 years ago
58 changed file(s) with 690 addition(s) and 2808 deletion(s). Raw diff Collapse all Expand all
4343 manual/md/reference-manual.run.xml
4444 manual/md/reference-manual.tex
4545 auto
46 build-stamp
47 doc/refman/reference-manual.bcf
48 doc/refman/reference-manual.epub
49 doc/refman/reference-manual.odt
50 doc/refman/reference-manual.run.xml
51 doc/refman/reference-manual.tex
52 install-stamp
53 make-cafeobj.lisp
54 tswd
55 version.lisp
56 xbin/cafeobj.in
57 dumps
215215
216216 ;;; IS-SKOLEM : method module -> Bool
217217 ;;;
218 (defun is-skolem (meth &optional (module (or *current-module* *last-module*)))
218 (defun is-skolem (meth &optional (module (get-context-module)))
219219 (declare (type method meth)
220 (type module module)
220 (type (or null module) module)
221221 (values boolean))
222222 (memq meth (module-skolem-functions module)))
223223
17871787 (princ "---"))
17881788 *full-lit-table*)))
17891789
1790 (defun show-demodulators (&optional (mod (or *current-module*
1791 *last-module*)))
1790 (defun show-demodulators (&optional (mod (get-context-module)))
17921791 (unless mod (return-from show-demodulators nil))
17931792 (with-in-module (mod)
17941793 (let* ((psys (module-proof-system mod))
384384 (with-in-module (*current-module*)
385385 (auto-db-reset *current-module*)
386386 (setq psys (module-proof-system *current-module*))
387 #||
388 (unless psys
389 (with-output-chaos-error ('no-psys)
390 (princ "no proof system prepared, do `db reset' first!"))
391 )
392 ||#
393 ;;
394387 (setq args (flatten-list args))
395388 (dolist (arg args)
396389 (cond ((is-nat arg)
436429 (princ "specified term is not valid as formula.")
437430 (term-print val)))
438431 (dolist (cl (formula->clause-1 val psys))
439 (push cl real-set)))))))))
440 )
432 (push cl real-set))))))))))
441433 ;;
442434 (dolist (cl (if (eq type :sos)
443435 (psystem-sos psys)
467459 (nreverse real-set)
468460 :test #'clause-equal)))
469461 (when put-sysgoal-in-sos
470 (setq put-sysgoal-in-sos nil)))
471 )
462 (setq put-sysgoal-in-sos nil))))
472463 ;;
473464 (if (eq type :sos)
474465 (dolist (cl (psystem-sos psys))
504495 (when put-sysgoal-in-sos
505496 (push :system-goal (psystem-sos psys)))
506497 ;;
507 psys
508 ))))
498 psys))))
509499
510500 ;;; EVAL-CLAUSE-SHOW
511501 ;;;
515505 (with-output-chaos-error ('no-context)
516506 (princ "no context module is given.")))
517507 (prepare-for-parsing *current-module*)
518 #||
519 (unless (module-proof-system *current-module*)
520 (with-output-chaos-error ('no-psys)
521 (princ "no proof system prepared, do `db reset' first!")))
522 ||#
523508 (auto-db-reset *current-module*)
524509 (with-in-module (*current-module*)
525510 (let* ((*parse-variables* nil)
538523 (dolist (cl (formula->clause-1 term
539524 (module-proof-system *current-module*)))
540525 (print-next)
541 (print-clause cl *standard-output*)))
542 )))
526 (print-clause cl *standard-output*))))))
543527
544528 ;;; EVAL-PN-LIST
545529 ;;;
564548 (princ " ")
565549 (dolist (cl (psystem-axioms psys))
566550 (print-next)
567 (print-clause cl *standard-output*)))
568 ))
551 (print-clause cl *standard-output*)))))
569552 (:sos
570553 (with-proof-context (*current-module*)
571 (print-sos-list)
572 #||
573 (dolist (cl (psystem-sos psys))
574 (print-next)
575 (print-clause cl *standard-output*))
576 ||#
577 ))
554 (print-sos-list) ))
578555 (:usable
579556 (with-proof-context (*current-module*)
580 (print-usable-list)
581 #||
582 (dolist (cl (psystem-usable psys))
583 (print-next)
584 (print-clause cl *standard-output*))
585 ||#
586 ))
557 (print-usable-list) ))
587558 (:passive
588559 (with-proof-context (*current-module*)
589560 (print-passive-list)))
590
591561 (:flag
592562 (pr-list-of-flag))
593563 (:param
596566 (pr-list-of-option))
597567 (:demod
598568 (with-proof-context (*current-module*)
599 (print-demodulators-list)
600 #||
601 (dolist (cl (psystem-demods psys))
602 (print-next)
603 (print-clause cl *standard-output*))
604 ||#
605 ))
569 (print-demodulators-list)))
606570 (otherwise
607571 (with-output-chaos-error ('invalid)
608 (format t "internal error, unknown list option ~a" arg)))
609 )))
572 (format t "internal error, unknown list option ~a" arg))))))
610573
611574 ;;; EVAL-RESOLVE
612575 ;;;
613576 (defun eval-resolve (ast)
614 (unless *current-module*
615 (with-output-chaos-error ('no-context)
616 (princ "no context (current module) is set!"))
617 )
618 (let ((out-file (%resolve-arg ast)))
619 (if (and out-file (not (equal out-file ".")))
620 (with-open-file (stream out-file :direction :output
621 :if-exists :append :if-does-not-exist :create)
622 (let ((*standard-output* stream))
623 (do-resolve (if *open-module*
624 *last-module*
625 *current-module*))))
626 (do-resolve (if *open-module*
627 *last-module*
628 *current-module*)))))
577 (let ((eval-context (get-context-module)))
578 (unless eval-context
579 (with-output-chaos-error ('no-context)
580 (princ "no context (current module) is set!")))
581 (let ((out-file (%resolve-arg ast)))
582 (if (and out-file (not (equal out-file ".")))
583 (with-open-file (stream out-file :direction :output
584 :if-exists :append :if-does-not-exist :create)
585 (let ((*standard-output* stream))
586 (do-resolve eval-context)))
587 (do-resolve eval-context)))))
629588
630589 (defun do-resolve (mod)
631590 (let ((time1 nil)
690649 (setf (pn-flag index) value)
691650 (dependent-flags index)
692651 ;; run hook
693 (funcall (pn-flag-hook index) value)
694 )))
652 (funcall (pn-flag-hook index) value))))
695653
696654 ;;; SHOW-OPTION
697655 ;;;
730688 (with-output-msg ()
731689 (format t "setting parameter ~s to ~d."
732690 name value)))
733 (setf (pn-parameter index) value)
734 )))
691 (setf (pn-parameter index) value))))
735692
736693 ;;; option reset
737694 ;;;
741698 (case command
742699 (:reset (init-pn-options))
743700 (:save (save-option-set name))
744 (:restore (restore-option-set name))
745 )))
701 (:restore (restore-option-set name)))))
746702
747703 ;;; DEMOD
748704 (defun perform-demodulation (ast)
753709 (perform-demodulation* preterm modexp mode result-as-text)))
754710
755711 (defun perform-demodulation* (preterm &optional modexp mode (result-as-text nil))
756 ;; (setq $$trials 1)
757712 (let ((*consider-object* t)
758713 (*rewrite-exec-mode* (eq mode :exec))
759714 (*rewrite-semantic-reduce* nil)
765720 (number-matches nil))
766721 (let ((mod (if modexp
767722 (eval-modexp modexp)
768 *last-module*)))
769 (unless (eq mod *last-module*)
723 (get-context-module))))
724 (unless (eq mod (get-context-module))
770725 (clear-term-memo-table *term-memo-table*))
771726 (if (or (null mod) (modexp-is-error mod))
772727 (if (null mod)
773728 (with-output-chaos-error ('no-context)
774 (princ "no module expression provided and no selected(current) module.")
775 )
729 (princ "no module expression provided and no selected(current) module."))
776730 (with-output-chaos-error ('no-such-module)
777731 (princ "incorrect module expression, no such module ")
778 (print-chaos-object modexp)
779 ))
780 (progn
781 (context-push-and-move *last-module* mod)
782 (with-in-module (mod)
783 (auto-db-reset mod))
784 (with-proof-context (mod)
785 (when *auto-context-change*
786 (change-context *last-module* mod))
787 (!setup-reduction mod)
788 (setq $$mod *current-module*)
789 (setq sort *cosmos*)
790 (when *show-stats* (setq time1 (get-internal-run-time)))
732 (print-chaos-object modexp)))
733 (progn
734 (context-push-and-move (get-context-module) mod)
735 (with-in-module (mod)
736 (auto-db-reset mod))
737 (with-proof-context (mod)
738 (when *auto-context-change*
739 (change-context (get-context-module) mod))
740 (!setup-reduction mod)
741 (setq $$mod (get-context-module))
742 (setq sort *cosmos*)
743 (when *show-stats* (setq time1 (get-internal-run-time)))
791744 (setq *rewrite-semantic-reduce*
792745 (and (eq mode :red)
793746 (module-has-behavioural-axioms mod)))
794747 ;;
795748 (let* ((*parse-variables* nil)
796 (term (simple-parse *current-module* preterm sort)))
749 (term (simple-parse (get-context-module) preterm sort)))
797750 (when (or (null (term-sort term))
798751 (sort<= (term-sort term) *syntax-err-sort* *chaos-sort-order*))
799752 (return-from perform-demodulation* nil))
802755 (setq time2 (get-internal-run-time))
803756 (setf time-for-parse
804757 (format nil "~,3f sec"
805 ;; (/ (float (- time2 time1)) internal-time-units-per-second)
806 (elapsed-time-in-seconds time1 time2)
807 )))
758 (elapsed-time-in-seconds time1 time2))))
808759 (unless *chaos-quiet*
809760 (fresh-all)
810761 (flush-all)
814765 (princ "-- demodulate in ")
815766 (princ "-- behavioural demodulate in "))
816767 )
817 (print-simple-mod-name *current-module*)
768 (print-simple-mod-name (get-context-module))
818769 (princ " : ")
819770 (let ((*print-indent* (+ 4 *print-indent*)))
820771 (term-print term))
821772 (flush-all))
822773 ;; ********
823 (reset-target-term term *last-module* mod)
774 (reset-target-term term (get-context-module) mod)
824775 ;; ********
825776 (setq $$matches 0)
826777 (setq time1 (get-internal-run-time))
827778 (let ((*rule-count* 0))
828779 (let ((res nil))
829780 (catch 'rewrite-abort
830 #||
831 (if (sort<= (term-sort term) *fopl-sentence-sort*
832 *current-sort-order*)
833 (dolist (sub (term-subterms term))
834 (unless (term-is-variable? sub)
835 (demod-atom sub)))
836 (setq res (demod-atom term)))
837 ||#
838 (setq res (demod-atom term))
839 )
781 (setq res (demod-atom term)))
840782 (unless res (setq res $$term))
841783 ;;
842784 (when *mel-sort*
848790 (setq time2 (get-internal-run-time))
849791 (setf time-for-reduction
850792 (format nil "~,3f sec"
851 ;; (/ (float (- time2 time1))
852 ;; internal-time-units-per-second)
853793 (elapsed-time-in-seconds time1 time2)))
854794 (setf number-matches $$matches)
855795 (setq $$matches 0)
864804 (print-check)
865805 (princ " : ")
866806 (print-sort-name (term-sort res)
867 *current-module*))
868 s
869 ))
807 (get-context-module))
808 s)))
870809 (stat
871810 (if *show-stats*
872811 (concatenate
890829 (print-check 0 3)
891830 (princ " : ")
892831 (print-sort-name (term-sort res)
893 *current-module*))
832 (get-context-module)))
894833 (when *show-stats*
895834 (format t "~%(~a for parse, ~s rewrites(~a), ~d matches"
896835 time-for-parse
901840 (format t ")~%")
902841 (format t ", ~d memo hits)~%"
903842 *term-memo-hash-hit*)))
904 (flush-all)))
905 ))
906 ))
843 (flush-all)))))))
907844 (context-pop-and-recover))))))
908845
909846 ;;; SIGMATCH
920857 (princ "no such module: ")
921858 (print-modexp (%sigmatch-mod2 ast))))
922859 (setq views (sigmatch mod1 mod2))
923 ;;
924860 (if views
925861 (princ views)
926 (princ "( )"))
927 ))
862 (princ "( )"))))
928863
929864 ;;; LEX
930865 (defun eval-pn-lex (ast)
931 (unless *current-module*
866 (unless (get-context-module)
932867 (with-output-chaos-error ('no-context)
933868 (princ "no context(current) module is specified.")))
934 (compile-module *current-module*)
935 (with-in-module (*current-module*)
869 (compile-module (get-context-module))
870 (with-in-module ((get-context-module))
936871 (let ((optokens (%pn-lex-ops ast))
937872 (prec-list nil))
938873 (dolist (e optokens)
946881 (with-in-module (mod)
947882 (dolist (opinfo ops)
948883 (dolist (meth (reverse (opinfo-methods opinfo)))
949 (push meth prec-list))))))
950 )
951 ))
884 (push meth prec-list)))))))))
952885 ;;
953886 (unless (memq :* prec-list)
954887 (push :* prec-list))
956889 (push :skolem prec-list))
957890 (setq prec-list (nreverse prec-list))
958891 ;;
959 (setf (module-op-lex *current-module*) prec-list)
960 )))
961
892 (setf (module-op-lex *current-module*) prec-list))))
962893
963894 ;;; EOF
667667 ;; must skolemize subformula first to avoid
668668 ;; problem in \Ax...\Ey...\Ex F(x,y)
669669 (skolem (term-arg-2 sentence))
670 (let* ((mod (or *current-module* *last-module*))
670 (let* ((mod (get-context-module))
671671 (skfun-name
672672 (make-skolem-function-name (term-sort
673673 (term-arg-1 sentence))
674 variables))
675 )
674 variables)))
676675 (multiple-value-bind (op meth)
677676 (declare-operator-in-module
678677 (list skfun-name)
770770
771771 (defun pn-check-invariance (args)
772772 (declare (type list args))
773 (let ((target-module (or *current-module*
774 *last-module*)))
773 (let ((target-module (get-context-module)))
775774 (declare (type (or null module) target-module))
776775 (unless target-module
777776 (with-output-chaos-error ('no-context)
8989
9090 ;;; PN-DB-RESET
9191 ;;;
92 (defun pn-db-reset (&optional (mod (or *current-module*
93 *last-module*)))
92 (defun pn-db-reset (&optional (mod (get-context-module)))
9493 (clear-all-index-tables)
9594 (reset-module-proof-system mod))
9695
188188 (unless (literal-sign lit)
189189 (princ "~(" stream)
190190 (setq .file-col. (1+ .file-col.)))
191 (with-in-module ((or *current-module* *last-module*))
191 (with-in-module ((get-context-module))
192192 (cond ((eq-literal? lit)
193193 (let* ((lhs (term-arg-1 (literal-atom lit)))
194194 (rhs (term-arg-2 (literal-atom lit)))
231231 (unless (literal-sign lit)
232232 (princ "~(" stream)
233233 (setq .file-col. (1+ .file-col.)))
234 (with-in-module ((or *current-module* *last-module*))
234 (with-in-module ((get-context-module))
235235 (setq .printed-vars-so-far.
236236 (append .printed-vars-so-far.
237237 (term-print (literal-atom lit) stream))))
238238 (unless (literal-sign lit)
239239 (princ ")" stream))
240 .printed-vars-so-far.)
241 )
240 .printed-vars-so-far.))
242241
243242 ;;;
244243 ;;; some gobal flags
0 20150324 tswd
1
2 refactor, over 20 years old codes
3 even 20 years a go, the style was already old fashioned.
4 (1) many globals to be eliminated
5 *memoized-module* ?
6 (2) clean up and reorganize modules
7 profiler -- new
8 context
9 (3) many dumplicated/similar code fragments
10 much codes have been added in ad hoc manner
11
012 20150225 np
113
214 we should add a check that a key is not define'd more than once
2234 --end-toplevel-options <args-to-cafeobj>*
2335 (Please refer to 3.2.3 Saving Core Image and 3.3 Command Line Options
2436 of the manual. )
37
38
392392 (defun print-cafeobj-prompt ()
393393 (fresh-all)
394394 (flush-all)
395 (cond ((eq *prompt* 'system)
396 (if *last-module*
397 (if (module-is-inconsistent *last-module*)
395 (let ((cur-module (get-context-module)))
396 (cond ((eq *prompt* 'system)
397 (if cur-module
398 (if (module-is-inconsistent cur-module)
398399 (progn
399400 (with-output-chaos-warning ()
400401 (format t "~a is inconsistent due to changes in some of its submodules."
401 (module-name *last-module*))
402 (module-name cur-module))
402403 (print-next)
403404 (princ "resetting the `current module' of the system.."))
404 (setq *last-module* nil)
405 (format *error-output* "~&CafeOBJ> ")
406 )
405 (reset-context-module)
406 (format *error-output* "~&CafeOBJ> "))
407407 (let ((*standard-output* *error-output*))
408 (print-simple-mod-name *last-module*)
408 (print-simple-mod-name cur-module)
409409 (princ "> ")))
410 (format *error-output* "CafeOBJ> "))
411 (setf *sub-prompt* nil))
412 ((eq *prompt* 'none))
413 (*prompt*
414 (let ((*standard-output* *error-output*))
415 (if (atom *prompt*)
416 (princ *prompt*)
417 (print-simple-princ-open *prompt*))
418 (princ " "))))
419 (flush-all))
410 ;; no context module
411 (format *error-output* "CafeOBJ> "))
412 (setf *sub-prompt* nil))
413 ;; prompt specified to NONE
414 ((eq *prompt* 'none))
415 ;; specified prompt
416 (*prompt*
417 (let ((*standard-output* *error-output*))
418 (if (atom *prompt*)
419 (princ *prompt*)
420 (print-simple-princ-open *prompt*))
421 (princ " "))))
422 (flush-all)))
420423
421424 ;;; SAVE INTERPRETER IMAGE
422425 ;;;_____________________________________________________________________________
602605 (setq *chaos-print-errors* t)
603606 (setq .in-in. nil)))))))
604607
605 (defun try-reduce-term (inp)
606 (perform-reduction* inp *current-module* nil nil))
607
608608 (defun handle-cafeobj-top-error (val)
609609 (if *chaos-input-source*
610610 (chaos-to-top val)
582582 ;;; ******************
583583 ;;; MODULE Constructs.
584584 ;;; ******************
585
586 ;;; it is an error unless a module is open.
585587 (defun cafeobj-eval-module-element-proc (inp)
586588 (if *open-module*
587 (with-in-module (*last-module*)
589 (with-in-module ((get-context-module))
588590 (multiple-value-bind (type ast)
589591 (parse-module-element inp)
590592 (declare (ignore type))
186186 (defun install-chaos-hard-wired-modules ()
187187 (setq *dribble-ast* nil)
188188 (setq *ast-log* nil)
189 (setq *last-module* nil *current-module* nil)
189 (reset-context-module)
190190 (setq *include-bool* nil)
191191 (setq *include-rwl* nil)
192192 (setq *regularize-signature* nil)
280280 (%bsort-decl "String" nil nil prin1 stringp nil))))
281281 (install-string)
282282 ;;
283 ;;
284 (setq *last-module* nil *current-module* nil)
283 (reset-context-module)
285284 (setq *include-bool* t)
286285 (setq *include-rwl* t)
287286 )
302301 (setq *ast-log* nil)
303302 (setq *include-bool* t)
304303 (setq *include-rwl* t)
305 (setq *last-module* nil
306 *current-module* nil)
304 (reset-context-module)
307305 (setq *regularize-signature* nil)
308306 ;; set recover proc.
309307 (setq *system-soft-wired*
5454 (defun token-is-sort-id (token)
5555 (and (stringp token)
5656 (<= 1 (length token))
57 (find-all-sorts-in (or *current-module* *last-module*)
58 token)))
57 (find-all-sorts-in (get-context-module) token)))
5958 (defun create-sort-id (token) token)
6059 (defun print-sort-id (x) (princ x))
6160 (defun is-sort-Id (x)
17831783 (apply-sort-memb-internal term module)))
17841784 term)
17851785
1786 (defun sort-to-sort-id-term (sort &optional (module (or *current-module*
1787 *last-module*)))
1786 (defun sort-to-sort-id-term (sort &optional (module (get-context-module)))
17881787 (let* ((name (string (sort-id sort)))
17891788 (op (find-method-in module (list name) nil *sort-id-sort*)))
17901789 (unless op
144144 (variable-name x))
145145 variables))
146146 subst
147 ',s-simbol))
148 )
149 )))
147 ',s-simbol))))))
150148 ((term-is-general-lisp-form? term)
151149 (setf fun-body
152150 (make-fun* `(lambda (subst)
184182 ',(reverse (mapcar #'(lambda (x)
185183 (variable-name x))
186184 variables))
187 subst))
188 )
189 )
185 subst))))
190186 (setf (term-builtin-value term)
191187 (list '|%Chaos| fun-body (term-builtin-value term)))
192188 term))
249245 (push (term-builtin-value value) bindings)
250246 (return-from invoke (values nil nil))))
251247 (return-from invoke (values value t))))
252 (return-from invoke (values nil nil)))
253 )
254 (return-from invoke (values nil nil))
255 )))
248 (return-from invoke (values nil nil))))
249 (return-from invoke (values nil nil)))))
256250 (if (sort-is-builtin sort)
257251 (values (make-bconst-term sort
258252 (apply fun bindings))
290284 (push (term-builtin-value value) bindings)
291285 (return-from invoke (values nil nil))))
292286 (return-from invoke (values value t))))
293 (return-from invoke (values nil nil)))
294 )
295 (return-from invoke (values nil nil))
296 )))
287 (return-from invoke (values nil nil))))
288 (return-from invoke (values nil nil)))))
297289 (values (make-bconst-term sort
298290 (apply fun bindings))
299 t)
300 ))))
291 t)))))
301292
302293 ;;;
303294 ;;;
341332 :rhs (make-applform (method-coarity top)
342333 top
343334 (list new-var
344 ;;(substitution-check-builtin
345 ;; (axiom-rhs rule))
346 (axiom-rhs rule)
347 ))
335 (axiom-rhs rule)))
348336 :condition (axiom-condition rule)
349337 :id-condition (axiom-id-condition rule)
350338 :type (axiom-type rule)
380368 :id-ext-theory
381369 :A-right-theory)
382370 :meta-and-or (axiom-meta-and-or rule)))
383 ;; (compute-rule-method ext-rule)
384371 (push ext-rule listext)
385
386372 ;; the middle associative extension:
387373 (setf ext-rule
388374 (make-rule
405391 :meta-and-or (axiom-meta-and-or rule)))
406392 ;;
407393 (push ext-rule listext)
408 (setf (axiom-A-extensions rule) listext))
409 )))
394 (setf (axiom-A-extensions rule) listext)))))
410395
411396
412397 ;;; COMPUTE-AC-EXTENSION : rule method -> List[Rule]
438423 :rhs (make-applform (method-coarity top)
439424 top
440425 (list new-var
441 (axiom-rhs rule)
442 ))
426 (axiom-rhs rule)))
443427 :condition (axiom-condition rule)
444428 :type (axiom-type rule)
445429 :behavioural (axiom-is-behavioural rule)
450434 ':ac-theory)
451435 :meta-and-or (axiom-meta-and-or rule)))
452436 ;;
453 (setf (axiom-AC-extension rule) (list ext-rule))
454 ))))
437 (setf (axiom-AC-extension rule) (list ext-rule))))))
455438
456439
457440 ;;; GIVE-AC-EXTENSION : rule -> List[Rule]
572555 (cond ((term$is-variable? t1-body)
573556 (or (eq t1 t2)
574557 (and (term$is-variable? t2-body)
575 #||
576 ;; (eq (variable$name t1-body) (variable$name t2-body))
577 (sort= (variable$sort t1-body) (variable$sort t2-body))
578 ||#
579 (variable= t1 t2)
580 )))
558 (variable= t1 t2))))
581559 ((term$is-variable? t2-body) nil)
582560 ((term$is-application-form? t1-body)
583561 (and (term$is-application-form? t2-body)
584 (if ;;(method-is-same-qual-method (term$method t1-body)
585 ;; (term$method t2-body))
586 (method-is-of-same-operator+ (term$method t1-body)
562 (if (method-is-of-same-operator+ (term$method t1-body)
587563 (term$method t2-body))
588564 (let ((sl1 (term$subterms t1-body))
589565 (sl2 (term$subterms t2-body)))
642618 (when (rule-is-similar? rule r)
643619 (when (and *chaos-verbose*
644620 (not (eq rule r))
645 (not (member (axiom-kind rule) .ext-rule-kinds.))
646 )
621 (not (member (axiom-kind rule) .ext-rule-kinds.)))
647622 (with-output-msg ()
648623 (format t "a similar pair of axioms is found:")
649624 (print-next)
731706 ;;; ***********************
732707 ;;; ADDING AXIOMS TO MODULE
733708 ;;; ***********************
734 #||
709
735710 (defun add-axiom-to-module (module ax)
736 (declare (type module module)
737 (type axiom ax)
738 (values t))
739 (if (memq (axiom-type ax) '(:equation :pignose-axiom :pignose-goal))
740 (push ax (module-equations module))
741 (push ax (module-rules module))))
742 ||#
743
744 (defun add-axiom-to-module (module ax)
745 (adjoin-axiom-to-module module ax)
746 )
711 (adjoin-axiom-to-module module ax))
747712
748713 (defun adjoin-axiom-to-module (module ax)
749714 (declare (type module module)
750715 (type axiom ax)
751716 (values t))
752 ;; (when (eq (object-context-mod ax) module)
753 ;; (let ((labels (axiom-labels ax)))
754 ;; (dolist (lab labels)
755 ;; (symbol-table-add (module-symbol-table module)
756 ;; lab
757 ;; ax)))
758 ;; )
759717 (if (memq (axiom-type ax) '(:equation :pignose-axiom :pignose-goal))
760718 (setf (module-equations module)
761719 (adjoin-rule ax (module-equations module)))
819777 (substitution-partial-image
820778 subst
821779 (rule-condition r1))))))
822 (matches? newcond (rule-condition r2))
823 ))))))))))
780 (matches? newcond (rule-condition r2))))))))))))
824781
825782 (defun rule-strictly-subsumes (r1 r2)
826783 (declare (type axiom r1 r2)
891848 :condition (axiom-condition rule)
892849 :labels (axiom-labels rule)
893850 :kind (axiom-kind rule)
894 :type (axiom-type rule)
895 ;; :meta-and-or (axiom-meta-and-or rule)
896 ;; :no-method-computation t
897 )))
851 :type (axiom-type rule))))
898852
899853 (defun make-rule-instance (rule subst)
900854 (declare (type axiom rule)
932886 ;; we always need rule specifier
933887 (when (equal "" rule-id)
934888 (with-output-chaos-error ('invalid-rule-spec)
935 (format t "No rule number or name is specified.")
936 ))
889 (format t "No rule number or name is specified.")))
937890 ;; get module in which the specified rule is looked up
938 (if (equal "" mod)
939 (setq mod *last-module*)
940 (if (and *last-module*
941 (equal "%" (module-name *last-module*))
942 (module-submodules *last-module*)
943 (equal mod
944 (module-name
945 (caar (module-submodules *last-module*)))))
946 (setq mod *last-module*)
947 ;; we also find in local modules
948 (setq mod (eval-modexp mod t))))
949 ;;
950 (unless mod
951 (with-output-chaos-error ('no-context)
952 (princ "no context module.")))
953 ;;
954 (when (modexp-is-error mod)
955 (let ((nxt (eval-mod (list (car rule-spec)))))
956 (if (modexp-is-error nxt)
957 (with-output-chaos-error ('invalid-module)
958 (format t "module is undefined or unreachable: ~a" (car rule-spec))
959 )
891 (let ((cur-context (get-context-module)))
892 (if (equal "" mod)
893 (setq mod cur-context)
894 (if (and cur-context
895 (equal "%" (module-name cur-context))
896 (module-submodules cur-context)
897 (equal mod
898 (module-name
899 (caar (module-submodules cur-context)))))
900 (setq mod cur-context)
901 ;; we also find in local modules
902 (setq mod (eval-modexp mod t))))
903 (unless mod
904 (with-output-chaos-error ('no-context)
905 (princ "no context module.")))
906 (when (modexp-is-error mod)
907 (let ((nxt (eval-mod (list (car rule-spec)))))
908 (if (modexp-is-error nxt)
909 (with-output-chaos-error ('invalid-module)
910 (format t "module is undefined or unreachable: ~a" (car rule-spec)))
960911 (setq mod nxt))))
961 ;; check context
962 (unless (eq *last-module* mod)
963 (let ((e-mod (assoc mod (module-all-submodules *last-module*))))
964 (unless e-mod
965 (with-output-chaos-error ('invalid-context)
966 (format t "specified module is out of current context: ")
967 (print-simple-mod-name mod)))
968 (unless (member (cdr e-mod)
969 '(:protecting :extending :using))
970 (with-output-chaos-error ('invalid-rule-ref)
971 (format t "you cannot refer the rule ~a of module " rule-spec)
972 (print-simple-mod-name mod)
973 (princ " directly.")))))
974 ;;
975 (with-in-module (mod)
976 ;; find specified rule
977 (if (and (< 0 (length rule-id))
978 (every #'digit-char-p rule-id))
979 (setq rule (get-rule-numbered mod (str-to-int rule-id)))
912 ;; check context
913 (unless (eq cur-context mod)
914 (let ((e-mod (assoc mod (module-all-submodules cur-context))))
915 (unless e-mod
916 (with-output-chaos-error ('invalid-context)
917 (format t "specified module is out of current context: ")
918 (print-simple-mod-name mod)))
919 (unless (member (cdr e-mod)
920 '(:protecting :extending :using))
921 (with-output-chaos-error ('invalid-rule-ref)
922 (format t "you cannot refer the rule ~a of module " rule-spec)
923 (print-simple-mod-name mod)
924 (princ " directly.")))))
925 ;; do search in 'mod'
926 (with-in-module (mod)
927 ;; find specified rule
928 (if (and (< 0 (length rule-id))
929 (every #'digit-char-p rule-id))
930 (setq rule (get-rule-numbered mod (str-to-int rule-id)))
980931 (setq rule (get-rule-labelled mod rule-id)))
981 ;; make rule reverse order if need
932 ;; make rule reverse order if need
982933 (when (nth 2 rule-spec) (setq rule (make-rule-reverse rule)))
983934 ;; apply variable substitution
984935 (when subst-list
985936 (setq rule
986 (make-rule-instance rule (compute-variable-substitution
987 rule subst-list))))
988 )
937 (make-rule-instance rule (compute-variable-substitution
938 rule subst-list)))))
989939 ;; the result
990 (when *on-axiom-debug*
991 (with-output-simple-msg ()
992 (princ "[compute-action-rule]: rule= ")
993 (print-chaos-object rule)))
994 ;;
995 (values rule mod)
996 ))
940 (when *on-axiom-debug*
941 (with-output-simple-msg ()
942 (princ "[compute-action-rule]: rule= ")
943 (print-chaos-object rule)))
944 (values rule mod))))
997945
998946
999947 ;;; CHECK-AXIOM-ERROR-METHOD : Module Axiom -> Axiom
1010958 (error-operators (module-error-methods module)))
1011959 (macrolet ((check-check (_eops)
1012960 ` (when (every #'(lambda (x)
1013 #||
1014 (and (memq x error-operators)
1015 (not (method-is-user-defined-error-method
1016 x)))
1017 ||#
1018 (memq x error-operators)
1019 )
961 (memq x error-operators))
1020962 ,_eops)
1021963 (setq ,_eops nil))))
1022964 ;;
10921034 (type list error-operators))
10931035 (macrolet ((check-check (_eops)
10941036 ` (when (every #'(lambda (x)
1095 #||
1096 (and (memq x error-operators)
1097 (not (method-is-user-defined-error-method
1098 x)))
1099 ||#
1100 (memq x error-operators)
1101 )
1037 (memq x error-operators))
11021038 ,_eops)
11031039 (setq ,_eops nil))))
11041040 ;;
4646 (values t))
4747 (compute-protected-modules module)
4848
49 ;; reset rewrite rule set.
50 ;; (setf (module-all-rules module) nil)
51
52 ;; adds axioms for record/class
53 (dolist (s (module-sorts module))
54 (cond ((class-sort-p s)
55 (declare-class-axioms module s))
56 ((record-sort-p s)
57 (declare-record-axioms module s))))
58
5949 ;; install own rules.
6050 (let ((axiom-set (module-axiom-set module)))
6151 (dolist (eq (axiom-set$equations axiom-set))
6252 (gen-rule-internal eq module))
6353 (dolist (rule (axiom-set$rules axiom-set))
6454 (gen-rule-internal rule module)))
65
66 ;; install rules of submodules
67 ;; (dolist (submodule (module-all-submodules module))
68 ;; (unless (eq 'using (cdr submodule))
69 ;; (transfer-axioms module (car submodule))))
7055
7156 ;; specialize rules of sumodules.
7257 (dolist (rule (gather-submodule-rules module))
8267 (and (term-is-application-form? t2)
8368 (dolist (sub (term-subterms t2) nil)
8469 (when (variable-occurs-in t1 sub)
85 (return-from variable-occurs-in t))))
86 ))
70 (return-from variable-occurs-in t))))))
8771
8872 (defparameter non-exec-labels '(|:nonexec| |:non-exec| |:no-ex| |:noex| |:noexec|))
8973
116100 (when (term-is-variable? (axiom-lhs rule))
117101 (when (variable-occurs-in (axiom-lhs rule)
118102 (axiom-rhs rule))
119 ;; (format t "..setting rule mark `need-copy'")
120103 (setf (axiom-need-copy rule) t))
121 ;;
122104 (unless (eq (axiom-type rule) :rule)
123105 (unless (axiom-non-exec rule)
124106 (with-output-chaos-warning ()
130112 (setf (axiom-kind rule) ':bad-rule)
131113 (setf (axiom-kind ax) ':bad-rule))
132114 (return-from gen-rule-internal nil))
133 ;;
134115 (let ((rhs-vars (term-variables (axiom-rhs rule)))
135116 (cond-vars (term-variables (axiom-condition rule))))
136117 (declare (type list rhs-vars cond-vars))
144125 (print-chaos-object rule)
145126 (print-next)
146127 (princ "is not a subset of variables in LHS, system does not guarantee the result of the rewriting.")))
147 ;; (setf (axiom-kind rule) ':bad-rule)
148 ;; (setf (axiom-kind ax) ':bad-rule))
149128 (add-rule-to-module module rule)
150129 (unless (term-is-variable? (axiom-lhs rule))
151130 (add-associative-extensions module
173152 (progn
174153 (setf (axiom-kind rule) ':bad-rule)
175154 (setf (axiom-kind ax) ':bad-rule))))
176 ;;
177155 ;; all is ok, we can use this axiom as a rewrite rule
178156 (t (add-rule-to-module module rule)
179157 (unless (term-is-variable? (axiom-lhs rule))
385363 :kind (if (eq ':id-theorem (axiom-kind r))
386364 ':id-ext-theory
387365 ':a-right-theory)))
388 ;; (compute-rule-method a-rule)
389366 (add-rule-to-method a-rule method-above (module-opinfo-table mod))
390367
391368 (setf a-rule
413390 :kind (if (eq ':id-theorem (axiom-kind r))
414391 ':id-ext-theory
415392 ':a-middle-theory)))
416 ;; (compute-rule-method a-rule)
417 (add-rule-to-method a-rule method-above (module-opinfo-table mod))))
418 ))))
393 (add-rule-to-method a-rule method-above (module-opinfo-table mod))))))))
419394
420395 (defun rule-check-down (mod method terms)
421396 (declare (ignore mod)
488463 (cdr term-s))
489464 cvi)))
490465 (unless (eq ':fail res)
491 (return res)))))))))
492 ))
466 (return res)))))))))))
493467
494468 ;;;-----------------------------------------------------------------------------
495469 (defun normalize-rules-in (mod)
508482 (values list))
509483 (prog1
510484 (list (intern (format nil "~a~a" label $rule-counter)))
511 (incf $rule-counter)))
512 )
485 (incf $rule-counter))))
513486
514487 (defun add-operator-theory-axioms (module opinfo)
515488 (declare (type module module)
690663 (not (eq r newrule)))
691664 (setf (axiom-labels newrule)
692665 (create-rule-name module "compl")))
693 ;; #||
694666 (when (axiom-extensions newrule)
695667 (dolist (e (axiom-a-extensions newrule))
696668 (setf (axiom-id-condition e) newidcond))
697669 (dolist (e (axiom-AC-extension newrule))
698670 (setf (axiom-id-condition e) newidcond)))
699 ;; ||#
700671 (dolist (e (axiom-extensions newrule))
701672 (when e
702673 (setf (axiom-id-condition e) newidcond)))
703 ;;
704 ;; (break)
705674 (unless (eq r rul)
706675 (adjoin-axiom-to-module module newrule)))))))))
707676
863832 :meta-and-or (rule-meta-and-or rul)
864833 :labels (cons (car (create-rule-name 'dummy "idcomp")) (axiom-labels rul))))
865834 ;;
866 ;;
867835 (when *gen-rule-debug*
868836 (format t "~%invert-val: ")
869837 (format t "~% given rule : ")
3636 #+:chaos-debug
3737 (declaim (optimize (speed 1) (safety 3) #-GCL (debug 3)))
3838
39 ;;; (defvar *on-operator-debug* nil)
4039 (defun on-debug-operator ()
4140 (setq *on-operator-debug* t))
4241 (defun off-debug-operator ()
4342 (setq *on-operator-debug* nil))
4443
45 ;;; *TODO, immediately*
44 ;;; *TODO*
4645 ;;; syntax of an operator can be regular-expression.
4746
4847 ;;; === DESCRIPTION ============================================================
323322 (setq theory (merge-operator-theory-in *current-module*
324323 method
325324 old-th
326 theory
327 ))
325 theory))
328326 (setq new-code (theory-code theory))))
329327 ;;
330328 ;; associativity
454452 (princ "invalid strategy ")
455453 (princ strat)
456454 (princ " for operator ")
457 (princ (method-symbol meth))
458 ))
455 (princ (method-symbol meth))))
459456 ;; complete
460457 (setf (method-supplied-strategy meth) (complete-strategy num-args strat))))
461458
475472 (t (with-output-chaos-warning ()
476473 (format t "unknown associativity declaration ~a assoc for operator ~a, ignored"
477474 assoc
478 (method-symbol meth)
479 ))
475 (method-symbol meth)))
480476 nil)))
481477
482478 ;;; PRECEDENCE ___________________________________________________________________
515511 (setf (method-constructor method) constr)
516512 (when constr
517513 (pushnew method (sort-constructor (method-coarity method))
518 :test #'eq))
519 )
514 :test #'eq)))
520515
521516 ;;; COHERENCY ---------------------------------------------------------------------
522517
525520 (type (or null t) coherent)
526521 (values (or null t)))
527522 (setf (method-coherent method) coherent))
528
529 ;;; COPIER ________________________________________________________________________
530 ;;; COPY-METHOD-INFO : from-method to-method
531 ;;;
532 #|| NOT USED
533 (defun copy-method-info (from to)
534 (let (sup-strat
535 theory
536 prec
537 memo
538 assoc
539 constr)
540 (when *on-operator-debug*
541 (format t "~&[copy-method-info]:")
542 (format t "~&-- copy from ") (print-chaos-object from)
543 (format t "~& to ") (print-chaos-object to))
544 (let ((from-module (method-module from)))
545 (with-in-module (from-module)
546 (setf sup-strat (method-supplied-strategy from)
547 theory (method-theory from)
548 prec (get-method-precedence from)
549 memo (method-memo from)
550 assoc (method-associativity from)
551 constr (method-constructor from))))
552 (let ((to-module (method-module to)))
553 (with-in-module (to-module)
554 (setf (method-supplied-strategy to) sup-strat
555 (method-theory to) theory
556 (method-precedence to) prec
557 (method-memo to) memo
558 (method-associativity to) assoc
559 (method-constructor to) constr)))
560 ))
561
562 ||#
563523
564524 ;;; ********************
565525 ;;; OPERATOR DECLARATION _______________________________________________________
587547 (unless mod
588548 (with-output-chaos-error ('no-such-module)
589549 (princ "declaring operator, no such module ")
590 (princ module)
591 ))
550 (princ module)))
592551
593552 ;; check arity, coarity
594553 (with-in-module (mod)
628587 (t
629588 (with-output-chaos-error ('no-such-sort)
630589 (princ "declaring operator, no such sort ")
631 (print-sort-ref coarity)
632 ))))
633 ;; name conflict check with existing variables
634 #||
635 (when (and (null r-arity)
636 (find-variable-in module (car op-name)))
637 (with-output-chaos-warning ()
638 (format t "declaring op ~s" op-name)
639 (print-next)
640 (princ " there already a variable with the same name.")
641 (princ " ... ignoring"))
642 (return-from declare-operator-in-module (values nil nil nil))
643 )
644 ||#
645 ;;
590 (print-sort-ref coarity)))))
646591 (multiple-value-bind (x y)
647592 (add-operator-declaration-to-module op-name
648593 (nreverse r-arity)
651596 constructor
652597 behavioural
653598 coherent
654 error-operator
655 )
656 (values x y nil))))
657 ))
599 error-operator)
600 (values x y nil))))))
658601
659602 (defun make-operator-in-module (op-name num-args module &optional qual-name)
660603 (declare (ignore qual-name)
670613 (unless arity
671614 (let ((opstr (car op-name))
672615 (sorts (module-all-sorts module)))
673 (dolist (bi sorts)
616 (dolist (bi sorts nil)
674617 (when (sort-is-builtin bi)
675618 (let ((token-pred (bsort-token-predicate bi)))
676619 (when (and token-pred
684627 (print-sort-name bi module)
685628 (print-next)
686629 (princ "... ignored.")
687 (return-from check-overloading-with-builtin t)
688 ))))))
689 )
690 nil)
630 (return-from check-overloading-with-builtin t)))))))))
691631
692632 (defun add-operator-declaration-to-module (op-name arity coarity module
693633 &optional
698638 (declare (type t op-name)
699639 (type list arity)
700640 (type (or symbol sort-struct) coarity)
701 (type t module)
702 (type (or null t)
703 constructor behavioural coherent error-operator))
641 (type (or null module) module))
704642 (let* ((mod (if (module-p module)
705643 module
706 (find-module-in-env module)))
644 (find-module-in-env module)))
707645 (op-infos (find-operators-in-module op-name (length arity) mod))
708646 (opinfo nil)
709647 (op nil))
716654 arity coarity)
717655 (format t "~% -- module = ~a, constructor = ~a, behavioural = ~a"
718656 module constructor behavioural)
719 (format t "~% -- coherent = ~a, error-operator = ~a" coherent error-operator)
720 )
657 (format t "~% -- coherent = ~a, error-operator = ~a" coherent error-operator))
721658 ;; checks hidden sort condition
722659 (let ((hidden? nil))
723660 (dolist (as arity)
727664 (format t "more than one hidden sort in the declaration of operator \"~{~a~}\""
728665 op-name)
729666 ))
730 (setf hidden? t)
731 #|| -----------------------------------------------
732 (when (and (not (sort= as *huniversal-sort*))
733 (not (eq module (sort-module as)))
734 behavioural)
735 (with-output-chaos-warning ()
736 (format t "behavioural operator \"~{~a~}\" has imported hidden sort " op-name)
737 (print-sort-name as)
738 (princ " in its arity.")
739 ))
740 --------------------------------------------------- ||#
741 ))
742 #|| NULL argument is acceptable...2012/6/28
743 (when (and behavioural (not hidden?))
744 (with-output-chaos-error ('invalid-op-decl)
745 (format t "behavioural operator must have exactly one hidden sort in its arity")
746 ))
747 ||#
667 (setf hidden? t)))
748668 (when (and behavioural coherent)
749669 (with-output-chaos-error ('invalid-op-decl)
750670 (format t "coherency is meaningless for behavioural operator.")
751671 ))
752672 (when (and coherent (not (some #'(lambda (x) (sort-is-hidden x)) arity)))
753673 (with-output-chaos-error ('invalid-op-decl)
754 (format t "coherency is only meaningfull for operator with hidden sort in its arity.")
755 ))
756 )
757
674 (format t "coherency is only meaningfull for operator with hidden sort in its arity."))))
758675 ;;
759676 (when *builtin-overloading-check*
760677 (when (check-overloading-with-builtin op-name arity coarity module)
761678 (return-from add-operator-declaration-to-module nil)))
762 ;;
763
679
764680 ;; uses pre-existing operator if it is the apropreate one,
765681 ;; i.e.,
766682 ;; (1) has method with coarity which is in the same connected component.
778694 xcoarity
779695 (module-sort-order mod)))
780696
781 (when *chaos-verbose* ;; *on-operator-debug*
697 (when *chaos-verbose*
782698 (with-output-simple-msg ()
783699 (format t "~&declaring overloading operator ~a : "
784700 (operator-name (opinfo-operator x)))
800716 (push opinfo (module-all-operators mod))
801717 (symbol-table-add (module-symbol-table mod) op-name op)
802718 (when *on-operator-debug*
803 (format t "~&opdecl: created new operator ~a" (operator-name op)))
804
805 )
719 (format t "~&opdecl: created new operator ~a" (operator-name op))))
806720 ;;
807721 (multiple-value-bind (ent? meth)
808722 (add-operator-declaration-to-table opinfo
857771 (unless opinfo (return-from declare-operator-precedence-in-module nil))
858772 (declare-operator-precedence (opinfo-operator opinfo) prec)))
859773
860 #||
861 (defun declare-operator-theory-in-module (op-name number-of-args
862 theory
863 &optional
864 (module
865 *current-module*))
866 (declare (type t op-name)
867 (type fixnum number-of-args)
868 (type op-theory theory)
869 (type module module)
870 (values t))
871 (let ((opinfo (find-operator-or-warn op-name number-of-args module)))
872 (unless opinfo (return-from declare-operator-theory-in-module nil))
873 (declare-operator-theory (opinfo-operator opinfo) theory)))
874 ||#
875
876774 (defun declare-operator-associativity-in-module (op-name number-of-args
877775 assoc
878776 &optional
954852 (declare (type list list-of-method)
955853 (type fixnum arg-pos num-args)
956854 (type hash-table so))
957 ;;
958 ;;(debug-msg ("~%===================================================="))
959 ;;(debug-msg ("~%arg-pos = ~d") arg-pos)
960 ;;(debug-msg ("~%mathods = ~s") list-of-method)
961 ;;;
962855 (if (= 0 num-args)
963856 ;; we assume the signature is regular, thus, constants has only one
964857 ;; declaration and it has no declaration for erro sort.
994887 (push m res))))))
995888 (let ((minimal-methods (get-minimal-methods)))
996889 (declare (type list minimal-methods))
997 ;;(debug-msg ("~%minimal-methods: ~s") minimal-methods)
998890 (let* ((num-entry (length minimal-methods))
999891 (result (make-list num-entry)))
1000892 (declare (type fixnum num-entry)
1004896 (let* ((s-ms (nth x minimal-methods))
1005897 (comparable-methods (find-comparable (car s-ms))))
1006898 (declare (type list s-ms comparable-methods))
1007 ;;(debug-msg ("~%comparable-methods: ~s") comparable-methods)
1008899 (setf (nth x result)
1009900 (cons (cons (car s-ms)
1010901 (if (= arg-pos (1- num-args))
1020911 num-args
1021912 so)
1022913 nil)))))
1023 result)))
1024 )))
914 result))))))
1025915
1026916
1027917 ;;; FIND-OPERATOR-METHOD operator arg-sort-list & optional opinfo-table sort-order
1056946 (when method (return-from find method))
1057947 ))))
1058948 ;; constant. only one method.
1059 method-table
1060 ))
949 method-table))
1061950
1062951 ;;; *****************
1063952 ;;; ADDING NEW METHOD___________________________________________________________
1073962 arity
1074963 coarity
1075964 &optional
1076 (module
1077 (or *current-module* *last-module*))
965 (module (get-context-module))
1078966 constructor
1079967 behavioural
1080968 coherent
1081969 error-operator)
1082970 (declare (type list opinfo arity)
1083971 (type sort-struct coarity)
1084 (type module module)
1085 (type (or null t)
1086 constructor
1087 behavioural
1088 coherent
1089 error-operator)
1090 (values (or null t) method))
972 (type (or null module) module))
1091973 ;;
1092974 (let ((meth nil))
1093975 (dolist (m (opinfo-methods opinfo))
1099981 (not (eq (method-name meth )
1100982 (method-name *beh-equal*)))
1101983 (not (method-is-error-method meth)))
1102 ;; (and meth *on-operator-debug*)
1103984 (with-output-chaos-warning ()
1104985 (format t "the operator of the same rank has already been declared: ")
1105986 (print-next)
1106987 (print-chaos-object meth)
1107988 (print-next)
1108 (format t "~%... ignored.")
1109 ;; (print-next)
1110 ;; (format t "ignoring this one.")
1111 )
1112 #||
1113 (return-from add-operator-declaration-to-table
1114 (values nil meth))
1115 ||#
1116 )
989 (format t "~%... ignored.")))
1117990 (let ((operator (opinfo-operator opinfo)))
1118991 (declare (type operator operator))
1119992 (when (and meth (not (eq (method-module meth) module)))
993 ;; the method is the imported one
1120994 (when (and (not (method-constructor meth))
1121995 constructor)
1122996 (with-output-chaos-warning ()
11471021 (print-simple-mod-name (method-module meth))
11481022 (print-next)
11491023 (princ "but being declared as coherent in ")
1150 (print-simple-mod-name module)
1151 #||
1152 (print-next)
1153 (princ "ignoring this `coherent' attribute.")
1154 ||#
1155 )))
1024 (print-simple-mod-name module))))
11561025 (unless meth
11571026 (setq meth (make-operator-method :name (operator-name operator)
11581027 :arity arity
1159 :coarity coarity
1160 )))
1028 :coarity coarity)))
11611029 (when (eq (method-module meth) module)
11621030 (setf (method-constructor meth) constructor)
11631031 (setf (method-is-behavioural meth) behavioural)
1164 ;; (setf (method-is-coherent meth) coherent)
11651032 (setf (method-is-user-defined-error-method meth)
11661033 error-operator))
11671034 ;;
12441111 (pushnew method (module-beh-attributes module) :test #'eq))
12451112 (if (sort-is-hidden (method-coarity method))
12461113 (pushnew method (module-non-beh-methods module) :test #'eq)
1247 (pushnew method (module-non-beh-attributes module) :test #'eq))
1248 ))
1249 (pushnew method (opinfo-methods opinfo) :test #'eq)
1250 ))
1114 (pushnew method (module-non-beh-attributes module) :test #'eq))))
1115 (pushnew method (opinfo-methods opinfo) :test #'eq)))
12511116
12521117 (defun add-method-to-table-very-fast (opinfo method module)
12531118 (declare (type list opinfo)
12641129 (if (sort-is-hidden (method-coarity method))
12651130 (push method (module-non-beh-methods module))
12661131 (push method (module-non-beh-attributes module)))))
1267 (push method (opinfo-methods opinfo))
1268 )
1132 (push method (opinfo-methods opinfo)))
12691133
12701134 ;;;
12711135 ;;; RECREATE-METHOD
13481212 ;;
13491213 (progn
13501214 (setf (method-theory newmeth) theory)
1351 (compute-method-theory-info-for-matching newmeth)
1352 ))
1215 (compute-method-theory-info-for-matching newmeth)))
13531216 ;;
13541217 newmeth))))
13551218
13781241 (coar2 (method-coarity meth2)))
13791242 (or (sort< coar2 coar1 so)
13801243 (and (sort= coar1 coar2)
1381 (sort-list<= (method-arity meth2) (method-arity meth1) so)))
1382 ))
1244 (sort-list<= (method-arity meth2) (method-arity meth1) so)))))
13831245
13841246 ;;;
13851247 ;;; DELETE-ERROR-OPERATORS-IN
13861248 ;;;
1387 (defun delete-error-operators-in (&optional (module (or *current-module*
1388 *last-module*)))
1249 (defun delete-error-operators-in (&optional (module (get-context-module)))
13891250 (declare (type module module)
13901251 (values t))
13911252 (let ((minfo (module-opinfo-table module))
14031264 (delete-if #'(lambda (x)
14041265 (and (method-is-error-method x)
14051266 (not (method-is-user-defined-error-method x))))
1406 (opinfo-methods opinfo))))
1407 ))
1267 (opinfo-methods opinfo))))))
14081268
14091269 ;;;
14101270 ;;; MAKE-OPERATOR-CLUSTERS-IN
14111271 ;;;
1412 (defun make-operator-clusters-in (&optional (module (or *current-module*
1413 *last-module*)))
1272 (defun make-operator-clusters-in (&optional (module (get-context-module)))
14141273 (declare (type module module)
14151274 (values t))
14161275 (let ((result nil)
14711330 (let ((*print-indent* (+ 2 *print-indent*)))
14721331 (fresh-line)
14731332 (princ "-- the result : ")
1474 (print-chaos-object pre)))
1475 )
1333 (print-chaos-object pre))))
14761334 (push info result))))))
14771335 ;;
14781336 (setf (module-all-operators module)
15061364 ;;;
15071365 (defun method-most-general-no-error (method methods
15081366 &optional
1509 (module (or *current-module*
1510 *last-module*)))
1367 (module (get-context-module)))
15111368 (declare (type method method)
15121369 (type list methods)
15131370 (type module module)
15221379 ;;;
15231380 ;;; SETUP-ERROR-OPERATORS-IN
15241381 ;;; *NOTE* assumption : no error operators are generated in the module yet.
1525 ;;;
1382 ;;; TODO--------
15261383 (defun get-new-error-sort-name-in (module sort-name)
15271384 (declare (type module module)
15281385 (type (or simple-string symbol) sort-name))
15411398 :test #'equal))
15421399 (eval-ast decl)))
15431400
1544 (defun setup-error-operators-in (&optional (module (or *current-module*
1545 *last-module*)))
1401 (defun setup-error-operators-in (&optional (module (or (get-context-module))))
15461402 (declare (type module module)
15471403 (values t))
15481404 (let ((all-error-operators nil))
15551411 (format t "~%[setup-error-operators-in]:BEFORE")
15561412 (format t "~& arity=~s" proto-arity)
15571413 (format t "~& coarity=~s" proto-coarity))
1558 #||
1559 (setq proto-arity
1560 (mapcar #'(lambda (sref)
1561 (if (%is-sort-ref sref)
1562 (let ((name (%sort-ref-name sref)))
1563 (setf (%sort-ref-name sref)
1564 (get-new-error-sort-name-in module name)))
1565 (get-new-error-sort-name-in module sref)))
1566 proto-arity))
1567 (if (%is-sort-ref proto-coarity)
1568 (setf (%sort-ref-name proto-coarity)
1569 (get-new-error-sort-name-in module
1570 (%sort-ref-name proto-coarity)))
1571 (setq proto-coarity
1572 (get-new-error-sort-name-in module proto-coarity)))
1573 (setf (%op-decl-arity eop-decl) proto-arity)
1574 (setf (%op-decl-coarity eop-decl) proto-coarity)
1575 ||#
15761414 (when *on-operator-debug*
15771415 (format t "~%[setup-error-operators-in]: declaring user defind errr op")
15781416 (format t "~% by decl : ") (print-chaos-object eop-decl))
16001438 (print-chaos-object (car opinfo)))
16011439
16021440 ;; avoid generate if there already ...
1603
1604 ;;#||
16051441 (when (some #'(lambda (x)
16061442 (method-is-error-method x))
16071443 (opinfo-methods opinfo))
16081444 (when *on-operator-debug*
16091445 (format t "~% * already exists"))
16101446 (return-from setup-error-operator nil))
1611 ;;||#
16121447 ;;
16131448 (let ((method-info-table (module-opinfo-table module))
16141449 (sort-order (module-sort-order module))
16151450 (pre-errs (module-error-methods module))
1616 (all-errs nil)
1617 )
1451 (all-errs nil))
16181452 ;; NOTE:
16191453 ;; all coarities of methods are in the same connected component.
16201454 (let ((proto-method nil)
16211455 (method-name nil)
16221456 (err-coarity nil)
16231457 (new-arities nil)
1624 (coherent nil)
1625 )
1458 (coherent nil))
16261459 ;;
16271460 (setq proto-method
16281461 (find-if #'(lambda (x) (method-is-universal* x))
16681501 (method-arity meth))))
16691502 (pushnew ar new-arities :test #'equal))
16701503 (setq coherent
1671 (or coherent (method-is-coherent meth)))
1672 )))
1504 (or coherent (method-is-coherent meth))))))
16731505 (dolist (arity new-arities)
16741506 (when *on-operator-debug*
16751507 (format t "~% * try for arity ")
17191551 (compute-method-theory-info-for-matching
17201552 pre
17211553 method-info-table)
1722 (setf (method-is-coherent pre) coherent)
1723 )
1554 (setf (method-is-coherent pre) coherent))
17241555 ;; not yet have, generate a new one.
17251556 (multiple-value-bind (ent? meth)
17261557 (add-operator-declaration-to-table opinfo
17351566 (print-chaos-object meth)
17361567 (format t "~% -- entered? ~a" ent?))
17371568 (when ent?
1738 ;;
17391569 (push meth all-errs)
17401570 (setf (method-theory meth method-info-table)
17411571 *the-empty-theory*
17431573 (method-is-behavioural proto-method))
17441574 (setf (method-is-coherent meth) coherent)
17451575 (compute-method-theory-info-for-matching
1746 meth method-info-table)
1747 )))
1748 ))
1576 meth method-info-table))))))
17491577 ;; returns the list of error operators.
17501578 all-errs)))
17511579
17761604 (setf (method-theory new-meth)
17771605 (method-theory meth))
17781606 (setf (method-theory-info-for-matching new-meth)
1779 (method-theory-info-for-matching meth))
1780 )))
1607 (method-theory-info-for-matching meth)))))
17811608
17821609 (defun make-if-then-else-op (module sort)
17831610 (declare (type module module)
18041631 (setf (method-theory new-meth)
18051632 (method-theory *bool-if*))
18061633 (setf (method-theory-info-for-matching new-meth)
1807 (method-theory-info-for-matching *bool-if*))
1808 )))
1634 (method-theory-info-for-matching *bool-if*)))))
18091635
18101636 (defun setup-if-then-else-in (module)
18111637 (declare (type module module)
18151641 (dolist (es sorts)
18161642 (make-if-then-else-op module es)))))
18171643
1818 #||
1819 (defun setup-sem-relations-in (module)
1820 (when (assq *truth-module* (module-all-submodules module))
1821 (let ((sorts (get-module-top-sorts module)))
1822 (dolist (es sorts)
1823 (if (sort-is-hidden es)
1824 (progn
1825 ;; _=*=_
1826 (make-sem-relation-op module
1827 *beh-equal*
1828 (list es es)
1829 *bool-sort*)
1830 ;; _=b=_
1831 (make-sem-relation-op module
1832 *beh-eq-pred*
1833 (list es es)
1834 *bool-sort*))
1835 (progn
1836 ;; _==_
1837 (make-sem-relation-op module
1838 *bool-equal*
1839 (list es es)
1840 *bool-sort*)
1841 ;; _=/=_
1842 (make-sem-relation-op module
1843 *bool-nonequal*
1844 (list es es)
1845 *bool-sort*)
1846 ))
1847 )))
1848 (when (assq *rwl-module* (module-all-submodules module))
1849 ;; _==>_
1850 (let ((sorts (get-module-top-sorts module)))
1851 (dolist (s sorts)
1852 (make-sem-relation-op module
1853 *rwl-predicate*
1854 (list s s)
1855 *bool-sort*))))
1856 )
1857
1858 ||#
1859
18601644 (defun setup-sem-relations-in (module)
18611645 (declare (type module module)
18621646 (values t))
18681652 (make-sem-relation-op module
18691653 *beh-equal*
18701654 (list es es)
1871 *bool-sort*)
1872 )))))
1655 *bool-sort*))))))
18731656
18741657 (defparameter memb-predicate-name-template
18751658 '("_" ":" 'sort-name))
19421725 (unless (operator-associativity op)
19431726 (if (operator-is-associative op)
19441727 (setf (operator-associativity op)
1945 :right)))
1946 )
1728 :right))))
19471729 ;; set (1) lowers and highers,
19481730 ;; (2) memo property
19491731 ;; (3) match theory.
19601742 (setf (method-overloaded-methods m)
19611743 (compute-overloaded-methods m methods))
19621744 (when (eq (method-module m) *current-module*)
1963 #||
1964 ;; (2) memo is now obsolete
1965 (unless (method-has-memo m)
1966 (setf (method-has-memo m) memo))
1967 ||#
19681745 ;; ** the rewrite strategy for default methods are always eager.
19691746 ;; we set the value here.
19701747 (when (method-is-error-method m)
19891766 (setf (method-theory m method-info-table) theory)
19901767 (compute-method-theory-info-for-matching
19911768 m
1992 method-info-table))
1993 ))
1994 ))
1995 ))
1996
1997 ;; setup method lookup table.
1998 ;; *** NOT USED NOW ***
1999 ;; (setf (opinfo-method-table opinfo)
2000 ;; (make-method-table (opinfo-methods opinfo)
2001 ;; *current-sort-order*))
2002 ;;
2003 #||
2004 ;; compute syntactic properties for each methods.
2005 (compute-method-syntactic-properties opinfo method-info-table)
2006 ;; set syntactic properties for error methods.
2007 (compute-error-method-syntactic-properties opinfo
2008 method-info-table)
2009 ||#
2010 ))
2011 ))))
1769 method-info-table))))))))))))))
20121770
20131771 (defun set-operator-syntactic-properties (module)
20141772 (with-in-module (module)
20181776 (compute-method-syntactic-properties opinfo method-info-table)
20191777 ;; set syntactic properties for error methods.
20201778 (compute-error-method-syntactic-properties opinfo
2021 method-info-table)
2022 ))))
1779 method-info-table)))))
20231780
20241781 (defun make-standard-token-seq (op-name-token number-of-args)
20251782 (declare (type fixnum number-of-args)
20491806 (methods (opinfo-methods opinfo))
20501807 (op-prec (operator-computed-precedence op))
20511808 (op-assoc (operator-associativity op))
2052 (token-sequence (operator-token-sequence op))
2053 )
1809 (token-sequence (operator-token-sequence op)))
20541810 (unless (operator-is-mixfix op)
20551811 ;; operator has a standard application form.
20561812 (setf token-sequence (make-standard-token-seq token-sequence
20681824 ;; assoc theory is interpreted as right-associative
20691825 (if (and (method-is-associative method
20701826 method-info-table)
2071 (null op-assoc)
2072 )
1827 (null op-assoc))
20731828 ':right
2074 op-assoc)))))
1829 op-assoc)))))
20751830 (declare (type fixnum prec lower-prec)
20761831 (type symbol assoc-decl))
20771832 ;;
21111866 gathering (cdr gathering)))
21121867 (t (push (cons 'token cur-item) res)))
21131868 (setq token-seq (cdr token-seq)))
2114 ;;
2115 ; (terpri)
2116 ; (print-chaos-object method)
2117 ; (format t " :form= ~S" form)
2118 ;;
21191869 (setf (method-form method) form))))))
21201870
21211871 (defun compute-error-method-syntactic-properties (opinfo method-info-table)
21591909 (type list token-seq)
21601910 (type symbol assoc-decl)
21611911 (values list))
2162 ;;
2163 ; (terpri)
2164 ; (print-chaos-object method)
2165 ; (format t " : assoc=~S" (method-is-associative method))
2166 ;;
21671912 (if assoc-decl
21681913 (if (eq assoc-decl ':left)
21691914 '(:left :right)
2170 '(:right :left))
1915 '(:right :left))
21711916 ;; if unary prefix use :left not :right
21721917 (if (not (operator-is-mixfix (method-operator method)))
21731918 (mapcar #'(lambda (x) (declare (ignore x)) '&) (method-arity method))
21821927 ;;;
21831928 ;;; CHECK-POLIMORHIC-OVERLODING-IN
21841929 ;;;
2185 (defun check-polimorphic-overloading-in (&optional (module (or *current-module*
2186 *last-module*)))
1930 (defun check-polimorphic-overloading-in (&optional (module (get-context-module)))
21871931 (declare (type module module)
21881932 (values t))
21891933 (with-in-module (module)
23582102 (return-from is-variable)))
23592103 ;; come here if the occ-th argument is a variable,or
23602104 ;; method is maximal. delay the evaluation.
2361 (push (1+ occ) end-strategy)
2362 ))
2105 (push (1+ occ) end-strategy)))
23632106 (setf (method-rewrite-strategy meth)
23642107 (complete-method-strategy meth
23652108 (append (reverse strategy)
23662109 (if (member 0 strategy) nil '(0))
2367 (reverse end-strategy))))
2368 ))))))))
2110 (reverse end-strategy))))))))))))
23692111
23702112 ;;; *NOTE* assumes *current-opinfo-table* is properly bound.
23712113 ;;;
23892131 (setf (method-rules-with-different-top mth)
23902132 (sort (method-rules-with-different-top mth)
23912133 #'method<=
2392 :key #'(lambda (x) (term-head (axiom-lhs x)))))
2393 ;;
2394 ))))
2134 :key #'(lambda (x) (term-head (axiom-lhs x)))))))))
23952135
23962136 (defun propagate-attributes (module)
23972137 (declare (type module module)
24482188 (print-chaos-object (opinfo-operator opinfo))
24492189 (print-next)
24502190 (term-print id) (princ " -- VS. -- ")
2451 (term-print nid)))
2452 )))
2453 )))
2191 (term-print nid)))))))))
24542192 ;;
24552193 (when id
24562194 (let ((idsrt (term-sort id))
24742212 ;;
24752213 (set-method-theory lower
24762214 newthy
2477 #|| set-method-theory calls this
2478 (check-method-theory-consistency
2479 lower
2480 newthy
2481 *current-opinfo-table*
2482 t)
2483 ||#
2484 *current-opinfo-table*
2485 t))
2486 ))
2487 )) ; end unless
2488 ) ; end dolist
2489 ) ; end dolist
2490 )
2491 ))
2492
2215 t)))))))))))
24932216
24942217 ;;; EOF
779779 (defvar .test-term-sort-membership-in-progress. nil)
780780
781781 (defun test-term-sort-membership (term sort-id-const
782 &optional
783 (module (or *current-module*
784 *last-module*)))
782 &optional (module (get-context-module)))
783
785784 (declare (type term term sort-id-const))
786785 (unless module
787786 (with-output-chaos-error ('no-context)
7373 modexp)))
7474 (let ((mod nil)
7575 (me (normalize-modexp modexp)))
76 (when (and (equal me "THE-LAST-MODULE")
77 *last-module*)
78 (return-from eval-modexp
79 (if reconstruct-if-need
80 (reconstruct-module-if-need *last-module*)
81 *last-module*)))
76 ;; "." -> current context module
8277 (when (and (equal me ".")
83 *current-module*)
84 (return-from eval-modexp
85 *current-module*))
78 (get-context-module))
79 (return-from eval-modexp (get-context-module)))
8680 (when (stringp me)
81 ;; simple name
8782 (let ((pos (position #\. (the simple-string me) :from-end t)))
8883 (if pos
8984 (let ((name (subseq (the simple-string me) 0 (the fixnum pos)))
9489 (if (modexp-is-error context)
9590 (with-output-chaos-error ('no-such-module)
9691 (format t "Could not evaluate modexpr ~a, " me)
97 (format t " no such module ~a" qual)
98 )
92 (format t " no such module ~a" qual))
9993 (setf mod (find-module-in-env name context))))
10094 (setq mod (find-module-in-env me (if also-local
101 *current-module*
95 (get-context-module)
10296 nil))))))
10397 (if mod
10498 (if reconstruct-if-need
111105 (declare (special *on-autoload*))
112106 (!input-file (cdr ent)))
113107 (setq mod (find-module-in-env me (if also-local
114 *current-module*
108 (get-context-module)
115109 nil)))
116110 (if mod
117111 mod
161155
162156 ;; Internal Error!
163157 (t (with-output-chaos-error ('invalid-modexp)
164 (format t "bad modexp form ~s" modexp)))
165 ))
158 (format t "bad modexp form ~s" modexp)))))
166159
167160 ;;; ************************
168161 ;;; SPECIFIC MODULE CREATORS____________________________________________________
177170 (declaim (special *copy-variables*))
178171 (defvar *copy-variables* nil)
179172
180 #||
181 (defun create-renamed-module (mod name)
182 (let ((*beh-proof-in-progress* t)
183 (*copy-variables* t))
184 (let ((newmod (eval-ast (%module-decl* (normalize-modexp name)
185 (module-kind mod)
186 :user
187 (list (%import* :using mod))))))
188 (add-modexp-defn (module-name newmod) newmod)
189 (compile-module newmod)
190 newmod)
191 ))
192 ||#
193173 (defun create-renamed-module (mod name)
194174 (let ((*beh-proof-in-progress* t)
195175 (*copy-variables* t)
201181 (import-module newmod :using mod)
202182 (add-modexp-defn (module-name newmod) newmod)
203183 (compile-module newmod)
204 newmod)
205 ))
184 newmod)))
206185
207186 (defun create-renamed-module-2 (mod name context-module)
208187 (let ((*copy-variables* t)
214193 (incorporate-module-copying newmod mod t nil context-module)
215194 (add-modexp-defn (module-name newmod) newmod)
216195 (compile-module newmod)
217 newmod)
218 ))
196 newmod)))
219197
220198 ;;; ***********
221199 ;;; CREATE-PLUS : Modexp -> Module
264242 ;;; ********************
265243 ;;; *NOTE* apply-modmorph must use memo tables since mapping may affect
266244 ;;; sub-modules (e.g. with "protecting A[X <= Y]")
267 ;;;
268 ;;;
269 #||
245
270246 (defun create-instantiation (modexp)
271247 (flet ((report-error (&rest ignore)
272248 (declare (ignore ignore))
288264 (with-output-chaos-error ('modexp-err)
289265 (princ "Unknown parameterized module in instantiation: ")
290266 (when (modexp-is-error modpar)
291 (princ (cdr modpar))
292 ))
293 )
294 #||
295 (when (eq *current-module* modpar)
296 (with-output-chaos-error ('modexp-eval)
297 (princ "module ")
298 (print-mod-name *current-module*)
299 (princ "cannot instantiate itself")
300 ))
301 ||#
267 (princ (cdr modpar)))))
302268 (unless (get-module-parameters modpar)
303269 (with-output-chaos-error ('modexp-eval)
304270 (princ "module ")
305271 (print-mod-name modpar)
306 (princ " has no parameters.")
307 ))
308 ;;
309 (let ((args (do ((r (%instantiation-args modexp) (cdr r))
310 (res nil))
311 ((null r) (nreverse res))
312 (push (eval-view-arg (car r)
313 modpar)
314 res))))
315 (let ((name (make-int-instantiation :module modpar
316 :args args))
317 (mappg (views-to-modmorph modpar args)))
318 (let ((module (apply-modmorph name mappg modpar)))
319 ;; (setf (module-name module) name) ; name is set by apply-modmorph.
320 (setf (module-decl-form module) modexp)
321 module)))))))))
322 ||#
323
324 (defun create-instantiation (modexp)
325 (flet ((report-error (&rest ignore)
326 (declare (ignore ignore))
327 (with-output-msg ()
328 (princ "could not evaluate instantiation: ")
329 (print-modexp modexp *standard-output* t t)
330 (chaos-error 'modexp-error))))
331 (with-chaos-error (#'report-error)
332 (cond ((int-instantiation-p modexp) ; evaluated internal modexp.
333 (let ((mappg (views-to-modmorph (int-instantiation-module modexp)
334 (int-instantiation-args modexp))))
335 (apply-modmorph modexp mappg (int-instantiation-module modexp))))
336 (t ; not yet evaluated, build from the
337 ; scratch.
338 (let* ((*auto-context-change* nil)
339 ;; parameter module must be a global
340 (modpar (eval-modexp (%instantiation-module modexp))))
341 (unless (module-p modpar)
342 (with-output-chaos-error ('modexp-err)
343 (princ "Unknown parameterized module in instantiation: ")
344 (when (modexp-is-error modpar)
345 (princ (cdr modpar))
346 ))
347 )
348 (unless (get-module-parameters modpar)
349 (with-output-chaos-error ('modexp-eval)
350 (princ "module ")
351 (print-mod-name modpar)
352 (princ " has no parameters.")
353 ))
272 (princ " has no parameters.")))
354273 ;;
355274 (let ((args nil)
356275 (mappg nil))
411330 type
412331 (if (eq type :visible)
413332 "sort"
414 "hsort")
415 )
416 ))
333 "hsort"))))
417334 sort))
418335
419336 (defun create-rename (modexp)
10561056 (defparameter *import-rwl-ast*
10571057 (%import* :protecting (%modexp* "RWL")))
10581058
1059 (defun include-rwl (&optional (module (or *current-module* *last-module*)))
1059 (defun include-rwl (&optional (module (get-context-module)))
10601060 (when *include-rwl*
10611061 (unless (module-includes-rwl module)
10621062 (with-in-module (module)
1063 (eval-import-modexp *import-rwl-ast*)
1064 )))
1065 )
1063 (eval-import-modexp *import-rwl-ast*)))))
10661064
10671065 ;;;
10681066 ;;; IMPORT-VARIABLES
134134 (return-from import-module-internal nil))
135135 (when (eq module submodule)
136136 (with-output-chaos-error ('invalid-import)
137 (princ "module cannot import itself!")
138 ))
137 (princ "module cannot import itself!")))
139138
140139 ;; compile submodule if need
141140 (compile-module submodule)
150149 (nm (cdr al)))
151150 (add-module-alias module mod nm)))
152151 ;;
153 #||
154 (when (and *include-bool*
155 (assq *bool-module*
156 (module-all-submodules submodule)))
157 (include-bool module))
158 ||#
159 ;;
160152 (with-in-module (module)
161 ;;
162 ;;
163153 (if parameter
164154 ;; PARAMETERIZED MODULE IMPORTATION.
165155 ;; We carete a new module with name `(formal-name "::" module-object)'
207197 (progn
208198 (when *on-import-debug*
209199 (format t "~&module is already imported, skipping.."))
210 (return-from import-module-internal t)
211 ))
212 ))
200 (return-from import-module-internal t)))))
213201 (when *check-import-mode*
214202 ;; other more complex importation check.
215203 ;; checks confliction among shared submodules.
277265 (add-imported-module newmod (cdr par)(cdar par))
278266 (incorporate-module newmod (cdr par) (cdar par)))
279267 (unless *chaos-quiet* (princ ")" *error-output*))
280 newmod)))
281 ))))
268 newmod)))))))
282269
283270 ;;; INCORPORATE-MODULE : Module Mode SubModule -> Module'
284271 ;;; Do the importation.
334321 :test #'eq))
335322 (let ((opinfos (module-all-operators submodule)))
336323 (dolist (opinfo opinfos)
337 (transfer-operator module submodule opinfo nil theory-mod))
338 )
339 ;; #||
324 (transfer-operator module submodule opinfo nil theory-mod)))
340325 ;; import error operators which might be reused.
341326 ;; (dolist (em (module-error-methods submodule))
342327 ;; (when (method-is-user-defined-error-method em)
343328 ;; (pushnew em (module-error-methods module) :test #'eq)))
344329 (dolist (em (module-error-methods submodule))
345330 (pushnew em (module-error-methods module) :test #'eq))
346 ;; ||#
347 ;; user defined error ops -----
348 #||
349 (when (module-error-op-decl submodule)
350 (format t "~&** importing error operator decl.")
351 (setf (module-error-op-decl module)
352 (nconc (module-error-op-decl module)
353 (copy-tree (module-error-op-decl submodule)))))
354 ||#
355 #||
356 (dolist (edecl (module-error-op-decl submodule))
357 (eval-ast edecl))
358 ||#
359331 ;; import macros
360332 (dolist (macro (module-macros submodule))
361333 (add-macro-to-module module macro))
389361 (using-find-sort (_sort)
390362 (or (cdr (assq _sort *import-sort-map*)) _sort))
391363
392 ;; for debug
393 #||
394 (!using-find-sort (_sort)
395 (or (cdr (assq _sort *import-sort-map*))
396 (progn (break) _sort)))
397 ||#
398364 (using-import-var (var)
399365 (let ((nm (variable-name var))
400366 (sort (using-find-sort (variable-sort var))))
408374 (setq val (make-variable-term sort nm))
409375 (when *copy-variables*
410376 (push (cons nm val) (module-variables module)))
411 (push (cons nm val) *import-local-vars*)
412 )))))
377 (push (cons nm val) *import-local-vars*))))))
413378 ;;
414379 (using-find-sort-err (s)
415380 (let ((sort (cdr (assq s *import-sort-map*))))
517482 mode
518483 s
519484 nil
520 (or theory-module submodule)))
521 ))))
485 (or theory-module submodule)))))))
522486 (using-import-subs (smod)
523487 (dolist (s (reverse (module-direct-submodules smod)))
524488 (using-import-sub (car s) (cdr s))))
632596 ;;
633597 ;; import error operator declarations
634598 ;;
635 #||
636 (when (module-error-op-decl submodule)
637 (setf (module-error-op-decl module)
638 (nconc (module-error-op-decl module)
639 (copy-tree (module-error-op-decl submodule)))))
640 ||#
641599 (dolist (eop (module-error-op-decl submodule))
642600 (when *on-import-debug*
643601 (with-output-msg ()
648606
649607 ;;
650608 ;; import variable declarations of error sorts
651 ;;
652 #||
653 (when (module-error-var-decl submodule)
654 (setf (module-error-var-decl module)
655 (nconc (module-error-var-decl module)
656 (copy-tree (module-error-var-decl submodule)))))
657 ||#
609 ;; nothing todo ... NO TODO
610
658611 ;;
659612 ;; copy macros
660613 ;;
666619 ;; (print macro)
667620 (add-macro-to-module module new-macro)))
668621
669 ;;(eval-psort-declaration (module-psort-declaration submodule)
670 ;; module)
671622 ;;
672623 ;; import equations & rules copying
673624 ;;
718669 (to-opinfo (module-opinfo-table module))
719670 (so (module-sort-order module)))
720671 ;; find the method group to be inserted
721 #||
722 (dolist (method (opinfo-methods opinfo))
723 (when (or (method-is-user-defined-error-method method)
724 (and (not (method-is-error-method method))
725 (not (method-is-for-regularity? method from-module))))
726 (setq new-opinfo
727 (dolist (x opinfos nil)
728 (when (or (null (method-arity method))
729 (is-in-same-connected-component*
730 (method-coarity method)
731 (method-coarity (or (cadr (opinfo-methods x))
732 (car (opinfo-methods x))))
733 so))
734 (return x))))
735 (return nil)))
736 ||#
737672 (dolist (method (opinfo-methods opinfo))
738673 (when (and (not (method-is-error-method method))
739674 (not (method-is-for-regularity? method from-module)))
749684 (return nil)))
750685 ;; create new operaotr info if could not find.
751686 (cond (new-opinfo
752 (setq new-op (opinfo-operator new-opinfo))
753 )
687 (setq new-op (opinfo-operator new-opinfo)))
754688 (t
755689 (when *on-import-debug*
756690 (format t "~%* creating new opinfo for operator ~s : "
773707 (when *on-import-debug*
774708 (format t "~&-- importing method ~s : " method)
775709 (print-chaos-object method))
776 ;;
777 #||
778 (when (modexp-add-method-to-table new-opinfo method module)
779 (when *on-import-debug*
780 (format t "~&-- importing method-theory ~s:"
781 (method-theory method from-opinfo))
782 (finish-output *error-output*)))
783 ||#
784710 (modexp-add-method-to-table new-opinfo method module)
785711 (transfer-operator-attributes method module from-module theory-mod)
786712 ;; import axioms
797723 (print-chaos-object method)))
798724 (add-rule-to-method (check-axiom-error-method module rule)
799725 method to-opinfo)
800 (pushnew rule (module-all-rules module) :test #'rule-is-similar?)
801 )
802 )
726 (pushnew rule (module-all-rules module) :test #'rule-is-similar?)))
803727 ;;
804728 (dolist (r (reverse (method-rules-with-different-top method
805729 from-opinfo)))
815739 method to-opinfo)
816740 (pushnew r (module-all-rules module) :test #'rule-is-similar?)))
817741 )))
818
819 ;;
820 #||
821 (dolist (method (reverse (opinfo-methods opinfo)))
822 (when (and ;; (not (method-is-error-method method))
823 (not (method-is-for-regularity? method from-module)))
824 (when *on-import-debug*
825 (format t "~&-- importing method ~s : " method)
826 (print-chaos-object method))
827 ;;
828 ;;#||
829 (when (modexp-add-method-to-table new-opinfo method module)
830 (when *on-import-debug*
831 (format t "~&-- importing method-theory ~s:"
832 (method-theory method from-opinfo))
833 (finish-output *error-output*)))
834 ;; ||#
835 (modexp-add-method-to-table new-opinfo method module)
836 (transfer-operator-attributes method module from-module theory-mod)
837 ;; import axioms
838 (let ((all-rules (module-all-rules module)))
839 (dolist (rule (rule-ring-to-list
840 (method-rules-with-same-top method from-opinfo)))
841 (when (or (not (memq rule all-rules))
842 (eq method (term-head (axiom-lhs rule))))
843 (when *on-import-debug*
844 (with-in-module (from-module)
845 (format t "~%-- importing axiom ")
846 (print-chaos-object rule)
847 (format t "~% for method : ")
848 (print-chaos-object method)))
849 (add-rule-to-method (check-axiom-error-method module rule)
850 method to-opinfo)
851 (pushnew rule (module-all-rules module) :test #'rule-is-similar?)
852 )
853 )
854 ;;
855 (dolist (r (reverse (method-rules-with-different-top method
856 from-opinfo)))
857 (when (or (not (memq r all-rules))
858 (eq method (term-head (axiom-lhs r))))
859 (when *on-import-debug*
860 (with-in-module (from-module)
861 (format t "~%-- importing axiom ")
862 (print-chaos-object r)
863 (format t "~% for method : ")
864 (print-chaos-object method)))
865 (add-rule-to-method (check-axiom-error-method module r)
866 method to-opinfo)
867 (pushnew r (module-all-rules module) :test #'rule-is-similar?)))
868 )))
869 ||#
870 ;;
871742 (when *on-import-debug*
872 (format t "~&* done transfer-operator"))
873 ))
874 ))
743 (format t "~&* done transfer-operator"))))))
875744
876745 (defun modexp-add-method-to-table (opinfo method module)
877746 (let ((pmeth (find method (opinfo-methods opinfo)
881750 (sort= (method-coarity x)
882751 (method-coarity y))))))
883752 (method-info-table (module-opinfo-table module)))
884 (if (eq pmeth method) ; (or (eq pmeth method)
885 ; ;; dirty kludge!
886 ; (and pmeth (method-is-of-same-operator-safe method *rwl-predicate*)))
753 (if (eq pmeth method)
887754 nil
888755 (progn
889756 (setf (get-method-info method method-info-table)
915782 (setf (method-theory method (module-opinfo-table to-module))
916783 new-theory)
917784 (compute-method-theory-info-for-matching method
918 (module-opinfo-table to-module))
919 )))
785 (module-opinfo-table to-module)))))
920786
921787 (defun modexp-merge-operator-theory (method to-module from-module
922788 &optional theory-mod)
937803 (setq meta-demod (method-is-meta-demod meth)))
938804 (with-in-module (to-module)
939805 (setf (method-is-coherent meth) coh)
940 (setf (method-is-meta-demod meth) meta-demod))
941 ))
806 (setf (method-is-meta-demod meth) meta-demod))))
942807
943808 ;;; *****************************************
944809 ;;; AUTOMATIC IMPORATION OF BUILT-IN MODULES.___________________________________
955820 (unless (memq *syntax-err-sort* (module-all-sorts module))
956821 (with-in-module (module)
957822 (eval-import-modexp *import-hard-wired-ast*))))
958
959 #||
960 (defun include-BOOL (&optional (module *current-module*))
961 (when *include-BOOL*
962 (unless (memq *Bool-sort*
963 (module-all-sorts module))
964 (with-in-module (module)
965 (eval-import-modexp *import-bool-ast*))))
966 (include-chaos-module)
967 )
968 ||#
969823
970824 (defun include-BOOL (&optional (module *current-module*))
971825 (when *include-BOOL*
973827 (module-all-submodules module))
974828 (with-in-module (module)
975829 (eval-import-modexp *import-bool-ast*))))
976 (include-chaos-module)
977 )
830 (include-chaos-module))
978831
979832 (defparameter *import-object-ast*
980833 (%import* :extending (%modexp* "OBJECT")))
982835 (defun include-object ()
983836 (unless (memq *class-id-sort*
984837 (module-all-sorts *current-module*))
985 (eval-import-modexp *import-object-ast*)
986 ))
838 (eval-import-modexp *import-object-ast*)))
987839
988840 (defparameter *import-record-ast*
989841 (%import* :extending (%modexp* "RECORD-STRUCTURE")))
996848 (defparameter *import-rwl-ast*
997849 (%import* :protecting (%modexp* "RWL")))
998850
999 (defun include-rwl (&optional (module (or *current-module* *last-module*)))
851 (defun include-rwl (&optional (module (get-context-module)))
1000852 (when *include-rwl*
1001853 (unless (module-includes-rwl module)
1002854 (with-in-module (module)
1003 (eval-import-modexp *import-rwl-ast*)
1004 )))
1005 )
855 (eval-import-modexp *import-rwl-ast*)))))
1006856
1007857 ;;;
1008858 ;;; IMPORT-VARIABLES
1018868 (with-output-chaos-warning ()
1019869 (format t "importing variable ~a, could not find sort ~a"
1020870 name
1021 (sort-id (variable-sort v)))))))
1022 ))
871 (sort-id (variable-sort v)))))))))
1023872
1024873 ;;; EOF
103103 ;; some cases the real module compilation is not done
104104 ;; while evaluating modexprs, and we also want
105105 ;; psort-declaration for consistency.
106 (setf (module-principal-sort newmod) s-mapped)
107 ))
108 )
106 (setf (module-principal-sort newmod) s-mapped))))
109107 ;;
110108 (when *chaos-verbose* (princ "[")) ; now we begin.
111109 (when *on-modexp-debug*
199197 (inherit-principal-sort x sortim)
200198 (unless (eq x sortim)
201199 (push (cons x sortim) sortmap)
202 (setf (modmorph-sort map) sortmap))
203 )
204 (inherit-principal-sort x x))
205 )))
206 )))
200 (setf (modmorph-sort map) sortmap)))
201 (inherit-principal-sort x x))))))))
207202 ;;
208203 (if *chaos-verbose*
209204 (print-in-progress "s") ; done mapping sorts
229224 (with-output-msg ()
230225 (format t " generating error sorts")))
231226 (generate-err-sorts so)
232 (setq no-error-sort t)
233 )
227 (setq no-error-sort t))
234228 ;;
235229 (if *chaos-verbose*
236230 (print-in-progress "<") ; done mapping sort relations
255249 (not (memq method
256250 (module-methods-for-regularity mod)))))
257251 (unless (assq method opmap)
258 (modmorph-recreate-method mod newmod sortmap method))
259 )))
252 (modmorph-recreate-method mod newmod sortmap method)))))
260253 ;;
261254 (if *chaos-verbose*
262255 (print-in-progress "o") ; done mapping operators
292285 (print-in-progress ","))
293286
294287 ;; THEOREMS ---------------------------------------------------------
295 #|| NO YET
296 (setf (module-theorems newmod)
297 (append
298 (mapcar #'(lambda (r)
299 (modmorph-recreate-axiom newmod sortmap
300 opmap modmap r))
301 (module-theorems mod))
302 (module-theorems newmod)))
303 ||#
288 ;; NO YET
304289
305290 ;; OK we've done, nothing to be done here already.
306291 ;;
325310 (modmorph-update-theory mod map opinfo))
326311 (propagate-attributes mod)
327312 (update-parse-information mod)
328 (mark-module-ready-for-parsing mod)
329 )
313 (mark-module-ready-for-parsing mod))
330314
331315 (defun fix-operator-mapping (mod map)
332316 (let ((opmap (modmorph-op map))
357341 (t nil))))
358342 opmap)))
359343
360 #||
361 (defun modmorph-find-error-method (module method opmap &optional sortmap)
362 (declare (type module module)
363 (type method method)
364 (type list opmap sortmap)
365 (values (or null method)))
366 (or (car (memq method (module-error-methods module)))
367 (let* ((alen (length (method-arity method)))
368 (opinfos (find-operators-in-module (method-symbol method)
369 alen
370 module)))
371 (declare (type fixnum alen)
372 (type list opinfos))
373 ;;
374 (unless opinfos
375 (let* ((name (method-symbol method))
376 (mapped? (find-if #'(lambda (x)
377 (and (equal (method-symbol
378 (the method (car x)))
379 name)
380 (= (the fixnum
381 (length (method-arity (car x))))
382 alen)))
383 opmap)))
384 (when mapped?
385 ;; (method :simple-map . method)
386 ;; (mehtod :replacement pvars term)
387 (setq name (if (memq (second mapped?)
388 '(:simple-map :simple-error-map))
389 (method-symbol (the method (cddr mapped?)))
390 (method-symbol (term-head (cadddr mapped?)))))
391 (setq opinfos (find-operators-in-module name alen module)))))
392 ;;
393 (let ((opinfo nil)
394 (err-method nil))
395 (let* ((ar (mapcar #'(lambda (x)
396 (declare (type sort* x))
397 (if (err-sort-p x)
398 (find-compatible-err-sort x module sortmap)
399 x))
400 (method-arity method)))
401 #||
402 (ar-names (mapcar #'(lambda(x)
403 (declare (type sort* x))
404 (sort-id x))
405 ar))
406 ||#
407 (cr (if (err-sort-p (method-coarity method))
408 (find-compatible-err-sort (method-coarity method)
409 module
410 sortmap)
411 (method-coarity method)))
412 #||
413 (cr-name (sort-id cr))
414 ||#
415 )
416 (declare (type sort* cr))
417 (block find-method
418 (dolist (oi opinfos)
419 (declare (type list oi))
420 (dolist (cand (opinfo-methods oi))
421 (declare (type method cand))
422 (when (and (sort-list= ar (method-arity cand))
423 (sort= cr (method-coarity cand)))
424 (setq opinfo oi)
425 (setq err-method cand)
426 (return-from find-method nil))
427 )))
428 ;;
429 (unless opinfo
430 ;; failed!....
431 ;; this means we need error method which are not generated
432 ;; yet. -- really?
433 ;; (break)
434 (let ((arity (mapcar #'(lambda (x)
435 (declare (type sort* x))
436 (if (err-sort-p x)
437 (let ((compo
438 (err-sort-components x)))
439 (mapcar #'(lambda(y)
440 (modmorph-assoc-image
441 sortmap
442 y))
443 compo))
444 (list (modmorph-assoc-image
445 sortmap
446 x))))
447 ar))
448 (coarity (let ((c cr))
449 (if (err-sort-p c)
450 (let ((compo (err-sort-components c)))
451 (mapcar #'(lambda (s)
452 (modmorph-assoc-image sortmap s))
453 compo))
454 (list (modmorph-assoc-image sortmap c)))))
455 (so (module-sort-order module)))
456 (declare (type sort-order so))
457 ;;
458 ;; (break)
459 ;;
460 (when (block
461 find-opinfo
462 (dolist (oi opinfos)
463 (declare (type list oi))
464 (let ((mm (opinfo-methods oi)))
465 (dolist (m mm)
466 (declare (type method m))
467 (block try1
468 (let ((xarity (method-arity m))
469 (xcoarity (method-coarity m)))
470 (declare (type list xarity)
471 (type sort* xcoarity))
472 (dotimes (pos (length xarity))
473 (declare (type fixnum pos))
474 (unless (some #'(lambda (y)
475 (declare (type sort* y))
476 (sort<= (the sort*
477 (nth pos xarity))
478 y
479 so))
480 (nth pos arity))
481 (return-from try1 nil)))
482 (unless (some #'(lambda (y)
483 (declare (type sort* y))
484 (sort<= xcoarity y so))
485 coarity)
486 (return-from try1 nil))
487 (setq opinfo oi)
488 (return-from find-opinfo t))
489 )))))
490 ;;
491 (setup-error-operator opinfo module)
492 (setq err-method (car (opinfo-methods opinfo)))
493 )))
494 )
495 ;;
496 (when *on-modexp-debug*
497 (format t "~%-- finding error method for : ")
498 (print-chaos-object method)
499 (format t "~% found : ")
500 (print-chaos-object err-method))
501 ;;
502 err-method))))
503 ||#
504
505344 (defun modmorph-find-mapped-sorts (module sort-l sortmap)
506345 (mapcar #'(lambda (x)
507346 (declare (type sort* x))
533372 (method-has-memo to) memo
534373 (method-associativity to) assoc
535374 (method-constructor to) constr)
536 (set-method-theory to theory)
537 ))
538 ))
375 (set-method-theory to theory)))))
539376
540377
541378 (defun modmorph-find-user-defined-error-method (method module sortmap)
568405 sortmap))
569406 (cr (car (modmorph-find-mapped-sorts module
570407 (list (method-coarity method))
571 sortmap)))
572 )
408 sortmap))))
573409 (declare (type sort* cr))
574410 (block find-method
575411 (dolist (oi opinfos)
581417 (sort= cr (method-coarity cand)))
582418 (setq opinfo oi)
583419 (setq err-method cand)
584 (return-from find-method nil))
585 )))
420 (return-from find-method nil)))))
586421 ;;
587422 (unless opinfo
588423 ;; failed!....
641476 coarity)
642477 (return-from try1 nil))
643478 (setq opinfo oi)
644 (return-from find-opinfo t))
645 )))))
479 (return-from find-opinfo t)))))))
646480 ;;
647481 (setup-error-operator opinfo module)
648 (setq err-method (car (opinfo-methods opinfo)))
649 )
482 (setq err-method (car (opinfo-methods opinfo))))
650483 (unless err-method
651484 ;; this means that the original method should be an
652485 ;; user defined error-method...
656489 (err-sort-p x))
657490 ar)
658491 (err-sort-p coarity))
659 ;; so bad ...
660 #||
661 (with-output-panic-message ()
662 (format t "well ... could not find proper error method for ")
663 (print-chaos-object method))
664 ||#
665492 (with-output-chaos-warning ()
666493 (format t "well ... could not find proper error method for ")
667494 (print-chaos-object method))
668 (return-from modmorph-find-proper-error-method method)
669 )
495 (return-from modmorph-find-proper-error-method method))
670496 ;; we declare err-method
671 ;; (format t "~&declaring new error method...")
672497 (multiple-value-bind (o m)
673498 (declare-operator-in-module
674499 (method-symbol method)
680505 nil
681506 t) ; error method?
682507 (declare (ignore o))
683 (setq err-method m))
684 ) ; end case no err-method
685 )
686 ) ; end case no op-info
687 )
508 (setq err-method m))) ; end case no err-method
509 )))
688510 ;;
689511 (when *on-modexp-debug*
690512 (format t "~%-- finding error method for : ")
744566 ; user defined one.
745567 (modmorph-find-user-defined-error-method method
746568 module
747 sortmap)))
748 )))
569 sortmap))))))
749570
750571
751572 (defun replace-error-method (mod term op-map sort-map)
789610 (some #'(lambda (x)
790611 (or (modmorph-module-is-mapped modmap (car x))
791612 (modmorph-submodule-is-mapped modmap (car x))))
792 (module-submodules mod))
793 )
613 (module-submodules mod)))
794614
795615 ;;;=============================================================================
796616 ;;; MOD-MORPH-IMPORT-SUBMODULES : MODULE NEW-MODULE MAP
834654 ;;
835655 (if (eq ':using mode)
836656 (modmorph-import-submodules mod newmod map submodule-image)
837 #||
838 (if (module-is-parameter-theory submodule-image)
839 (let* ((mod-name (module-name submodule-image))
840 (formal-name (first mod-name))
841 (real-sub (third mod-name)))
842 (import-module newmod mode real-sub formal-name))
843 (import-module newmod mode submodule-image))
844 ||#
845 (import-module newmod mode submodule-image)
846 )
847 ))
657 (import-module newmod mode submodule-image))))
848658
849659 ;;;-----------------------------------------------------------------------------
850660 ;;; MODMORPH-MAP-SUBMODULE
891701 args)))
892702 (let ((new-name (%instantiation* smod args)))
893703 ;; * * *
894 (apply-modmorph (normalize-modexp new-name) map smod)
895 )))
704 (apply-modmorph (normalize-modexp new-name) map smod))))
896705 ;;
897706 (t (let ((nm (modmorph-construct-name map
898707 ;; (module-name smod)
951760 ,(parameter-module-context smod))))
952761 (t (normalize-modexp
953762 (%rename* s-name
954 (%rename-map (modmorph-name map)))))
955 )))
763 (%rename-map (modmorph-name map))))))))
956764 (t (let ((*modmorph-expanded* nil))
957765 (let ((val (modmorph-reconstruct-name map
958766 (if (module-p smod)
968776 ;;; want result in canonical form
969777 (defun modmorph-reconstruct-name (map me)
970778 (when *on-modexp-debug*
971 (format t "~%[modmorph-reconstruct-name]:")
972 #||
973 (format t "~%-- given map ")
974 (print-mapping map)
975 (format t "~%-- given modexp ")
976 (print-chaos-object me)
977 ||#
978 )
779 (format t "~%[modmorph-reconstruct-name]:"))
979780 ;;
980781 (when (modexp-is-?name? me)
981782 (when *on-modexp-debug*
1107908 (setf (view-decl-form view) (view-decl-form me))
1108909 view))
1109910 ;;
1110 (t (break "modmorph-reconstruct-name: missing case"))
1111 ))
911 (t (break "modmorph-reconstruct-name: missing case"))))
1112912
1113913 (defun target-of-view-arg (vw)
1114914 (when (modexp-is-?name? vw)
1117917 ((module-p vw) vw)
1118918 ((view-p vw) (view-target vw))
1119919 ((%is-view vw) (%view-target vw))
1120 (t (break "target-of-view-arg: unknown view argument"))
1121 ))
920 (t (break "target-of-view-arg: unknown view argument"))))
1122921
1123922 (eval-when (:execute :compile-toplevel :load-toplevel)
1124923 (declaim (type fixnum *anon-view-name*))
1167966 (when *on-modexp-debug*
1168967 (format t "~&*result view=")
1169968 (print-chaos-object view))
1170 (%!arg* arg-name view)))
1171 ))
969 (%!arg* arg-name view)))))
1172970
1173971 (defun modmorph-reconstruct-view-sort-mapping (mod map s-maps)
1174972 (declare (ignore mod))
14101208 (string (sort-id s1)))
14111209 (print-chaos-object module)
14121210 ;; (break)
1413 (return-from modmorph-sort-image nil)))
1414 )))
1415 )))
1211 (return-from modmorph-sort-image nil)))))))))
14161212
14171213 (defun modmorph-sorts-image (module sortmap sortlist)
14181214 (mapcar #'(lambda (x) (modmorph-sort-image module sortmap x))
14491245 op-symbol
14501246 arity
14511247 coarity
1452 sortmap))
1453 ))))
1248 sortmap))))))
14541249
14551250 (defun modmorph-recreate-method-aux-1 (oldmodule module
14561251 method
14581253 arity
14591254 coarity
14601255 sort-map)
1461 (recreate-method oldmodule method module op-symbol arity coarity sort-map)
1462 )
1256 (recreate-method oldmodule method module op-symbol arity coarity sort-map))
14631257
14641258 (defun modmorph-recreate-method-aux-2 (oldmodule module sortmap method)
14651259 (declare (ignore sortmap))
14941288 (cdr idinf)))))
14951289 thy)))
14961290 (t thy)))
1497 (compute-method-theory-info-for-matching method minfo))
1498 ) ; dolist
1291 (compute-method-theory-info-for-matching method minfo))) ; dolist
14991292 )))
15001293
15011294 ;;; TERMS
15361329 (when *on-modexp-debug*
15371330 (format t "~& variable not found in *modmorph-local-vars*"))
15381331 (push new-var *modmorph-local-vars*)
1539 new-var))))
1540 ))
1332 new-var))))))
15411333 (t (let ((head (term-head term))
15421334 (new-head nil))
15431335 ;; look in the mapping
15861378 (method-arity head))
15871379 (modmorph-sort-image lookmod
15881380 sortmap
1589 (method-coarity head)))
1590 ))))))
1381 (method-coarity head)))))))))
15911382 ;;
15921383 (unless new-head
15931384 (with-output-chaos-error ('no-such-operator)
16131404 modmap
16141405 tm))
16151406 (term-subterms term))
1616 module))
1617 )))))
1407 module)))))))
16181408
16191409 ;;; AXIOMS
16201410
16701460 (if (atom (car nm2)) (list nm2) nm2))
16711461 (modmorph-merge-assoc (modmorph-sort m1) (modmorph-sort m2) warn)
16721462 (modmorph-merge-op-assoc (modmorph-op m1) (modmorph-op m2) warn)
1673 (modmorph-merge-assoc (modmorph-module m1) (modmorph-module m2) warn))
1674 ))
1463 (modmorph-merge-assoc (modmorph-module m1) (modmorph-module m2) warn))))
16751464
16761465 (defun modmorph-merge-assoc (a1 a2 &optional warn)
16771466 (let ((res a2))
16961485 (print-chaos-object (cdr m))
16971486 (print-next)
16981487 (print-chaos-object (cdr im)))
1699 )))
1700 ;; (push (cons (car m) (cdr im)) res)
1701 )
1702 (push m res))
1703 ))
1704 res
1705 ))
1488 ))))
1489 (push m res))))
1490 res))
17061491
17071492 (defun modmorph-op-map-is-ident (map)
17081493 (if (eq :simple-map (second map))
17421527 (print-chaos-object (caddr m))
17431528 (print-next)
17441529 (print-chaos-object (caddr m)))))
1745 )))
1746 ;; (push (cons (car m) (cdr im)) res)
1747 )
1748 (push m res))
1749 ))
1750 res
1751 ))
1530 ))))
1531 (push m res))))
1532 res))
17521533
17531534 ;; im1 & im2 are of the form
17541535 ;;; (:simple-map . method) -- or --
18261607 ;;;
18271608 (defvar .mapping-debug. nil)
18281609
1829 (defun mapping-image (term-list term &optional (module (or *current-module*
1830 *last-module*)))
1610 (defun mapping-image (term-list term &optional (module (get-context-module)))
18311611 (when .mapping-debug.
18321612 (format t "~&[mapping-image] term = ")
18331613 (print-chaos-object term)
18461626 (term-head term)
18471627 (mapcar #'(lambda (st) (mapping-image term-list st))
18481628 (term-subterms term))
1849 module)
1850 )))
1629 module))))
18511630
18521631 (defun mapping-image-2 (map term_list term)
18531632 (cond ((term-is-variable? term)
18861665 (term-subterms term))
18871666 (if (module-p as)
18881667 as
1889 om))
1890 ))))
1668 om))))))
18911669
18921670 ;;;
18931671 (defun view-get-image-of-axioms (view)
18191819 ;;;
18201820 (defvar .mapping-debug. nil)
18211821
1822 (defun mapping-image (term-list term &optional (module (or *current-module*
1823 *last-module*)))
1822 (defun mapping-image (term-list term &optional (module (get-context-module)))
18241823 (when .mapping-debug.
18251824 (format t "~&[mapping-image] term = ")
18261825 (print-chaos-object term)
137137 (fresh-all)
138138 (flush-all)
139139 (format t "~&[")
140 (if *last-module*
141 (print-simple-mod-name *last-module*)
140 (if (get-context-module)
141 (print-simple-mod-name (get-context-module))
142142 (princ "*"))
143 (princ "]> ")
144 ))
143 (princ "]> ")))
145144
146145 (defun handle-chaos-error (val)
147146 (if *chaos-input-source*
217217 body))
218218 *current-module*
219219 hidden )))
220
221 ;;;-----------------------------------------------------------------------------
222 ;;; DECLARE-RECORD
223 ;;;
224 (defun declare-record (record-decl)
225 (I-miss-current-module declare-record)
226 (include-BOOL)
227 (include-RECORD)
228 (let ((rsort (declare-record-in-module *current-module*
229 (%record-decl-name record-decl)
230 (%record-decl-supers record-decl)
231 (%record-decl-attributes
232 record-decl)
233 (%record-decl-hidden record-decl))))
234 (set-needs-parse)
235 (set-needs-rule)
236 rsort))
237
238 ;;;-----------------------------------------------------------------------------
239 ;;; DECLARE-CLASS
240 ;;;
241 (defun declare-class (class-decl)
242 (I-miss-current-module declare-class)
243 (include-BOOL)
244 (include-OBJECT)
245 (let ((csort (declare-class-in-module *current-module*
246 (%class-decl-name class-decl)
247 (%class-decl-supers class-decl)
248 (%class-decl-attributes class-decl)
249 (%class-decl-hidden class-decl))))
250 (set-needs-parse)
251 (set-needs-rule)
252 csort))
253220
254221 ;;;=============================================================================
255222 ;;; OPERATOR, OPERATOR ATTRIBUTES
375342 (set-needs-rule)
376343 meth)
377344 nil))))
378
379 ;;; DECLARE-OPERATOR-ATTRIBUTES : decl -> operator
380 ;;; returns operator if success, otherwise nil.
381 ;;;
382 #||
383 (defun declare-operator-attributes (decl)
384 (I-miss-current-module declare-operator-attributes)
385 ;; *NOTE* qualifier in opref is ignored, is it OK?
386 (let ((name (%opref-name (%opattr-decl-opref decl)))
387 (num-args (%opref-num-args (%opattr-decl-opref decl)))
388 (attr (%opattr-decl-attribute decl)))
389 (let ((opinfo (find-qual-operator-in *current-module* name num-args)))
390 (unless opinfo
391 (with-output-chaos-error ('operator-not-found)
392 (format t "declaring attributes, could not found unique operator ~a."
393 name)
394 ))
395 (let ((op (opinfo-operator opinfo))
396 (memo (%opattrs-memo attr))
397 (theory (%opattrs-theory attr))
398 (assoc (%opattrs-assoc attr))
399 (prec (%opattrs-prec attr))
400 (strat (%opattrs-strat attr)))
401 ;; (when memo (declare-operator-memo-attr op memo))
402 (when memo
403 (with-output-chaos-warning ()
404 (format t "memo attribute is now obsolate.")))
405 (when theory (declare-operator-theory op theory))
406 (when assoc (declare-operator-associativity op assoc))
407 (when prec (declare-operator-precedence op prec))
408 (when strat (declare-operator-strategy op strat))
409 (set-needs-parse)
410 (set-needs-rule)
411 ;; save the declaration form.
412 (push decl (module-opattrs *current-module*))
413 op))))
414 ||#
415345
416346 ;;;=============================================================================
417347 ;;; AXIOMS, VARIABLES
781711 ;;; DECLARE-MODULE : module-declaration-form -> module
782712 ;;;
783713 (defun declare-module (decl)
784 ;; need not *current-module*
785714 (let ((mod nil) ; will bound created module.
786715 (name (%module-decl-name decl))
787716 (kind (%module-decl-kind decl))
828757 ;;
829758 (propagate-module-change modval)
830759 ;;
831 (when (eq modval *last-module*)
832 (setq *last-module* nil)
760 (when (eq modval (get-context-module))
761 (reset-context-module)
833762 (setq recover-same-context t))
834763
835764 (when (eq modval *memoized-module*)
844773 $$term-context nil
845774 $$subterm nil
846775 $$action-stack nil
847 $$selection-stack nil))
848 )
776 $$selection-stack nil)))
849777
850778 ;; process declaration forms.
851779 (setf mod (create-module name))
877805 (let ((real-mod (find-module-in-env name nil)))
878806 (final-setup real-mod)
879807 (if recover-same-context
880 (setq *last-module* real-mod)
881 (if auto-context?
882 (change-context *last-module* real-mod)))
808 (reset-context-module real-mod)
809 (if auto-context?
810 (change-context (get-context-module) real-mod)))
883811 ;;
884812 (unless (module-is-parameter-theory real-mod)
885813 (print-in-progress " done."))
938866 (parameter (%import-parameter decl))
939867 (alias (%import-alias decl))
940868 (new-mod nil))
941 (when (and (%is-modexp modexp)
942 (equal (%modexp-value modexp) "THE-LAST-MODULE"))
943 (setf (%modexp-value modexp) *last-module*))
944 (setf new-mod (import-module *current-module* mode modexp parameter alias))
869 (setf new-mod (import-module (get-context-module) mode modexp parameter alias))
945870 new-mod))
946871
947872 ;;; !ADD-US
948873 ;;;-----------------------------------------------------------------------------
949874 ;;; NOT YET
950
951 #||
952 (defun !add-us (e)
953 ;; expansion top-level-eval
954 (let ((mepars (parse-modexp (third e))))
955 (if (and (consp mepars) (eq 'with (car mepars)))
956 (!add-using-with *current-module* mepars)
957 (let ((val (eval-modexp mepars nil nil)))
958 (if (eq *TRUTH-module* val)
959 (with-output-chaos-warning ()
960 (princ "using TRUTH not allowed, replaced by extending")
961 (print-next)
962 (princ "in module ") (print-mod-name *current-module*)
963 (import-module *current-module* :extending val))
964 (if (eq *current-module* val)
965 (with-output-chaos-warning ()
966 (princ "module cannot use itself (ignored)."))
967 (import-module *current-module* :using val))))
968 )))
969 ||#
970
971 #||
972 ;;; handle using X with A and B
973 (defun !add-using-with (module mepars)
974 (let ((mod (eval-modexp (cadr mepars))))
975 (when (modexp-is-error mod)
976 (with-output-chaos-error ()
977 (princ "cannot evaluate module: ")
978 (print-modexp (cadr mepars))
979 (chaos-to-top)))
980 (let ((submods (let ((*current-module* mod))
981 (mapcar #'(lambda (me)
982 (let ((val (eval-modexp me)))
983 (when (modexp-is-error val)
984 (with-output-chaos-error ()
985 (princ "cannot evaluate module: ")
986 (print-modexp me)
987 (terpri)
988 (chaos-to-top)))
989 val))
990 (if (equal '(nil) (caddr mepars))
991 nil
992 (caddr mepars))))))
993 (incorporate-using-with module mod submods))))
994875
995876 ;;; Labels in Axioms env.
996877 ;;;-----------------------------------------------------------------------------
1010891 (princ "label ")
1011892 (princ l)
1012893 (princ " contains an initial digit (ignored)") (terpri))
1013 (push l res)))
1014 )
1015 (nreverse res)
1016 ))
1017
1018
1019 ;;;=============================================================================
1020 ;;; MISC.
1021
1022 ;;; AS
1023 ;;;-----------------------------------------------------------------------------
1024 ;;; !ADD-AS
1025 ;;;
1026 (defun !add-as (e)
1027 (unless (module-is-prepare-for-parsing *current-module*)
1028 (prepare-for-parsing *current-module*))
1029 (with-in-module (*current-module*)
1030 (let* ((so (module-sort-order *current-module*))
1031 (sort (find-sort-in *current-module* (nth 1 e)))
1032 (tm (parser$parses *current-module* (nth 3 e)
1033 (if sort sort *cosmos*)))
1034 (cnd (parser$parses *current-module* (nth 5 e))))
1035 (when (null sort)
1036 (princ "Unknown sort in sort constraint"))
1037 (when (null tm)
1038 (princ "No parse for term in sort constraint") (terpri))
1039 (when (or (null cnd) (not (null (cdr cnd))))
1040 (princ "No single parse for condition in sort constraint") (terpri))
1041 (if (and tm (not (null (cdr tm))))
1042 (when tm (princ "Term in sort constraint is ambiguous") (terpri))
1043 (when (and tm (null (cdr tm)))
1044 (when (and sort tm)
1045 (unless (sort-order$is-included-in so sort (term$sort (car tm)))
1046 (princ "Specified sort and sort of term incompatible")))
1047 (when (and tm cnd (null (cdr tm)) (null (cdr cnd)))
1048 (unless (subsetp (term$vars (car cnd)) (term$vars (car tm)))
1049 (princ "Condition variables not subset of those in term") (terpri)))
1050 )))
1051 (error "** Error: general sort constraint not currently handled (ignored)")
1052 (terpri)
1053 (princ " ")
1054 (princ "as ")
1055 (simple-princ-open (nth 1 e))
1056 (princ " : ")
1057 (simple-princ-open (nth 3 e))
1058 (princ " if ")
1059 (simple-princ-open (nth 5 e))
1060 (princ " .")
1061 (terpri)
1062 ))
1063
1064 ;;; OP-AS
1065 (defun !add-op-as (e)
1066 ;(!add-sort-constraint
1067 ; (nth 7 e) (nth 5 e) (nth 9 e))
1068 (with-output-chaos-warning ()
1069 (princ "operator assertion being treated simply as a")
1070 (princ " declaration") (print-next)
1071 (princ "for operator: ") (print-simple-princ-open (nth 1 e)) (terpri))
1072 (!add-op
1073 `("op" ,(nth 1 e) ":" ,(nth 3 e) "->" ,(nth 5 e)
1074 ,@(if (equal "." (nth 10 e)) nil (list (nth 10 e)))
1075 "."))
1076 )
1077
1078 ||#
894 (push l res))))
895 (nreverse res)))
1079896
1080897 ;;; EOF
147147 (number-matches nil))
148148 (let ((mod (if modexp
149149 (eval-modexp modexp)
150 *last-module*)))
151 (unless (eq mod *last-module*)
150 (get-context-module))))
151 (unless (eq mod (get-context-module))
152152 (clear-term-memo-table *term-memo-table*))
153153 (if (or (null mod) (modexp-is-error mod))
154154 (if (null mod)
155155 (with-output-chaos-error ('no-context)
156 (princ "no module expression provided and no selected(current) module.")
157 )
156 (princ "no module expression provided and no selected(current) module."))
158157 (with-output-chaos-error ('no-such-module)
159158 (princ "incorrect module expression, no such module ")
160 (print-chaos-object modexp)
161 ))
159 (print-chaos-object modexp)))
162160 (progn
163 (context-push-and-move *last-module* mod)
161 (context-push-and-move (get-context-module) mod)
162 (when *auto-context-change*
163 (change-context (get-context-module) mod))
164164 (with-in-module (mod)
165 (when *auto-context-change*
166 (change-context *last-module* mod))
167165 (!setup-reduction mod)
168166 (setq $$mod *current-module*)
169167 (setq sort *cosmos*)
177175 (when (or (null (term-sort term))
178176 (sort<= (term-sort term) *syntax-err-sort* *chaos-sort-order*))
179177 (return-from perform-reduction* nil))
180 #||
181 (setq term (car (canonicalize-variables (list term) mod)))
182 ||#
183178 (when *rewrite-stepping* (setq *steps-to-be-done* 1))
184179 (when *show-stats*
185180 (setq time2 (get-internal-run-time))
186181 (setf time-for-parse
187182 (format nil "~,3f sec"
188 ;; (/ (float (- time2 time1)) internal-time-units-per-second)
189 (elapsed-time-in-seconds time1 time2)
190 )))
183 (elapsed-time-in-seconds time1 time2))))
191184 (unless *chaos-quiet*
192 ;; (fresh-all)
193185 (flush-all)
194186 (if (eq mode :exec) ; *rewrite-exec-mode*
195187 (format t "~%-- execute in ")
197189 (format t "~%-- execute! in ")
198190 (if (eq mode :red)
199191 (format t "~%-- reduce in ")
200 (format t"~%-- behavioural reduce in "))
201 ))
192 (format t"~%-- behavioural reduce in "))))
202193 (print-simple-mod-name *current-module*)
203194 (princ " : ")
204195 (let ((*print-indent* (+ 4 *print-indent*)))
206197 (term-print-with-sort term))
207198 (flush-all))
208199 ;; ********
209 (reset-target-term term *last-module* mod)
200 (reset-target-term term (get-context-module) mod)
210201 ;; ********
211202 (setq $$matches 0)
212203 (setq time1 (get-internal-run-time))
219210 *cexec-normalize*)
220211 (rewrite-exec term *current-module* mode)
221212 (rewrite term *current-module* mode))))
222 ;;
223 #|| ============= TODO
224 (when (term-op-contains-theory $$term)
225 (reset-reduced-flag $$term)
226 (setq term $$term)
227 (catch 'rewrite-abort
228 (let ((*do-empty-match* nil))
229 (if (and *rewrite-exec-mode*
230 *cexec-normalize*)
231 (rewrite-exec term *current-module* mode)
232 (rewrite term *current-module* mode)))))
233 ||#
234 ;;
235213 (setq res $$term)
236214 (when *mel-sort*
237 (setq res (setq $$term (apply-sort-memb res mod)))
238 )
215 (setq res (setq $$term (apply-sort-memb res mod))))
239216 ;;
240217 (setq time2 (get-internal-run-time))
241218 (setf time-for-reduction
275252 (format t ")~%")
276253 (format t ", ~d memo hits)~%"
277254 *term-memo-hash-hit*)))
278 (flush-all)
279 ;; (terpri)
280 ))
281 ))
282 ))
255 (flush-all)))))))
283256 (context-pop-and-recover))))))
284257
285258 (defun perform-meta-reduction (pre-term &optional modexp mode)
289262 sort)
290263 (let ((mod (if modexp
291264 (eval-modexp modexp)
292 *last-module*)))
265 (get-context-module))))
293266 (if (or (null mod) (modexp-is-error mod))
294267 (if (null mod)
295268 (with-output-chaos-error ('no-context)
298271 (princ "incorrect module expression, no such module ")
299272 (print-chaos-object modexp)))
300273 (progn
301 (context-push-and-move *last-module* mod)
274 (context-push-and-move (get-context-module) mod)
302275 (setq sort *cosmos*)
276 (when *auto-context-change*
277 (change-context (get-context-module) mod)) ;;; what?
303278 (with-in-module (mod)
304 ;;
305 (change-context *last-module* mod)
306 ;;
307 (!setup-reduction mod)
279 (!setup-reduction *current-module*)
308280 (setq $$mod *current-module*)
309281 (setq *rewrite-semantic-reduce*
310282 (and (eq mode :red)
364336 (defun do-parse-term* (preterm &optional modexp)
365337 (let ((mod (if modexp
366338 (eval-modexp modexp)
367 *last-module*)))
339 (get-context-module))))
368340 (unless mod
369341 (with-output-chaos-error ('no-context)
370342 (princ "no module expression provided and no selected(current) module.")))
373345 (princ "incorrect module expression, not such module: ")
374346 (print-chaos-object modexp)))
375347 ;;
376 (context-push-and-move *last-module* mod)
348 (context-push-and-move (get-context-module) mod)
377349 (with-in-module (mod)
378350 (prepare-for-parsing *current-module*)
379351 (let ((*parse-variables* nil))
384356 (!setup-reduction mod)
385357 (setq res (apply-sort-memb res
386358 mod)))
387 (reset-target-term res *last-module* mod)
359 (reset-target-term res *current-module* mod)
388360 ;; ********
389361 (format t "~&")
390362 (term-print-with-sort res *standard-output*)
391 (flush-all)
392 ;; (break "...")
393 #||
394 (when *chaos-verbose*
395 (print-term-tree res t))
396 ||#
397 )))
363 (flush-all))))
398364 (context-pop-and-recover)))
399365
400366 ;;; *TODO*
402368 (declare (ignore mod prompt))
403369 (with-output-simple-msg ()
404370 (princ "sorry, red-loop is not implemented yet.."))
405 (return-from red-loop nil)
406 #||
407 (setq $$trials 1)
408 (setq mod (eval-modexp-top mod))
409 (if (modexp-is-error mod)
410 (with-output-chaos-error ('no-such-module)
411 (princ "undefined module")
412 )
413 (let (in
414 (flag nil)
415 (top-level (at-top-level)))
416 (!setup-reduction mod)
417 (loop
418 (chaos-init)
419 (when (and prompt top-level)
420 (terpri)
421 (print-mod-name mod) (princ "> "))
422 (let ((cur (!set-single-reader '("[" "]" "_"))))
423 (progn
424 (setq in (read-seq-of-term '(|.|)))
425 (!set-reader cur)))
426 (when (null in) (return))
427 (unless top-level
428 (if flag
429 (progn (princ "---------------------------------------")
430 (terpri))
431 (setq flag t)))
432 (perform-reduction in) ; should ...
433 )))
434 :done
435 ||#
436 )
371 (return-from red-loop nil))
437372
438373 (defun check-bad-rules (mod)
439374 (declare (ignore mod))
457392 (defun under-debug-rewrite ()
458393 (or $$trace-rewrite $$trace-rewrite-whole *rewrite-stepping*
459394 *rewrite-count-limit* *rewrite-stop-pattern*))
460
461 #||
462 (defun rewrite-debug-on ()
463 (setf (symbol-function 'apply-one-rule)
464 (symbol-function 'apply-one-rule-dbg)))
465
466 (defun rewrite-debug-off ()
467 (unless (under-debug-rewrite)
468 (setf (symbol-function 'apply-one-rule)
469 (symbol-function 'apply-one-rule-simple))))
470 ||#
471395
472396 (defun rewrite-debug-on () ())
473397 (defun rewrite-debug-off () ())
500424 (if (= len (length count))
501425 (set-rewrite-count-limit num)
502426 (with-output-chaos-error ('invalid-value)
503 (format t "invalid rewrite count limit ~a" count)
504 ))))))
427 (format t "invalid rewrite count limit ~a" count)))))))
505428
506429 (defun set-rewrite-count-limit (num)
507430 (if (integerp num)
563486 (if (or (null pat)
564487 (member pat '(("none") ("off") ("nil") ("null"))))
565488 (set-rewrite-stop-pattern 'none)
566 (let ((mod (or *current-module*
567 *last-module*
489 (let ((mod (or (get-context-module)
568490 (with-output-chaos-error ('no-context)
569 (princ "no context (current) module is specified.")))
570 ))
491 (princ "no context (current) module is specified.")))))
571492 (let* ((*parse-variables* (module-variables mod))
572493 (term (simple-parse mod
573494 pat *cosmos*)))
617538 (when (modexp-is-error mod)
618539 (with-output-chaos-error ('no-such-module)
619540 (format t "incorrect module expression, unknown module?")
620 (print-modexp modexp)
621 ))
541 (print-modexp modexp)))
622542 (describe-module mod)))
623543
624544
630550 ;; (*current-module* nil)
631551 mod)
632552 (setf mod (if (null modexp)
633 *last-module*
553 (get-context-module)
634554 (eval-modexp modexp)))
635555 (when (modexp-is-error mod)
636556 (with-output-chaos-error ('no-such-module)
637557 (princ "incorrect module expression or uknown module")
638 (print-modexp modexp)
639 ))
558 (print-modexp modexp)))
640559 ;;
641560 (unless mod
642561 (with-output-chaos-error ('no-context)
643 (princ "no module to be opened!")
644 ))
562 (princ "no module to be opened!")))
645563 ;;
646564 (unless *chaos-quiet*
647565 (fresh-all)
653571 (!open-module mod)
654572 (unless *chaos-quiet*
655573 (print-in-progress ". done.")
656 (terpri)
657 )
658 ))
574 (terpri))))
659575
660576 (defparameter *module-open-form*
661577 (%module-decl* "%"
672588 (princ "closing this module...") (print-next)
673589 (eval-close-module nil)))
674590 (setq *open-module* mod)
675 (setq *last-before-open* *last-module*)
676 (setq *last-module* mod)
591 (setq *last-before-open* (get-context-module))
677592 (clear-term-memo-table *term-memo-table*)
678593 (let ((*chaos-quiet* t)
679 (*copy-variables* t))
594 (*copy-variables* t)
595 open-mod)
680596 (setf (%module-decl-kind *module-open-form*) (module-kind mod))
681 (setq *last-module* (eval-ast *module-open-form*))
682 (import-module *last-module* :using mod)
683 ;; (import-module *last-module* :including mod)
684 ;; (import-variables mod *last-module*)
685 (compile-module *last-module*))
686 (change-context *last-before-open* *last-module*)
687 *last-module*))
597 (setq open-mod (eval-ast *module-open-form*))
598 (import-module open-mod :using mod)
599 (compile-module open-mod)
600 (change-context *last-before-open* open-mod)
601 open-mod)))
688602
689603 ;;; ************
690604 ;;; CLOSE-MODULE
692606 (defun eval-close-module (&rest ast)
693607 (declare (ignore ast))
694608 (if *open-module*
695 (let ((saved-open *open-module*))
696 (when (and saved-open (equal "%" (module-name saved-open)))
697 ;; (delete-module-all saved-open)
698 ;; discard all resources
699 (initialize-module *open-module*)
700 (setq *open-module* nil))
701 (change-context *last-module* *last-before-open*)
609 (let ((omod (eval-modexp "%")))
610 (initialize-module omod)
611 (when (eq omod (get-context-module))
612 (change-context (get-context-module) *last-before-open*))
702613 (setq *open-module* nil)
703 (when *current-module*
704 (change-current-module *last-module*))
705614 (setq *last-before-open* nil))
706615 (with-output-chaos-warning ()
707 (princ "no module is open.")
708 )))
709
616 (princ "no module is open."))))
710617
711618 ;;; ***********
712619 ;;; SAVE SYSTEM
758665 (print-centering
759666 "* NOTE : DO NOT MODIFY THIS FILE ULESS YOU REALLY KNOW WHAT YOU ARE DOING!."
760667 .fill-space.
761 stream)
762 )
668 stream))
763669 (terpri stream)
764670 (princ "|#" stream)
765671 (format stream "~&(in-package \"CHAOS\")")
862768 (when msg?
863769 (with-output-simple-msg ()
864770 (format t "~&** done restting system.")
865 (force-output)))
866 ))
771 (force-output)))))
867772
868773 ;;; **********
869774 ;;; FULL-RESET
894799 ;;
895800 (when msg?
896801 (print-in-progress " done")
897 (terpri)
898 )
802 (terpri))
899803 (setq *chaos-features* nil)
900804 (setq *open-module* nil)
901805 (setq *last-before-open* nil)
13601264
13611265 ;; operator strictness
13621266 (:strictness
1363 (let ((mod (if *last-module* *last-module*
1364 (if *current-module*
1365 *current-module*
1267 (let ((mod (or (get-context-module)
13661268 (with-output-chaos-error ('no-context)
1367 (princ "no context (current) module.")
1368 )))))
1269 (princ "no context (current) module.")))))
13691270 ;;
13701271 (!setup-reduction mod)
13711272 (with-in-module (mod)
13771278 (check-operator-strictness op mod t))
13781279 (with-output-chaos-error ('no-such-operator)
13791280 (princ "no such operator")
1380 (print-chaos-object parsedop)
1381 ))
1382 ))
1281 (print-chaos-object parsedop)))))
13831282 (check-operator-strictness-whole mod t)))))
13841283
13851284 ;; TRS compatibility
14001299 (format t "~&- rewrite rule")
14011300 (let ((*print-indent* (+ 2 *print-indent*)))
14021301 (print-next)
1403 (print-chaos-object (car r-ms))
1404 )
1302 (print-chaos-object (car r-ms)))
14051303 (format t "~& violates the compatibility,")
14061304 (format t "~& and following operator(s) can possibly be affected:")
14071305 (let ((*print-indent* (+ 2 *print-indent*)))
14111309 (with-output-simple-msg ()
14121310 (format t ">> module is compatible."))))))
14131311 ;;;
1414 ;;;
1415 ;;;
14161312 (:coherency
1417 (let ((mod (if *last-module* *last-module*
1418 (if *current-module*
1419 *current-module*
1313 (let ((mod (or (get-context-module)
14201314 (with-output-chaos-error ('no-context)
1421 (princ "no context (current) module.")
1422 )))))
1315 (princ "no context (current) module.")))))
14231316 ;;
14241317 (!setup-reduction mod)
14251318 (with-in-module (mod)
14311324 (check-operator-coherency op mod t))
14321325 (with-output-chaos-error ('no-such-operator)
14331326 (princ "no such operator")
1434 (print-chaos-object parsedop)
1435 ))
1436 ))
1327 (print-chaos-object parsedop)))))
14371328 (check-operator-coherency-whole mod)))))
14381329 ;;
14391330 ;; SENSIBILITY of the signature
14671358 (pn-check-refinement args))
14681359 ;; unknown
14691360 (t (with-output-chaos-error ('invalid-arg)
1470 (format t "unknown option to check: ~a" (%check-what ast))
1471 )))))
1361 (format t "unknown option to check: ~a" (%check-what ast)))))))
14721362
14731363 ;;; *************
14741364 ;;; TRAM COMPILER
14851375 ;; first we check the context
14861376 (let ((mod (if modexp
14871377 (eval-modexp modexp)
1488 *last-module*)))
1378 (get-context-module))))
14891379 ;;
14901380 (when (or (null mod) (modexp-is-error mod))
14911381 (if (null mod)
15281418 (princ (cadr result)))
15291419 (force-output))
15301420 (progn
1531 (context-push-and-move *last-module* mod)
1421 (context-push-and-move (get-context-module) mod)
15321422 (let ((*print-indent* (+ 4 *print-indent*)))
15331423 (with-in-module (mod)
15341424 (setq $$term (car result))
15451435 (terpri)
15461436 (princ (cadr result)))
15471437 (force-output)
1548 (reset-target-term $$term *last-module* mod)))
1438 (reset-target-term $$term (get-context-module) mod)))
15491439 (context-pop-and-recover)))))
15501440 ;;
15511441 (otherwise (with-output-panic-message ()
15521442 (format t "internal error, unknown tram command ~a"
15531443 command)
1554 (chaos-error 'panic))))
1555 )))
1444 (chaos-error 'panic)))))))
15561445
15571446 ;;; ********
15581447 ;;; AUTOLOAD
15631452 (let ((entry (assoc modname *autoload-alist* :test #'equal)))
15641453 (if entry
15651454 (setf (cdr entry) file)
1566 (push (cons modname file) *autoload-alist*)))
1567 ))
1455 (push (cons modname file) *autoload-alist*)))))
15681456
15691457 ;;; *********************
15701458 ;;; MISC SUPOORT ROUTINES
16111499 (number-matches 0))
16121500 (let ((mod (if modexp
16131501 (eval-modexp modexp)
1614 *last-module*)))
1615 (unless (eq mod *last-module*)
1502 (get-context-module))))
1503 (unless (eq mod (get-context-module))
16161504 (clear-term-memo-table *term-memo-table*))
16171505 (when (or (null mod) (modexp-is-error mod))
16181506 (if (null mod)
16211509 (with-output-chaos-error ('no-such-module)
16221510 (princ "no such module: ")
16231511 (print-chaos-object modexp))))
1624 ;;
1625 (context-push-and-move *last-module* mod)
1512 (context-push-and-move (get-context-module) mod)
1513 (when *auto-context-change*
1514 (change-context (get-context-module) mod))
16261515 (with-in-module (mod)
1627 (when *auto-context-change*
1628 (change-context *last-module* mod))
16291516 (!setup-reduction mod)
16301517 (setq $$mod *current-module*)
16311518 (setq sort *cosmos*)
16551542 (print-simple-mod-name *current-module*)
16561543 (print-check 0 3)
16571544 (princ " : ")
1658 ;; (print-check)
16591545 (let ((*print-indent* (+ 4 *print-indent*)))
16601546 (term-print lhs)
16611547 (print-check 0 4)
16621548 (princ " == ")
1663 ;; (print-check)
16641549 (term-print rhs))
16651550 (flush-all))
1666 ;;
16671551 (setq $$matches 0)
16681552 (setq time1 (get-internal-run-time))
16691553
17131597 (let ((modexp (%inspect-modexp ast))
17141598 mod)
17151599 (setf mod (if (null modexp)
1716 *last-module*
1600 (get-context-module)
17171601 (eval-modexp modexp)))
17181602 (when (modexp-is-error mod)
17191603 (with-output-chaos-error ('no-such-module)
17341618 (modexp (%what-is-module ast))
17351619 (mod nil))
17361620 (setf mod (if (null modexp)
1737 *last-module*
1621 (get-context-module)
17381622 (eval-modexp modexp)))
17391623 (when (modexp-is-error mod)
17401624 (with-output-chaos-error ('no-such-module))
17531637 (modexp (%look-up-module ast))
17541638 (mod nil))
17551639 (setf mod (if (null modexp)
1756 (or *last-module*
1640 (or (get-context-module)
17571641 (with-output-chaos-error ('no-context)
17581642 (format t "~%No context module is set.")))
17591643 (eval-modexp modexp)))
4646 ;;;
4747 (defun modexp-top-level-eval (modexp)
4848 (let ((meparse (parse-modexp modexp)))
49 (if (equal "THE-LAST-MODULE" meparse)
50 (if *last-module*
51 *last-module*
52 (with-output-chaos-error ('no-context)
53 (princ "no context (current) module")
54 ))
55 (eval-modexp-top (normalize-modexp meparse)))
56 ))
49 (eval-modexp-top (normalize-modexp meparse))))
5750
5851 ;;; EVAL-MOD
5952 ;;; similar to MODEXP-TOP-LEVEL-EVAL.
6154 ;;;
6255 (defun eval-mod (toks &optional (change-context *auto-context-change*))
6356 (if (null toks)
64 (if *last-module*
65 *last-module*
57 (or (get-context-module)
6658 (with-output-chaos-error ('no-context)
67 (princ "no selected(current) module.")
68 ))
59 (princ "no selected(current) module.")))
6960 (if (equal '("%") toks)
7061 (if *open-module*
7162 (let ((mod (find-module-in-env (normalize-modexp "%"))))
7465 (princ "could not find % module!!!!")
7566 (chaos-error 'panic)))
7667 (when change-context
77 (change-context *last-module* mod))
68 (change-context (get-context-module) mod))
7869 mod)
7970 (with-output-chaos-warning ()
8071 (princ "no module is opening.")
8879 (if (integerp val)
8980 (let ((nmod (print-nth-mod val))) ;;; !!!
9081 (when change-context
91 (change-context *last-module* nmod))
82 (change-context (get-context-module) nmod))
9283 nmod)
9384 (with-output-chaos-error ('no-such-module)
94 (format t "could not evaluate the modexp ~a" toks)
95 )))
85 (format t "could not evaluate the modexp ~a" toks))))
9686 (with-output-chaos-error ('no-such-module)
9787 (format t "undefined module? ~a" toks)
9888 ))
9989 (progn
10090 (when change-context
101 (change-context *last-module* val))
91 (change-context (get-context-module) val))
10292 val))))))
10393
10494 ;;; what to do with this one?
131121 (sub (nth-sub (1- no) mod)))
132122 (if sub
133123 (when change-context
134 (change-context *last-module* sub))
124 (change-context (get-context-module) sub))
135125 (progn (princ "** Waring : No such sub-module") (terpri) nil))))
136126 ((and (equal "param" it)
137127 (cadr toks)
142132 (param (nth (1- no) params)))
143133 (if param
144134 (when change-context
145 (change-context *last-module* (cdr param)))
135 (change-context (get-context-module) (cdr param)))
146136 (with-output-chaos-error ('no-such-parameter)
147137 (princ "No such parameter")
148138 ))))
149139 ((and (null toks) change-context force?)
150 (when *last-module*
151 (change-context *last-module* nil)))
152 (t (eval-mod toks change-context))
153 )))
140 (when (get-context-module)
141 (change-context (get-context-module) nil)))
142 (t (eval-mod toks change-context)))))
154143
155144 ;;; EOF
143143 ;;;
144144 ;;; SHOW-FMOD*
145145 ;;;
146 (defun show-fmod* (&optional (module (or *last-module*
147 *current-module*)))
146 (defun show-fmod* (&optional (module (get-context-module)))
148147 (let ((trs (get-module-trs module)))
149 ;;
150148 (princ "fmod ")
151149 (print-mod-name module *standard-output* nil t)
152150 (princ " is ")
4747 ;;; ************
4848 ;;; REWRITE RULE : internal use only
4949 ;;; ************
50
51 #||
52 (defterm rewrite-rule (object)
53 :visible (type ; type, either ':equation or ':rule
54 lhs ;
55 rhs
56 condition
57 behavioural
58 id-condition
59 first-match-method
60 next-match-method
61 labels
62 trace-flag)
63 :int-printer print-rule-object
64 :print print-rule-internal)
65 ||#
6650
6751 (defstruct (rewrite-rule (:include object (-type 'rewreite-rule))
6852 (:copier nil)
126110
127111 ;;; Extended rewrite rule
128112 ;;;
129 #||
130 (defterm ex-rewrite-rule (rewrite-rule)
131 :visible (type
132 lhs
133 rhs
134 condition
135 behavioural
136 id-condition
137 first-match-method
138 next-match-method
139 extensions)
140 :int-printer print-rule-object
141 :print print-rule-internal)
142 ||#
143113
144114 (defstruct (ex-rewrite-rule (:include rewrite-rule (-type 'ex-rewrite-rule))
145115 (:copier nil)
194164 ;;; *****
195165 ;;; definition of axiom structure.
196166 ;;;
197 #||
198
199 (defterm axiom (rewrite-rule)
200 :visible (type ; :equation, :rule
201 lhs ; left hand side.
202 rhs ; right hand side.
203 condition ; condition
204 behavioural ; t iff axiom is behavioural
205 )
206 :hidden (kind ; internaly categorized kind name of an
207 ;; ac-extension :
208 ;; a-extensions : these are now local to module.
209 )
210 :int-printer print-axiom-object
211 :print print-axiom-internal
212 )
213
214 ||#
215
216167 (defstruct (axiom (:include rewrite-rule (-type 'axiom))
217168 (:copier nil)
218169 (:constructor make-axiom)
228179 (setf (symbol-function 'is-axiom) (symbol-function 'axiom-p))
229180 )
230181
231 #||
232 (defstruct (axiom-exts (:type list))
233 (ac-extension nil)
234 (a-extensions nil))
235 ||#
236
237182 (defun print-axiom-object (obj stream &rest ignore)
238183 (declare (ignore ignore))
239184 (if *current-module*
246191 (defmacro is-axiom? (*--obj) `(is-axiom ,*--obj))
247192
248193 ;;; Primitive structure accessors ----------------------------------------------
249
250 ;;; (defmacro axiom-lhs (_a) `(%axiom-lhs ,_a))
251 ;;; (defmacro axiom-rhs (_a) `(%axiom-rhs ,_a))
252 ;;; (defmacro axiom-condition (_a) `(%axiom-condition ,_a))
253 ;;; (defmacro axiom-type (_a) `(%axiom-type ,_a))
254 ;;; (defmacro axiom-id-condition (_a) `(%axiom-id-condition ,_a))
255 ;;; (defmacro axiom-ac-extension (_a) `(%axiom-ac-extension ,_a))
256 ;;; (defmacro axiom-a-extensions (_a) `(%axiom-a-extensions ,_a))
257 ;;; (defmacro axiom-kind (_a) `(%axiom-kind ,_a))
258 ;;; (defmacro axiom-first-match-method (_a) `(%axiom-first-match-method ,_a))
259 ;;; (defmacro axiom-next-match-method (_a) `(%axiom-next-match-method ,_a))
260 ;;; (defmacro axiom-labels (_a) `(%axiom-labels ,_a))
261194
262195 (defmacro axiom-is-behavioural (_a) `(axiom-behavioural ,_a))
263196
288221 (list (cons axiom extensions)))))
289222 extensions))
290223
291 ;; the following two macros are now just a synonym to axiom-extensions
292 #||
293 (defmacro axiom-ac-extension (_x &optional
294 (ext-rule-table '*current-ext-rule-table*))
295 `(axiom-exts-ac-extension (gethash ,_x ,ext-rule-table)))
296
297 (defmacro axiom-a-extensions (_x &optional
298 (ext-rule-table '*current-ext-rule-table*))
299 `(axiom-exts-a-extensions (gethash ,_x ,ext-rule-table)))
300
301 ||#
302224 (defmacro axiom-ac-extension (_x &optional
303225 (_ext-rule-table '*current-ext-rule-table*))
304226 `(axiom-extensions ,_x ,_ext-rule-table))
315237 (_ext-rule-table '*current-ext-rule-table*))
316238 `(axiom-extensions ,_ax ,_ext-rule-table))
317239
318 #||
319 (defun !axiom-a-extensions (ax &optional
320 (ext-rule-table *current-ext-rule-table*))
321 (let ((exts (axiom-extensions ax ext-rule-table)))
322 (if exts
323 (axiom-exts-a-extensions exts)
324 nil)))
325
326 (defsetf !axiom-a-extensions (_ax &optional
327 (ext-rule-table '*current-ext-rule-table*))
328 (_value)
329 ` (let ((exts (axiom-extensions ,_ax ,ext-rule-table)))
330 (unless exts
331 (setf (axiom-extensions ,_ax ,ext-rule-table)
332 (make-axiom-exts)))
333 (setf (axiom-exts-a-extensions exts) ,_value)))
334
335 ||#
336
337
338240 ;;; the basic constructor
339241 ;;; create-axiom
340242 ;;;
341 #||
342 (defun create-axiom (lhs rhs condition type behavioural id-condition
343 ac-extension
344 a-extensions kind first-match-method next-match-method
345 labels)
346 (let ((r (axiom* type lhs rhs condition behavioural)))
347 (setf (axiom-id-condition r) id-condition)
348 (when (or ac-extension a-extensions)
349 (setf (axiom-extensions r) (make-axiom-exts)))
350 (if ac-extension
351 (setf (axiom-ac-extension r) ac-extension))
352 (if a-extensions
353 (setf (axiom-a-extensions r) a-extensions))
354 (setf (axiom-kind r) kind)
355 (setf (axiom-first-match-method r) first-match-method)
356 (setf (axiom-next-match-method r) next-match-method)
357 (setf (axiom-labels r) labels)
358 r))
359 ||#
360
361243 (defun create-axiom (lhs
362244 rhs
363245 condition
384266 (setf (axiom-next-match-method r) next-match-method)
385267 (setf (axiom-labels r) labels)
386268 (setf (axiom-meta-and-or r) meta-and-or)
387 (set-context-module r)
269 (set-object-context-module r)
388270 r))
389271
390272 (defmacro rule-is-builtin (_rule_)
391273 ` (term$is-lisp-code? (term-body (rule-rhs ,_rule_))))
392
393 #||
394 (defun deallocate-axiom (ax)
395 (deallocate-non-var (axiom-lhs ax))
396 (deallocate-non-var (axiom-rhs ax))
397 (let ((cond (axiom-condition ax)))
398 (when (and cond
399 (not (or (eq *bool-true* cond)
400 (eq *bool-false* cond))))
401 (deallocate-non-var cond)))
402 (when (axiom-ac-extension ax)
403 (deallocate-axiom (axiom-ac-extension ax)))
404 (mapc #'deallocate-axiom (axiom-a-extensions ax)))
405 ||#
406274
407275 ;;; AXIOM-CONTAINS-ERROR-METHOD? : Axiom -> Bool
408276 ;;; retrurns true iff the axiom contains terms with error-method as top.
428296
429297 ;;; *NOT YET*
430298
431 #|
432 (defterm theorem (object)
433 :visible (value) ; the theorem itself
434 :hidden (type ; type of thorem
435 ; :eq = equation
436 ; :rule = rule
437 ; :fop = first order predicate
438 ; :hol = higher order predicate
439 module ; module object in which the theorem is
440 ; specified.
441 valid ; flag
442 ; nil = unknown.
443 ; :valid = the thorem is poved to be valid.
444 ; :invalid = the theorem is proved to be
445 ; invalid.
446 ))
447 |#
448
449
450299 ;;; EOF
4242
4343 (defun print-macro (macro stream &rest ignore)
4444 (declare (ignore ignore))
45 (let ((mod (or *last-module* *current-module*)))
45 (let ((mod (get-context-module)))
4646 (if mod
4747 (with-in-module (mod)
4848 (term-print (macro-lhs macro) stream)
4848 ;;; MODULE __________________________________________________________________
4949 ;;; STRUCTURE
5050 ;;; *********
51 #||
52 (defterm module (top-object)
53 :visible (name) ; module name (modexpr).
54 :hidden (signature ; own signature.
55 axiom-set ; set of own axioms.
56 theorems ; set of own theorems, not used yet.
57 parse-dictionary ; infos for term parsing.
58 ex-info ; various compiled informations.
59 trs ; corresponding semi-compiled TRS.
60 context ; run time context
61 )
62 :int-printer print-module-object
63 :print print-module-internal)
64
65 (defstruct (module (:include top-object (-type 'module))
66 (:conc-name "MODULE-")
67 (:constructor make-module)
68 (:constructor module* (name))
69 (:print-function print-module-object)
70 )
71 (signature nil :type (or null signature-struct))
72 ; own signature.
73 (axiom-set nil :type (or null axiom-set))
74 ; set of own axioms.
75 (theorems nil :type list) ; set of own theorems, not used yet.
76 (parse-dictionary nil :type (or null parse-dictionary))
77 ; infos for term parsing.
78 (ex-info nil :type list) ; various compiled informations.
79 (trs nil :type (or null trs)) ; corresponding semi-compiled TRS.
80 (context nil
81 :type (or null module-context))
82 ; run time context
83 (alias nil :type list)
84 )
85
86 (eval-when (:execute :load-toplevel)
87 (setf (get 'module :type-predicate) (symbol-function 'module-p))
88 (setf (get 'module :eval) nil)
89 (setf (get 'module :print) 'print-module-internal)
90 )
91
92 ||#
93
94 ;;; type predicate
95
96 ;;; (defmacro module-p (_object) `(is-module ,_object))
9751
9852 ;;; module name
9953 ;;; name ::= string
249203 (return-from get-importing-path
250204 (nconc path im2))))))))))
251205
252 (defun get-real-importing-mode (module2 &optional (module (or *current-module*
253 *last-module*)))
206 (defun get-real-importing-mode (module2 &optional (module (get-context-module)))
254207 (declare (type module module2 module)
255208 (values symbol))
256 ;;
257209 (let ((path (get-importing-path module2 module)))
258210 (let ((mode nil))
259211 (dolist (e path mode)
418370 ;;; gathers own signature infomations of a module. stored in module's `signature'
419371 ;;; slot.
420372
421 #||
422 (defstruct (signature-struct (:conc-name "SIGNATURE$")
423 ;; #+gcl (:static t)
424 )
425 (sorts nil :type list) ; list of own sorts.
426 (sort-relations nil :type list) ; list of subsort relations.
427 (operators nil :type list) ; list of operators declared in the
428 ; module.
429 (opattrs nil :type list) ; explicitly declared operator
430 ; attributes in a form of AST.
431 (principal-sort nil :type atom) ; principal sort of the module.
432 )
433
434 ||#
435373
436374 ;;; accessors via module, all are setf'able.
437375
466404 ;;; *********
467405 ;;; gathers own axioms and explicitly declared variables of a module.
468406 ;;; stored in module's `axioms' slot.
469
470 #||
471 (defstruct (axiom-set (:conc-name "AXIOM-SET$")
472 ;; #+gcl (:static t)
473 )
474 (variables nil :type list) ; assoc list of explicitly declared
475 ; variables.
476 ; ((variable-name . variable) ...)
477 (equations nil :type list) ; list of equtions declared in the module.
478 (rules nil :type list) ; list of rules declared in the module.
479 )
480
481 ||#
482407
483408 ;;; accessors from module object, all are setf'able.
484409
516441 ;;; builtin-info part of builtin sorts.
517442 ;;;
518443
519 #||
520 (defstruct (parse-dictionary (:conc-name "DICTIONARY-")
521 ;; #+gcl (:static t)
522 )
523 (table (make-hash-table :test #'equal :size 50)
524 :type (or null hash-table))
525 (builtins nil :type list)
526 (juxtaposition nil :type list) ; list of juxtaposition methods.
527 )
528 ||#
529
530444 ;;; accessors via module, all are setf'able
531445
532446 (defmacro module-dictionary-table (_mod) `(dictionary-table
565479 ;;; ***
566480 ;;; TRS________________________________________________________________________
567481 ;;; ***
568
569 #||
570 (let ((.ext-rule-table-symbol-num. 0))
571 (declare (type fixnum .ext-rule-table-symbol-num.))
572 (defun make-ext-rule-table-name ()
573 (declare (values symbol))
574 (intern (format nil "ext-rule-table-~d" (incf .ext-rule-table-symbol-num.))))
575 )
576
577 ;;; The structure TRS is a representative of flattened module.
578
579 (defstruct (TRS (:conc-name trs$)
580 ;; #+gcl (:static t)
581 )
582 (module nil :type (or null module)) ; the reverse pointer
583 ;; SIGNATURE INFO
584 (opinfo-table (make-hash-table :test #'eq)
585 :type (or null hash-table))
586 ; operator infos
587 (sort-order (make-hash-table :test #'eq)
588 :type (or null hash-table))
589 ; transitive closure of sort-relations
590 ;; (ext-rule-table (make-hash-table :test #'eq))
591 (ext-rule-table (make-ext-rule-table-name)
592 :type symbol)
593 ; assoc table of rule A,AC extensions
594 ;;
595 (sorts nil :type list) ; list of all sorts
596 (operators nil :type list) ; list of all operators
597 ;; REWRITE RULES
598 (rules nil :type list) ; list of all rewrite rules.
599 ;; INFO FOR EXTERNAL INTERFACE -----------------------------------
600 (sort-name-map nil :type list)
601 (op-info-map nil :type list)
602 (op-rev-table nil :type list)
603 ;; GENERATED OPS & AXIOMS for equalities & if_then_else_fi
604 ;; for proof support system.
605 (sort-graph nil :type list)
606 (err-sorts nil :type list)
607 (dummy-methods nil :type list)
608 (sem-relations nil :type list) ; without error sorts
609 (sem-axioms nil :type list) ; ditto
610 ;; a status TRAM interface generated?
611 (tram nil :type symbol) ; nil,:eq, or :all
612 )
613
614 ||#
615482
616483 ;;; accessor via module, all are setf'able.
617484 (defmacro module-rewrite-rules (_mod) `(trs$rules (module-trs ,_mod)))
706573 (if (trs$opinfo-table trs)
707574 (clrhash (trs$opinfo-table trs)))
708575 (setf (trs$opinfo-table trs) nil)
709 #||
710 (if (trs$ext-rule-table trs)
711 (clrhash (trs$ext-rule-table trs)))
712 ||#
713576 (setf (get (trs$ext-rule-table trs) :ext-rules) nil)
714577 )
715578
717580 ;;; CONTEXT_____________________________________________________________________
718581 ;;; *******
719582 ;;; holds some run time context infos.
720
721 #||
722 (defstruct (module-context
723 ;; #+gcl (:static t)
724 )
725 (bindings nil :type list) ; top level let binding
726 (special-bindings nil :type list) ; users $$variables ...
727 ($$term nil :type list) ; $$term
728 ($$subterm nil :type list) ; $$subterm
729 ($$action-stack nil :type list) ; action stack for apply
730 ($$selection-stack nil :type list) ; selection stack for choose
731 ($$stop-pattern nil :type list) ; stop pattern
732 )
733 ||#
734
735583 ;;; accessors via module, all are setf'able.
736584
737585 (defmacro module-bindings (_mod) `(module-context-bindings (module-context
748596
749597 ;;; intialization
750598 (defun initialize-module-context (context)
751 (declare (type module-context context)
599 (declare (type module-dyn-context context)
752600 (values t))
753601 (setf (module-context-bindings context) nil
754602 (module-context-special-bindings context) nil
760608 )
761609
762610 (defun clean-up-context (context)
763 (declare (type module-context context)
611 (declare (type module-dyn-context context)
764612 (values t))
765613 (initialize-module-context context))
766614
794642 ;;; beh-axioms-prooved nil ;
795643 ;;; psort-decl ; declaration form of principal sort
796644 ;;; error-op-decl ; declaration forms of explicit error
797 ;;; ; operators. may contain illegual ones.
798 ;;; macros
799 ;;;
645
800646 (defun module-infos (mod) (object-misc-info mod))
647
801648 (defsetf module-infos (mod) (values)
802649 `(setf (object-misc-info ,mod) ,values))
803650
823670 (defmacro module-hidden (_mod)
824671 ` (getf (object-misc-info ,_mod) ':module-hidden))
825672
826 ;;; KIND
827 (defmacro module-kind (_mod)
828 `(getf (object-misc-info ,_mod) ':module-kind))
829
830 (defmacro module-is-theory (_mod_) `(eq :theory (module-kind ,_mod_)))
831
832 (defmacro module-is-object (_mod_) `(eq :object (module-kind ,_mod_)))
833
834 (defmacro module-is-final (_mod_) `(eq :theory (module-kind ,_mod_)))
835
836 (defmacro module-is-loose (_mod_)
837 ` (memq (module-kind ,_mod_) '(:module :ots)))
838
839 (defmacro module-is-initial (_mod_) `(eq (module-kind ,_mod_) :object))
840
841673 ;;; REGULARITY
842674 (defmacro module-is-regular (_mod)
843675 `(getf (object-misc-info ,_mod) ':modle-is-regular))
844
845 ;;; ALL-SUBMODULES-LIST -- cached data
846 ;;; OBSOLETE
847 ;;; (defun module-all-submodules-list (mod)
848 ;;; (or (object-misc-info-all-submodules-list (object-misc-info mod))
849 ;;; (setf (object-misc-info-all-submodules-list (object-misc-info mod))
850 ;;; (mapcar #'car (module-all-submodules mod)))))
851676
852677 ;;; ADD-IMPORTED-MODULE : module mode submodule [alias] -> void
853678 ;;; (for downward comatibility.)
1074899 (defmacro module-ambig-sorts (_m) `(getf (object-misc-info ,_m) ':ambig-sorts))
1075900 (defmacro module-ambig-ops (_m) `(getf (object-misc-info ,_m) ':ambig-ops))
1076901
1077 ;;; EX-INFO INITIALIZATION -----------------------------------------------------
1078 ;;; OBSOLETE
1079
1080 #||
1081 (defun initialize-module-ex-info (ex-info)
1082 (setf (module-ex-info-protected-modules ex-info) nil
1083 (module-ex-info-hard-wired ex-info) nil
1084 (module-ex-info-kind ex-info) nil
1085 (module-ex-info-all-submodules-list ex-info) nil
1086 (module-ex-info-infos ex-info) nil))
1087
1088 (defun clean-up-ex-info (ex-info)
1089 (initialize-module-ex-info ex-info))
1090
1091 ||#
1092
1093902 ;;; *************
1094903 ;;; Module status_______________________________________________________________
1095904 ;;; *************
1099908 ;;; 1 : regularized -- NOT USED.
1100909 ;;; 2 : prepared for parsing
1101910 ;;; 3 : prepared for rewriting
1102 ;;;
911 (defparameter module-initial -1)
912 (defparameter module-inconsistent 0)
913 (defparameter module-regularized 1)
914 (defparameter module-ready-parsing 2)
915 (defparameter module-ready-rewriting 3)
916
1103917 ;;; o Adding new sort or operator declarations makes the module status to 0.
1104918 ;;; o Adding new rule makes the module status to at most 2.
1105919 ;;; o Some changes in any submodule makes the status to 0.
1106920 ;;; (should be more fine grained checking for statu change).
1107 ;;;
1108 ;;; (defmacro module-status (_mod) `(object-status ,_mod))
1109921
1110922 ;;; initial inconsistent status
1111923
1112 (defmacro module-is-inconsistent (_module)
1113 `(object-is-inconsistent ,_module))
924 (defun module-is-inconsistent (_module)
925 (object-is-inconsistent _module))
1114926
1115927 (defun mark-module-as-inconsistent (_module)
1116928 (mark-object-as-inconsistent _module))
1118930 ;;; parsing preparation
1119931
1120932 (defmacro need-parsing-preparation (_module)
1121 `(< (module-status ,_module) 2))
933 `(< (module-status ,_module) module-ready-parsing))
1122934
1123935 (defmacro module-is-ready-for-parsing (_module)
1124 `(>= (module-status ,_module) 2))
936 `(>= (module-status ,_module) module-ready-parsing))
1125937
1126938 (defmacro mark-module-ready-for-parsing (_module)
1127 `(setf (module-status ,_module) (max 2 (module-status ,_module))))
939 `(setf (module-status ,_module) (max module-ready-parsing (module-status ,_module))))
1128940
1129941 (defmacro mark-need-parsing-preparation (_module)
1130 `(setf (module-status ,_module) (min 1 (module-status ,_module))))
942 `(setf (module-status ,_module) (min module-regularized (module-status ,_module))))
1131943
1132944 ;;; rewriting preparation
1133945
1134946 (defmacro need-rewriting-preparation (_module)
1135 `(< (module-status ,_module) 3))
947 `(< (module-status ,_module) module-ready-rewriting))
1136948
1137949 (defmacro module-is-ready-for-rewriting (_module)
1138 `(>= (module-status ,_module) 3))
950 `(>= (module-status ,_module) module-ready-rewriting))
1139951
1140952 (defmacro mark-module-as-consistent (_module)
1141 `(setf (module-status ,_module) 3))
953 `(setf (module-status ,_module) module-ready-rewriting))
1142954
1143955 (defmacro mark-module-ready-for-rewriting (_module)
1144956 `(mark-module-as-consistent ,_module))
1145957
1146958 (defmacro mark-module-need-rewriting-preparation (_module)
1147 `(setf (module-status ,_module) (min 2 (module-status ,_module))))
959 `(setf (module-status ,_module) (min module-ready-parsing (module-status ,_module))))
1148960
1149961 ;;; some handy procs.
1150962
1157969 (defmacro needs-rule (&optional (_module '*current-module*))
1158970 `(compile-module ,_module))
1159971
1160 ;;; *******
1161 ;;; PRINTER
1162 ;;; *******
1163
1164 (defun print-module-object (obj stream &rest ignore)
1165 (declare (ignore ignore)
1166 (type module obj)
1167 (type stream stream)
1168 (values t))
1169 (if (or (module-is-inconsistent obj)
1170 (null (module-name obj)))
1171 (format stream "[:module \"~a\"]" (module-name obj))
1172 (cond ((module-is-object obj)
1173 (format stream ":mod![\"~a\"]" (module-print-name obj)))
1174 ((module-is-theory obj)
1175 (format stream ":mod*[\"~a\"]" (module-print-name obj)))
1176 (t (format stream ":mod[\"~a\"]" (module-print-name obj))))))
1177
1178972 ;;; *********************************
1179973 ;;; Constructing RUN TIME ENVIRONMENT -----------------------------------------
1180974 ;;; *********************************
1185979 ;;; module.
1186980 ;;; *current-opinfo-table* : operator information table of the current module .
1187981 ;;;
1188
1189982 (defmacro with-in-module ((_module_) &body body)
1190983 (once-only (_module_)
1191984 ` (block with-in-module
13931186 (defun initialize-module (mod)
13941187 (declare (type module mod)
13951188 (values t))
1396 ;;
1397 (setf (module-status mod) -1) ; initial state.
1189 (setf (module-status mod) module-initial) ; initial state.
13981190 (setf (module-decl-form mod) nil)
13991191 ;; interface
14001192 (if (the (or null ex-interface) (module-interface mod))
14161208 (setf (module-parse-dictionary mod) (make-parse-dictionary)))
14171209 ;; misc infos
14181210 (setf (object-misc-info mod) nil)
1419 ;;; (if (object-misc-info mod)
1420 ;;; (initialize-module-ex-info (module-ex-info mod))
1421 ;;; (setf (module-ex-info mod) (make-module-ex-info)))
14221211 ;; trs
14231212 (if (the (or null trs) (module-trs mod))
14241213 (initialize-trs (module-trs mod) mod)
14251214 (setf (module-trs mod) (make-trs :module mod)))
14261215 ;; context
1427 (if (the (or null module-context) (module-context mod))
1216 (if (the (or null module-dyn-context) (module-context mod))
14281217 (initialize-module-context (module-context mod))
1429 (setf (module-context mod) (make-module-context)))
1218 (setf (module-context mod) (make-module-dyn-context :object mod)))
14301219 ;; symbol table
14311220 (setf (module-alias mod) nil)
14321221 (setf (module-symbol-table mod) (make-symbol-table))
14331222 ;; print name
1434 ;; (setf (module-print-name mod) (make-module-print-name2 mod))
14351223 (setf (module-print-name mod) (make-module-print-name mod))
14361224 ;;
14371225 (clear-tmp-sort-cache)
5252 ;;; definition of semantic object & internal data structure.
5353 ;;; all objects defined in this file inherits either %object or %int-object.
5454
55 #||
56 ;;; term structure of semantic object of Chaos.
57 (defterm object () :category ':object)
58
59 (defterm static-object () :category ':static-object)
60
61 ;;; structure of internal object of Chaos.
62 (defterm int-object () :category ':int-object)
63
64 (defterm static-int-object () :category ':static-int-object)
65 ||#
66
6755 (defstruct (object (:conc-name "OBJECT-")
6856 (:constructor make-object)
6957 (:constructor object* nil)
7664 (defmacro object-info (_obj _info)
7765 ` (getf (object-misc-info ,_obj) ,_info))
7866
79 (defun set-context-module (obj &optional (mod *current-module*))
80 (setf (object-context-mod obj) mod))
81
82 (eval-when (:execute :load-toplevel)
83 (setf (symbol-function 'is-object)(symbol-function 'object-p))
84 (setf (get 'object ':type-predicate) (symbol-function 'is-object))
85 (setf (get 'object :eval) nil)
86 (setf (get 'object :print) nil))
67 (defun set-object-context-module (obj &optional (context-mod (get-context-module)))
68 (setf (object-context-mod obj) context-mod))
69
70 ; (eval-when (:execute :load-toplevel)
71 ; (setf (symbol-function 'is-object)(symbol-function 'object-p))
72 ; (setf (get 'object ':type-predicate) (symbol-function 'is-object))
73 ; (setf (get 'object :eval) nil)
74 ; (setf (get 'object :print) nil))
8775
8876 ;;; *********
8977 ;;; INTERFACE
127115 ((module-p nm) (canonicalize-object-name (module-name nm)))
128116 (t
129117 ;; do nothing
130 ;; (error "internal error, illegal name object ~s" nm)
131118 )))
132119
133120 (defun symbol-table-add (table nm obj)
156143 (t (pushnew obj (stable-unknowns tbl))))
157144 tbl)))
158145
159 (defun symbol-table-get (name &optional (module *current-module*))
146 (defun symbol-table-get (name &optional (module (get-context-module)))
160147 (let ((gname (canonicalize-object-name name)))
161148 (gethash gname (symbol-table-map
162149 (module-symbol-table module)))))
163150
164 #||
165 (defun pr-symbol-table (st stream &rest ignore)
166 (let ((names (copy-list (symbol-table-names st))))
167 (setq names (sort names #'ob<))
168 (dolist (name names)
169 (pr-name name (gethash name (symbol-table-map st)) stream))))
170
171 (defun get-object-type (obj)
172 (cond ((module-p obj) :module)
173 ((sort-p obj) :sort)
174 ((operator-p obj) :operator)
175 ((axiom-p obj) :axiom)
176 ((term-is-variable? obj) :variable)
177 (t :unknown)))
178
179 (defun get-obj-info (obj)
180 (let ((type (get-object-type obj)))
181 (cond ((or (eq type :variable)
182 (eq (object-context-mod obj) *current-module*))
183 (list obj type "of the current module"))
184 ((eq type :unknown)
185 (list obj type "unknown type of object"))
186 ((object-context-mod obj)
187 (list obj
188 type
189 (concatenate 'string "of module "
190 (with-output-to-string (str)
191 (print-mod-name (object-context-mod obj)
192 str
193 t)))))
194 (t (list obj type "")))))
195
196 (defun pr-name (name objs stream)
197 (format stream "~&~A~8T" name)
198 (dolist (obj objs)
199 (let ((info (get-obj-info obj)))
200 (format stream ": ~A ~A~%" (second info) (third info)))))
201 ||#
202
203151 ;;;=============================================================================
204152 ;;; TOP-OBJECT _________________________________________________________________
205153 ;;; **********
206154
207155 ;;; represents common structure of top-level semantic objects.
208156 ;;;
209 #||
210 (defterm top-object (object) ; was (static-object)
211 :visible (name) ; name.
212 :hidden (interface ; external interface.
213 status ; object status.
214 decl-form ; declaration form
215 )
216 )
217 ||#
218157 (defstruct (top-object (:conc-name "TOP-OBJECT-")
219158 (:constructor make-top-object)
220159 (:constructor top-object* (name))
226165 (decl-form nil :type list)
227166 (symbol-table (make-symbol-table) :type symbol-table))
228167
229 (eval-when (:execute :load-toplevel)
230 (setf (symbol-function 'is-top-object)(symbol-function 'top-object-p))
231 (setf (get 'top-object ':type-predicate) (symbol-function 'is-top-object))
232 (setf (get 'top-object :eval) nil)
233 (setf (get 'top-object :print) nil))
168 ; (eval-when (:execute :load-toplevel)
169 ; (setf (symbol-function 'is-top-object)(symbol-function 'top-object-p))
170 ; (setf (get 'top-object ':type-predicate) (symbol-function 'is-top-object))
171 ; (setf (get 'top-object :eval) nil)
172 ; (setf (get 'top-object :print) nil))
234173
235174 ;;;
236175 ;;; basic accessors via top-object
333272 (declare (type ex-interface interface)
334273 (values t))
335274 (setf (interface$parameters interface) nil)
336 (setf (interface$exporting-objects interface) nil)
337 )
275 (setf (interface$exporting-objects interface) nil))
338276
339277 (defun clean-up-ex-interface (interface)
340278 (declare (type ex-interface interface)
341279 (values t))
342280 (setf (interface$dag interface) nil)
343281 (setf (interface$parameters interface) nil)
344 (setf (interface$exporting-objects interface) nil)
345 )
282 (setf (interface$exporting-objects interface) nil))
346283
347284 ;;;
348285 ;;; setting dependency
364301 (dag-node-subnodes sub-dag))))
365302 (push s-node (dag-node-subnodes dag)))))
366303 ;; make exporting relation
367 ;; (pushnew (cons object mode) (object-exporting-objects subobject) :test #'equal)
368 (pushnew (cons object mode) (object-exporting-objects subobject) :test #'equal)
369 )
304 (pushnew (cons object mode) (object-exporting-objects subobject) :test #'equal))
370305
371306 ;;; ******
372307 ;;; STATUS
435370 ;;; builtin-info part of builtin sorts.
436371 ;;;
437372
438 (defstruct (parse-dictionary (:conc-name "DICTIONARY-")
439 ;; #+gcl (:static t)
440 )
373 (defstruct (parse-dictionary (:conc-name "DICTIONARY-"))
441374 (table (make-hash-table :test #'equal :size 50)
442375 :type (or null hash-table))
443376 (builtins nil :type list)
540473 (let ((mod (trs$module obj)))
541474 (format stream "'[:trs \"~a\"]" (make-module-print-name2 mod))))
542475
543 ;;; *******
544 ;;; CONTEXT_____________________________________________________________________
545 ;;; *******
546 ;;; holds some run time context infos.
547
548 (defstruct (module-context
549 ;; #+gcl (:static t)
550 )
476 ;;; ******************
477 ;;; MODULE-DYN-CONTEXT___________________________________________________________
478 ;;; ******************
479 ;;; holds run time dynamic infomation of a module.
480
481 (defstruct (module-dyn-context (:conc-name "MODULE-CONTEXT-"))
482 (object nil :type (or null object)) ; module
551483 (bindings nil :type list) ; top level let binding
552484 (special-bindings nil :type list) ; users $$variables ...
553485 ($$term nil :type list) ; $$term
563495 ;;; MODULE __________________________________________________________________
564496 ;;; STRUCTURE
565497 ;;; *********
566 #||
567 (defterm module (top-object)
568 :visible (name) ; module name (modexpr).
569 :hidden (signature ; own signature.
570 axiom-set ; set of own axioms.
571 theorems ; set of own theorems, not used yet.
572 parse-dictionary ; infos for term parsing.
573 ex-info ; various compiled informations.
574 trs ; corresponding semi-compiled TRS.
575 context ; run time context
576 )
577 :int-printer print-module-object
578 :print print-module-internal)
579 ||#
580
581498 (defstruct (module (:include top-object (-type 'module))
582499 (:conc-name "MODULE-")
583500 (:constructor make-module)
584501 (:constructor module* (name))
585 (:print-function print-module-object)
586 )
502 (:print-function print-module-object))
587503 (print-name "" :type string)
588504 (signature nil :type (or null signature-struct))
589505 ; own signature.
592508 (theorems nil :type list) ; set of own theorems, not used yet.
593509 (parse-dictionary nil :type (or null parse-dictionary))
594510 ; infos for term parsing.
595 ;; (ex-info nil :type list) ; various compiled informations.
596 (trs nil :type (or null trs)) ; corresponding semi-compiled TRS.
511 (trs nil :type (or null trs)) ; corresponding semi-compiled TRS.
597512 (context nil
598 :type (or null module-context))
513 :type (or null module-dyn-context))
599514 ; run time context
600 (alias nil :type list)
601 )
602
603 (eval-when (:execute :load-toplevel)
604 (setf (get 'module :type-predicate) (symbol-function 'module-p))
605 (setf (get 'module :eval) nil)
606 (setf (get 'module :print) 'print-module-internal)
607 )
515 (alias nil :type list) ; alias names for a module generated from complex modexpr
516 )
517
518 ;;; KIND
519 (defmacro module-kind (_mod)
520 `(getf (object-misc-info ,_mod) ':module-kind))
521
522 (defmacro module-is-theory (_mod_) `(eq :theory (module-kind ,_mod_)))
523
524 (defmacro module-is-object (_mod_) `(eq :object (module-kind ,_mod_)))
525
526 (defmacro module-is-final (_mod_) `(eq :theory (module-kind ,_mod_)))
527
528 (defmacro module-is-loose (_mod_)
529 ` (memq (module-kind ,_mod_) '(:module :ots)))
530
531 (defmacro module-is-initial (_mod_) `(eq (module-kind ,_mod_) :object))
532
533 ;;; PRINTER
534
535 (defun print-module-object (obj stream &rest ignore)
536 (declare (ignore ignore)
537 (type module obj)
538 (type stream stream)
539 (values t))
540 (if (or (module-is-inconsistent obj)
541 (null (module-name obj)))
542 (format stream ":module[\"~a\"]" (module-name obj))
543 (cond ((module-is-object obj)
544 (format stream ":mod![\"~a\"]" (module-print-name obj)))
545 ((module-is-theory obj)
546 (format stream ":mod*[\"~a\"]" (module-print-name obj)))
547 (t (format stream ":mod[\"~a\"]" (module-print-name obj))))))
608548
609549 ;;; ****
610550 ;;; VIEW _______________________________________________________________________
633573
634574 (defun print-view-struct-object (obj stream &rest ignore)
635575 (declare (ignore ignore))
636 (format stream "#<view ~a: ~s => ~s | ~s>"
576 (format stream ":view[~a: ~s => ~s | ~s]"
637577 (view-struct-name obj)
638578 (view-struct-src obj)
639579 (view-struct-target obj)
124124 ;;; ********
125125 ;;; OPERATOR __________________________________________________________________
126126 ;;; ********
127 #||
128 (defterm operator (object) ; (static-object)
129 :visible (name) ; list of `symbol' & `number of arguments'.
130 :hidden (module
131 strategy
132 theory
133 syntax
134 memo
135 print-name
136 hidden
137 )
138 :int-printer print-operator-object
139 :print print-operator-internal)
140
141 ||#
142
143127 (defstruct (operator (:include object (-type 'operator))
144128 (:constructor make-operator)
145129 (:constructor operator* (name))
146130 (:copier nil)
147131 (:print-function print-operator-object))
148132 (name nil :type list)
149 (module nil :type (or null module))
150133 (strategy nil :type list)
151134 (theory nil :type (or null op-theory))
152135 (syntax nil :type (or null opsyntax))
161144
162145 (defun print-operator-object (obj stream &rest ignore)
163146 (declare (ignore ignore))
164 (format stream "(:op ~s : ~x)" (operator-name obj) (addr-of obj))
165 )
166
167 ;;; (defmacro operator-p (_o) `(is-operator ,_o))
147 (format stream ":op[~s : ~x]" (operator-name obj) (addr-of obj)))
168148
169149 ;;; Basic accessors ----------------------------------------------------------
170
171 ;;; (defmacro operator-name (_operator) `(%operator-name ,_operator))
150 (defmacro operator-module (op)
151 `(object-context-mod ,op))
172152 (defmacro operator-symbol (_operator) `(car (operator-name ,_operator)))
173153 (defmacro operator-num-args (_operator) `(cdr (operator-name ,_operator)))
174 ;;; (defmacro operator-module (_operator) `(%operator-module ,_operator))
175 ;;; (defmacro operator-hidden (_operator) `(%operator-hidden ,_operator))
176154
177155 ;;; id = (name . module)
178156 (defmacro operator-id (__operator)
180158 `(cons (operator-name ,__operator) (operator-module ,__operator))))
181159 (defmacro operator-module-id (__operator) `(module-name (operator-module
182160 ,__operator)))
183 ;;; (defmacro operator-strategy (__operator) `(%operator-strategy ,__operator))
184161 (defmacro operator-rewrite-strategy (__operator)
185162 `(operator-strategy ,__operator))
186 ;;; (defmacro operator-theory (__operator) `(%operator-theory ,__operator))
187 ;;; (defmacro operator-syntax (__operator) `(%operator-syntax ,__operator))
188 ;;; (defmacro operator-print-name (__operator) `(%operator-print-name ,__operator))
189 ;;; (defmacro operator-memo (__operator) `(%operator-memo ,__operator))
190163
191164 (defun explode-operator-name (op-symbol)
192165 (declare (type list op-symbol)
241214
242215 ;;; Constructor of Operator body.-----------------------------------------------
243216 (defvar *opname-table* nil)
244 #||
245 (eval-when (:execute :load-toplevel)
246 (setf *opname-table* (make-hash-table :test #'equal)))
247 ||#
248217
249218 (defun canonicalize-op-name (name)
250219 (declare (type list name)
253222 (prog1
254223 name
255224 (push (cons name name) *opname-table*))))
256
257 #||
258 (defvar .operator-recycler.)
259 (eval-when (:execute :load-toplevel)
260 (setq .operator-recycler. (make-hash-table :test #'equal)))
261
262 (defun allocate-operator (name num-args module)
263 (let* ((name (canonicalize-op-name (cons name num-args)))
264 (key (cons name module))
265 (op (gethash key .operator-recycler.)))
266 (if op
267 op
268 (progn
269 (setq op (operator* name))
270 (setf (operator-module op) module)
271 (when (modexp-is-simple-name (module-name module))
272 (setf (gethash key .operator-recycler.) op))
273 op))))
274
275 ||#
276225
277226 (defun allocate-operator (name num-args module)
278227 (declare (type list name)
301250 (operator-syntax o) syntax
302251 (operator-print-name o) print-name)
303252 o))
304
305253
306254 ;;; accessors of an operator's syntax via operator.
307255 ;;;
457405 ;;; *****************************************************************************
458406
459407 ;;; * NOTE* The slots defined here are all module idependent.
460 #||
461 (defterm method (object)
462 :visible (name ; operator name (canonicalized).
463 arity ; arity, list of argument sorts.
464 coarity) ; coarity
465 :hidden (module ; the module it belongs.
466 constructor ; flag, t iff the method is a
467 ; constructor. not yet used.
468 supplied-strategy ; user supplied rewrite strategy.
469 form ; describes the form of a term with the
470 ; method as top. used for parsing.
471 precedence ; precedence used for parsing.
472 associativity ; associative info used for parsing.
473 memo ; t iff the rewriting will be memoized.
474 behavioural ; t iff the method is behavioural method.
475 coherent ; t iff the method is behaviourally coherent.
476 error ; t iff the method is error method and user
477 ; defined.
478 )
479 :int-printer print-method-object
480 :print print-method-internal)
481 ||#
482408
483409 (defstruct (method (:include object (-type 'method))
484410 (:constructor make-method)
488414 (name nil :type list) ; operator name (canonicalized).
489415 (arity nil :type list) ; arity, list of argument sorts.
490416 (coarity nil :type (or null sort*)) ; coarity
491 (module nil :type (or null module)) ; the module it belongs.
492417 (constructor nil :type (or null t)) ; flag, t iff the method is a
493418 ; constructor. not yet used.
494419 (supplied-strategy nil :type list) ; user supplied rewrite strategy.
498423 ; precedence used for parsing.
499424 (associativity nil :type symbol) ; associative info used for parsing.
500425 (behavioural nil :type (or null t)) ; t iff the method is behavioural method.
501 ;; (coherent nil :type (or null t)) ; t iff the method is behaviourally coherent.
502426 (error nil :type (or null t)) ; t iff the method is error method and user
503427 ; defined.
504428 (derived-from nil :type (or null method))
505429 (has-memo nil :type (or null t))
506 (id-symbol nil :type symbol)
507 )
430 (id-symbol nil :type symbol))
508431
509432 (eval-when (:execute :load-toplevel)
510433 (setf (symbol-function 'is-method) (symbol-function 'method-p))
513436
514437 (defun print-method-object (obj stream &rest ignore)
515438 (declare (ignore ignore))
516 (format stream "[:operator ~a]" (method-name obj)))
439 (format stream ":op[~a]" (method-name obj)))
517440
518441 ;;; Primitive constructor ------------------------------------------------------
519442
521444 ;;;
522445 (defmacro create-operator-method (_name _arity _coarity)
523446 `(let ((meth (method* ,_name ,_arity ,_coarity)))
524 (set-context-module meth)
447 (set-object-context-module meth)
525448 meth))
526449
527450 ;;; Primitive type predicate ---------------------------------------------------
530453
531454 ;;; Primitive accessors --------------------------------------------------------
532455
533 ;;; (defmacro method-name (_m) `(%method-name ,_m))
456 (defmacro method-module (m)
457 `(object-context-mod ,m))
458
534459 (defmacro method-symbol (_m) `(car (method-name ,_m)))
535 ;;; (defmacro method-arity (_m) `(%method-arity ,_m))
536 ;;; (defmacro method-coarity (_m) `(%method-coarity ,_m))
537 ;;; (defmacro method-constructor (_m) `(%method-constructor ,_m))
538 ;;; (defmacro method-form (_m) `(%method-form ,_m))
539 ;;; (defmacro method-supplied-strategy (_m) `(%method-supplied-strategy ,_m))
540 ;;; (defmacro method-precedence (_m) `(%method-precedence ,_m))
541 ;;; (defmacro method-memo (_m) `(%method-memo ,_m))
542 ;;; (defmacro method-module (_m) `(%method-module ,_m))
543 ;;; (defmacro method-associativity (_m) `(%method-associativity ,_m))
544 ;;; (defmacro method-behavioural (_m) `(%method-behavioural ,_m))
460
545461 (defmacro method-is-behavioural (_m) `(method-behavioural ,_m)) ; synonym
546 ;;; (defmacro method-is-coherent (_m) `(method-coherent ,_m))
462
547463 (defmacro method-is-user-defined-error-method (_m)
548464 `(method-error ,_m))
549465
578494 (type list arity)
579495 (type sort* coarity)
580496 (values method))
581 #||
582 (let ((key (list name arity coarity)))
583 (or (gethash key .operator-method-recycler.)
584 (let ((meth (method* name arity coarity)))
585 (setf (gethash key .operator-method-recycler.) meth)
586 meth)))
587 ||#
588 (create-operator-method name arity coarity)
589 )
497 (create-operator-method name arity coarity))
590498
591499 (defun make-method-id-symbol (method)
592500 (let* ((nam (method-name method))
603511 (values method))
604512 (let ((meth (allocate-operator-method name arity coarity)))
605513 (declare (type method meth))
606 (setf (method-module meth) *current-module*
514 (setf (method-module meth) (get-context-module)
607515 (method-constructor meth) nil
608516 (method-supplied-strategy meth) nil
609517 (method-precedence meth) nil
624532
625533 ;;; The same object.
626534 (defmacro method= (*_*meth1 *_*meth2) `(eq ,*_*meth1 ,*_*meth2))
535
627536 (defun method-w= (m1 m2)
628537 (or (method= m1 m2)
629538 (when (and (sort= (method-coarity m1) *sort-id-sort*)
675584 ;;; ***********
676585 ;;; Internal structure constaining module dependent infos of a method.
677586 ;;; does not appear explicitly in Chaos program.
678
679 #||
680 (defterm !method-info (int-object) ; (static-int-object) ; internal term.
681 :hidden (operator ; pointer to the operator.
682 theory ; equational theory.
683 lower-methods ; list of lower comparable methods,
684 ; sorted lower->higher, exclusive.
685 overloaded-methods ; list of overloaded methods,
686 ; sortd higher->lower, exclusive.
687 rules-with-same-top ; rewrite rules with lhs and rhs have a
688 ; common top method.
689 rules-with-different-top ; rewrite rules with lhs and rhs have
690 ; different top method.
691 strictly-overloaded ; t iff the method is strictly
692 ; overloaded ,i.e., has no incomparable
693 ; overloaded method.
694 rew-strategy ; rewrite strategy.
695 has-trans ; flag, t iff the method has transitivity
696 ; axioms.
697 ))
698
699 ||#
700
701587 (defstruct (!method-info (:include object (-type '!method-info))
702588 (:copier nil)
703589 (:constructor make-!method-info)
736622 ;;;
737623 ;;; GET-METHOD-INFO
738624 ;;;
739 #||
740 (defun get-method-info (method &optional (opinfo-table *current-opinfo-table*))
741 (if (and (eq method .method1.) (eq opinfo-table .method-tab1.))
742 .method-val1.
743 (if (and (eq method .method2.) (eq opinfo-table .method-tab2.))
744 .method-val2.
745 (let ((res (gethash method opinfo-table)))
746 (if res
747 (progn
748 (setq .method2. .method1.
749 .method-tab2. .method-tab1.
750 .method-val2. .method-val1.)
751 (setq .method1. method
752 .method-tab1. opinfo-table
753 .method-val1. res)
754 res)
755 #||
756 (with-output-chaos-error ()
757 (format t "context is inconsistent, could not get info for operator:")
758 (format t "~& ~a" (method-name method))
759 (chaos-to-top))
760 ||#
761 nil
762 )))))
763 ||#
764
765625 (declaim (inline get-method-info))
766626
767627 (#+GCL si::define-inline-function #-GCL defun
839699 `(!method-info-coherent (get-method-info ,_m ,_info-table)))
840700
841701 ;;; CONSTRUCTOR ----------------------------------------------------------------
842
843 #||
844 (defvar .method-info-recycler. (make-hash-table :test #'equal))
845 (defun allocate-method-info (method module)
846 (let* ((key (list method module))
847 (minfo (gethash key .method-info-recycler.)))
848 (if minfo
849 minfo
850 (progn
851 (setq minfo (!method-info*))
852 (when (modexp-is-simple-name (module-name module))
853 (setf (gethash key .method-info-recycler.) minfo))
854 minfo))))
855 ||#
856702
857703 (defun allocate-method-info (meth mod)
858704 (declare (ignore meth mod)
880726 ;;;
881727 ;;; METHOD-THEORY-INFO-FOR-MATCHING
882728 ;;;
883 #||
884 (defun method-theory-info-for-matching (method &optional (info-table
885 *current-opinfo-table*))
886 (declare (type method method)
887 (type hash-table info-table)
888 (values theory-info))
889 (let* ((th (method-theory method info-table))
890 (info (theory-info th)))
891 (declare (type op-theory th)
892 (type theory-info info))
893 (if (zero-rule-only th)
894 (%svref *theory-info-array*
895 (logandc2 (the fixnum (theory-info-code info)) .Z.))
896 info)))
897 ||#
898
899729 (defun compute-method-theory-info-for-matching (method &optional
900730 (info-table
901731 *current-opinfo-table*))
1146976 (if (err-sort-p a)
1147977 (return-from method-is-error-method t))))))
1148978
1149 #||
1150 (defun method-is-universal (method)
1151 (and (method-arity method)
1152 (every #'(lambda (x) (or (sort= x *universal-sort*)
1153 (sort= x *huniversal-sort*)
1154 (sort= x *cosmos*)))
1155 (method-arity method))))
1156 ||#
1157
1158979 (defun method-is-universal (method)
1159980 (declare (type method method)
1160981 (values (or null t)))
13281149 ;;; *NOTE* second method is assumed to be just associative.
13291150 ;;;
13301151 #-GCL (declaim (inline method-is-associative-restriction-of))
1331 #||
1332 (defun method-is-associative-restriction-of (meth1
1333 meth2
1334 &optional
1335 (so *current-sort-order*)
1336 (info *current-opinfo-table*))
1337 (declare (type method meth1 meth2)
1338 (type sort-order so)
1339 (type hash-table info)
1340 (values (or null t)))
1341 (or (method= meth1 meth2)
1342 (and (eq (method-name meth1) (method-name meth2))
1343 (sort<= (method-coarity meth1)
1344 (method-coarity meth2)
1345 so)
1346 (sort-list<= (method-arity meth1)
1347 (method-arity meth2)
1348 so)
1349 (theory-contains-associativity (method-theory meth1 info)))))
1350 ||#
13511152 #-GCL
13521153 (defun method-is-associative-restriction-of (meth1
13531154 meth2)
13731174 ;;;
13741175 ;;; *NOTE* second method is assumed to be associative-commutive.
13751176 ;;;
1376 #||
1377 (defun method-is-AC-restriction-of (meth1
1378 meth2
1379 &optional
1380 (so *current-sort-order*)
1381 (info *current-opinfo-table*))
1382 (declare (type method meth1 meth2)
1383 (type sort-order so)
1384 (type hash-table info)
1385 (values (or null t)))
1386 (or (method= meth1 meth2)
1387 (and (eq (method-name meth1) (method-name meth2))
1388 (sort<= (method-coarity meth1)
1389 (method-coarity meth2)
1390 so)
1391 (sort-list<= (method-arity meth1)
1392 (method-arity meth2)
1393 so)
1394 (theory-contains-AC (method-theory meth1 info)))))
1395 ||#
1396
13971177 #-GCL (declaim (inline method-is-ac-restriction-of))
1398
13991178 #-GCL
14001179 (defun method-is-AC-restriction-of (meth1
14011180 meth2
14221201 ;;;
14231202 ;;; *NOTE* second method is assumed to be just commutive.
14241203 ;;;
1425 #||
1426 (defun method-is-commutative-restriction-of (meth1
1427 meth2
1428 &optional
1429 (so *current-sort-order*)
1430 (info *current-opinfo-table*))
1431 (declare (type method meth1 meth2)
1432 (type sort-order so)
1433 (type hash-table info)
1434 (values (or null t)))
1435 (or (method= meth1 meth2)
1436 (and (eq (method-name meth1) (method-name meth2))
1437 (sort<= (method-coarity meth1)
1438 (method-coarity meth2)
1439 so)
1440 (sort-list<= (method-arity meth1)
1441 (method-arity meth2)
1442 so)
1443 (theory-contains-commutativity (method-theory meth1 info)))))
1444 ||#
1445
14461204 #-GCL (declaim (inline method-is-commutative-restriction-of))
1447
14481205 #-GCL
14491206 (defun method-is-commutative-restriction-of (meth1 meth2)
14501207 (declare (type method meth1 meth2)
17721529 method)
17731530 (car res)))
17741531 (return-from lowest-method! method))))))
1775
1776 #||
1777 (defun lowest-method! (method lower-bound &optional (module *current-module*))
1778 (declare (type method method)
1779 (type list lower-bound)
1780 (type module module)
1781 (values (or null method)))
1782 (let ((*current-sort-order* (module-sort-order module))
1783 (*current-opinfo-table* (module-opinfo-table module))
1784 (res nil))
1785 (declare (type hash-table *current-sort-order* *current-opinfo-table*))
1786 (let ((over-methods (method-overloaded-methods method *current-opinfo-table*)))
1787 (declare (type list over-methods))
1788 (when *on-debug*
1789 (format t "~%* lowest-method! : over-methods =")
1790 (dolist (m over-methods)
1791 (terpri)
1792 (princ " ")
1793 (print-chaos-object m)))
1794 ;;
1795 (if over-methods
1796 (progn
1797 (dolist (meth over-methods)
1798 (declare (type method meth))
1799 (when (and (sort-list<= lower-bound (method-arity meth))
1800 (not (member
1801 meth
1802 res
1803 :test #'(lambda (x y)
1804 (method-is-instance-of y
1805 x
1806 *current-sort-order*)))))
1807 (push meth res)))
1808 (or (choose-lowest-op res)
1809 method))
1810 (return-from lowest-method! method)))))
1811 ||#
18121532
18131533 (defun lowest-method* (method &optional lower-bound (module *current-module*))
18141534 (declare (type method method)
19871707 (setf (operator-associativity op) nil)
19881708 (setf (operator-computed-precedence op) nil)
19891709 (setf (operator-theory op) *the-empty-theory*)
1990 (set-context-module op module)
1710 (set-object-context-module op module)
19911711 op)))
19921712
19931713 ;;; EOF
5757 ;;; never be created as a term body (abstract class).
5858 ;;;
5959
60 #||
61 (defterm sort-struct (object) ; (static-object)
62 ;; :name ""
63 :visible (id ; sort name, symbol.
64 hidden) ; flag, t iff the sort is hidden sort.
65
66 :hidden (module ; module in which the sort is declared.
67 ; type = module object.
68 constructor ; the list of constructor methods of the
69 ; sort. not used.
70 inhabited ; temporary flag used for regularizing
71 ; the signature of a module.
72 )
73 :int-printer print-sort-internal
74 :print print-sort-internal)
75 ||#
76
7760 (defstruct (sort-struct (:conc-name "SORT-STRUCT-")
7861 (:include object (-type 'sort-struct))
7962 (:copier nil)
8265 (:constructor sort-struct* (id hidden)))
8366 (id nil :type symbol)
8467 (hidden nil :type (or null t))
85 (module nil :type (or null module))
8668 (constructor nil :type list)
8769 (inhabited nil :type (or null t))
88 (derived-from nil :type (or null sort-struct))
89 )
70 (derived-from nil :type (or null sort-struct)))
71
72 (eval-when (:execute :load-toplevel :compile-toplevel)
73 (defmacro sort-module (sort)
74 `(object-context-mod ,sort))
75 )
9076
9177 (eval-when (:execute :load-toplevel)
9278 (setf (symbol-function 'sort-sort-struct) (symbol-function 'sort-struct-p))
173159 (type (or null t) hidden))
174160 (let ((sort (sort* id hidden)))
175161 (setf (sort-module sort) module)
176 (set-context-module sort module)
162 (set-object-context-module sort module)
177163 sort))
178164
179165 ;;; *SORT-TABLE*
180 #||
181 (defvar *sort-table* (make-hash-table :test #'equal))
182 (defmacro get-sort-named (sort-name_ module_)
183 `(gethash (cons ,sort-name_ ,module_) *sort-table*))
184 (defun clear-all-sorts () (clrhash *sort-table*))
185 (defun register-sort (sort)
186 (setf (gethash (cons (sort-id sort) (sort-module sort)) *sort-table*) sort))
187
188 ||#
189166
190167 (defvar *sort-table* nil)
191168 (defun get-sort-named (sort-name module)
326303 (setf (crsort-maker s) (if (eq p-type 'class-sort)
327304 (list nil nil nil nil)
328305 (list nil nil)))
329 (set-context-module s module)
306 (set-object-context-module s module)
330307 s))
331308
332309 (defun new-record-sort (id module &optional hidden)
430407 (let ((bs (bsort* id hidden)))
431408 (setf (sort-module bs) module
432409 (bsort-info bs) info)
433 (set-context-module bs module)
410 (set-object-context-module bs module)
434411 bs))
435412
436413 ;;; Predicate ------------------------------------------------------------------
513490 (let ((as (and-sort* id hidden)))
514491 (setf (sort-module as) module
515492 (and-sort-components as) and-components)
516 (set-context-module as module)
493 (set-object-context-module as module)
517494 as))
518495
519496 ;;; Predicates -----------------------------------------------------------------
567544 (let ((os (or-sort* id hidden)))
568545 (setf (sort-module os) module
569546 (or-sort-components os) or-components)
570 (set-context-module os module)
547 (set-object-context-module os module)
571548 os))
572549
573550 ;;; Predicate ------------------------------------------------------------------
618595 (setf (sort-module es) module
619596 (err-sort-components es) components
620597 (err-sort-lowers es) lowers)
621 (set-context-module es module)
598 (set-object-context-module es module)
622599 es))
623600
624601 ;;; Predicates ----------------------------------------------------------------
4040 ;;; all the functions which handle module's context.
4141 ;;;
4242
43 ;;; INSTANCE DB ****************************************************************
44 ;;; instance db stores all the instances of class sort.
45 ;;; made for persistent object
46 ;;; we store term-body of an instance in the instance db.
47 ;;; retrieving the instance always creates new term.
48 ;;; this is for avoiding destructive replacement of term body.
49 ;;;
50
51 ;;; (defvar *instance-db* (make-hash-table :test #'equal))
52 ;;; (defun clear-instance-db () (clrhash *instance-db*))
53
54 (defmacro make-id-key (___id)
55 (once-only (___id)
56 ` (cond ((term-is-builtin-constant? ,___id)
57 (term-builtin-value ,___id))
58 (t (method-symbol (term-method ,___id))))))
59
60 (defun find-instance (id &optional class (module *current-module*))
61 (or (find-instance-aux id module class)
62 (dolist (sub (module-all-submodules module) nil)
63 (when (not (eq (cdr sub) :using))
64 (let ((inst (find-instance-aux id (car sub) class)))
65 (if inst (return-from find-instance inst)))))))
66
67 (defun find-instance-aux (id module &optional class)
68 (let ((db (module-instance-db module)))
69 (unless db (return-from find-instance-aux nil))
70 (let ((body (gethash (make-id-key id) db)))
71 (if body
72 (progn
73 (term$unset-reduced-flag body)
74 (when class
75 (unless (sort<= (term$sort body) class (module-sort-order module))
76 (return-from find-instance-aux nil)))
77 (list body))
78 nil))))
79
80 (defun register-instance (object)
81 (unless (term-eq *void-object* object)
82 (let ((module (sort-module (term-sort object)))
83 (id (term-arg-1 object)))
84 (let ((db (module-instance-db module)))
85 (unless db
86 (initialize-module-instance-db module)
87 (setq db (module-instance-db module)))
88 (setf (gethash (make-id-key id) db) (term-body object))))))
89
90 (defun delete-instance (object)
91 (unless (term-eq *void-object* object)
92 (let ((module (sort-module (term-sort object))))
93 (let ((db (module-instance-db module)))
94 (unless db
95 (return-from delete-instance nil))
96 (remhash (make-id-key (term-arg-1 object)) db)))))
43 ;;; GET-CONTEXT : null | module
44 (defun get-context ()
45 (if *current-module*
46 (module-context *current-module*)
47 nil))
48
49 ;;; GET-CONTEXT-MODULE
50 (defun get-context-module ()
51 *current-module*)
52
53 ;;; RESET-CONTEXT-MODULE
54 (defun reset-context-module (&optional (mod nil))
55 (setf *current-module* mod))
56
57 ;;; GET-OBJECT-CONTEXT object -> null | module
58 ;;;
59 (defun get-object-context (obj)
60 (or (get-context-module) (object-context-mod obj)))
9761
9862 ;;; BINDINGS *******************************************************************
9963
100 ;;; GET BOUND VALUES
64 ;;; GET-BOUND-VALUE : let-symbol -> value (a term) | null
65
10166 (defun is-special-let-variable? (name)
10267 (declare (values (or null t)))
10368 (and (>= (length (the simple-string name)) 3)
11075 :test #'(lambda (x y)
11176 (eq x (car y))))))
11277
113 (defun get-bound-value (let-sym &optional (mod *current-module*))
78 (defun get-bound-value (let-sym &optional (mod (get-context-module)))
11479 (or (cdr (assoc let-sym (module-bindings mod) :test #'equal))
11580 (when *allow-$$term*
11681 (cond ((equal let-sym "$$term")
137102 (cdr (assoc let-sym (module-bindings mod) :test #'equal)))
138103 (t nil)))))
139104
140 (defun set-bound-value (let-sym value &optional (mod *current-module*))
105 (defun set-bound-value (let-sym value &optional (mod (get-context-module)))
141106 (when (or (equal let-sym "$$term")
142107 (equal let-sym "$$subterm"))
143108 (with-output-chaos-error ('misc-error)
175140 (module-context-$$subterm context) $$subterm
176141 (module-context-$$action-stack context) $$action-stack
177142 (module-context-$$selection-stack context) $$selection-stack
178 (module-context-$$stop-pattern context) *rewrite-stop-pattern*
179 ;; (module-context-$$ptree context) *proof-tree*
180 ))))
143 (module-context-$$stop-pattern context) *rewrite-stop-pattern*))))
181144
182145 (defun new-context (mod)
183146 (unless mod
186149 $$action-stack nil
187150 $$selection-stack nil
188151 $$term-context nil
189 *last-module* nil
190 ;; !!!
191152 *current-module* nil
192 *rewrite-stop-pattern* nil
193 ;; *proof-tree* nil
194 )
153 *rewrite-stop-pattern* nil)
195154 (return-from new-context nil))
196 ;;
197155 (let ((context (module-context mod)))
198156 (setf $$term (module-context-$$term context)
199157 $$subterm (module-context-$$subterm context)
200158 $$action-stack (module-context-$$action-stack context)
201159 $$selection-stack (module-context-$$selection-stack context)
202 *rewrite-stop-pattern* (module-context-$$stop-pattern context)
203 ;;*proof-tree* (module-context-$$ptree context)
204 )
160 *rewrite-stop-pattern* (module-context-$$stop-pattern context))
205161 (setf $$term-context mod)
206 (setq *last-module* mod)
207 ;; !!!!!
208 (setq *current-module* mod)
209 ;; !!!!!
162 (reset-context-module mod)
210163 (clear-method-info-hash)
211164 t))
212165
222175 ;; save current context
223176 (save-context from)
224177 ;; restore new context
225 (new-context to)
226 )
178 (new-context to))
227179
228180 (defun reset-target-term (term old-mod mod)
229181 (if (eq mod old-mod)
233185 $$selection-stack nil)
234186 (save-context mod)
235187 (new-context mod))
236 ;; we do not change globals, instead set in context of mod.
237 (save-context mod)))
188 ;; we do not change globals, instead set in context of mod.
189 (save-context mod)))
238190 ;;;
239191 (defun context-push (mod)
240192 (push mod *old-context*))
247199 (change-context old new))
248200
249201 (defun context-pop-and-recover ()
250 (when (or *last-module* *current-module*)
202 (when (get-context-module)
251203 (let ((old (context-pop)))
252 (unless (member old (list *last-module* *current-module*))
204 (unless (eq old (get-context-module))
253205 ;; eval-mod may change the current context implicitly.
254206 ;; in this case we do not recover context.
255 (change-context *last-module* old)))))
207 (change-context (get-context-module) old)))))
256208
257209 ;;; EOF
210
9696
9797 ;;; FIND-SORTS-IN-MODULE
9898
99 (defun find-sorts-in-module (sort-name &optional (module (or *current-module*
100 *last-module*)))
99 (defun find-sorts-in-module (sort-name &optional (module (get-context-module)))
101100 (declare (type symbol sort-name)
102101 (type module module))
103102 (let ((res nil))
107106
108107 ;;; FIND-SORT
109108 ;;;
110 #||
111 (defun find-sort (sort-name-or-name-ref &optional (module (or *current-module*
112 *last-module*)))
113 (unless module
114 (error "Internal error,module is not specified: find-sort"))
115 (unless (module-p module)
116 (error "Internal error, find-sort: invalid module ~A" module))
117 (let ((sort (get-sort-named sort-name-or-name-ref module)))
118 (if sort sort
119 (let ((ambig-sorts (find-sorts-in-module sort-name-or-name-ref module)))
120 (cond ((= (length ambig-sorts) 1) (car ambig-sorts))
121 ((> (length ambig-sorts) 1)
122 (with-output-chaos-warning ()
123 (princ "sort name ")
124 (princ sort-name-or-name-ref)
125 (princ " is ambiguous, arbitrary take ")
126 (print-chaos-object (setf sort (car (nreverse ambig-sorts))))
127 (princ " as the resolved name.")
128 )
129 sort)
130 (t (or (get-sort-named sort-name-or-name-ref '*chaos-module*)
131 (with-output-chaos-warning ()
132 (princ "no such sort ")
133 (princ sort-name-or-name-ref)
134 nil))))))))
135
136 ||#
137109
138110 ;;; FIND-SORT-IN : Module Sort-Name -> Sort
139111 ;;;
8080 (and (fboundp (car ast))
8181 (symbol-function (car ast))))))
8282 (cond (evaluator
83 (let ((module (or ;; (chaos-eval-context ast)
84 ;; *chaos-eval-context*
85 *current-module*
86 *last-module*)))
83 (let ((module (get-context-module)))
8784 (when (and module (not (module-p module)))
8885 (setq module (find-module-in-env
8986 (normalize-modexp (string module)))))
590590 (cond ((chaos-ast? object)
591591 (let ((printer (ast-printer object)))
592592 (if printer
593 (let ((mod (or *current-module* *last-module*)))
593 (let ((mod (get-context-module)))
594594 (if mod
595595 (with-in-module (mod)
596596 (funcall printer object stream))
601601 ((and (chaos-object? object) (not (stringp object)))
602602 (let ((printer (object-printer object)))
603603 (if printer
604 (let ((mod (or *current-module*
605 *last-module*)))
604 (let ((mod (get-context-module)))
606605 (if mod
607606 (with-in-module (mod)
608607 (funcall printer object stream))
611610 (*print-pretty* nil))
612611 (prin1 object stream)))))
613612 ((term? object)
614 (let ((mod (or *current-module* *last-module*)))
613 (let ((mod (get-context-module)))
615614 (if mod
616615 (with-in-module (mod)
617616 (term-print object stream))
227227 (return-from get-importing-path
228228 (nconc path im2))))))))))
229229
230 (defun get-real-importing-mode (module2 &optional (module (or *current-module*
231 *last-module*)))
230 (defun get-real-importing-mode (module2 &optional (module (get-context-module)))
232231 (declare (type module module2 module)
233232 (values symbol))
234233 ;;
7777 (setq modexp (car modexp)))
7878 ;;
7979 (when (and (equal modexp "*the-current-module*")
80 *current-module*)
81 (setq modexp *current-module*))
82 ;; (when (and (equal modexp "THE-LAST-MODULE") *last-module*)
83 ;; (setq modexp *last-module*))
84 ;;
80 (get-context-module))
81 (setq modexp (get-context-module)))
8582 (cond ((module-p modexp) (normalize-modexp (module-name modexp)))
8683 ((stringp modexp) (canonicalize-simple-module-name modexp))
8784 ((atom modexp) modexp)
297297
298298 ;;; top level modexp printer ---------------------------------------------------
299299
300 #||
301300 (defun get-context-name (obj)
302 (let ((context-mod (object-context-mod obj)))
303 (if context-mod
304 (with-output-to-string (str)
305 (print-mod-name context-mod str t))
306 nil)))
307 ||#
308
309 (defun get-context-name (obj)
310 (let ((context-mod (object-context-mod obj)))
301 (let ((context-mod (get-object-context obj)))
311302 (if context-mod
312303 (get-module-print-name context-mod)
313304 nil)))
314305
315 (defun get-context-name-extended (obj &optional (context *current-module*))
306 (defun get-context-name-extended (obj &optional (context (get-context-module)))
316307 (let ((cmod (object-context-mod obj)))
308 (declare (type (or null module) cmod))
317309 (unless cmod (return-from get-context-name-extended nil))
318 ;;
319 (when context
320 (let ((als (assoc cmod (module-alias context))))
321 (when als
322 (return-from get-context-name-extended (cdr als)))))
323 ;;
310 (let ((als (assoc cmod (module-alias context))))
311 (when als
312 (return-from get-context-name-extended (cdr als))))
324313 (let ((name (get-module-print-name cmod)))
325314 (unless (module-is-parameter-theory cmod)
326315 (cond ((modexp-is-simple-name name)
878867
879868 (defun print-sort-internal (sort &optional (stream *standard-output*) ignore)
880869 (declare (ignore ignore))
881 (print-sort-name sort (or *current-module* *last-module*) stream))
870 (print-sort-name sort (get-object-context sort) stream))
882871
883872 (defun print-record-internal (sort &optional (stream *standard-output*) ignore)
884873 (declare (ignore ignore))
885 (print-sort-name sort (or *current-module* *last-module*) stream))
874 (print-sort-name sort (get-object-context sort) stream))
886875
887876 (defun print-class-internal (sort &optional (stream *standard-output*) ignore)
888877 (declare (ignore ignore))
889 (print-sort-name sort (or *current-module* *last-module*) stream))
878 (print-sort-name sort (get-object-context sort) stream))
890879
891880 (defun print-bsort-internal (sort &optional (stream *standard-output*) ignore)
892881 (declare (ignore ignore))
893 (print-sort-name sort (or *current-module* *last-module*) stream))
882 (print-sort-name sort (get-object-context sort) stream))
894883
895884 (defun print-and-sort-internal (sort &optional (stream *standard-output*) ignore)
896885 (declare (ignore ignore))
897 (print-sort-name sort (or *current-module* *last-module*) stream))
886 (print-sort-name sort (get-object-context sort) stream))
898887
899888 (defun print-or-sort-internal (sort &optional (stream *standard-output*) ignore)
900889 (declare (ignore ignore))
901 (print-sort-name sort (or *current-module* *last-module*) stream))
890 (print-sort-name sort (get-object-context sort) stream))
902891
903892 (defun print-err-sort-internal (sort &optional (stream *standard-output*) ignore)
904893 (declare (ignore ignore))
905 (print-sort-name sort (or *current-module* *last-module*) stream))
894 (print-sort-name sort (get-object-context sort) stream))
906895
907896 ;;; MODULE ************
908
909 ;;; (defun print-module-internal (module &optional (stream *standard-output*))
910 ;;; (print-mod-name module stream t t))
911897
912898 (defun print-module-internal (module &optional (stream *standard-output*) ignore)
913899 (declare (ignore ignore))
957943
958944 (defun print-method-internal (meth &optional (stream *standard-output*) ignore)
959945 (declare (ignore ignore))
960 (let ((mod (or *current-module* *last-module*))
946 (let ((mod (get-object-context meth))
961947 (.file-col. .file-col.))
962948 (format stream "~{~A~} :" (method-symbol meth))
963949 (setq .file-col. (file-column stream))
11951181
11961182 ;;; PRINT-SORT-NAME : sort &optional module stream -> Void
11971183 ;;;
1198 (defun print-sort-name (s &optional
1199 (module (or *current-module* *last-module*))
1200 (stream *standard-output*))
1184 (defun print-sort-name (s &optional (module (get-object-context s))
1185 (stream *standard-output*))
12011186 (unless (sort-struct-p s) (break "print-sort-name: given non sort: ~s" s))
12021187 (let ((*standard-output* stream)
12031188 (mod-name (get-module-print-name (sort-module s))))
5959 (defun make-applform (sort meth &optional args)
6060 (declare (type sort* sort)
6161 (type method meth)
62 (type list args)
63 (values term))
64 (if *consider-object*
65 (if (method-is-object-constructor meth)
66 (let ((id (car args)) ; the first argument is always an object
67 ; identifier.
68 (class sort))
69 #+:debug-term
70 (progn
71 (format t "~&object construction: ")
72 (print-object meth)
73 (force-output))
74 (if (not (term-is-variable? id)) ; non variable means the term
75 ; denotes a concrete instance.
76 (let ((instance nil))
77 (setf instance (find-instance id class))
78 (if instance
79 (progn (setf (term-arg-3 instance) (third args))
80 instance)
81 (progn (setf instance
82 (make-applform-simple sort meth args))
83 (register-instance instance)
84 instance)))
85 (make-applform-simple sort meth args)
86 ))
87 (make-applform-simple sort meth args) )
88 (make-applform-simple sort meth args)))
62 (type list args))
63 (make-applform-simple sort meth args))
8964
9065 ;;; ******************
9166 ;;; RESET-REDUCED-FLAG
288263 (term-print-with-sort term)))))
289264 (if (term$is-builtin-constant? body)
290265 ;; built-in constant term
291 (let ((so (module-sort-order
292 (if *current-module*
293 *current-module*
294 (or *last-module*
295 (sort-module (term$sort body))))))
296 (isrt (term$sort body))
297 (val (term$builtin-value body)))
266 (let* ((isrt (term$sort body))
267 (cm (get-object-context isrt))
268 (so (if cm
269 (module-sort-order cm)
270 (with-output-chaos-error ('internal-error)
271 (format t "Internal Error, No context module [ULP]."))))
272 (val (term$builtin-value body)))
298273 (declare (type sort-order so)
299274 (type sort* isrt)
300275 (type t val))
314289
315290 ;; application form
316291 (let* ((head (term$head body))
317 (mod (if *current-module*
318 *current-module*
319 (or *last-module*
320 (operator-module (method-operator head)))))
292 (mod (get-object-context (method-operator head)))
321293 (son nil)
322294 (t1 nil)
323295 (t2 nil)
325297 (new-head nil))
326298 (declare (type method head)
327299 (type module mod))
328 ;; #||
329 (when (method-is-error-method head)
330 (when *term-debug*
331 (with-output-msg ()
332 (format t "ULP:ERR_TERM: ")
333 (term-print-with-sort term)))
334 ;; recursively
335 (dolist (sub (term-subterms term))
336 (update-lowest-parse sub)))
337 ;; ||#
338
339300 ;; ----------------------------
340301 ;; special case if_then_else_fi
341302 ;; ----------------------------
824785 ;;; method, otherwise, given method is used.
825786 (defvar **sa-debug** nil)
826787 (defun make-term-with-sort-check (meth subterms
827 &optional (module (or *current-module*
828 *last-module*)))
788 &optional (module (get-context-module)))
829789 (declare (type method meth)
830790 (type list subterms)
831791 (type module module))
14661426 (sort-is-hidden x))
14671427 (mapcar #'(lambda (y) (term-sort y))
14681428 (term-subterms term)))
1469 ;; patch Tue May 26 10:11:22 JST 1998
14701429 (or (method-is-behavioural (term-head term))
14711430 (method-is-coherent (term-head term)))
14721431 t)
1473 (every #'term-can-be-in-beh-axiom? (term-subterms term))))
1474 )
1432 (every #'term-can-be-in-beh-axiom? (term-subterms term)))))
14751433 (t t)))
14761434
14771435 (defun term-is-non-behavioural? (term)
170170 (term-subterms term)))
171171 (simple-copy-term term))))
172172
173 (defun get-qualified-op-pattern (tok &optional (module (or *current-module*
174 *last-module*)))
173 (defun get-qualified-op-pattern (tok &optional (module (get-context-module)))
175174 (labels ((destruct-op-name (expr)
176175 (let ((pos (position #\_ expr)))
177176 (declare (type (or null fixnum) pos))
16821681 (print-terletox-list terletox-list))
16831682 terletox-list))
16841683
1685 (defun test-sort-memb-predicate (term &optional (module (or *current-module*
1686 *last-module*)))
1684 (defun test-sort-memb-predicate (term &optional (module (get-context-module)))
16871685 (unless module
16881686 (with-output-chaos-error ('no-context)
16891687 (princ "checking _:_, no context module is given!")))
16901688 (with-in-module (module)
16911689 (let ((arg1 (term-arg-1 term))
16921690 (id-const (term-arg-2 term)))
1693 ;; (format t "~&arg1 = ")(print arg1)
1694 ;; (format t "~&id-const = ") (print id-const)
16951691 (let ((sorts (gather-sorts-with-id id-const module))
16961692 (term-sort (term-sort arg1)))
16971693 (unless sorts
7373 (term-is-congruent-2? (macro-rhs macro1)
7474 (macro-rhs macro2))))
7575
76 (defun expand-macro (term &optional (module (or *current-module*
77 *last-module*)))
76 (defun expand-macro (term &optional (module (get-context-module)))
7877 (labels ((apply-macro-rule (macro term)
7978 (block the-end
8079 (multiple-value-bind (global-state subst no-match E-equal)
368368 ;;; parse-convert : term -> term'
369369 ;;;
370370 (defun parse-convert (term
371 &optional (module (or *current-module* *last-module*)))
371 &optional (module (get-context-module)))
372372 ;; #define macro expand
373373 (when *macroexpand*
374374 (setq term (expand-macro term module)))
3939 ;;;
4040 ;;; CHECK COMPATIBILITY
4141 ;;;
42 (defun check-compatibility (&optional (module (or *last-module*
43 *current-module*)))
42 (defun check-compatibility (&optional (module (get-context-module)))
4443 (unless module
4544 (with-output-chaos-error ('no-context)
46 (princ "no context (current) module is specified!")
47 ))
45 (princ "no context (current) module is specified!")))
4846 ;;
4947 (unless *on-preparing-for-parsing*
5048 (prepare-for-parsing module))
4848 ;;; 1. constants(operations free from axioms) are always strict.
4949 ;;;
5050 (defun check-method-strictness (meth &optional
51 (module (or *current-module*
52 *last-module*
53 ))
51 (module (get-context-module))
5452 report?)
5553
5654 (unless module
5755 (with-output-chaos-error ('no-cntext)
58 (princ "checking lazyness: no context module is specified!")
59 ))
56 (princ "checking lazyness: no context module is specified!")))
6057 ;;
6158 (with-in-module (module)
6259 (cond ((and (null (method-rules-with-different-top meth))
144141 (values (reverse strategy) (reverse end-strategy)))))
145142 )))
146143
147 (defun check-operator-strictness (op &optional (module (or *last-module*
148 *current-module*))
149 report?)
144 (defun check-operator-strictness (op &optional (module (get-context-module))
145 report?)
150146 (unless module
151147 (with-output-chaos-error ('no-context)
152 (princ "no context (current) module is given!")
153 ))
148 (princ "no context (current) module is given!")))
154149 ;;
155150 (let* ((opinfo (if (opinfo-p op)
156151 (prog1 op (setq op (opinfo-operator op)))
179174 (push (list meth str-list1 str-list2) res))))
180175 (nreverse res)))
181176
182 (defun check-operator-strictness-whole (&optional (module (or *last-module*
183 *current-module*))
177 (defun check-operator-strictness-whole (&optional (module (get-context-module))
184178 report?)
185179 (unless module
186180 (with-output-chaos-error ('no-context)
187 (princ "no context (current) module is specified!")
188 ))
181 (princ "no context (current) module is specified!")))
189182 ;;
190183 (let ((result nil))
191184 (dolist (opinfo (ops-to-be-shown module))
243236 nil))))
244237
245238 (defun check-method-coherency (meth &optional
246 (module (or *current-module* *last-module*))
239 (module (get-context-module))
247240 (warn t))
248241 (unless module
249242 (with-output-chaos-error ('no-cntext)
520513
521514 (defun check-operator-coherency (op
522515 &optional
523 (module (or *current-module* *last-module*))
524 (warn t)
525 )
516 (module (get-context-module))
517 (warn t))
526518
527519 (unless module
528520 (with-output-chaos-error ('no-context)
529 (princ "no context (current) module is given!")
530 ))
521 (princ "no context (current) module is given!")))
531522 ;;
532523 (let* ((opinfo (if (opinfo-p op)
533524 (prog1 op (setq op (opinfo-operator op)))
616607 ))
617608
618609 (defun check-method-congruency (meth iobservers
619 &optional (module (or *current-module*
620 *last-module*)))
610 &optional (module (get-context-module)))
621611 (unless module
622612 (with-output-panic-message ()
623613 (princ "congruence check: no context module!")))
5454 (non-empties nil) ; non-empty methods.
5555 )
5656
57 (defun print-sop (sop &optional (module (or *current-module*
58 *last-module*)))
57 (defun print-sop (sop &optional (module (get-context-module)))
5958 (with-in-module (module)
6059 (format t "~%** SOP : operator ")
6160 (print-chaos-object (sop-operator sop))
600599 (cadr m)))
601600
602601 (defun check-method-redundancy (arity coarity method-list
603 &optional (module (or *current-module*
604 *last-module*)))
602 &optional (module (get-context-module)))
605603 (let ((so (module-sort-order module))
606604 (redundant-methods nil)
607605 (not-tobe-added? nil))
4949 (with-output-msg ()
5050 (princ "no current context, `select' some module first."))
5151 (return-from show-context nil))
52 (if (eq *last-module* mod)
52 (if (eq (get-context-module) mod)
5353 (format t "~&-- current context :")
5454 (progn (format t "~&-- context of : ")
5555 (print-chaos-object mod)))
56 (context-push-and-move *last-module* mod)
56 (context-push-and-move (get-context-module) mod)
5757 (with-in-module (mod)
5858 (format t "~&[module] ")
5959 (print-chaos-object *current-module*)
7979
8080 ;;; SHOW BINDINGS
8181
82 (defun show-bindings (&optional (module *last-module*))
82 (defun show-bindings (&optional (module (get-context-module)))
8383 (unless module
8484 (with-output-msg ()
8585 (princ "no context (current module) is specified.")
106106
107107 ;;; show apply selection
108108
109 (defun show-apply-selection (&optional (module *last-module*))
109 (defun show-apply-selection (&optional (module (get-context-module)))
110110 (unless module
111111 (with-output-msg ()
112112 (princ "no context (current module) is specified.")
137137 (terpri)
138138 (incf depth)))))
139139
140 ;; (format t "~&[selections] ~{~a~^ of ~}" $$selection-stack)
141
142140 ;;;
143141 ;;; print-pending
144142 ;;;
145 (defun print-pending (&optional (module *last-module*))
143 (defun print-pending (&optional (module (get-context-module)))
146144 (unless module
147145 (with-output-msg ()
148146 (princ "no context (current module) is specified.")
184182 (with-output-chaos-warning ()
185183 (format t "unknown option for `show term' : ~a" tree?))
186184 (return-from show-term nil))
187 (unless *last-module*
185 (unless (get-context-module)
188186 (with-output-msg ()
189187 (princ "no current context, `select' some module first.")
190188 (return-from show-term nil)))
191189 (unless target
192190 (setq target "$$term"))
193 (with-in-module (*last-module*)
191 (with-in-module ((get-context-module))
194192 (when (stringp target)
195193 ;; let variable
196194 (catch 'term-context-error
389387 (check-qualified-sort-name sort)
390388 (cond (modexp
391389 (setq mod (eval-modexp modexp))
392 #||
393 (find-module-in-env-ext modexp (or
394 *current-module*
395 *last-module*)
396 :no-error)
397 ||#
398390 (unless (module-p mod)
399391 (with-output-msg ()
400392 (format t "no such module ~a" modexp)
401393 (return-from show-sort nil))))
402 (t (setq mod (or *current-module*
403 *last-module*))
394 (t (setq mod (get-context-module))
404395 (unless (module-p mod)
405396 (with-output-msg ()
406397 (princ "no context(current) module, select some first.")
434425 (defun get-module-from-opref (parsedop)
435426 (let ((mod nil))
436427 (cond ((%opref-module parsedop)
437 #||
438 (setq mod (find-module-in-env-ext (%opref-module parsedop)
439 (or *current-module*
440 *last-module*)
441 :no-error))
442 ||#
443428 (setq mod (%opref-module parsedop))
444429 (unless (module-p mod)
445430 (setq mod (eval-modexp (%opref-module parsedop)))
450435 (print-next)
451436 (princ "no such module ")
452437 (princ (%opref-module parsedop))))))
453 (t (setq mod (or *current-module*
454 *last-module*))
438 (t (setq mod (get-context-module))
455439 (unless mod
456440 (with-output-chaos-error ('no-context)
457441 (princ "no context module is given.")))))
506490 (defun show-param (toks no &optional describe)
507491 (let ((mod (if toks
508492 (eval-mod-ext toks)
509 (or *last-module* *current-module*))))
493 (get-context-module))))
510494 (unless mod
511495 (with-output-msg ()
512496 (format t "no context (current module) is specified.")
5858 kinds)
5959 kinds)))
6060
61 (defun make-sort-tree (sort &optional (mod (or *current-module* *last-module*)))
61 (defun make-sort-tree (sort &optional (mod (get-object-context sort)))
6262 (let* ((so (module-sort-order mod))
6363 (kind (the-err-sort sort so))
6464 (sls (module-sort-relations mod))
7676
7777 (defun print-sort-tree (sort &optional
7878 (stream *standard-output*)
79 (mod (or *current-module* *last-module*)))
79 (mod (get-object-context sort)))
8080 (!print-sort-tree sort stream mod nil))
8181
8282 (defun print-sort-graph (sort &optional
8383 (stream *standard-output*)
84 (mod (or *current-module* *last-module*)))
84 (mod (get-object-context sort)))
8585 (!print-sort-tree sort stream mod t))
8686
8787 (defun !print-sort-tree (sort stream mod show-as-graph)
103103
104104 ;;; PRINT-MODULE-SORT-TREE
105105
106 (defun print-module-sort-tree (&optional (mod (or *current-module* *last-module*))
106 (defun print-module-sort-tree (&optional (mod (get-context-module))
107107 (stream *standard-output*))
108108 (!print-module-sort-tree mod stream nil))
109109
110 (defun print-module-sort-graph (&optional (mod (or *current-module* *last-module*))
110 (defun print-module-sort-graph (&optional (mod (get-context-module))
111111 (stream *standard-output*))
112112 (!print-module-sort-tree mod stream t))
113113
144144 ;;
145145 (with-output-chaos-error ('tram-fail)
146146 (format t "failed to invoke TRAM compiler")
147 (when *last-module*
148 (context-pop-and-recover))
149 ))
147 (when (get-context-module)
148 (context-pop-and-recover))))
150149
151150 ;;
152151 (setq *tram_in_file* in-file
153152 *tram_out_file* out-file)
154153
155 #||
156 ;; wait for a while untill i/o files are prepared
157 (dotimes (x 30)
158 x
159 (sleep 1)
160 (when (probe-file in-file) (return nil)))
161 ||#
162
163154 ;; try open streams
164155 (setq out-stream (open out-file
165156 :direction :output
171162 (unless (and in-stream out-stream)
172163 (with-output-chaos-error ('tram-fail)
173164 (format t "failed to open TRAM I/O streams")
174 (when *last-module*
175 (context-pop-and-recover))
176 ))
177 (setq *tram-process* (cons in-stream out-stream))
178 ))
165 (when (get-context-module)
166 (context-pop-and-recover))))
167 (setq *tram-process* (cons in-stream out-stream))))
179168
180169 (defun kill-tram-process ()
181170 (setq *tram-last-module* nil)
289278 (format t "Unkonwn TRAM term ~s is returned.~
290279 ~% This can happen if signature is not regular..."
291280 tram-term)
292 (when *last-module*
281 (when (get-context-module)
293282 (context-pop-and-recover))
294283 (chaos-error 'tram-panic)))))))
295284
751740 ;;; TRAM-COMPILE-CHAOS-MODULE
752741 ;;;
753742 (defun tram-compile-chaos-module (&optional all?
754 (module (or *current-module*
755 *last-module*))
743 (module (get-context-module))
756744 debug)
757745 ;;
758746 (unless debug (run-tram-process-if-need))
7272
7373 (defvar *top-level-definition-in-progress* nil)
7474
75 ;;; *last-module* bounds the module object which was the target of the operation
76 ;;; in the last time, i.e., whenever the context is swithced, *last-module*
77 ;;; bounds the last `current module'.
7875 ;;; *open-module* bounds the 'opening' module.
7976 ;;;
8077 (declaim (special *open-module* ; the module crrently opened.
81 *last-module* ; the module which was the current last
82 ; time.
8378 *last-before-open* ; the module which was *last* before the
8479 ; currently opened module.
8580 ))
8984 (defvar *current-opinfo-table* nil)
9085 (defvar *current-ext-rule-table* nil)
9186 (defvar *open-module* nil)
92 (defvar *last-module* nil)
9387 (defvar *last-before-open* nil)
9488
9589 ;;; Feature for require & provide
33 -- imperative program", MIT Press.
44 -- * program codes are converted from OBJ to CafeOBJ
55 --
6 "ZZ extends CafeoBJ's built-in representation of the integers with an
6
7 #|------------------------------
8 ZZ extends CafeoBJ's built-in representation of the integers with an
79 equality predicate, _is_, and with some equations that are useful for
810 manipulating inequalities. In paricular, these equations are useful
911 as lemmas in the correctness proof given in the book. For example,
1820 lemma for the proof. In fact, there is not set of equations that can allow
1921 the automatic verification of all properties of integer expressions which
2022 contain indeterminate values such as `s[['X]]'; in other words, first order
21 arithmetic is \"undecidable\".
22 "
23 arithmetic is "undecidable".
24 --------------------------|#
2325
2426 module ZZ {
2527 imports {
2628 protecting (INT)
2729 }
2830 signature {
29 "The predicate _is_ is intended to represent equality on integers.
31 #|
32 The predicate _is_ is intended to represent equality on integers.
3033 The reason for introducing a new equality predicate rather then
3134 using CafeOBJ's builtin equality _==_ is that we want to use
3235 integer expressions which indeterminate values in program
33 correctness proofs (cf. Section 2.1.1 of Chapter2). "
36 correctness proofs (cf. Section 2.1.1 of Chapter2).
37 |#
3438 op _is_ : Int Int -> Bool
3539 }
3640 axioms {
124124 (:file "match-method")
125125 (:file "axiom")
126126 (:file "gen-rule")
127 (:file "cr")
128127 (:file "rwl")
129128 (:file "beh")
130129 (:file "module")
160160 "construct/match-method"
161161 "construct/axiom"
162162 "construct/gen-rule"
163 "construct/cr"
164163 "construct/rwl"
165164 "construct/beh"
166165 "construct/module"
8787 nil))))
8888
8989 (defun disp-term (x)
90 (with-in-module (*last-module*)
90 (with-in-module ((get-context-module))
9191 (term-print x)
9292 (princ " : ")
9393 (print-sort-name (term-sort x) *current-module*)))
513513 trmtoks
514514 avar
515515 aterm)
516 ;; (!setup-parse *last-module*)
517 (with-in-module (*last-module*)
516 (with-in-module ((get-context-module))
518517 (loop (when (null substtoks) (return))
519518 ;; <varid> = <term>
520519 (setq varnm (cadr substtoks))
14201420 (bind nil)
14211421 ;; the followings are experimental
14221422 (if nil))
1423 (let ((module (or *current-module* *last-module*))
1423 (let ((module (get-context-module))
14241424 max-r
14251425 max-d)
14261426 (unless module
4141 ;;; *****
4242
4343 (defun eval-start-command (ast)
44 (do-eval-start-th (%start-target ast) *last-module*))
44 (do-eval-start-th (%start-target ast) (get-context-module)))
4545
4646 (defun do-eval-start-th (pre-term &optional context)
4747 (catch 'apply-context-error
4848 (let ((mod (if context
4949 (eval-modexp context)
50 *last-module*)))
50 (get-context-module))))
5151 (if (or (null mod) (modexp-is-error mod))
5252 (if (null mod)
5353 (with-output-chaos-error ('invalid-module)
6464 (setq target (get-bound-value (car pre-term))))
6565 (unless target
6666 (return-from do-eval-start-th nil))
67 (when (eq mod *last-module*)
67 (when (eq mod (get-context-module))
6868 (setq $$action-stack nil))
6969 (reset-reduced-flag target)
70 (reset-target-term target *last-module* mod)))
70 (reset-target-term target *current-module* mod)))
7171 (t
7272 (let ((*parse-variables* nil))
7373 (let ((res (simple-parse *current-module*
7575 *cosmos*)))
7676 (when (term-is-an-error res)
7777 (return-from do-eval-start-th nil))
78 (when (eq *last-module* mod)
78 (when (eq (get-context-module) mod)
7979 (setq $$action-stack nil))
80 (reset-target-term res *last-module* mod))))))
80 (reset-target-term res *current-module* mod))))))
8181 ;; try use $$term
8282 (progn
8383 (when (or (null $$term) (eq 'void $$term))
8585 (format t "no target term is given!")
8686 (return-from do-eval-start-th nil)))
8787 (check-apply-context mod)
88 (when (eq *last-module* mod)
88 (when (eq (get-context-module) mod)
8989 (setq $$action-stack nil))
9090 (reset-reduced-flag $$term)
91 (reset-target-term $$term *last-module* mod)
92 )))
93 ;; (clear-found-rules)
91 (reset-target-term $$term (get-context-module) mod))))
9492 (when (command-final) (command-display))
9593 t)))
9694
111109 (setq $$subterm $$term)
112110 (setq $$selection-stack nil)
113111 (return-from eval-choose-command nil))
114 (with-in-module (*last-module*)
112 (with-in-module ((get-context-module))
115113 (multiple-value-bind (subterm-sort subterm)
116114 (compute-selection $$subterm selectors)
117115 (declare (ignore subterm-sort))
156154 (with-output-chaos-error ('invalid-term)
157155 (princ "term to be applied is not defined.")
158156 ))
159 (unless *last-module*
157 (unless (get-context-module)
160158 (with-output-chaos-error ('no-context-module)
161 (princ "no current module.")
162 ))
159 (princ "no current module.")))
163160 ;; real work begins here ------------------------------
164 (with-in-module (*last-module*)
161 (with-in-module ((get-context-module))
165162 (multiple-value-bind (subterm-sort subterm)
166163 (compute-selection $$term selectors)
167164 (setq *-applied-* t)
168165 (case action
169166 (:reduce ; full reduction on selections.
170 (!setup-reduction *last-module*)
167 (!setup-reduction *current-module*)
171168 (let ((*rewrite-semantic-reduce*
172 (module-has-behavioural-axioms *last-module*))
169 (module-has-behavioural-axioms *current-module*))
173170 (*rewrite-exec-mode* nil))
174171 (term-replace subterm (@copy-term subterm))
175172 (reset-reduced-flag subterm)
176 (rewrite subterm *last-module*)))
173 (rewrite subterm *current-module*)))
177174 (:breduce
178 (!setup-reduction *last-module*)
175 (!setup-reduction *current-module*)
179176 (let ((*rewrite-semantic-reduce* nil)
180177 (*rewrite-exec-mode* nil))
181178 (term-replace subterm (@copy-term subterm))
182179 (reset-reduced-flag subterm)
183 (rewrite subterm *last-module*)))
180 (rewrite subterm *current-module*)))
184181 (:exec
185 (!setup-reduction *last-module*)
182 (!setup-reduction *current-module*)
186183 (let ((*rewrite-semantic-reduce*
187 (module-has-behavioural-axioms *last-module*))
184 (module-has-behavioural-axioms *current-module*))
188185 (*rewrite-exec-mode* t))
189186 (term-replace subterm (@copy-term subterm))
190187 (reset-reduced-flag subterm)
191 (rewrite subterm *last-module*)))
188 (rewrite subterm *current-module*)))
192189 ;;
193190 (:print ; print selections.
194191 (format t "~&term ")
214211 (update-lowest-parse $$term)
215212 (when (nth 2 rule-spec) ; reverse order
216213 (setq $$term (@copy-term $$term)))
217 (reset-target-term $$term *last-module* *last-module*))
218 ) ; end :apply
214 (reset-target-term $$term *current-module* *current-module*))) ; end :apply
219215 (t (with-output-panic-message ()
220216 (format t "unknown apply action : ~a" action)
221217 (chaos-error 'unknown-action))))
450446 (when (and rev (or (rule-is-builtin rule)
451447 (eq (axiom-type rule) :rule)))
452448 (format t "~&This rule cannot be applied reversed."))
453 (when (and *last-module*
449 (when (and (get-context-module)
454450 (not (rule-is-builtin rule)))
455451 (format t "~&(This rule rewrites up.)"))))))))
456452 t))
4646 ;;; ******
4747
4848 (defun eval-match-command (ast)
49 (unless *last-module*
49 (unless (get-context-module)
5050 (with-output-chaos-error ('no-current-module)
5151 (princ "no current module.")))
5252 (let ((type (%match-type ast))
5757 $$subterm
5858 $$term))
5959 (t (let* ((*parse-variables* nil)
60 (parsed (with-in-module (*last-module*)
60 (parsed (with-in-module ((get-context-module))
6161 (simple-parse *current-module*
6262 (%match-target ast)
6363 *cosmos*))))
8484
8585 (defun find-rewrite-rules-top (target what &optional (type :match))
8686 (let* ((real-target (supply-psuedo-variables target))
87 (patterns (find-matching-rules what real-target *last-module* type)))
87 (patterns (find-matching-rules what real-target (get-context-module) type)))
8888 (unless patterns
89 (with-in-module (*last-module*)
89 (with-in-module ((get-context-module))
9090 (format t "~&no rules found for term : ")
9191 (term-print target))
9292 (return-from find-rewrite-rules-top nil))
9393 ;; report the result
9494 (format t "~&== matching rules to term : ")
95 (with-in-module (*last-module*)
95 (with-in-module ((get-context-module))
9696 (let ((*fancy-print* nil))
9797 (term-print target))
9898 (dolist (pat patterns)
127127
128128 (defun find-rewrite-rules-all (target what &optional (type :match))
129129 (let* ((real-target (supply-psuedo-variables target))
130 (patterns (find-matching-rules-all what real-target *last-module* type)))
130 (patterns (find-matching-rules-all what real-target (get-context-module) type)))
131131 (unless patterns
132 (with-in-module (*last-module*)
132 (with-in-module ((get-context-module))
133133 (format t "~&no rules found for term : ")
134134 (term-print target))
135135 (return-from find-rewrite-rules-all nil))
136136 ;; report the result
137137 (format t "~&== matching rules to term : ")
138 (with-in-module (*last-module*)
138 (with-in-module ((get-context-module))
139139 (let ((*fancy-print* nil))
140140 (term-print target))
141141 (dolist (pat patterns)
192192 nil
193193 'next-match)
194194 'next-unify)))
195 (with-in-module (*last-module*)
195 (with-in-module ((get-context-module))
196196 (let* ((*parse-variables* (mapcar #'(lambda (x)
197197 (cons (variable-name x) x))
198198 (term-variables target)))