Codebase list cafeobj / 08de427
New command 'bshow [tree]', and 'bresolve'. Now 'binspect' does not try to resolve the abs bterm. tswd 8 years ago
8 changed file(s) with 119 addition(s) and 89 deletion(s). Raw diff Collapse all Expand all
24192419 :doc "TODO"
24202420 )
24212421
2422 (define ("bresolve" ":bresoleve")
2422 (define ("bresolve" ":bresolve")
24232423 :category :proof
24242424 :parser identity
24252425 :evaluator bresolve
2426 :title "{binspect :binspect}"
2426 :title "`{binspect | :binspect}`"
24272427 :doc "TODO"
24282428 )
24292429
2430 (define ("bshow" ":bshow")
2431 :category :proof
2432 :parser citp-parse-bshow
2433 :evaluator bshow
2434 :title "`{bshow | :bshow} [tree]`"
2435 :doc "TODO"
2436 )
24302437 ;;;
24312438
24322439 (define ("commands" "com")
823823 (binspect
824824 (:rdr #..term-delimiting-chars. (:if-present in :modexp |:|)) (:seq-of :term) |.|)
825825 ((:+ |:bresolve| bresolve))
826 ((:+ |:bshow| bshow) :args)
826827 )) ; end Top-Form
827828
828829 ;; some separated definitions of non-terminals.
4545 ;;; MEMOIZE
4646 ;;; -------
4747
48 (deftype term-hash-key () '(unsigned-byte 29))
49
5048 (defconstant term-hash-mask #x1FFFFFFF)
5149
5250 (declaim (type fixnum term-hash-size))
5654 `(and (method-p ,m) (method-has-memo ,m)))
5755
5856 #-GCL (declaim (inline term-hash-equal))
59 #-(or GCL CMU)
60 (defun term-hash-equal (x)
61 (logand term-hash-mask (sxhash x)))
62 #+CMU
63 (defun term-hash-equal (x)
64 (sxhash x))
65 #+GCL
57
58 #-GCL
59 (defun term-hash-equal (x) (sxhash x))
60
61 #+GCL
6662 (si:define-inline-function term-hash-equal (x) (sxhash x))
6763
6864 #+GCL
7773 #-GCL
7874 (declaim (inline term-hash-eq))
7975 #-GCL
80 (defun term-hash-eq (object)
81 (ash (+ (the term-hash-key
82 (logand term-hash-mask
83 (the (unsigned-byte 32) (addr-of object))))
84 3)
85 -3))
76 (defun term-hash-eq (object) (truncate (addr-of object) 4))
8677
8778 #-GCL
8879 (declaim (inline term-hash-comb))
8980 #-GCL
9081 (defun term-hash-comb (x y)
91 ;; (declare (type term-hash-key x y))
92 (the term-hash-key (logand term-hash-mask (logand term-hash-mask (+ x y)))))
82 (logxor (ash x -5) y (ash (logand x 31) 26)))
9383
9484 ;;;
9585 ;;; term-hash
9686 ;;;
97 ;;; (defvar *on-term-hash-debug* nil)
98
99 (defstruct term-hash
100 (size term-hash-size :type (unsigned-byte 14) :read-only t)
101 (table nil :type (or null simple-array)) )
102
10387 (defun hash-term (term)
10488 (cond ((term-is-applform? term)
10589 (let ((res (sxhash (the symbol (method-id-symbol (term-head term))))))
17391739
17401740
17411741
1742 VMINOR=.4b12
1742 VMINOR=.4b13
17431743 VMEMO=PigNose0.99
17441744 PATCHLEVEL=
17451745
3131 AC_INIT([CafeOBJ],[1.5], [], [cafeobj], [http://www.cafeobj.org/])
3232 AC_CONFIG_SRCDIR([make-cafeobj.lisp.in])
3333 AC_PREREQ(2.6)
34 VMINOR=.4b12
34 VMINOR=.4b13
3535 VMEMO=PigNose0.99
3636 PATCHLEVEL=
3737 AC_SUBST(VMINOR)
4141 ;;; Utilities to support investigating big boolean term of xor-and normal form.
4242 ;;;=============================================================================
4343
44 #||
4445 ;;; *********
4546 ;;; TERM HASH
4647 ;;; *********
100101 var)))))
101102
102103 )
104 ||#
105
106 (defvar .bterm-assoc-table. nil)
107 (defvar .bvar-num. 0)
108 (declaim (type fixnum .bvar-num.))
109
110 (defun clear-bterm-memo-table ()
111 (setq .bterm-assoc-table. nil))
112
113 (defun reset-bvar ()
114 (setq .bvar-num. 0)
115 (clear-bterm-memo-table))
116
117 (defun make-bterm-variable ()
118 (let ((varname (intern (format nil "P-~d" (incf .bvar-num.)))))
119 (make-variable-term *bool-sort* varname)))
120
121 (defun get-hashed-bterm-variable (term)
122 (let ((ent (assoc term .bterm-assoc-table. :test #'term-equational-equal)))
123 (if ent
124 (cdr ent)
125 (let ((var (make-bterm-variable)))
126 (push (cons term var) .bterm-assoc-table.)
127 var))))
103128
104129 ;;; =======================================================================
105130 ;;; ABSTRACTED representation of a _xor_-_and_ normal form of boolean term.
108133 ;;; abstracted boolean term.
109134 ;;; each non _and_ or _xor_ boolean sub-term is abstracted by a
110135 ;;; variable.
111 (defstruct (abst-bterm (:print-function print-abst-bterm))
136 (defstruct (abst-bterm)
112137 (module nil) ; context module
113138 (term nil) ; the original term
114139 (subst nil) ; list of substitution
117142
118143 (defstruct (abst-and (:include abst-bterm)))
119144
120 (defun print-abst-bterm (bt stream &rest ignore)
145 (defun print-abst-bterm (bt &optional (stream *standard-output*) &rest ignore)
121146 (declare (ignore ignore))
122 (with-in-module ((get-context-module))
123 (princ ":abt[" stream)
147 (with-in-module ((abst-bterm-module bt))
148 (if (abst-and-p bt)
149 (princ ":and[" stream)
150 (princ ":xor[" stream))
124151 (let ((*print-indent* (+ 2 *print-indent*))
125152 (num 0))
126153 (declare (type fixnum *print-indent* num))
132159 (progn
133160 (let ((var (car sub))
134161 (term (cdr sub)))
135 (term-print-with-sort var)
162 (term-print var)
136163 (princ " |-> ")
137 (term-print-with-sort term))))))
164 (term-print term))))))
138165 (princ " ]" stream)))
139166
140167 ;;;
142169 (declare (type abst-bterm bterm))
143170 (dolist (sub (abst-bterm-subst bterm))
144171 (if (abst-bterm-p sub)
145 (find-bvar-subst var sub)
172 (let ((subst (find-bvar-subst var sub)))
173 (when subst (return-from find-bvar-subst subst)))
146174 (when (variable= var (car sub))
147 (return-from find-bvar-subst (cdr sub)))))
148 nil)
175 (return-from find-bvar-subst (cdr sub))))))
149176
150177 ;;;
151178 ;;; abstract-boolen-term : bool-term -> abst-bterm
241268
242269 ;;; print-bterm-tree : bterm -> void
243270 (defun print-bterm-tree (bterm &optional (mode :vertical))
244 (declare (type abst-bterm bterm)
245 (ignore mode)) ; for NOW ** TODO for :vertical
246 (let ((aterm (make-bterm-representation bterm)))
247 (print-term-graph aterm *chaos-verbose*)))
271 (declare (type abst-bterm bterm))
272 (with-in-module ((abst-bterm-module bterm))
273 (let ((aterm (make-bterm-representation bterm)))
274 (if (eq mode :vertical)
275 (print-term-graph aterm *chaos-verbose*)
276 (print-term-horizontal (make-bterm-representation bterm) *current-module*)))))
248277
249278 ;;; print-abs-bterm : bterm &key mode
250279 ;;; mode :simple print term representation
259288 (:horizontal (print-bterm-tree bterm :horizontal))
260289 (otherwise
261290 (with-output-chaos-error ('invalid-mode)
262 (format t "Invalid print mode ~a." mode))))
263 ;; shows substitution
264 (format t "~%, where")
265 (dolist (subst (abst-bterm-subst bterm))
266 (format t "~%~4T~A |-> " (variable-name (car subst)))
267 (term-print-with-sort (cdr subst)))
268 )
291 (format t "Invalid print mode ~a." mode)))))
269292
270293 ;;; ===========================================================================================
271294 ;;; RESOLVER
321344 ;;; resolve-abst-bterm : bterm
322345 ;;; retuns a list of substitution which makes bterm to be true.
323346 ;;;
324 (defvar .maximum-bterm-vars. 10)
325
326347 (defun resolve-abst-bterm (bterm &optional (module (get-context-module)))
327348 (declare (type abst-bterm bterm))
328349 (with-in-module (module)
329350 (let* ((abst-term (make-bterm-representation bterm))
330351 (variables (term-variables abst-term))
331352 (answers nil))
332 (when (> (length variables) .maximum-bterm-vars.)
333 (with-output-chaos-warning ()
334 (format t "Sorry, but the current system can not handle more than ~d variables."
335 .maximum-bterm-vars.)
336 (return-from resolve-abst-bterm nil)))
337353 (dolist (subst (assign-tf variables))
338354 (let ((target (substitution-image-cp subst abst-term)))
339355 (reset-reduced-flag target)
340 (when *debug-bterm*
341 (with-in-module ((get-context-module))
342 (format t "~%[resolver_target] ")
343 (term-print-with-sort target)
344 (print-next)
345 (format t "~% mod = ~a" *current-module*)
346 (print-next)
347 (print-method-brief (term-head target))
348 (print-next)
349 (format t " str: ~a" (method-rewrite-strategy (term-head target)))))
350 (setq target (reducer-no-stat target module :red))
351 (when *debug-bterm*
352 (with-in-module ((get-context-module))
353 (format t "~% --> ")
354 (term-print-with-sort $$term)))
356 (let ((*always-memo* t))
357 (setq target (reducer-no-stat target module :red)))
355358 (when (is-true? $$term)
356359 (push subst answers))))
357 answers)))
360 (nreverse answers))))
358361
359362 ;;; make-abst-boolean-term : term -> Values (abst-bterm List(substitution))
360363 ;;;
370373 (with-in-module (module)
371374 (reset-reduced-flag term)
372375 (when *citp-verbose*
373 (format t "~%-- computing normal form of~% ")
374 (term-print term))
375 (let ((target (reducer-no-stat term module :red)))
376 (format t "~%-- computing normal form."))
377 (let* ((*always-memo* t)
378 (target (reducer-no-stat term module :red)))
379 (format t "~%--> ")
380 (term-print term)
376381 ;; abstract
377382 (when *citp-verbose*
378383 (format t "~%-- starting abstraction"))
384389 (format t "~%** Abstracted boolean term:")
385390 (with-in-module (module)
386391 (print-next)
387 (print-term-horizontal *abst-bterm-representation* module)
388 (format t "~% where")
389 (let ((*print-indent* (+ 2 *print-indent*)))
390 (dolist (var (nreverse (term-variables *abst-bterm-representation*)))
391 (let ((mapping (find-bvar-subst var bterm)))
392 (unless mapping
393 (with-output-chaos-error ('internal-err)
394 (format t "Could not find the mapping of variable ~a." (variable-name var))))
395 (print-next)
396 (term-print-with-sort var)
397 (princ " |-> ")
398 (term-print-with-sort mapping))))))))))
392 (term-print *abst-bterm-representation*)
393 (when *citp-verbose*
394 (print-term-horizontal *abst-bterm-representation* module))
395 (print-bterm-substitution bterm *abst-bterm-representation*)))))))
396
397 (defun print-bterm-substitution (bterm &optional
398 (term-representation *abst-bterm-representation*))
399 (declare (type abst-bterm bterm))
400 (with-in-module ((abst-bterm-module bterm))
401 (print-next)
402 (princ "where")
403 (let ((*print-indent* (+ 2 *print-indent*)))
404 (dolist (var (nreverse (term-variables term-representation)))
405 (let ((mapping (find-bvar-subst var bterm)))
406 (unless mapping
407 (with-output-chaos-error ('internal-err)
408 (format t "Could not find the mapping of variable ~a." (variable-name var))))
409 (print-next)
410 (term-print var)
411 (princ " |-> ")
412 (term-print mapping)))))
413 (terpri))
399414
400415 ;;; try-resolve-bterm
401416 ;;;
413428 (let ((num 0))
414429 (declare (type fixnum num))
415430 (let ((*print-indent* (+ 2 *print-indent*)))
416 (dolist (sub (reverse ans))
431 (dolist (sub ans)
417432 (print-next)
418433 (format t "(~d): " (incf num))
419434 (print-substitution sub))))))
446461 (defun bresolve ()
447462 (try-resolve-bterm))
448463
464 ;;; bshow
465 ;;;
466 (defun bshow (tree?)
467 (unless *abst-bterm*
468 (return-from bshow nil))
469 (with-in-module ((abst-bterm-module *abst-bterm*))
470 (if (equal tree? "tree")
471 (print-term-horizontal *abst-bterm-representation* *current-module*)
472 (if (equal tree? ".")
473 (term-print *abst-bterm-representation*)
474 (with-output-chaos-error ('invalid-parameter)
475 (format t "Unknown option ~s" tree?))))
476 (print-bterm-substitution *abst-bterm* *abst-bterm-representation*)))
477
449478 ;;; EOF
280280 (setq goal-name nil)
281281 (setq preterm (nth 1 args))))
282282 (list mode goal-name preterm)))
283
284 ;;; bshow | :bshow
285 ;;;
286 (defun citp-parse-bshow (args)
287 (let ((param (cadr args)))
288 (or param ".")))
283289
284290
285291
911911 (*print-xmode* :fancy))
912912 (dolist (as assumptions)
913913 (print-next)
914 (print-axiom-brief as)))))
914 (print-axiom-brief as)
915 (princ " .")))))
915916 (let ((proved (goal-proved goal)))
916917 (when proved
917918 (print-next)
919920 (let ((*print-indent* (+ 2 *print-indent*)))
920921 (dolist (ax proved)
921922 (print-next)
922 (print-axiom-brief ax)))))
923 (print-axiom-brief ax)
924 (princ " .")))))
923925 (let ((targets (goal-targets goal)))
924926 (when targets
925927 (print-next)
929931 (let ((*print-indent* (+ 2 *print-indent*)))
930932 (dolist (target targets)
931933 (print-next)
932 (print-axiom-brief target)))))))
934 (print-axiom-brief target)
935 (princ " .")))))))
933936 (let ((subnodes (ptree-node-subnodes node)))
934937 (when subnodes
935938 (let ((*print-indent* (+ 2 *print-indent*)))