Codebase list cafeobj / b280d0e
* Started implementing inspecter for Bool valued terms. tswd 8 years ago
34 changed file(s) with 760 addition(s) and 311 deletion(s). Raw diff Collapse all Expand all
17871787 (princ "---"))
17881788 *full-lit-table*)))
17891789
1790 (defun show-demodulators (&optional (mod (get-context-module)))
1790 (defun show-demodulators (&optional (mod (get-context-module t)))
17911791 (unless mod (return-from show-demodulators nil))
17921792 (with-in-module (mod)
17931793 (let* ((psys (module-proof-system mod))
579579 ;;;
580580 (defun eval-resolve (ast)
581581 (let ((eval-context (get-context-module)))
582 (unless eval-context
583 (with-output-chaos-error ('no-context)
584 (princ "no context (current module) is set!")))
585582 (let ((out-file (%resolve-arg ast)))
586583 (if (and out-file (not (equal out-file ".")))
587584 (with-open-file (stream out-file :direction :output
712709 (result-as-text (%demod-return-text ast)))
713710 (perform-demodulation* preterm modexp mode result-as-text)))
714711
712 ;;; *** TODO ***
713 ;;; use reducer.
715714 (defun perform-demodulation* (preterm &optional modexp mode (result-as-text nil))
716715 (let ((*consider-object* t)
717716 (*rewrite-exec-mode* (eq mode :exec))
724723 (number-matches nil))
725724 (let ((mod (if modexp
726725 (eval-modexp modexp)
727 (get-context-module))))
728 (unless (eq mod (get-context-module))
726 (get-context-module t))))
727 (unless (eq mod (get-context-module t))
729728 (clear-term-memo-table *term-memo-table*))
730729 (if (or (null mod) (modexp-is-error mod))
731730 (if (null mod)
735734 (princ "incorrect module expression, no such module ")
736735 (print-chaos-object modexp)))
737736 (progn
738 (context-push-and-move (get-context-module) mod)
737 (context-push-and-move (get-context-module t) mod)
739738 (with-in-module (mod)
740739 (auto-db-reset mod))
741740 (with-proof-context (mod)
742741 (when *auto-context-change*
743 (change-context (get-context-module) mod))
742 (change-context (get-context-module t) mod))
744743 (!setup-reduction mod)
745744 (setq $$mod (get-context-module))
746745 (setq sort *cosmos*)
867866
868867 ;;; LEX
869868 (defun eval-pn-lex (ast)
870 (unless (get-context-module)
871 (with-output-chaos-error ('no-context)
872 (princ "no context(current) module is specified.")))
873869 (compile-module (get-context-module))
874870 (with-in-module ((get-context-module))
875871 (let ((optokens (%pn-lex-ops ast))
771771 (defun pn-check-invariance (args)
772772 (declare (type list args))
773773 (let ((target-module (get-context-module)))
774 (declare (type (or null module) target-module))
775 (unless target-module
776 (with-output-chaos-error ('no-context)
777 (princ "check invariance: no context module is specified!")))
778 ;;
774 (declare (type module target-module))
779775 (compile-module target-module)
780776 (multiple-value-bind (pred-name object-pat init-name loop-after
781777 loop-after-sub
234234 ;;; CafeOBJ INTERPRETER TOPLEVEL HELP
235235 ;;;
236236 (defun print-context-info ()
237 (let ((cmod (get-context-module)))
237 (let ((cmod (get-context-module t)))
238238 (cond ((null cmod)
239239 (format t "~&You are at top level, no context module is set."))
240240 (*open-module*
385385 (defun print-cafeobj-prompt ()
386386 (fresh-all)
387387 (flush-all)
388 (let ((cur-module (get-context-module)))
388 (let ((cur-module (get-context-module t)))
389389 (cond ((eq *prompt* 'system)
390390 (if cur-module
391391 (if (module-is-inconsistent cur-module)
23092309 :category :proof
23102310 :parser citp-parse-red
23112311 :evaluator eval-citp-red
2312 :title "`{ :red | :exec | :bred } <term> .`"
2313 :doc "TODO"
2312 :title "`{ :red | :exec | :bred } [in <goal-name> :] <term> .`"
2313 :doc "reduce the term in specified goal <goal-name>. "
23142314 )
23152315
23162316 (define (":verbose")
23422342 :parser citp-parse-ctf
23432343 :evaluator eval-citp-ctf
23442344 :title "`:ctf { eq [ <label-exp> ] <term> = <term> .}`"
2345 :doc "TODO"
2346 )
2347
2348 (define (":pctf")
2349 :category :proof
2350 :parser citp-parse-pctf
2351 :evaluator eval-citp-pctf
2352 :title "`:pctf { <bool-term> . ... <bool-term> .}`"
2353 :doc "TODO"
2354 )
2355
2356 (define (":pctf-")
2357 :category :proof
2358 :parser citp-parse-pctf
2359 :evaluator eval-citp-pctf
2360 :title "`:pctf- { <bool-term> . ... <bool-term> . }`"
23452361 :doc "TODO"
23462362 )
23472363
23832399 :category :proof
23842400 :parser citp-parse-spoiler
23852401 :evaluator identity
2386 :title "`:spoiler { on | off}"
2402 :title "`:spoiler { on | off}`"
23872403 :doc "TODO"
23882404 )
2405
2406 (define (":binspect")
2407 :category :proof
2408 :parser parse-citp-binspect
2409 :evaluator eval-citp-binspect
2410 :title "`:binspect [in <goal-name> :] <boolean-term> .`"
2411 :doc "TODO"
2412 )
2413
2414 (define ("binspect")
2415 :category :proof
2416 :parser parse-citp-binspect
2417 :evaluator eval-citp-binspect
2418 :title "`binspect [in <module-name> :] <boolean-term> .`"
2419 :doc "TODO"
2420 )
2421
2422 ; (define ("binspect")
2423 ; :cateogory :proof
2424 ; :parser parse-citp-binspect
2425 ; :evaluator eval-citp-binspect
2426 ; :title "`binspect [in <module-name> :] <boolean-term> .`"
2427 ; :doc "TODO"
2428 ; )
23892429
23902430 ;;;
23912431
24102450 ")
24112451
24122452 ;;;
2413 ) ; end eval-when
2453 ) ; end eval-when
24142454 ;;; EOF
807807
808808 |}|)
809809 (\[ :term |.| \])))
810 ((:+ |:pctf| |:pctf-|)
811 |{| (:many-of (\[ :term |.| \])) |}|)
810812 ((:+ |:csp| |:csp-|)
811813 |{| (:many-of #.EqDeclaration
812814 #.RlDeclaration
816818 |}|)
817819 ((:+ |:show| |:sh| |:describe| |:desc|) :args)
818820 (|:spoiler| (:one-of (on) (off) (|.|)))
821 ((:+ |:binspect|)
822 (:rdr #..term-delimiting-chars. (:if-present in :symbol |:|)) (:seq-of :term) |.|)
819823 )) ; end Top-Form
820824
821825 ;; some separated definitions of non-terminals.
6060 (*rule-count* 0)
6161 (*term-memo-hash-hit* 0)
6262 ($$term nil)
63 ($$term-context nil)
6364 ($$cond nil)
6465 ($$target-term nil)
6566 ($$norm nil)
8182 *term-memo-hash-hit*
8283 $$target-term
8384 $$term
85 $$term-context
8486 $$cond
8587 $$target-term
8688 $$norm
171173
172174 ;; reset-term-memo-table
173175 (defun reset-term-memo-table (module)
174 (unless (eq module (get-context-module))
176 (unless (eq module (get-context-module t))
175177 (clear-term-memo-table *term-memo-table*)))
176178
177179 ;; prepare-reduction-env
4949
5050 (defconstant term-hash-mask #x1FFFFFFF)
5151
52 (declaim (type fixnum term-hash-size))
5253 (defconstant term-hash-size 9001)
5354
5455 (defmacro method-has-memo-safe (m)
894894 (with-output-chaos-error ('invalid-rule-spec)
895895 (format t "No rule number or name is specified.")))
896896 ;; get module in which the specified rule is looked up
897 (let ((cur-context (get-context-module)))
897 (let ((cur-context (get-context-module t)))
898898 (if (equal "" mod)
899899 (setq mod cur-context)
900900 (if (and cur-context
969969 error-operator)
970970 (declare (type list opinfo arity)
971971 (type sort-struct coarity)
972 (type (or null module) module))
972 (type module module))
973973 ;;
974974 (let ((meth nil))
975975 (dolist (m (opinfo-methods opinfo))
13981398 :test #'equal))
13991399 (eval-ast decl)))
14001400
1401 (defun setup-error-operators-in (&optional (module (or (get-context-module))))
1401 (defun setup-error-operators-in (&optional (module (get-context-module)))
14021402 (declare (type module module)
14031403 (values t))
14041404 (let ((all-error-operators nil))
7575 (me (normalize-modexp modexp)))
7676 ;; "." -> current context module
7777 (when (and (equal me ".")
78 (get-context-module))
79 (return-from eval-modexp (get-context-module)))
78 (get-context-module t))
79 (return-from eval-modexp (get-context-module t)))
8080 (when (stringp me)
8181 ;; simple name
8282 (let ((pos (position #\. (the simple-string me) :from-end t)))
9393 (format t "~% no such module ~s" qual)))
9494 (setf mod (find-module-in-env name context))))
9595 (setq mod (find-module-in-env me (if also-local
96 (get-context-module)
96 (get-context-module t)
9797 nil))))))
9898 (if mod
9999 (if reconstruct-if-need
106106 (declare (special *on-autoload*))
107107 (!input-file (cdr ent)))
108108 (setq mod (find-module-in-env me (if also-local
109 (get-context-module)
109 (get-context-module t)
110110 nil)))
111111 (if mod
112112 mod
137137 (fresh-all)
138138 (flush-all)
139139 (format t "~%[")
140 (if (get-context-module)
140 (if (get-context-module t)
141141 (print-simple-mod-name (get-context-module))
142142 (princ "*"))
143143 (princ "]> ")))
753753 ;;
754754 (propagate-module-change modval)
755755 ;;
756 (when (eq modval (get-context-module))
756 (when (eq modval (get-context-module t))
757757 (reset-context-module)
758758 (setq recover-same-context t))
759759
803803 (if recover-same-context
804804 (reset-context-module real-mod)
805805 (if auto-context?
806 (change-context (get-context-module) real-mod)))
806 (change-context (get-context-module t) real-mod)))
807807 ;;
808808 (unless (module-is-parameter-theory real-mod)
809809 (print-in-progress " done."))
135135 stat-form ; statistics in string
136136 term-form) ; normalized term in string form
137137 ;; prepare rewriting context
138 (when (or (null mod) (modexp-is-error mod))
139 (if mod
140 (with-output-chaos-error ('no-such-module)
141 (princ "Incorrect module expression, no such module ")
142 (print-chaos-object modexp))
143 (with-output-chaos-error ('no-context)
144 (princ "No module expression provided, and no selected (current) module."))))
138 (when (modexp-is-error mod)
139 (with-output-chaos-error ('no-such-module)
140 (princ "Incorrect module expression, no such module ")
141 (print-chaos-object modexp)))
142 ;; set rewrting context
143 (context-push-and-move (get-context-module t) mod)
144 (when *auto-context-change*
145 (change-context (get-context-module t) mod))
145146 ;; parse target term
146147 (setq term (prepare-term preterm mod))
147 ;; set rewrting context
148 (context-push-and-move (get-context-module) mod)
149 (when *auto-context-change*
150 (change-context (get-context-module) mod))
151148 ;; print out prelude message
152149 (unless *chaos-quiet*
153150 (with-in-module (mod)
192189 (let ((mod (if modexp
193190 (eval-modexp modexp)
194191 (get-context-module))))
195 (if (or (null mod) (modexp-is-error mod))
196 (if (null mod)
197 (with-output-chaos-error ('no-context)
198 (princ "no module expression provided and no selected(current) module."))
199 (with-output-chaos-error ('no-such-module)
200 (princ "incorrect module expression, no such module ")
201 (print-chaos-object modexp)))
192 (if (modexp-is-error mod)
193 (with-output-chaos-error ('no-such-module)
194 (princ "incorrect module expression, no such module ")
195 (print-chaos-object modexp))
202196 (progn
203 (context-push-and-move (get-context-module) mod)
197 (context-push-and-move (get-context-module t) mod)
204198 (setq sort *cosmos*)
205199 (when *auto-context-change*
206 (change-context (get-context-module) mod)) ;;; what?
200 (change-context (get-context-module t) mod)) ;;; what?
207201 (with-in-module (mod)
208202 (!setup-reduction *current-module*)
209203 (setq $$mod *current-module*)
265259 (defun do-parse-term* (preterm &optional modexp)
266260 (let ((mod (if modexp
267261 (eval-modexp modexp)
268 (get-context-module))))
269 (unless mod
270 (with-output-chaos-error ('no-context)
271 (princ "no module expression provided and no selected(current) module.")))
262 (get-context-module)))
263 (target-term nil))
272264 (when (modexp-is-error mod)
273265 (with-output-chaos-error ('no-such-module)
274266 (princ "incorrect module expression, not such module: ")
275267 (print-chaos-object modexp)))
276268 ;;
277 (context-push-and-move (get-context-module) mod)
269 (context-push-and-move (get-context-module t) mod)
278270 (with-in-module (mod)
279271 (prepare-for-parsing *current-module*)
280272 (let ((*parse-variables* nil))
281 (let ((res (simple-parse *current-module* preterm *cosmos*)))
282 (setq res (car (canonicalize-variables (list res) mod)))
283 ;; ******** MEL
284 (when *mel-sort*
285 (!setup-reduction mod)
286 (setq res (apply-sort-memb res
287 mod)))
288 (reset-target-term res *current-module* mod)
289 ;; ********
290 (format t "~%")
291 (term-print-with-sort res *standard-output*)
292 (flush-all))))
293 (context-pop-and-recover)))
273 (setq target-term (car (canonicalize-variables (list (simple-parse *current-module* preterm *cosmos*)) mod)))
274 ;; ******** MEL
275 (when *mel-sort*
276 (!setup-reduction mod)
277 (setq target-term (apply-sort-memb target-term mod)))
278 (reset-target-term target-term *current-module* mod)
279 ;; ********
280 (format t "~%")
281 (term-print-with-sort target-term *standard-output*)
282 (flush-all)))
283 (context-pop-and-recover)
284 (values target-term mod)))
294285
295286 ;;; *TODO*
296287 (defun red-loop (mod &optional prompt)
415406 (if (or (null pat)
416407 (member pat '(("none") ("off") ("nil") ("null"))))
417408 (set-rewrite-stop-pattern 'none)
418 (let ((mod (or (get-context-module)
419 (with-output-chaos-error ('no-context)
420 (princ "no context (current) module is specified.")))))
409 (let ((mod (get-context-module)))
421410 (let* ((*parse-variables* (module-variables mod))
422411 (term (simple-parse mod
423412 pat *cosmos*)))
517506 (princ "closing this module...") (print-next)
518507 (eval-close-module nil)))
519508 (setq *open-module* mod)
520 (setq *last-before-open* (get-context-module))
509 (setq *last-before-open* (get-context-module t))
521510 (clear-term-memo-table *term-memo-table*)
522511 (let ((*chaos-quiet* t)
523512 (*copy-variables* t)
537526 (if *open-module*
538527 (let ((omod (eval-modexp "%")))
539528 (initialize-module omod)
540 (when (eq omod (get-context-module))
541 (change-context (get-context-module) *last-before-open*))
529 (when (eq omod (get-context-module t))
530 (change-context (get-context-module t) *last-before-open*))
542531 (setq *open-module* nil)
543532 (setq *last-before-open* nil))
544533 (with-output-chaos-warning ()
11991188
12001189 ;; operator strictness
12011190 (:strictness
1202 (let ((mod (or (get-context-module)
1203 (with-output-chaos-error ('no-context)
1204 (princ "no context (current) module.")))))
1205 ;;
1191 (let ((mod (get-context-module)))
12061192 (!setup-reduction mod)
12071193 (with-in-module (mod)
12081194 (if args
12451231 (format t ">> module is compatible."))))))
12461232 ;;;
12471233 (:coherency
1248 (let ((mod (or (get-context-module)
1249 (with-output-chaos-error ('no-context)
1250 (princ "no context (current) module.")))))
1251 ;;
1234 (let ((mod (get-context-module)))
12521235 (!setup-reduction mod)
12531236 (with-in-module (mod)
12541237 (if args
13111294 (let ((mod (if modexp
13121295 (eval-modexp modexp)
13131296 (get-context-module))))
1314 ;;
1315 (when (or (null mod) (modexp-is-error mod))
1316 (if (null mod)
1317 (with-output-chaos-error ('no-context)
1318 (princ "no module expression provided and no selected(current) module.")
1319 )
1297 (when (modexp-is-error mod)
13201298 (with-output-chaos-error ('no-such-module)
13211299 (princ "incorrect module expression, no such module ")
1322 (print-chaos-object modexp)
1323 )))
1300 (print-chaos-object modexp)))
13241301 ;; process specified command
13251302 (case command
13261303 ((:compile :compile-all)
13531330 (princ (cadr result)))
13541331 (force-output))
13551332 (progn
1356 (context-push-and-move (get-context-module) mod)
1333 (context-push-and-move (get-context-module t) mod)
13571334 (let ((*print-indent* (+ 4 *print-indent*)))
13581335 (with-in-module (mod)
13591336 (setq $$term (car result))
13701347 (terpri)
13711348 (princ (cadr result)))
13721349 (force-output)
1373 (reset-target-term $$term (get-context-module) mod)))
1350 (reset-target-term $$term (get-context-module t) mod)))
13741351 (context-pop-and-recover)))))
13751352 ;;
13761353 (otherwise (with-output-panic-message ()
14461423 (number-matches 0))
14471424 (let ((mod (if modexp
14481425 (eval-modexp modexp)
1449 (get-context-module))))
1426 (get-context-module t))))
14501427 (unless (eq mod (get-context-module))
14511428 (clear-term-memo-table *term-memo-table*))
14521429 (when (or (null mod) (modexp-is-error mod))
14561433 (with-output-chaos-error ('no-such-module)
14571434 (princ "no such module: ")
14581435 (print-chaos-object modexp))))
1459 (context-push-and-move (get-context-module) mod)
1436 (context-push-and-move (get-context-module t) mod)
14601437 (when *auto-context-change*
1461 (change-context (get-context-module) mod))
1438 (change-context (get-context-module t) mod))
14621439 (with-in-module (mod)
14631440 (!setup-reduction mod)
14641441 (setq $$mod *current-module*)
15841561 (modexp (%look-up-module ast))
15851562 (mod nil))
15861563 (setf mod (if (null modexp)
1587 (or (get-context-module)
1588 (with-output-chaos-error ('no-context)
1589 (format t "~%No context module is set.")))
1564 (get-context-module)
15901565 (eval-modexp modexp)))
15911566 (when (modexp-is-error mod)
15921567 (with-output-chaos-error ('no-such-module)
5454 ;;;
5555 (defun eval-mod (toks &optional (change-context *auto-context-change*))
5656 (if (null toks)
57 (or (get-context-module)
58 (with-output-chaos-error ('no-context)
59 (princ "no selected(current) module.")))
60 (if (equal '("%") toks)
61 (if *open-module*
62 (let ((mod (find-module-in-env (normalize-modexp "%"))))
63 (unless mod
64 (with-output-panic-message ()
65 (princ "could not find % module!!!!")
66 (chaos-error 'panic)))
67 (when change-context
68 (change-context (get-context-module) mod))
69 mod)
70 (with-output-chaos-warning ()
71 (princ "no module is opening.")
72 (chaos-error 'no-open-module)))
73 (let ((val (modexp-top-level-eval toks)))
74 (if (modexp-is-error val)
75 (if (and (null (cdr toks))
76 (<= 4 (length (car toks)))
77 (equal "MOD" (subseq (car toks) 0 3)))
78 (let ((val (read-from-string (subseq (car toks) 3))))
79 (if (integerp val)
80 (let ((nmod (print-nth-mod val))) ;;; !!!
81 (when change-context
82 (change-context (get-context-module) nmod))
83 nmod)
84 (with-output-chaos-error ('no-such-module)
85 (format t "could not evaluate the modexp ~a" toks))))
57 (get-context-module)
58 (if (equal '("%") toks)
59 (if *open-module*
60 (let ((mod (find-module-in-env (normalize-modexp "%"))))
61 (unless mod
62 (with-output-panic-message ()
63 (princ "could not find % module!!!!")
64 (chaos-error 'panic)))
65 (when change-context
66 (change-context (get-context-module t) mod))
67 mod)
68 (with-output-chaos-warning ()
69 (princ "no module is opening.")
70 (chaos-error 'no-open-module)))
71 (let ((val (modexp-top-level-eval toks)))
72 (if (modexp-is-error val)
73 (if (and (null (cdr toks))
74 (<= 4 (length (car toks)))
75 (equal "MOD" (subseq (car toks) 0 3)))
76 (let ((val (read-from-string (subseq (car toks) 3))))
77 (if (integerp val)
78 (let ((nmod (print-nth-mod val))) ;;; !!!
79 (when change-context
80 (change-context (get-context-module t) nmod))
81 nmod)
8682 (with-output-chaos-error ('no-such-module)
87 (format t "undefined module? ~a" toks)
88 ))
89 (progn
90 (when change-context
91 (change-context (get-context-module) val))
92 val))))))
83 (format t "could not evaluate the modexp ~a" toks))))
84 (with-output-chaos-error ('no-such-module)
85 (format t "undefined module? ~a" toks)))
86 (progn
87 (when change-context
88 (change-context (get-context-module t) val))
89 val))))))
9390
9491 ;;; what to do with this one?
9592
121118 (sub (nth-sub (1- no) mod)))
122119 (if sub
123120 (when change-context
124 (change-context (get-context-module) sub))
121 (change-context (get-context-module t) sub))
125122 (progn (princ "** Waring : No such sub-module") (terpri) nil))))
126123 ((and (equal "param" it)
127124 (cadr toks)
132129 (param (nth (1- no) params)))
133130 (if param
134131 (when change-context
135 (change-context (get-context-module) (cdr param)))
132 (change-context (get-context-module t) (cdr param)))
136133 (with-output-chaos-error ('no-such-parameter)
137134 (princ "No such parameter")
138135 ))))
139136 ((and (null toks) change-context force?)
140 (when (get-context-module)
137 (when (get-context-module t)
141138 (change-context (get-context-module) nil)))
142139 (t (eval-mod toks change-context)))))
143140
4242
4343 (defun print-macro (macro stream &rest ignore)
4444 (declare (ignore ignore))
45 (let ((mod (get-context-module)))
45 (let ((mod (get-context-module t)))
4646 (if mod
4747 (with-in-module (mod)
4848 (term-print (macro-lhs macro) stream)
4747 nil))
4848
4949 ;;; GET-CONTEXT-MODULE
50 (defun get-context-module ()
51 *current-module*)
50 (defun get-context-module (&optional no-error)
51 (or *current-module*
52 (if no-error
53 nil
54 (with-output-chaos-error ('no-context)
55 (format t "No context module is set.")))))
5256
5357 ;;; RESET-CONTEXT-MODULE
5458 (defun reset-context-module (&optional (mod nil))
5761 ;;; GET-OBJECT-CONTEXT object -> null | module
5862 ;;;
5963 (defun get-object-context (obj)
60 (or (get-context-module) (object-context-mod obj)))
64 (or (get-context-module t) (object-context-mod obj)))
6165
6266 ;;; BINDINGS *******************************************************************
6367
106110 (when (or (equal let-sym "$$term")
107111 (equal let-sym "$$subterm"))
108112 (with-output-chaos-error ('misc-error)
109 (princ "sorry, but you cannot use \"$$term\" or \"$$subterm\" as let variable.")
110 ))
113 (princ "sorry, but you cannot use \"$$term\" or \"$$subterm\" as let variable.")))
111114 ;;
112115 (let* ((special nil)
113116 (bindings (if (is-special-let-variable? let-sym)
148151 $$subterm nil
149152 $$action-stack nil
150153 $$selection-stack nil
151 $$term-context nil
152154 *current-module* nil
153155 *rewrite-stop-pattern* nil)
154156 (return-from new-context nil))
181183 (if (eq mod old-mod)
182184 (progn
183185 (setq $$term term
186 $$term-context mod
184187 $$subterm term
185188 $$selection-stack nil)
186189 (save-context mod)
187190 (new-context mod))
188191 ;; we do not change globals, instead set in context of mod.
189 (save-context mod)))
192 (progn
193 (setq $$term-context mod)
194 (save-context mod))))
190195 ;;;
191196 (defun context-push (mod)
192197 (push mod *old-context*))
199204 (change-context old new))
200205
201206 (defun context-pop-and-recover ()
202 (when (get-context-module)
203 (let ((old (context-pop)))
204 (unless (eq old (get-context-module))
205 ;; eval-mod may change the current context implicitly.
206 ;; in this case we do not recover context.
207 (change-context (get-context-module) old)))))
207 (let ((old (context-pop)))
208 (unless (eq old (get-context-module t))
209 ;; eval-mod may change the current context implicitly.
210 ;; in this case we do not recover context.
211 (change-context (get-context-module t) old))))
208212
209213 ;;; EOF
210214
8080 (and (fboundp (car ast))
8181 (symbol-function (car ast))))))
8282 (cond (evaluator
83 (let ((module (get-context-module)))
83 (let ((module (get-context-module t)))
8484 (when (and module (not (module-p module)))
8585 (setq module (find-module-in-env
8686 (normalize-modexp (string module)))))
595595 (cond ((chaos-ast? object)
596596 (let ((printer (ast-printer object)))
597597 (if printer
598 (let ((mod (get-context-module)))
598 (let ((mod (get-context-module t)))
599599 (if mod
600600 (with-in-module (mod)
601601 (funcall printer object stream))
606606 ((and (chaos-object? object) (not (stringp object)))
607607 (let ((printer (object-printer object)))
608608 (if printer
609 (let ((mod (get-context-module)))
609 (let ((mod (get-context-module t)))
610610 (if mod
611611 (with-in-module (mod)
612612 (funcall printer object stream))
615615 (*print-pretty* nil))
616616 (prin1 object stream)))))
617617 ((term? object)
618 (let ((mod (get-context-module)))
618 (let ((mod (get-context-module t)))
619619 (if mod
620620 (with-in-module (mod)
621621 (term-print object stream))
7777 (setq modexp (car modexp)))
7878 ;;
7979 (when (and (equal modexp "*the-current-module*")
80 (get-context-module))
80 (get-context-module t))
8181 (setq modexp (get-context-module)))
8282 (cond ((module-p modexp) (normalize-modexp (module-name modexp)))
8383 ((stringp modexp) (canonicalize-simple-module-name modexp))
823823 ;;; same as make-term-with-sort-check, but specialized to binary operators.
824824
825825 (defun make-term-with-sort-check-bin (meth subterms
826 &optional (module *current-module*))
826 &optional (module (get-context-module)))
827827 (declare (type method meth)
828828 (type list subterms)
829829 (type (or null module) module)
16811681 terletox-list))
16821682
16831683 (defun test-sort-memb-predicate (term &optional (module (get-context-module)))
1684 (unless module
1685 (with-output-chaos-error ('no-context)
1686 (princ "checking _:_, no context module is given!")))
16871684 (with-in-module (module)
16881685 (let ((arg1 (term-arg-1 term))
16891686 (id-const (term-arg-2 term)))
4040 ;;; CHECK COMPATIBILITY
4141 ;;;
4242 (defun check-compatibility (&optional (module (get-context-module)))
43 (unless module
44 (with-output-chaos-error ('no-context)
45 (princ "no context (current) module is specified!")))
46 ;;
4743 (unless *on-preparing-for-parsing*
4844 (prepare-for-parsing module))
49 ;;
5045 (with-in-module (module)
5146 (let ((rules (module-all-rules module))
5247 (non-decreasing-rules nil))
5050 (defun check-method-strictness (meth &optional
5151 (module (get-context-module))
5252 report?)
53
54 (unless module
55 (with-output-chaos-error ('no-cntext)
56 (princ "checking lazyness: no context module is specified!")))
57 ;;
5853 (with-in-module (module)
5954 (cond ((and (null (method-rules-with-different-top meth))
6055 (rule-ring-is-empty (method-rules-with-same-top meth)))
4949 (with-output-msg ()
5050 (princ "no current context, `select' some module first."))
5151 (return-from show-context nil))
52 (if (eq (get-context-module) mod)
52 (if (eq (get-context-module t) mod)
5353 (format t "~%-- current context :")
5454 (progn (format t "~%-- context of : ")
5555 (print-chaos-object mod)))
56 (context-push-and-move (get-context-module) mod)
56 (context-push-and-move (get-context-module t) mod)
5757 (with-in-module (mod)
5858 (format t "~%[module] ")
5959 (print-chaos-object *current-module*)
8080 ;;; SHOW BINDINGS
8181
8282 (defun show-bindings (&optional (module (get-context-module)))
83 (unless module
84 (with-output-msg ()
85 (princ "no context (current module) is specified.")
86 (return-from show-bindings nil)))
8783 (with-in-module (module)
8884 (let ((bindings (module-bindings *current-module*)))
8985 (format t "~&[bindings] ")
107103 ;;; show apply selection
108104
109105 (defun show-apply-selection (&optional (module (get-context-module)))
110 (unless module
111 (with-output-msg ()
112 (princ "no context (current module) is specified.")
113 (return-from show-apply-selection nil)))
106 (declare (ignore module)) ; TODO
114107 (when $$term-context
115108 (with-in-module ($$term-context)
116109 (format t "$$subterm = ")
141134 ;;; print-pending
142135 ;;;
143136 (defun print-pending (&optional (module (get-context-module)))
144 (unless module
145 (with-output-msg ()
146 (princ "no context (current module) is specified.")
147 (return-from print-pending nil)))
148137 (with-in-module (module)
149138 (format t"~&[pending actions] ")
150139 (if (null $$action-stack)
182171 (with-output-chaos-warning ()
183172 (format t "unknown option for `show term' : ~a" tree?))
184173 (return-from show-term nil))
185 (unless (get-context-module)
174 (unless (get-context-module t)
186175 (with-output-msg ()
187176 (princ "no current context, `select' some module first.")
188177 (return-from show-term nil)))
381370 (with-output-msg ()
382371 (format t "no such module ~a" modexp)
383372 (return-from show-sort nil))))
384 (t (setq mod (get-context-module))
385 (unless (module-p mod)
386 (with-output-msg ()
387 (princ "no context(current) module, select some first.")
388 (return-from show-sort nil)))))
373 (t (setq mod (get-context-module))))
389374 (with-in-module (mod)
390375 (let ((srt (find-sort-in mod sort-n)))
391376 (if srt
429414 (print-next)
430415 (princ "no such module ")
431416 (princ (%opref-module parsedop))))))
432 (t (setq mod (get-context-module))
433 (unless mod
434 (with-output-chaos-error ('no-context)
435 (princ "no context module is given.")))))
417 (t (setq mod (get-context-module))))
436418 mod))
437419
438420 (defun resolve-operator-reference (opref &optional (no-error nil))
489471 (let ((mod (if toks
490472 (eval-mod-ext toks)
491473 (get-context-module))))
492 (unless mod
493 (with-output-msg ()
494 (format t "no context (current module) is specified.")
495 (return-from show-param nil)))
474 (when (modexp-is-error mod)
475 (with-output-chaos-error ('invalid-modexp)
476 (format t "Invalid module expression: ~s" toks)))
496477 (let ((param (find-parameterized-submodule no mod)))
497478 (if (and param (not (modexp-is-error param)))
498479 (progn
499480 (with-in-module (param)
500481 (if describe
501482 (describe-module param)
502 (show-module param)))
483 (show-module param)))
503484 (terpri))
504 (with-output-msg ()
505 (if (null (module-parameters mod))
506 (princ "module has no parameters.")
507 (format t "no such parameter ~a" (if (integerp no)
508 (1+ no)
509 no))))))
510 ))
485 (with-output-msg ()
486 (if (null (module-parameters mod))
487 (princ "module has no parameters.")
488 (format t "no such parameter ~a" (if (integerp no)
489 (1+ no)
490 no))))))))
511491
512492 ;;; ************
513493 ;;; SHOW MODULES
187187 (:file "case")
188188 (:file "proof-struct")
189189 (:file "apply-tactic")
190 (:file "citp")))
190 (:file "citp")
191 (:file "bterm-inspector")))
191192 (:module "BigPink"
192193 :components ((:module codes
193194 :serial t
233233 "thstuff/case"
234234 "thstuff/proof-struct"
235235 "thstuff/apply-tactic"
236 "thstuff/citp"))
236 "thstuff/citp"
237 "thstuff/bterm-inspector"))
237238 (:module-group :bigpink
238239 (:definitions
239240 "BigPink/codes/types"
22512251 (defun reduce-in-goal (mode goal-name token-seq)
22522252 (with-citp-debug ()
22532253 (format t "~%~s in ~s : ~s" (string mode) goal-name token-seq))
2254 (let ((next-goal-node (if goal-name
2255 (find-goal-node *proof-tree* goal-name)
2256 (get-next-proof-context *proof-tree*))))
2257 (unless next-goal-node
2258 (with-output-chaos-error ('no-target)
2259 (if goal-name
2260 (format t ":~a could not find the goal ~s." mode goal-name)
2261 (format t "No default target goal."))))
2254 (let ((next-goal-node (get-target-goal-node goal-name)))
22622255 ;; do rewriting
22632256 (perform-reduction* token-seq (goal-context (ptree-node-goal next-goal-node)) mode)))
22642257
24582451 (pr-goal (ptree-node-goal gn))))
24592452 (ptree-node-subnodes ptree-node))))))
24602453
2454 ;;; :pctf or :pctf-
2455 ;;;
2456 (defun apply-pctf (s-forms dash? &optional (verbose *citp-verbose*))
2457 ;; TODO
2458 s-forms
2459 dash?
2460 verbose
2461 )
2462
24612463 ;;; -----------------------------------------------------
24622464 ;;; :csp or :csp-
24632465 ;;;
0 ;;;-*-Mode:LISP; Package: CHAOS; Base:10; Syntax:Common-lisp -*-
1 ;;;
2 ;;; Copyright (c) 2000-2014, Toshimi Sawada. All rights reserved.
3 ;;;
4 ;;; Redistribution and use in source and binary forms, with or without
5 ;;; modification, are permitted provided that the following conditions
6 ;;; are met:
7 ;;;
8 ;;; * Redistributions of source code must retain the above copyright
9 ;;; notice, this list of conditions and the following disclaimer.
10 ;;;
11 ;;; * Redistributions in binary form must reproduce the above
12 ;;; copyright notice, this list of conditions and the following
13 ;;; disclaimer in the documentation and/or other materials
14 ;;; provided with the distribution.
15 ;;;
16 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
17 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 ;;;
28 (in-package :chaos)
29 #|=============================================================================
30 System:CHAOS
31 Module:thstuff
32 File:bool-term.lisp
33 =============================================================================|#
34 #-:chaos-debug
35 (declaim (optimize (speed 3) (safety 0) #-GCL (debug 0)))
36 #+:chaos-debug
37 (declaim (optimize (speed 1) (safety 3) #-GCL (debug 3)))
38
39 (defvar *debug-bterm* nil)
40 ;;;=============================================================================
41 ;;; Utilities to support investigating big boolean term of xor-and normal form.
42 ;;;=============================================================================
43
44 ;;; *********
45 ;;; TERM HASH
46 ;;; *********
47 (declaim (type fixnum bterm-hash-size))
48 (defconstant bterm-hash-size 1001)
49
50 (let ((.bterm-hash. nil)
51 (bvar-num 0))
52 (declare (type fixnum bvar-num))
53
54 (defun clear-bterm-memo-table ()
55 (dotimes (x bterm-hash-size)
56 (setf (svref .bterm-hash. x) nil)))
57
58 (defun bterm-hash ()
59 .bterm-hash.)
60
61 (defun bterm-hash-ref (index)
62 (declare (type fixnum index))
63 (svref .bterm-hash. index))
64
65 (defun dump-bterm-hash ()
66 (with-in-module ((get-context-module))
67 (dotimes (x bterm-hash-size)
68 (let ((ent (svref .bterm-hash. x)))
69 (when ent
70 (format t "~%(~d): " x)
71 (let ((*print-indent* (+ *print-indent* 2)))
72 (dolist (elt ent)
73 (print-next)
74 (term-print-with-sort (car elt))
75 (princ " |-> ")
76 (term-print-with-sort (cdr elt)))))))))
77
78 (defun reset-bvar ()
79 (setq bvar-num 0)
80 (unless .bterm-hash.
81 (setq .bterm-hash. (alloc-svec bterm-hash-size)))
82 (clear-bterm-memo-table))
83
84 (defun make-bterm-variable ()
85 (let ((varname (intern (format nil "P-~d" (incf bvar-num)))))
86 (make-variable-term *bool-sort* varname)))
87
88 (defun get-hashed-bterm-variable (term)
89 (let ((val (hash-term term))
90 (var nil))
91 (declare (type fixnum val))
92 (let* ((ind (mod val bterm-hash-size))
93 (ent (svref .bterm-hash. ind)))
94 (setq var (cdr (assoc term ent :test #'term-is-similar?)))
95 (if var
96 var
97 (progn
98 (setf var (make-bterm-variable))
99 (setf (svref .bterm-hash. ind) (cons (cons term var) ent))
100 var)))))
101
102 )
103
104 ;;; =======================================================================
105 ;;; ABSTRACTED representation of a _xor_-_and_ normal form of boolean term.
106
107 ;;; ABS-BTERM:
108 ;;; abstracted boolean term.
109 ;;; each non _and_ or _xor_ boolean sub-term is abstracted by a
110 ;;; variable.
111 (defstruct (abst-bterm (:print-function print-abst-bterm))
112 ; by variables
113 (term nil) ; the original term
114 (subst nil) ; list of substitution
115 ; or instance of abst-bterm(for _and_ abstraction)
116 )
117
118 (defstruct (abst-and (:include abst-bterm)))
119
120 (defun print-abst-bterm (bt stream &rest ignore)
121 (declare (ignore ignore))
122 (with-in-module ((get-context-module))
123 (princ ":abt[" stream)
124 (let ((*print-indent* (+ 2 *print-indent*))
125 (num 0))
126 (declare (type fixnum *print-indent* num))
127 (dolist (sub (abst-bterm-subst bt))
128 (print-next nil *print-indent* stream)
129 (format stream "(~d) " (incf num))
130 (if (abst-bterm-p sub)
131 (print-abst-bterm sub stream)
132 (progn
133 (let ((var (car sub))
134 (term (cdr sub)))
135 (term-print-with-sort var)
136 (princ " |-> ")
137 (term-print-with-sort term))))))
138 (princ " ]" stream)))
139
140 ;;;
141 (defun find-bvar-subst (var bterm)
142 (declare (type abst-bterm bterm))
143 (dolist (sub (abst-bterm-subst bterm))
144 (if (abst-bterm-p sub)
145 (find-bvar-subst var sub)
146 (when (variable= var (car sub))
147 (return-from find-bvar-subst (cdr sub)))))
148 nil)
149
150 ;;;
151 ;;; abstract-boolen-term : bool-term -> abst-bterm
152 ;;;
153
154 ;;; xtract-xor-subterms : term
155 ;;; returns ac subterms of the given term iff the top op is _xor_
156 (defun xtract-xor-subterms (term)
157 (let ((args (if (method= (term-head term) *bool-xor*)
158 (list-ac-subterms term *bool-xor*)
159 nil)))
160 args))
161
162 ;;; xtract-and-subterms : term
163 ;;; returns ac subterms of the given term iff the top op is _and_
164 (defun xtract-and-subterms (term)
165 (if (method= (term-head term) *bool-and*)
166 (list-ac-subterms term *bool-and*)
167 nil))
168
169 (defun make-and-abstraction (term subterms)
170 (let ((subst nil))
171 (dolist (sub subterms)
172 (push (cons (get-hashed-bterm-variable sub) sub) subst))
173 (make-abst-and :term term :subst (nreverse subst))))
174
175 (defun abstract-boolean-term (term)
176 (let ((bterm (make-abst-bterm :term term))
177 (xor-subs (xtract-xor-subterms term))
178 (subst nil))
179 ;; reset variable number & term hash
180 (reset-bvar)
181 (if xor-subs
182 ;; top operator is _xor_
183 ;; we further decompose by _and_
184 (dolist (xs xor-subs)
185 (let ((as (xtract-and-subterms xs)))
186 (if as
187 (push (make-and-abstraction xs as) subst)
188 (push (cons (get-hashed-bterm-variable xs) xs) subst))))
189 ;; top operator is not xor
190 (let ((as (xtract-and-subterms term)))
191 (when as
192 (push (make-and-abstraction term as) subst))))
193 (setf (abst-bterm-subst bterm) (nreverse subst))
194 bterm))
195
196 ;;;
197 ;;; make-bterm-representation : bterm -> boolen term
198 ;;; from bterm make a concrete representation of abstracted boolean term
199 ;;;
200 (defun make-and-representation (abst-and)
201 (declare (type abst-and abst-and))
202 (make-right-assoc-normal-form *bool-and*
203 (mapcar #'car (abst-and-subst abst-and))))
204
205 (defun make-xor-representation (bterm)
206 (declare (type abst-bterm bterm))
207 (make-right-assoc-normal-form *bool-xor*
208 (mapcar #'(lambda (x) (if (abst-and-p x)
209 (make-and-representation x)
210 (car x)))
211 (abst-bterm-subst bterm))))
212
213 (defun make-bterm-representation (bterm)
214 (let ((subst (abst-bterm-subst bterm)))
215 ;; no _xor nor _and_ ops in original term
216 (unless subst
217 (return-from make-bterm-representation (abst-bterm-term bterm)))
218 ;; sole _and_ term.
219 (when (and (null (cdr subst))
220 (abst-and-p (car subst)))
221 (return-from make-bterm-representation (make-and-representation (car subst))))
222 ;; _xor_ normal form
223 (make-xor-representation bterm)))
224
225 ;;; ===========================================================================================
226 ;;; PRINTERS
227 ;;; abst-bterm printers
228
229 ;;; simple-print-bterm : bterm -> void
230 (defun simple-print-bterm (bterm)
231 (declare (type abst-bterm bterm))
232 (let ((aterm (make-bterm-representation bterm)))
233 (term-print-with-sort aterm)))
234
235 ;;; print-bterm-tree : bterm -> void
236 (defun print-bterm-tree (bterm &optional (mode :vertical))
237 (declare (type abst-bterm bterm)
238 (ignore mode)) ; for NOW ** TODO for :vertical
239 (let ((aterm (make-bterm-representation bterm)))
240 (print-term-graph aterm *chaos-verbose*)))
241
242 ;;; print-abs-bterm : bterm &key mode
243 ;;; mode :simple print term representation
244 ;;; :tree print term representation as vertical tree structure
245 ;;; :horizontal print term representation horizontal tree structure
246 ;;; also shows a substitution used for abstruction.
247 ;;;
248 (defun print-abs-bterm (bterm &key (mode :simple))
249 (case mode
250 (:simple (simple-print-bterm bterm))
251 (:tree (print-bterm-tree bterm))
252 (:horizontal (print-bterm-tree bterm :horizontal))
253 (otherwise
254 (with-output-chaos-error ('invalid-mode)
255 (format t "Invalid print mode ~a." mode))))
256 ;; shows substitution
257 (format t "~%, where")
258 (dolist (subst (abst-bterm-subst bterm))
259 (format t "~%~4T~A |-> " (variable-name (car subst)))
260 (term-print-with-sort (cdr subst)))
261 )
262
263 ;;; ===========================================================================================
264 ;;; RESOLVER
265 ;;; computes possible solutions (assignments) which makes abstracted boolean term to be 'true.'
266 ;;;
267
268 ;;; assign-tf
269 ;;; make all posssible variable substitutions with the domain {'true' ,'false'}.
270 ;;;
271 (defun make-tf-combination (rows columns)
272 (let ((assignment nil)
273 (subst (make-array (list rows columns))))
274 (flet ((change-parity ()
275 (if (is-true? assignment)
276 (setq assignment *bool-false*)
277 (setq assignment *bool-true*))))
278 (dotimes (c columns)
279 (setq assignment nil)
280 (let ((cycle (expt 2 c)))
281 (dotimes (r rows)
282 (if (not assignment)
283 (setq assignment *bool-true*)
284 (if (= 0 (mod r cycle))
285 (change-parity)))
286 #||
287 (when *debug-bterm*
288 (format t "~%cycle=~d, columns=~d, row=~d: ~s" cycle c r (if (is-true? assignment)
289 "true"
290 "false")))
291 ||#
292 (setf (aref subst r c) assignment))))
293 subst)))
294
295 (defun assign-tf (list-vars)
296 (let* ((columns (length list-vars))
297 (rows (expt 2 columns))
298 (assignments (make-tf-combination rows columns))
299 (l-subst nil))
300 (dotimes (r rows)
301 (let ((subst nil))
302 (dotimes (c columns)
303 (push (cons (nth c list-vars) (aref assignments r c)) subst))
304 (push (nreverse subst) l-subst)))
305 (when *debug-bterm*
306 (with-in-module ((get-context-module))
307 (let ((num 0))
308 (dolist (sub (reverse l-subst))
309 (format t "~%(~d): " (incf num))
310 (print-substitution sub)))))
311 (nreverse l-subst)))
312
313 ;;;
314 ;;; resolve-abst-bterm : bterm
315 ;;; retuns a list of substitution which makes bterm to be true.
316 ;;;
317 (defun resolve-abst-bterm (bterm &optional (module (get-context-module)))
318 (declare (type abst-bterm bterm))
319 (let* ((abst-term (make-bterm-representation bterm))
320 (variables (term-variables abst-term))
321 (list-subst (assign-tf variables))
322 (answers nil))
323 (dolist (subst list-subst)
324 (let ((target (substitution-image-cp subst abst-term)))
325 (when *debug-bterm*
326 (with-in-module ((get-context-module))
327 (format t "~%[resolver_target] ")
328 (term-print-with-sort target)))
329 (reducer-no-stat target module :red)
330 (when *debug-bterm*
331 (with-in-module ((get-context-module))
332 (format t "~% --> ")
333 (term-print-with-sort target)))
334 (when (is-true? target)
335 (push subst answers))))
336 answers))
337
338 ;;; try-resolve-boolean-term : term -> Values (abst-bterm List(substitution))
339 ;;;
340 (defvar *abst-bterm* nil)
341 (defvar *abst-bterm-representation* nil)
342
343 (defun try-resolve-boolean-term (term &optional (module (get-context-module)))
344 (unless (sort= (term-sort term) *bool-sort*)
345 (with-output-chaos-warning ()
346 (format t "Given term is not of sort Bool. Ignored.")
347 (return-from try-resolve-boolean-term nil)))
348 (multiple-value-bind (target app?)
349 (normalize-term-in module term)
350 (declare (ignore app?))
351 ;; abstract
352 (let ((bterm (abstract-boolean-term target)))
353 (setq *abst-bterm* bterm)
354 (setq *abst-bterm-representation*
355 (make-bterm-representation bterm))
356 (let ((*print-indent* (+ 2 *print-indent*)))
357 (format t "~%** Abstracted boolean term:")
358 (with-in-module (module)
359 (print-next)
360 (term-print-with-sort *abst-bterm-representation*)
361 (format t "~%,where")
362 (let ((*print-indent* (+ 2 *print-indent*)))
363 (dolist (var (term-variables *abst-bterm-representation*))
364 (let ((mapping (find-bvar-subst var bterm)))
365 (unless mapping
366 (with-output-chaos-error ('internal-err)
367 (format t "Could not find the mapping of variable ~a." (variable-name var))))
368 (print-next)
369 (term-print-with-sort var)
370 (princ " |-> ")
371 (term-print-with-sort mapping))))
372 ;; find answers
373 (let ((ans (resolve-abst-bterm bterm module)))
374 (cond (ans
375 (format t "~%** The following assignment(s) can make the term 'true'.")
376 (let ((num 0))
377 (declare (type fixnum num))
378 (let ((*print-indent* (+ 2 *print-indent*)))
379 (dolist (sub ans)
380 (print-next)
381 (format t "(~d): " (incf num))
382 (print-substitution sub)))))
383 (t
384 (format t "~%** No solution was found.")))
385 (values bterm ans)))))))
386
387 ;;;
388 (defun binspect-in-goal (goal-name preterm)
389 (let* ((goal-node (get-target-goal-node goal-name))
390 (context-module (goal-context (ptree-node-goal goal-node)))
391 (target (do-parse-term* preterm context-module)))
392 (try-resolve-boolean-term target context-module)))
393
394 (defun binspect-in-module (mod-name preterm)
395 (multiple-value-bind (target context-module)
396 (do-parse-term* preterm mod-name)
397 (try-resolve-boolean-term target context-module)))
398
399 ;;; TOP LEVEL FUNCTION
400 ;;; binspect-in
401 (defun binspect-in (mode goal-or-module-name preterm)
402 (cond ((eq mode :citp)
403 (binspect-in-goal goal-or-module-name preterm))
404 (t
405 (binspect-in-module goal-or-module-name preterm))))
406
407 ;;; EOF
13221322 (let ((module (get-context-module))
13231323 max-r
13241324 max-d)
1325 (unless module
1326 (with-output-chaos-error ('no-context)
1327 (format t "no context module..")))
13281325 (if (integerp max-result)
13291326 (setq max-r max-result)
13301327 (if (term-is-builtin-constant? max-result)
181181 ((":bred") (setq mode :bred)))
182182 (if (= 4 (length e))
183183 (progn
184 (setq goal-name (cadr (cadr e))); (find-goal-node *proof-tree* (cadr (cadr e)))
184 (setq goal-name (cadr (cadr e)))
185185 (setq preterm (nth 2 e)))
186186 (progn
187187 (setq goal-name nil)
213213 (if (equal (first args) ":ctf-")
214214 (cons form (cons term? :dash))
215215 (cons form (cons term? nil)))))
216
217 ;;; citp-parse-pctf
218 ;;; :pctf { [<term> .] ... [<term> . ] }
219 ;;; (":pctf" "{" (("[" (<Term>1) "." "]") ... ("[" (<Termn>) "." "]")) "}")
220 ;;; ==> (:pctf-? <Term1> ... <Termn>)
221 (defun citp-parse-pctf (args)
222 (let ((pctf-minus? (equal (car args) ":pctf-"))
223 (list-terms (third args))
224 (pre-terms nil))
225 (print list-terms)
226 (dolist (lt list-terms)
227 (push (second lt) pre-terms))
228 (cons pctf-minus? (nreverse pre-terms))))
216229
217230 ;;; citp-parse-csp
218231 ;;; :csp { <axiom> ... }
250263 (format t "~&:spoiler flag is ~s" (if *citp-spoiler* "on" "off"))))
251264 t))
252265
266 ;;;
267 ;;; {:binspect | binspect} [in <goal-name> : ] <boolean-term> .
268 ;;;
269 (defun parse-citp-binspect (args)
270 (let (mode
271 goal-name
272 preterm)
273 (if (equal (first args) ":binspect")
274 (setq mode :citp)
275 (setq mode :general))
276 (if (= 4 (length args))
277 (progn
278 (setq goal-name (cadr (cadr args)))
279 (setq preterm (nth 2 args)))
280 (progn
281 (setq goal-name nil)
282 (setq preterm (nth 1 args))))
283 (list mode goal-name preterm)))
284
285
286
253287 ;;; ================================
254288 ;;; CITP related command evaluators
255289 ;;; ================================
363397 (reset-rewrite-counters)
364398 (begin-rewrite)
365399 (apply-ctf (car ax-form) (cadr ax-form) (cddr ax-form))
400 (end-rewrite)
401 (report-citp-stat)
402 (check-success *proof-tree*)))
403
404 ;;; :pctf
405 ;;;
406 (defun eval-citp-pctf (ax-form)
407 (check-ptree)
408 (with-in-module (*current-module*)
409 (reset-rewrite-counters)
410 (begin-rewrite)
411 ;; TODO
412 ax-form
413 ;; (apply-pctf (car ax-form) (cadr ax-form) (cddr ax-form))
366414 (end-rewrite)
367415 (report-citp-stat)
368416 (check-success *proof-tree*)))
402450 (t (with-output-chaos-error ('unknown)
403451 (format t "Unknown parameter to :show/:describe ~S" target))))))
404452
453
454 ;;; :binspect
455 ;;;
456 (defun eval-citp-binspect (args)
457 (let ((mode (first args))
458 (goal-or-mod (second args))
459 (preterm (third args)))
460 (binspect-in mode goal-or-mod preterm)))
461
405462 ;;; EOF
6464 (setq target (get-bound-value (car pre-term))))
6565 (unless target
6666 (return-from do-eval-start-th nil))
67 (when (eq mod (get-context-module))
68 (setq $$action-stack nil))
67 (setq $$action-stack nil)
6968 (reset-reduced-flag target)
7069 (reset-target-term target *current-module* mod)))
7170 (t
7574 *cosmos*)))
7675 (when (term-is-an-error res)
7776 (return-from do-eval-start-th nil))
78 (when (eq (get-context-module) mod)
79 (setq $$action-stack nil))
77 (setq $$action-stack nil)
8078 (reset-target-term res *current-module* mod))))))
8179 ;; try use $$term
8280 (progn
8583 (format t "no target term is given!")
8684 (return-from do-eval-start-th nil)))
8785 (check-apply-context mod)
88 (when (eq (get-context-module) mod)
89 (setq $$action-stack nil))
86 (setq $$action-stack nil)
9087 (reset-reduced-flag $$term)
9188 (reset-target-term $$term (get-context-module) mod))))
9289 (when (command-final) (command-display))
146143 (where-spec (%apply-where-spec ast))
147144 (selectors (%apply-selectors ast)))
148145 (catch 'apply-context-error
149 (if (eq action :help)
150 (apply-help)
151 (progn
152 ;; check some evaluation env
153 (when (or (null $$term) (eq 'void $$term))
154 (with-output-chaos-error ('invalid-term)
155 (princ "term to be applied is not defined.")
156 ))
157 (unless (get-context-module)
158 (with-output-chaos-error ('no-context-module)
159 (princ "no current module.")))
160 ;; real work begins here ------------------------------
161 (with-in-module ((get-context-module))
162 (multiple-value-bind (subterm-sort subterm)
163 (compute-selection $$term selectors)
164 (setq *-applied-* t)
165 (case action
166 (:reduce ; full reduction on selections.
167 (!setup-reduction *current-module*)
168 (let ((*rewrite-semantic-reduce*
169 (module-has-behavioural-axioms *current-module*))
170 (*rewrite-exec-mode* nil))
171 (term-replace subterm (@copy-term subterm))
172 (reset-reduced-flag subterm)
173 (rewrite subterm *current-module*)))
174 (:breduce
175 (!setup-reduction *current-module*)
176 (let ((*rewrite-semantic-reduce* nil)
177 (*rewrite-exec-mode* nil))
178 (term-replace subterm (@copy-term subterm))
179 (reset-reduced-flag subterm)
180 (rewrite subterm *current-module*)))
181 (:exec
182 (!setup-reduction *current-module*)
183 (let ((*rewrite-semantic-reduce*
184 (module-has-behavioural-axioms *current-module*))
185 (*rewrite-exec-mode* t))
186 (term-replace subterm (@copy-term subterm))
187 (reset-reduced-flag subterm)
188 (rewrite subterm *current-module*)))
189 ;;
190 (:print ; print selections.
191 (format t "~%term ")
192 (disp-term subterm)
193 (format t "~&tree form")
194 (print-term-tree subterm))
195 (:apply ; apply specified rule.
196 (setq *-applied-* nil)
197 (let* ((actrule (compute-action-rule rule-spec
198 substitution
199 selectors))
200 (*-inside-apply-with-extensions-*
201 (and
202 (let ((arlhs (rule-lhs actrule)))
203 (and (term-is-application-form? arlhs)
204 (method-is-associative (term-head arlhs)))))))
205 (if (eq :within where-spec)
206 (let ((*-inside-apply-all-* t))
207 (catch 'apply-all-quit
208 (@apply-all actrule subterm-sort subterm)))
209 (@apply-rule actrule subterm-sort subterm)))
210 (when *-applied-*
211 (update-lowest-parse $$term)
212 (when (nth 2 rule-spec) ; reverse order
213 (setq $$term (@copy-term $$term)))
214 (reset-target-term $$term *current-module* *current-module*))) ; end :apply
215 (t (with-output-panic-message ()
216 (format t "unknown apply action : ~a" action)
217 (chaos-error 'unknown-action))))
218 ;;
219 (unless *-applied-*
220 (with-output-chaos-warning ()
221 (princ "rule not applied")))
222 ;;
223 (command-final)
224 (command-display))))))))
146 (when (eq action :help)
147 (apply-help)
148 (return-from eval-apply-command nil))
149 ;; check some evaluation env
150 (when (or (null $$term) (eq 'void $$term))
151 (with-output-chaos-error ('invalid-term)
152 (princ "term to be applied is not defined.")))
153 ;; real work begins here ------------------------------
154 (with-in-module ((get-context-module))
155 (multiple-value-bind (subterm-sort subterm)
156 (compute-selection $$term selectors)
157 (setq *-applied-* t)
158 (case action
159 (:reduce ; full reduction on selections.
160 (!setup-reduction *current-module*)
161 (let ((*rewrite-semantic-reduce*
162 (module-has-behavioural-axioms *current-module*))
163 (*rewrite-exec-mode* nil))
164 (term-replace subterm (@copy-term subterm))
165 (reset-reduced-flag subterm)
166 (rewrite subterm *current-module*)))
167 (:breduce
168 (!setup-reduction *current-module*)
169 (let ((*rewrite-semantic-reduce* nil)
170 (*rewrite-exec-mode* nil))
171 (term-replace subterm (@copy-term subterm))
172 (reset-reduced-flag subterm)
173 (rewrite subterm *current-module*)))
174 (:exec
175 (!setup-reduction *current-module*)
176 (let ((*rewrite-semantic-reduce*
177 (module-has-behavioural-axioms *current-module*))
178 (*rewrite-exec-mode* t))
179 (term-replace subterm (@copy-term subterm))
180 (reset-reduced-flag subterm)
181 (rewrite subterm *current-module*)))
182 ;;
183 (:print ; print selections.
184 (format t "~%term ")
185 (disp-term subterm)
186 (format t "~&tree form")
187 (print-term-tree subterm))
188 (:apply ; apply specified rule.
189 (setq *-applied-* nil)
190 (let* ((actrule (compute-action-rule rule-spec
191 substitution
192 selectors))
193 (*-inside-apply-with-extensions-*
194 (and
195 (let ((arlhs (rule-lhs actrule)))
196 (and (term-is-application-form? arlhs)
197 (method-is-associative (term-head arlhs)))))))
198 (if (eq :within where-spec)
199 (let ((*-inside-apply-all-* t))
200 (catch 'apply-all-quit
201 (@apply-all actrule subterm-sort subterm)))
202 (@apply-rule actrule subterm-sort subterm)))
203 (when *-applied-*
204 (update-lowest-parse $$term)
205 (when (nth 2 rule-spec) ; reverse order
206 (setq $$term (@copy-term $$term)))
207 (reset-target-term $$term *current-module* *current-module*))) ; end :apply
208 (t (with-output-panic-message ()
209 (format t "unknown apply action : ~a" action)
210 (chaos-error 'unknown-action))))
211 ;;
212 (unless *-applied-*
213 (with-output-chaos-warning ()
214 (princ "rule not applied")))
215 ;;
216 (command-final)
217 (command-display))))))
225218
226219 (defvar *copy-conditions*)
227220 (declaim (special *copy-conditons*))
446439 (when (and rev (or (rule-is-builtin rule)
447440 (eq (axiom-type rule) :rule)))
448441 (format t "~%This rule cannot be applied reversed."))
449 (when (and (get-context-module)
442 (when (and (get-context-module t)
450443 (not (rule-is-builtin rule)))
451444 (format t "~%(This rule rewrites up.)"))))))))
452445 t))
4646 ;;; ******
4747
4848 (defun eval-match-command (ast)
49 (unless (get-context-module)
50 (with-output-chaos-error ('no-current-module)
51 (princ "no current module.")))
5249 (let ((type (%match-type ast))
5350 (target (case (%match-target ast)
5451 (:top $$term)
700700 (push x nodes))))
701701 (nreverse nodes)))
702702
703 ;;;
704703 ;;; get-unproved-goals : ptree -> List(goal)
705704 ;;;
706705 (defun get-unproved-goals (ptree)
707706 (mapcar #'(lambda (y) (ptree-node-goal y)) (get-unproved-nodes ptree)))
708707
709 ;;;
710708 ;;; print-unproved-goals
711709 ;;;
712710 (defun print-unproved-goals (ptree &optional (stream *standard-output*))
717715 (dolist (goal (get-unproved-goals ptree))
718716 (pr-goal goal stream)))
719717
720 ;;;
721718 ;;; get-next-pfoof-context : ptree -> ptree-node
722719 ;;;
723720 (defun get-next-proof-context (ptree)
726723
727724 (defun next-proof-target-is-specified? ()
728725 *next-default-proof-node*)
726
727 ;;; get-target-goal-node
728 ;;; given goal-name or NULL, returns the next targetted goal node.
729 ;;;
730 (defun get-target-goal-node (&optional goal-name)
731 (let ((next-goal-node (if goal-name
732 (find-goal-node *proof-tree* goal-name)
733 (get-next-proof-context *proof-tree*))))
734 (unless next-goal-node
735 (with-output-chaos-error ('no-target)
736 (if goal-name
737 (format t "Could not find the goal ~s." goal-name)
738 (format t "No default target goal."))))
739 next-goal-node))
729740
730741 ;;;
731742 ;;; select-next-goal : goal-name