| 1 |
;;;-*-Mode:LISP; Package: CHAOS; Base:10; Syntax:Common-lisp -*- |
| 2 |
;;; |
| 3 |
;;; Copyright (c) 2000-2018, Toshimi Sawada. All rights reserved. |
| 4 |
;;; |
| 5 |
;;; Redistribution and use in source and binary forms, with or without |
| 6 |
;;; modification, are permitted provided that the following conditions |
| 7 |
;;; are met: |
| 8 |
;;; |
| 9 |
;;; * Redistributions of source code must retain the above copyright |
| 10 |
;;; notice, this list of conditions and the following disclaimer. |
| 11 |
;;; |
| 12 |
;;; * Redistributions in binary form must reproduce the above |
| 13 |
;;; copyright notice, this list of conditions and the following |
| 14 |
;;; disclaimer in the documentation and/or other materials |
| 15 |
;;; provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED |
| 18 |
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 |
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 21 |
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 23 |
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 |
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 |
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
;;; |
| 29 |
(in-package :chaos) |
| 30 |
#|============================================================================= |
| 31 |
System:CHAOS |
| 32 |
Module:thstuff |
| 33 |
File:bool-term.lisp |
| 34 |
=============================================================================|# |
| 35 |
#-:chaos-debug |
| 36 |
(declaim (optimize (speed 3) (safety 0) #-GCL (debug 0))) |
| 37 |
#+:chaos-debug |
| 38 |
(declaim (optimize (speed 1) (safety 3) #-GCL (debug 3))) |
| 39 |
|
| 40 |
;;;============================================================================= |
| 41 |
;;; Utilities to support investigating big boolean term of xor-and normal form. |
| 42 |
;;;============================================================================= |
| 43 |
|
| 44 |
(defvar .bterm-assoc-table. nil) |
| 45 |
(defvar .bvar-num. 0) |
| 46 |
(declaim (type fixnum .bvar-num.)) |
| 47 |
|
| 48 |
(defun clear-bterm-memo-table () |
| 49 |
(setq .bterm-assoc-table. nil)) |
| 50 |
|
| 51 |
(defun reset-bvar () |
| 52 |
(setq .bvar-num. 0) |
| 53 |
(clear-bterm-memo-table)) |
| 54 |
|
| 55 |
(defun make-bterm-variable () |
| 56 |
(let ((varname (intern (format nil "`P-~d" (incf .bvar-num.))))) |
| 57 |
(make-pconst-term *bool-sort* varname))) |
| 58 |
|
| 59 |
(defun get-bterm-variable (term) |
| 60 |
(unless (or (is-true? term) |
| 61 |
(is-false? term)) |
| 62 |
(let ((ent (assoc term .bterm-assoc-table. :test #'term-equational-equal))) |
| 63 |
(if ent |
| 64 |
(cdr ent) |
| 65 |
(let ((var (make-bterm-variable))) |
| 66 |
(push (cons term var) .bterm-assoc-table.) |
| 67 |
var))))) |
| 68 |
|
| 69 |
;;; ======================================================================= |
| 70 |
;;; Abstracted representation of a _xor_-_and_ normal form of boolean term. |
| 71 |
|
| 72 |
;;; ABS-BTERM: |
| 73 |
;;; abstracted boolean term. |
| 74 |
;;; each non _and_ or _xor_ boolean sub-term is abstracted by a |
| 75 |
;;; variable. |
| 76 |
(defstruct (abst-bterm (:print-function print-bterm)) |
| 77 |
(module nil) ; context module |
| 78 |
(term nil) ; the original term |
| 79 |
(subst nil) ; list of substitution |
| 80 |
; or instance of abst-bterm(for _and_ abstraction) |
| 81 |
) |
| 82 |
|
| 83 |
(defstruct (abst-and (:include abst-bterm))) |
| 84 |
|
| 85 |
(defun print-bterm (bt &optional (stream *standard-output*) &rest ignore) |
| 86 |
(declare (ignore ignore)) |
| 87 |
(with-in-module ((abst-bterm-module bt)) |
| 88 |
(if (abst-and-p bt) |
| 89 |
(princ ":and[" stream) |
| 90 |
(princ ":xor[" stream)) |
| 91 |
(let ((*print-indent* (+ 2 *print-indent*)) |
| 92 |
(num 0)) |
| 93 |
(declare (type fixnum *print-indent* num)) |
| 94 |
(dolist (sub (abst-bterm-subst bt)) |
| 95 |
(print-next nil *print-indent* stream) |
| 96 |
(format stream "(~d) " (incf num)) |
| 97 |
(if (abst-bterm-p sub) |
| 98 |
(print-bterm sub stream) |
| 99 |
(progn |
| 100 |
(let ((var (car sub)) |
| 101 |
(term (cdr sub))) |
| 102 |
(when (consp var) |
| 103 |
(princ (string (variable-print-name var))) |
| 104 |
(princ " = ")) |
| 105 |
(term-print term)))))) |
| 106 |
(princ " ]" stream))) |
| 107 |
|
| 108 |
(defun abst-bterm-variables (bterm) |
| 109 |
(let ((vars nil)) |
| 110 |
(dolist (sub (abst-bterm-subst bterm)) |
| 111 |
(if (abst-bterm-p sub) |
| 112 |
(setq vars (nconc vars (abst-bterm-variables sub))) |
| 113 |
(when (consp (car sub)) |
| 114 |
(pushnew (car sub) vars)))) |
| 115 |
(delete-duplicates vars))) |
| 116 |
|
| 117 |
;;;========================================================================== |
| 118 |
;;; golobals |
| 119 |
|
| 120 |
(defvar *abst-bterm* |
| 121 |
nil |
| 122 |
"binds abstracted boolean term") |
| 123 |
|
| 124 |
(defvar *abst-bterm-representation* |
| 125 |
nil |
| 126 |
"term constructed from *abst-bterm*") |
| 127 |
|
| 128 |
|
| 129 |
(defvar *abst-bterm-target-variable* |
| 130 |
nil |
| 131 |
"binds a variable targeted to heuristic inspection (bguess).") |
| 132 |
|
| 133 |
;;;=========================================================================== |
| 134 |
;;; make abst-bterm from a term of sort 'Bool' |
| 135 |
|
| 136 |
;;; xtract-xor-subterms : term |
| 137 |
;;; returns ac subterms of the given term iff the top op is _xor_ |
| 138 |
;;; |
| 139 |
(defun xtract-xor-subterms (term) |
| 140 |
(if (method= (term-head term) *bool-xor*) |
| 141 |
(list-ac-subterms term *bool-xor*) |
| 142 |
nil)) |
| 143 |
|
| 144 |
;;; xtract-and-subterms : term |
| 145 |
;;; returns ac subterms of the given term iff the top op is _and_ |
| 146 |
;;; |
| 147 |
(defun xtract-and-subterms (term) |
| 148 |
(if (method= (term-head term) *bool-and*) |
| 149 |
(list-ac-subterms term *bool-and*) |
| 150 |
nil)) |
| 151 |
|
| 152 |
;;; xtract-tfs : term -> List({'true'|'false'}) |
| 153 |
;;; |
| 154 |
(defun xtract-tfs (mode term) |
| 155 |
(let ((subs (if (eq mode :xor) |
| 156 |
(xtract-xor-subterms term) |
| 157 |
(xtract-and-subterms term))) |
| 158 |
(res nil)) |
| 159 |
(dolist (x subs) |
| 160 |
(when (or (is-true? x) (is-false? x)) |
| 161 |
(push x res))) |
| 162 |
res)) |
| 163 |
|
| 164 |
;;; abstract-boolean-term : bool-term -> abst-bterm |
| 165 |
;;; |
| 166 |
(defun make-and-abstraction (term subterms module) |
| 167 |
(let ((subst nil)) |
| 168 |
(dolist (sub subterms) |
| 169 |
(let ((ss (get-bterm-variable sub))) |
| 170 |
(if ss |
| 171 |
(push (cons ss sub) subst) |
| 172 |
;; true of false |
| 173 |
(push (cons nil sub) subst)))) |
| 174 |
(make-abst-and :term term :subst (nreverse subst) :module module))) |
| 175 |
|
| 176 |
;;; assign-tf |
| 177 |
;;; make all posssible variable substitutions with the domain {'true' ,'false'}. |
| 178 |
;;; |
| 179 |
(defun make-tf-combination (rows columns) |
| 180 |
(let ((assignment nil) |
| 181 |
(subst (make-array (list rows columns)))) |
| 182 |
(flet ((change-parity () |
| 183 |
(if (is-true? assignment) |
| 184 |
(setq assignment *bool-false*) |
| 185 |
(setq assignment *bool-true*)))) |
| 186 |
(dotimes (c columns) |
| 187 |
(setq assignment nil) |
| 188 |
(let ((cycle (expt 2 c))) |
| 189 |
(dotimes (r rows) |
| 190 |
(if (not assignment) |
| 191 |
(setq assignment *bool-true*) |
| 192 |
(if (= 0 (mod r cycle)) |
| 193 |
(change-parity))) |
| 194 |
(setf (aref subst r c) assignment)))) |
| 195 |
subst))) |
| 196 |
|
| 197 |
(defun assign-tf (list-vars) |
| 198 |
(let* ((columns (length list-vars)) |
| 199 |
(rows (expt 2 columns)) |
| 200 |
(assignments (make-tf-combination rows columns)) |
| 201 |
(l-subst nil)) |
| 202 |
(dotimes (r rows) |
| 203 |
(let ((subst nil)) |
| 204 |
(dotimes (c columns) |
| 205 |
(push (cons (nth c list-vars) (aref assignments r c)) subst)) |
| 206 |
(push (nreverse subst) l-subst))) |
| 207 |
(when *debug-bterm* |
| 208 |
(with-in-module ((get-context-module)) |
| 209 |
(let ((num 0)) |
| 210 |
(dolist (sub (reverse l-subst)) |
| 211 |
(format t "~%(~d): " (incf num)) |
| 212 |
(print-substitution sub))))) |
| 213 |
(nreverse l-subst))) |
| 214 |
|
| 215 |
;;; make-abst-boolean-term : term -> Values (abst-bterm List(substitution)) |
| 216 |
;;; |
| 217 |
(defun make-abst-boolean-term (term module) |
| 218 |
(unless (sort= (term-sort term) *bool-sort*) |
| 219 |
(with-output-chaos-warning () |
| 220 |
(format t "Given term is not of sort Bool. Ignored.") |
| 221 |
(return-from make-abst-boolean-term nil))) |
| 222 |
(!setup-reduction module) |
| 223 |
(with-in-module (module) |
| 224 |
(reset-reduced-flag term) |
| 225 |
(let* ((*always-memo* t) |
| 226 |
(target (reducer term module :red))) |
| 227 |
(format t "~%~a" (generate-statistics-form-rewriting-only)) |
| 228 |
(format t "~%--> ") |
| 229 |
(term-print term) |
| 230 |
;; abstract |
| 231 |
(let ((bterm (abstract-boolean-term target module)) |
| 232 |
(abs-start (get-internal-run-time))) |
| 233 |
(when bterm |
| 234 |
(setq *abst-bterm* bterm) |
| 235 |
(setq *abst-bterm-representation* |
| 236 |
(make-bterm-representation bterm)) |
| 237 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 238 |
(format t "~%** Abstracted boolean term:") |
| 239 |
(format t "~%(consumed ~,3f sec. for abstraction)~%" |
| 240 |
(elapsed-time-in-seconds abs-start (get-internal-run-time))) |
| 241 |
(with-in-module (module) |
| 242 |
(print-next) |
| 243 |
(term-print *abst-bterm-representation*) |
| 244 |
(when *citp-verbose* |
| 245 |
(print-term-horizontal *abst-bterm-representation* module)) |
| 246 |
(print-bterm-substitution bterm *abst-bterm-representation*)))))))) |
| 247 |
|
| 248 |
;;; find-bvar-subst : variable abst-bterm -> assigned term |
| 249 |
;;; returns the assigned term of the variable. |
| 250 |
;;; |
| 251 |
(defun find-bvar-subst (var bterm) |
| 252 |
(declare (type abst-bterm bterm)) |
| 253 |
(dolist (sub (abst-bterm-subst bterm)) |
| 254 |
(if (abst-bterm-p sub) |
| 255 |
(let ((res (find-bvar-subst var sub))) |
| 256 |
(when res (return-from find-bvar-subst res))) |
| 257 |
(when (eq (variable-name var) (variable-name (car sub))) |
| 258 |
(return-from find-bvar-subst (cdr sub)))))) |
| 259 |
|
| 260 |
(defun print-bterm-substitution (bterm &optional |
| 261 |
(term-representation *abst-bterm-representation*)) |
| 262 |
(declare (type abst-bterm bterm)) |
| 263 |
(with-in-module ((abst-bterm-module bterm)) |
| 264 |
(let ((vars (sort (term-pvariables term-representation) |
| 265 |
#'(lambda (x y) |
| 266 |
(string< (string (variable-name x)) |
| 267 |
(string (variable-name y))))))) |
| 268 |
(unless vars (return-from print-bterm-substitution nil)) |
| 269 |
(print-next) |
| 270 |
(princ "where") |
| 271 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 272 |
(dolist (var vars) |
| 273 |
(let ((mapping (find-bvar-subst var bterm))) |
| 274 |
(unless mapping |
| 275 |
(with-output-chaos-error ('internal-err) |
| 276 |
(format t "Could not find the mapping of variable ~a." (variable-name var)))) |
| 277 |
(print-next) |
| 278 |
(princ (string (variable-print-name var))) |
| 279 |
(princ " = ") |
| 280 |
(term-print mapping))))) |
| 281 |
(terpri))) |
| 282 |
|
| 283 |
(defun print-bterm-with-subst (substl bterm) |
| 284 |
(declare (type abst-bterm bterm)) |
| 285 |
(with-in-module ((abst-bterm-module bterm)) |
| 286 |
(let ((done nil)) |
| 287 |
(dolist (subst substl) |
| 288 |
(dolist (sub subst) |
| 289 |
(let ((var (car sub))) |
| 290 |
(unless (member var done) |
| 291 |
(push var done) |
| 292 |
(let ((mapping (find-bvar-subst var bterm))) |
| 293 |
(unless mapping |
| 294 |
(with-output-chaos-error ('internal-error) |
| 295 |
(format t "Could not find the mapping of variable ~a." (variable-name var)))) |
| 296 |
(princ (string (variable-print-name var))) |
| 297 |
(princ " = ") |
| 298 |
(term-print mapping) |
| 299 |
(print-next))))))))) |
| 300 |
|
| 301 |
;;; abstract-boolean-term : term context-module -> abst-bterm |
| 302 |
;;; converts given boolen term into abst-bterm. |
| 303 |
;;; |
| 304 |
(defun abstract-boolean-term (term module) |
| 305 |
(let ((bterm (make-abst-bterm :term term :module module)) |
| 306 |
(xor-subs (xtract-xor-subterms term)) |
| 307 |
(subst nil)) |
| 308 |
;; reset variable number & term hash |
| 309 |
(reset-bvar) |
| 310 |
(if xor-subs |
| 311 |
;; top operator is _xor_ |
| 312 |
;; we further decompose by _and_ |
| 313 |
(dolist (xs xor-subs) |
| 314 |
(let ((as (xtract-and-subterms xs))) |
| 315 |
(if as |
| 316 |
(push (make-and-abstraction xs as module) subst) |
| 317 |
(let ((ss (get-bterm-variable xs))) |
| 318 |
(if ss |
| 319 |
(push (cons ss xs) subst) |
| 320 |
;; true or false |
| 321 |
(push (cons nil xs) subst)))))) |
| 322 |
;; top operator is not xor |
| 323 |
(let ((as (xtract-and-subterms term))) |
| 324 |
(if as |
| 325 |
(let ((and-abst (make-and-abstraction term as module))) |
| 326 |
(push and-abst subst)) |
| 327 |
;; we only accept xor-and normal form |
| 328 |
(with-output-msg () |
| 329 |
(format t "'bresolve' does not treate trivial form like this.") |
| 330 |
(print-next) |
| 331 |
(term-print term) |
| 332 |
(return-from abstract-boolean-term nil))))) |
| 333 |
(setf (abst-bterm-subst bterm) (nreverse subst)) |
| 334 |
bterm)) |
| 335 |
|
| 336 |
;;; make-bterm-representation : bterm -> boolen term |
| 337 |
;;; from bterm make a concrete representation of abstracted boolean term |
| 338 |
;;; |
| 339 |
(defun make-and-representation (abst-and) |
| 340 |
(declare (type abst-and abst-and)) |
| 341 |
(let ((repre (make-right-assoc-normal-form |
| 342 |
*bool-and* |
| 343 |
(mapcar #'(lambda (x) (if (consp (car x)) |
| 344 |
(car x) |
| 345 |
;; true or false |
| 346 |
(cdr x))) |
| 347 |
(abst-and-subst abst-and))))) |
| 348 |
(update-lowest-parse repre) |
| 349 |
repre)) |
| 350 |
|
| 351 |
(defun make-xor-representation (bterm) |
| 352 |
(declare (type abst-bterm bterm)) |
| 353 |
(let ((repre (make-right-assoc-normal-form |
| 354 |
*bool-xor* |
| 355 |
(mapcar #'(lambda (x) (if (abst-and-p x) |
| 356 |
(make-and-representation x) |
| 357 |
(if (consp (car x)) |
| 358 |
(car x) |
| 359 |
;; true or false |
| 360 |
(cdr x)))) |
| 361 |
(abst-bterm-subst bterm))))) |
| 362 |
(update-lowest-parse repre) |
| 363 |
repre)) |
| 364 |
|
| 365 |
(defun make-bterm-representation (bterm) |
| 366 |
(let ((subst (abst-bterm-subst bterm))) |
| 367 |
;; no _xor nor _and_ ops in original term |
| 368 |
(unless subst |
| 369 |
(return-from make-bterm-representation (abst-bterm-term bterm))) |
| 370 |
;; sole _and_ term. |
| 371 |
(when (and (null (cdr subst)) |
| 372 |
(abst-and-p (car subst))) |
| 373 |
(return-from make-bterm-representation (make-and-representation (car subst)))) |
| 374 |
;; _xor_ normal form |
| 375 |
(make-xor-representation bterm))) |
| 376 |
|
| 377 |
;;; =========================================================================================== |
| 378 |
;;; PRINTERS |
| 379 |
;;; abst-bterm printers |
| 380 |
|
| 381 |
;;; simple-print-bterm : bterm -> void |
| 382 |
(defun simple-print-bterm (bterm) |
| 383 |
(declare (type abst-bterm bterm)) |
| 384 |
(let ((aterm (make-bterm-representation bterm))) |
| 385 |
(term-print-with-sort aterm))) |
| 386 |
|
| 387 |
;;; print-bterm-tree : bterm -> void |
| 388 |
(defun print-bterm-tree (bterm &optional (mode :vertical)) |
| 389 |
(declare (type abst-bterm bterm)) |
| 390 |
(with-in-module ((abst-bterm-module bterm)) |
| 391 |
(let ((aterm (make-bterm-representation bterm))) |
| 392 |
(if (eq mode :vertical) |
| 393 |
(print-term-graph aterm *chaos-verbose*) |
| 394 |
(print-term-horizontal (make-bterm-representation bterm) *current-module*))))) |
| 395 |
|
| 396 |
;;; print-bterm-grinding : term -> void |
| 397 |
;;; |
| 398 |
(defun print-bterm-grinding (bt) |
| 399 |
(with-in-module ((abst-bterm-module bt)) |
| 400 |
(print-next) |
| 401 |
(let ((torf nil)) |
| 402 |
(if (abst-and-p bt) |
| 403 |
(progn |
| 404 |
(setq torf (xtract-and-subterms (abst-bterm-term bt))) |
| 405 |
(princ ">> and --->")) |
| 406 |
(progn |
| 407 |
(setq torf (xtract-xor-subterms (abst-bterm-term bt))) |
| 408 |
(when torf |
| 409 |
(princ ">> xor ***>")))) |
| 410 |
(let ((bs nil)) |
| 411 |
(dolist (sub (abst-bterm-subst bt)) |
| 412 |
(if (abst-bterm-p sub) |
| 413 |
(print-bterm-grinding sub) |
| 414 |
(let ((var (car sub)) |
| 415 |
(term (cdr sub))) |
| 416 |
(if (consp var) |
| 417 |
(push (cons (string (variable-print-name var))term) bs) |
| 418 |
(push sub bs))))) |
| 419 |
(when bs |
| 420 |
(dolist (vt (sort bs #'(lambda (x y) |
| 421 |
(string< (string (car x)) (string (car y)))))) |
| 422 |
(print-next) |
| 423 |
(when (stringp (car vt)) |
| 424 |
(princ (car vt)) |
| 425 |
(princ " = ")) |
| 426 |
(term-print (cdr vt))))) |
| 427 |
(print-next) |
| 428 |
(if (abst-and-p bt) |
| 429 |
(princ "<----------") |
| 430 |
(when torf |
| 431 |
(princ "<**********")))))) |
| 432 |
|
| 433 |
;;; print-abst-bterm : bterm &key mode |
| 434 |
;;; mode :simple print term representation |
| 435 |
;;; :tree print term representation as vertical tree structure |
| 436 |
;;; :horizontal print term representation horizontal tree structure |
| 437 |
;;; also shows a substitution used for abstruction. |
| 438 |
;;; |
| 439 |
(defun print-abst-bterm (bterm &key (mode :simple)) |
| 440 |
(case mode |
| 441 |
(:simple (simple-print-bterm bterm)) |
| 442 |
(:tree (print-bterm-tree bterm)) |
| 443 |
(:horizontal (print-bterm-tree bterm :horizontal)) |
| 444 |
(:grind (print-bterm-grinding bterm)) |
| 445 |
(otherwise |
| 446 |
(with-output-chaos-error ('invalid-mode) |
| 447 |
(format t "Invalid print mode ~a." mode))))) |
| 448 |
|
| 449 |
;;; =========================================================================================== |
| 450 |
;;; RESOLVER |
| 451 |
;;; computes possible solutions (assignments) which makes abstracted boolean term to be 'true.' |
| 452 |
;;; |
| 453 |
|
| 454 |
(defmacro pvar-image (sigma var) |
| 455 |
`(cdr (assoc ,var ,sigma :test #'(lambda (x y) |
| 456 |
(eq (variable-name x) (variable-name y)))))) |
| 457 |
|
| 458 |
(defun subst-pcimage-cp (sigma term) |
| 459 |
(declare (type list sigma) |
| 460 |
(type term term)) |
| 461 |
(let ((*consider-object* t)) |
| 462 |
(cond ((term-is-pconstant? term) |
| 463 |
(let ((im (pvar-image sigma term))) |
| 464 |
(if im;; i.e. im = sigma(term) |
| 465 |
(values im nil) |
| 466 |
(values term t)))) |
| 467 |
((term-is-builtin-constant? term) term) ; shold we copy? |
| 468 |
(t (let ((l-result nil) |
| 469 |
(modif-sort nil)) |
| 470 |
(dolist (s-t (term-subterms term)) |
| 471 |
(multiple-value-bind (image-s-t same-sort) |
| 472 |
(subst-pcimage-cp sigma s-t) |
| 473 |
(unless same-sort (setq modif-sort t)) |
| 474 |
(push image-s-t l-result))) |
| 475 |
(setq l-result (nreverse l-result)) |
| 476 |
(if modif-sort |
| 477 |
(let ((term-image (make-term-with-sort-check (term-head term) |
| 478 |
l-result))) |
| 479 |
(values term-image |
| 480 |
(sort= (term-sort term) |
| 481 |
(term-sort term-image)))) |
| 482 |
(values (make-applform (term-sort term) |
| 483 |
(term-head term) |
| 484 |
l-result) |
| 485 |
t))))))) |
| 486 |
|
| 487 |
;;; find-bterm-solution-with-subst : List(substitution) abst-bterm-representation -> List(Substitution) |
| 488 |
;;; retuns a list of substitution which makes bterm to be true. |
| 489 |
;;; |
| 490 |
(defun find-bterm-solution-with-subst (all-subst abst-term &optional (module (get-context-module))) |
| 491 |
(let ((answers nil)) |
| 492 |
(dolist (subst all-subst) |
| 493 |
(let ((target (subst-pcimage-cp subst abst-term))) |
| 494 |
(reset-reduced-flag target) |
| 495 |
(let ((*always-memo* t)) |
| 496 |
(setq target (reducer-no-stat target module :red))) |
| 497 |
(when (is-true? $$term) |
| 498 |
(push subst answers)))) |
| 499 |
(nreverse answers))) |
| 500 |
|
| 501 |
;;; resolve-bterm-by-wf : bterm [limit] -> void |
| 502 |
;;; working hourse |
| 503 |
;;; |
| 504 |
(defun resolve-bterm-by-wf (bterm &optional (comb-limit nil) (all? nil)) |
| 505 |
(declare (type (or null fixnum) comb-limit) |
| 506 |
(type abst-bterm bterm)) |
| 507 |
(with-in-module ((abst-bterm-module bterm)) |
| 508 |
(let* ((abst-term (make-bterm-representation bterm)) |
| 509 |
(vars (reverse (term-pvariables abst-term))) |
| 510 |
(init (mapcar #'list vars)) |
| 511 |
(len (length vars)) |
| 512 |
(comb (make-array len)) |
| 513 |
(lim (or comb-limit len))) |
| 514 |
(declare (type list init) |
| 515 |
(type fixnum len) |
| 516 |
(type simple-array comb)) |
| 517 |
;; |
| 518 |
(when (> lim len) |
| 519 |
(with-output-chaos-warning () |
| 520 |
(format t "Too many combination limit ~D. Reset to ~D" lim len)) |
| 521 |
(setq lim len)) |
| 522 |
;; initial combinations: no predicate combinations |
| 523 |
(dotimes (i len) |
| 524 |
(setf (aref comb i) (list (nth i init)))) |
| 525 |
;; repeat step by step |
| 526 |
;; untill reaches to limited number of predicate combinations |
| 527 |
(dotimes (i lim) |
| 528 |
;; do the job: |
| 529 |
(let ((answers nil)) |
| 530 |
(dotimes (n len) |
| 531 |
(dolist (variables (aref comb n)) |
| 532 |
(let ((ans (find-bterm-solution-with-subst (assign-tf variables) abst-term))) |
| 533 |
(when ans |
| 534 |
(push ans answers))))) |
| 535 |
(when answers |
| 536 |
(let ((num 0)) |
| 537 |
(declare (type fixnum num)) |
| 538 |
(format t "~%** (~d) The following assignment(s) makes the term to be 'true'." |
| 539 |
(1+ i)) |
| 540 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 541 |
(dolist (solution (nreverse answers)) |
| 542 |
(dolist (subst solution) |
| 543 |
(format t "~%[~d] " (incf num)) |
| 544 |
(print-substitution subst)) |
| 545 |
(format t "~%where") |
| 546 |
(print-next) |
| 547 |
(print-bterm-with-subst solution bterm)))) |
| 548 |
(unless all? |
| 549 |
(return-from resolve-bterm-by-wf t)))) |
| 550 |
;; prepare next predicate combinations |
| 551 |
(dotimes (j len) |
| 552 |
(let ((bases (aref comb j))) |
| 553 |
(let ((next nil) |
| 554 |
(seedbase (nthcdr (+ 1 j i) vars))) |
| 555 |
(dotimes (k (length bases)) |
| 556 |
(let ((base (nth k bases)) |
| 557 |
(seedl (nthcdr k seedbase))) |
| 558 |
(dolist (seed seedl) |
| 559 |
(let ((new (append base (list seed)))) |
| 560 |
(when new |
| 561 |
(push new next)))))) |
| 562 |
(setf (aref comb j) (reverse next))))))))) |
| 563 |
|
| 564 |
;;; try-resolve-bterm |
| 565 |
;;; finds all variable assignments which make *abst-bterm* to be 'true'. |
| 566 |
;;; |
| 567 |
(defun try-resolve-bterm (&optional (comb-limit nil) (all? nil)) |
| 568 |
;; find solutions |
| 569 |
(resolve-bterm-by-wf *abst-bterm* comb-limit all?)) |
| 570 |
|
| 571 |
(defvar *binspect-mod-name* "|binspect|") |
| 572 |
(defvar *binspect-mod-decl* |
| 573 |
(format nil "module ~a {pr(BOOL)}" *binspect-mod-name*)) |
| 574 |
|
| 575 |
(defun get-binspect-module () |
| 576 |
(let ((mod (eval-modexp *binspect-mod-name*))) |
| 577 |
(when (modexp-is-error mod) |
| 578 |
(let ((*chaos-quiet* t)) |
| 579 |
(with-input-from-string (*standard-input* *binspect-mod-decl*) |
| 580 |
(process-cafeobj-input)) |
| 581 |
(setq mod (eval-modexp *binspect-mod-name*)))) |
| 582 |
mod)) |
| 583 |
|
| 584 |
;;; binspect-intro-predicates : bterm |
| 585 |
;;; |
| 586 |
(defun binspect-intro-predicates (bterm module) |
| 587 |
(unless module |
| 588 |
(with-output-chaos-error ('no-module) |
| 589 |
(format t "binspect: internal error, no module."))) |
| 590 |
(with-in-module (module) |
| 591 |
(let ((vars (abst-bterm-variables bterm))) |
| 592 |
(dolist (v vars) |
| 593 |
(when *debug-bterm* |
| 594 |
(format t "~%.. introducing predicate ~a" (string (variable-print-name v)))) |
| 595 |
(push (cons (variable-name v) v) (module-variables module)) |
| 596 |
(symbol-table-add (module-symbol-table module) |
| 597 |
(variable-name v) |
| 598 |
v)) |
| 599 |
(set-needs-parse module) |
| 600 |
(compile-module module)))) |
| 601 |
|
| 602 |
;;; ====== |
| 603 |
;;; bguess |
| 604 |
;;; ****************************************************** |
| 605 |
|
| 606 |
;;; get-bterm-predicate-combinations : pred-or-all bterm strategy |
| 607 |
;;; |
| 608 |
(defun make-pred-ordered-pairs (vars) |
| 609 |
(let ((combs nil)) |
| 610 |
(do* ((preds vars (cdr preds)) |
| 611 |
(pred (car preds) (car preds))) |
| 612 |
((endp preds)) |
| 613 |
(dolist (v vars) |
| 614 |
(unless (eq v pred) |
| 615 |
(push (cons pred v) combs)))) |
| 616 |
(sort combs #'(lambda (x y) |
| 617 |
(string< (string (variable-name x)) (string (variable-name y))))))) |
| 618 |
|
| 619 |
(defun make-pred-combinations (vars) |
| 620 |
(let ((combs nil)) |
| 621 |
(do* ((preds vars (cdr preds)) |
| 622 |
(pred (car preds) (car preds))) |
| 623 |
((endp preds)) |
| 624 |
(dolist (p preds) |
| 625 |
(unless (eq p pred) |
| 626 |
(push (cons p pred) combs)))) |
| 627 |
(sort combs #'(lambda (x y) |
| 628 |
(string< (string (variable-name x)) (string (variable-name y))))))) |
| 629 |
|
| 630 |
(defun get-bterm-predicate-combinations (var bterm strat) |
| 631 |
(let ((vars (abst-bterm-variables bterm)) |
| 632 |
(combinations nil)) |
| 633 |
(cond ((eq var :all) |
| 634 |
(if (eq strat :imply) |
| 635 |
(make-pred-ordered-pairs vars) |
| 636 |
(make-pred-combinations vars))) |
| 637 |
(t (dolist (v vars combinations) |
| 638 |
(unless (eq v var) |
| 639 |
(push (cons var v) combinations))))))) |
| 640 |
|
| 641 |
(defun bguess-ax-form (mode pred1 pred2) |
| 642 |
(let ((ax-form nil)) |
| 643 |
(case mode |
| 644 |
(:imply |
| 645 |
;; pred1 and pred2 = pred1 |
| 646 |
(setq ax-form (format nil "eq[:bimply]: ~a:Bool and ~a:Bool = ~a:Bool ." pred1 pred2 pred1))) |
| 647 |
(:and |
| 648 |
;; pred1 and pred2 = false |
| 649 |
(setq ax-form (format nil "eq[:band]: ~a:Bool and ~a:Bool = false ." pred1 pred2))) |
| 650 |
(:or |
| 651 |
;; pred1 or pred2 = true |
| 652 |
;; i.e. prd1 xor (pred2 xor (pred1 and pred2)) = true . |
| 653 |
(setq ax-form (format nil "eq[:bor]: ~a:Bool xor (~a:Bool xor (~a:Bool and ~a:Bool)) = true ." pred1 pred2 pred1 pred2))) |
| 654 |
(otherwise |
| 655 |
(with-output-chaos-error ('unknown-mode) |
| 656 |
(format t "Internal error, bguess unknown mode: ~s" mode)))) |
| 657 |
ax-form)) |
| 658 |
|
| 659 |
;;; do-bguess : strat -> void |
| 660 |
;;; do the guess work |
| 661 |
;;; |
| 662 |
|
| 663 |
;;; report-bguess-result |
| 664 |
;;; |
| 665 |
(defun report-bguess-result (solutions bterm depth) |
| 666 |
(when solutions |
| 667 |
(format t "~&** (~d) each of the following equation~p makes the inspected term 'true'" |
| 668 |
(incf depth) |
| 669 |
(length solutions)) |
| 670 |
(with-in-module ((get-binspect-module)) |
| 671 |
(let ((num 0)) |
| 672 |
(dolist (hypo solutions) |
| 673 |
(print-next) |
| 674 |
(format t "[~d] " (incf num)) |
| 675 |
(print-axiom-brief hypo) (princ " .") |
| 676 |
(when (term-subterms (axiom-lhs hypo)) |
| 677 |
(print-bterm-substitution bterm (axiom-lhs hypo)))))))) |
| 678 |
|
| 679 |
(defun do-bguess (mode &optional (num-comb 0)) |
| 680 |
(let ((*chaos-quiet* t) |
| 681 |
(*no-prompt* t)) |
| 682 |
(declare (special *no-prompt* *chaos-quiet*)) |
| 683 |
;; for each predicate combination do the followings |
| 684 |
(let ((pred-combinations (get-bterm-predicate-combinations |
| 685 |
*abst-bterm-target-variable* |
| 686 |
*abst-bterm* |
| 687 |
mode)) |
| 688 |
(module nil) |
| 689 |
(solutions nil)) |
| 690 |
(dolist (comb pred-combinations) |
| 691 |
(let ((hypo nil)) |
| 692 |
;; 1. open |binspect| |
| 693 |
(setq module (!open-module (eval-modexp *binspect-mod-name*))) |
| 694 |
;; 2. intro axiom according to strategy |
| 695 |
(with-input-from-string (*standard-input* |
| 696 |
(bguess-ax-form mode |
| 697 |
(string (variable-print-name (car comb))) |
| 698 |
(string (variable-print-name (cdr comb))))) |
| 699 |
(setq hypo (process-cafeobj-input))) |
| 700 |
;; 3. reduce bterm |
| 701 |
(compile-module module) |
| 702 |
(with-in-module (module) |
| 703 |
(let ((target (make-bterm-representation *abst-bterm*)) |
| 704 |
(*always-memo* t)) |
| 705 |
(setq target (reducer target *current-module* :red)) |
| 706 |
;; 4. determine the result |
| 707 |
(when (is-true? target) ;; (inspect-bguess-result target list-hypo *abst-bterm*) |
| 708 |
(push hypo solutions)))) |
| 709 |
(eval-close-module))) |
| 710 |
;; all done |
| 711 |
(report-bguess-result (nreverse solutions) *abst-bterm* num-comb) |
| 712 |
solutions))) |
| 713 |
|
| 714 |
;;; binspect-in-goal : goal-name term-form |
| 715 |
;;; abstract boolean term in the context of the goal given by goal-name. |
| 716 |
;;; |
| 717 |
(defun binspect-in-goal (goal-name preterm) |
| 718 |
(let* ((goal-node (get-target-goal-node goal-name)) |
| 719 |
(context-module (goal-context (ptree-node-goal goal-node))) |
| 720 |
(target (do-parse-term* preterm context-module))) |
| 721 |
(make-abst-boolean-term target context-module) |
| 722 |
(binspect-intro-predicates *abst-bterm* (get-binspect-module)))) |
| 723 |
|
| 724 |
;;; binspect-in-module |
| 725 |
;;; abstract boolean term in the context of a module |
| 726 |
;;; |
| 727 |
(defun binspect-in-module (mod-name preterm) |
| 728 |
(multiple-value-bind (target context-module) |
| 729 |
(do-parse-term* preterm mod-name) |
| 730 |
(make-abst-boolean-term target context-module)) |
| 731 |
(binspect-intro-predicates *abst-bterm* (get-binspect-module))) |
| 732 |
|
| 733 |
;;;========================================================================= |
| 734 |
;;; TOP LEVEL FUNCTIONS |
| 735 |
;;; |
| 736 |
|
| 737 |
;;; binspect-in |
| 738 |
;;; make abstracted boolean term. |
| 739 |
;;; :binspect [in <goal-name> :] <boolean-term> . |
| 740 |
;;; binspect [in <module-name> :] <boolean-term> . |
| 741 |
;;; |
| 742 |
(defun binspect-in (mode goal-or-module-name preterm) |
| 743 |
(let ((*chaos-quiet* t) |
| 744 |
(*no-prompt* t)) |
| 745 |
(cond ((eq mode :citp) |
| 746 |
(binspect-in-goal goal-or-module-name preterm)) |
| 747 |
(t |
| 748 |
(binspect-in-module goal-or-module-name preterm))))) |
| 749 |
|
| 750 |
(defun check-bterm-context () |
| 751 |
(unless *abst-bterm* |
| 752 |
(with-output-chaos-error ('no-bterm) |
| 753 |
(format t "No abstracted boolean term is specified. ~%Please do 'binspect' or ':binspect' first.")))) |
| 754 |
|
| 755 |
;;; bresolve |
| 756 |
;;; finds variable assignments which make abst bterm 'true'. |
| 757 |
;;; |
| 758 |
(defun bresolve (args) |
| 759 |
(check-bterm-context) |
| 760 |
(let* ((rargs (cdr args)) |
| 761 |
(limit-arg (and rargs |
| 762 |
(not (equal "all" (car rargs))) |
| 763 |
(car rargs))) |
| 764 |
(all? (and rargs (or (equal "all" (car rargs)) |
| 765 |
(equal "all" (cadr rargs))))) |
| 766 |
(limit nil)) |
| 767 |
(when (>= (length rargs) 3) |
| 768 |
(with-output-chaos-warning () |
| 769 |
(format t "Unknown arguments: ~{~a ~}" (cddr rargs)) |
| 770 |
(print-next) |
| 771 |
(princ "ignored..."))) |
| 772 |
(when (and limit-arg |
| 773 |
(not (equal "." limit-arg))) |
| 774 |
(setq limit (read-from-string limit-arg)) |
| 775 |
(unless (and (integerp limit) (< 0 limit)) |
| 776 |
(with-output-chaos-error ('invalid-limit) |
| 777 |
(format t "bresolve: invalid <limit> argument ~a" limit-arg)))) |
| 778 |
(try-resolve-bterm limit all?))) |
| 779 |
|
| 780 |
;;; bshow |
| 781 |
;;; print out abst bterm. |
| 782 |
;;; bshow [{tree | grind}] |
| 783 |
(defun bshow (tree?) |
| 784 |
(unless *abst-bterm* |
| 785 |
(return-from bshow nil)) |
| 786 |
(with-in-module ((abst-bterm-module *abst-bterm*)) |
| 787 |
(if (equal tree? "tree") |
| 788 |
(print-term-horizontal *abst-bterm-representation* *current-module*) |
| 789 |
(if (equal tree? "grind") |
| 790 |
(print-abst-bterm *abst-bterm* :mode :grind) |
| 791 |
(if (equal tree? ".") |
| 792 |
(term-print *abst-bterm-representation*) |
| 793 |
(with-output-chaos-error ('invalid-parameter) |
| 794 |
(format t "Unknown option ~s" tree?))))) |
| 795 |
(unless (equal tree? "grind") |
| 796 |
(print-bterm-substitution *abst-bterm* *abst-bterm-representation*)))) |
| 797 |
|
| 798 |
;;; find-variable-in-abst-bterm : name bterm -> variable |
| 799 |
;;; |
| 800 |
(defun find-variable-in-abst-bterm (name bterm) |
| 801 |
(declare (type simple-string name)) |
| 802 |
(let ((var nil)) |
| 803 |
(dolist (sub (abst-bterm-subst bterm) var) |
| 804 |
(if (abst-bterm-p sub) |
| 805 |
(progn |
| 806 |
(setq var (find-variable-in-abst-bterm name sub)) |
| 807 |
(when var (return-from find-variable-in-abst-bterm var))) |
| 808 |
(when (string= name (variable-print-name (car sub))) |
| 809 |
(return-from find-variable-in-abst-bterm (car sub))))))) |
| 810 |
|
| 811 |
(defun bstart (&optional name) |
| 812 |
(check-bterm-context) |
| 813 |
(if name |
| 814 |
(let ((var (find-variable-in-abst-bterm name *abst-bterm*))) |
| 815 |
(unless var |
| 816 |
(with-output-chaos-error ('no-var) |
| 817 |
(format t "No such predicate ~s in abstracted boolean term." name))) |
| 818 |
(format t "~%** Setting target predicate to '~A'" name) |
| 819 |
(setq *abst-bterm-target-variable* var)) |
| 820 |
(setq *abst-bterm-target-variable* :all))) |
| 821 |
|
| 822 |
;;; bgues |
| 823 |
;;; bguess {imply | and | or} with <predicate-name> |
| 824 |
;;; |
| 825 |
(defun bguess (args) |
| 826 |
(let ((strategy (first args)) |
| 827 |
(pred (third args))) |
| 828 |
(bstart pred) |
| 829 |
(cond ((member strategy '("imply" ":imply" "imp" ":imp") :test #'equal) |
| 830 |
(do-bguess :imply)) |
| 831 |
((member strategy '("and" ":and") :test #'equal) |
| 832 |
(do-bguess :and)) |
| 833 |
((member strategy '("or" ":or") :test #'equal) |
| 834 |
(do-bguess :or)) |
| 835 |
(t (with-output-chaos-error ('unknown-strat) |
| 836 |
(format t "Unknown strategy ~s" strategy)))))) |
| 837 |
|
| 838 |
;;; bgrind |
| 839 |
;;; |
| 840 |
(defun bgrind-in (mode goal-or-module preterm) |
| 841 |
(let ((*chaos-quiet* t) |
| 842 |
(*no-prompt* t)) |
| 843 |
(cond ((eq mode :citp) |
| 844 |
(bgrind-in-goal goal-or-module preterm)) |
| 845 |
(t |
| 846 |
(bgrind-in-module goal-or-module preterm))))) |
| 847 |
|
| 848 |
(defun bgrind-in-goal (goal-name preterm) |
| 849 |
(let* ((goal-node (get-target-goal-node goal-name)) |
| 850 |
(context-module (goal-context (ptree-node-goal goal-node))) |
| 851 |
(target (do-parse-term* preterm context-module))) |
| 852 |
(show-grind context-module target))) |
| 853 |
|
| 854 |
(defun bgrind-in-module (mod-name preterm) |
| 855 |
(multiple-value-bind (target context-module) |
| 856 |
(do-parse-term* preterm mod-name) |
| 857 |
(show-grind context-module target))) |
| 858 |
|
| 859 |
(defun show-grind (context-module target) |
| 860 |
(unless (eq *bool-sort* (term-sort target)) |
| 861 |
(with-output-chaos-error ('term-is-not-bool) |
| 862 |
(format t "Given term is not of sort Bool."))) |
| 863 |
(with-in-module (context-module) |
| 864 |
(format t "~%** Start normalization...") |
| 865 |
(let* ((*always-memo* t) |
| 866 |
(bt (reducer target context-module :red))) |
| 867 |
(format t "~%~a" (generate-statistics-form-rewriting-only)) |
| 868 |
(bgrind-bool-term bt t t)))) |
| 869 |
|
| 870 |
;;; bgrind-bool-term |
| 871 |
;;; built-in op |
| 872 |
;;; accepting boolean term and print it in 'grind' form |
| 873 |
;;; |
| 874 |
(defun bgrind-bool-term (bt &optional (doit *grind-bool-term*) (do-report nil)) |
| 875 |
(when doit |
| 876 |
(let ((abs-start (get-internal-run-time))) |
| 877 |
(when do-report |
| 878 |
(format t "~%** Start term abstraction...")) |
| 879 |
(let ((abst (abstract-boolean-term bt *current-module*))) |
| 880 |
(when do-report |
| 881 |
(format t "~%(consumed ~,3f sec. for abstraction)" |
| 882 |
(elapsed-time-in-seconds abs-start |
| 883 |
(get-internal-run-time)))) |
| 884 |
(print-bterm-grinding abst)))) |
| 885 |
bt) |
| 886 |
|
| 887 |
;;; EOF |