| 1 |
;;;-*- Mode:LISP; Package:CHAOS; Base:10; Syntax:Common-lisp -*- |
| 2 |
;;; |
| 3 |
;;; Copyright (c) 2000-2015, 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: basics.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 |
;;; === Common Basic Functions for APPLY family |
| 41 |
;;; ------------------------------------------------------------------------ |
| 42 |
|
| 43 |
;;; ***** |
| 44 |
;;; UTILS |
| 45 |
;;; ***** |
| 46 |
|
| 47 |
(defun check-apply-context (mod) |
| 48 |
(unless (check-$$term-context mod) |
| 49 |
(with-output-chaos-warning () |
| 50 |
(format t "the target term : ") |
| 51 |
(print-chaos-object $$term) |
| 52 |
(print-next) |
| 53 |
(format t "isn't proper in the context : ") |
| 54 |
(print-chaos-object mod) |
| 55 |
(print-next) |
| 56 |
(format t "please re-`start' with supplying new one!") |
| 57 |
(throw 'apply-context-error nil)))) |
| 58 |
|
| 59 |
(defun command-display () |
| 60 |
(if $$action-stack |
| 61 |
(format t "~%-- condition(~s) " (length $$action-stack)) |
| 62 |
(format t "~&result ")) |
| 63 |
(disp-term $$term)) |
| 64 |
|
| 65 |
(defun command-final () |
| 66 |
(when $$action-stack |
| 67 |
;; $$action-stack == list of |
| 68 |
;; ($$term term rule condition rhs-instance sort) |
| 69 |
;; 0 1 2 3 4 |
| 70 |
(if (term-is-similar? $$term *bool-true*) |
| 71 |
(progn |
| 72 |
(command-display) |
| 73 |
(format t "~%-- condition is satisfied, applying rule") |
| 74 |
(format t "~&-- shifiting focus back to previous context") |
| 75 |
(let ((cur (car $$action-stack))) |
| 76 |
(setq $$term (car cur)) |
| 77 |
(term-replace (nth 1 cur) (nth 4 cur)) |
| 78 |
(setq $$action-stack (cdr $$action-stack))) |
| 79 |
t) |
| 80 |
(if (term-is-similar? $$term *bool-false*) |
| 81 |
(progn |
| 82 |
(command-display) |
| 83 |
(format t "~%-- condition is not satisfied, rule not applied") |
| 84 |
(format t "~&-- shifting focus back to previous context") |
| 85 |
(setq $$term (caar $$action-stack)) |
| 86 |
(setq $$action-stack (cdr $$action-stack)) |
| 87 |
t) |
| 88 |
nil)))) |
| 89 |
|
| 90 |
(defun disp-term (x) |
| 91 |
(with-in-module ((get-context-module)) |
| 92 |
(term-print x) |
| 93 |
(princ " : ") |
| 94 |
(print-sort-name (term-sort x) *current-module*))) |
| 95 |
|
| 96 |
(defun disp-term* (x) |
| 97 |
(term-print x) |
| 98 |
(princ " : ") |
| 99 |
(print-sort-name (term-sort x) *current-module*)) |
| 100 |
|
| 101 |
;;; |
| 102 |
;;; apply-help |
| 103 |
;;; |
| 104 |
(defun apply-help () |
| 105 |
(format t "~%Apply a selected rule, possibly with an instantiation,") |
| 106 |
(format t " to selected subterm(s).") |
| 107 |
(format t "~&Syntax:") |
| 108 |
(format t "~& apply { reduction | red | print | bred | exec | <RuleSpec> [ <VarSubst> ] }") |
| 109 |
(format t "~& { at | within } <Selectors>") |
| 110 |
(format t "~& <RuleSpec> ::= [ + | - ] [<ModId>.]<RuleId>") |
| 111 |
(format t "~& <RuleId> ::= <Natural> | <Identifier>") |
| 112 |
(format t "~& <VarSubst> ::= with <Variable> = <Term> { , <Variable> = <Term> }*") |
| 113 |
(format t "~& <Selectors> ::= top | term | subterm | <Selector> { of <Selector> }* .") |
| 114 |
(format t "~& <Selector> ::= (<Natural>+) | `[' <Natural> [ .. <Natural> ] `]' |") |
| 115 |
(format t "~& `{' <Natural> {, <Natural>}* `}'") |
| 116 |
) |
| 117 |
|
| 118 |
;;; ******************* |
| 119 |
;;; COMPUTING SELECTION |
| 120 |
;;; ******************* |
| 121 |
|
| 122 |
(defun str-to-int (x) |
| 123 |
(if (equal "" x) |
| 124 |
0 |
| 125 |
(read-from-string x))) |
| 126 |
|
| 127 |
(defun !make-right-associative (method subs) |
| 128 |
(if (null (cdr subs)) (car subs) |
| 129 |
(make-right-assoc-normal-form method subs))) |
| 130 |
|
| 131 |
;;; <OccurSelection> |
| 132 |
;;; ________________ |
| 133 |
(defun compute-occur-selection (sort term occs) |
| 134 |
(let ((cursrt sort) (cur term)) |
| 135 |
(dolist (i occs) |
| 136 |
(if (and (<= 0 i) (<= i (length (term-subterms cur)))) |
| 137 |
(unless (zerop i) |
| 138 |
(setq cursrt (nth (1- i) (method-arity (term-head cur)))) |
| 139 |
(setq cur (nth (1- i) (term-subterms cur)))) |
| 140 |
(with-output-chaos-warning () |
| 141 |
(format t "no such occurrenct, occurrence ~a is not correct for term :" |
| 142 |
occs) |
| 143 |
(print-next) |
| 144 |
(term-print term) |
| 145 |
(print-next) |
| 146 |
(format t "ignoring it.") |
| 147 |
(return-from compute-occur-selection (values sort term))))) |
| 148 |
(values cursrt cur) )) |
| 149 |
|
| 150 |
;;; <SubseqSelection> |
| 151 |
;;; _________________ |
| 152 |
;;; compute-subseq-selection : sort term m n |
| 153 |
;;; |
| 154 |
;;; <SubseqSelection> ::= [ m .. n ] | [ m ] |
| 155 |
;;; - case [ m ] --> [ m .. m ] |
| 156 |
;;; |
| 157 |
(defun compute-subseq-selection (sort term m1 n1) |
| 158 |
(let ((m (1- m1)) ; *note* sequence specifier is 1 origin. |
| 159 |
(n (1- n1))) |
| 160 |
(if (term-is-variable? term) |
| 161 |
(with-output-chaos-warning () |
| 162 |
(format t "found variable in selection of subsequence selection : ") |
| 163 |
(term-print term) |
| 164 |
(values sort term)) |
| 165 |
(let ((method (term-head term))) |
| 166 |
;; [ ... ] is meaningful only for associative operators. |
| 167 |
(if (method-is-associative method) |
| 168 |
(let ((lst (list-assoc-subterms term method))) |
| 169 |
(if (and (<= m n) (<= 1 m1) (<= n1 (length lst))) |
| 170 |
(if (or (< 1 m1) (< n1 (length lst))) |
| 171 |
(let ((res (!make-right-associative |
| 172 |
method |
| 173 |
(subseq lst m (1+ n))))) |
| 174 |
(term-replace term |
| 175 |
(!make-right-associative |
| 176 |
method |
| 177 |
(append (subseq lst 0 m) |
| 178 |
(list res) |
| 179 |
(subseq lst (1+ n))))) |
| 180 |
(values (car (method-arity method)) res)) |
| 181 |
(values sort term)) |
| 182 |
(with-output-chaos-warning () |
| 183 |
(format t "selection [~a .. ~a] is out of range for term :~% " |
| 184 |
m1 n1) |
| 185 |
(term-print term) |
| 186 |
(print-next) |
| 187 |
(format t "selected the whole subterms instead.") |
| 188 |
(values sort term) |
| 189 |
))) |
| 190 |
(with-output-chaos-warning () |
| 191 |
(format t "found non-associative operator in selection of subsequence slection : ~% ") |
| 192 |
(print-chaos-object method) |
| 193 |
(values sort term))))))) |
| 194 |
|
| 195 |
;;; <SubsetSelection> |
| 196 |
;;; _________________ |
| 197 |
|
| 198 |
(defun compute-subset-selection (sort term occs) |
| 199 |
(if (term-is-variable? term) |
| 200 |
(with-output-chaos-warning () |
| 201 |
(format t "found variable in subset selection ~a: " occs) |
| 202 |
(term-print term) |
| 203 |
(print-next) |
| 204 |
(format t "ignoring the selection and select whole subterms instead.") |
| 205 |
(values sort term)) |
| 206 |
(let ((method (term-head term))) |
| 207 |
(if (and (method-is-associative method) |
| 208 |
(method-is-commutative method)) |
| 209 |
(let ((lst (list-AC-subterms term method))) |
| 210 |
(let ((len (length lst)) |
| 211 |
(sel nil) |
| 212 |
(rest nil) |
| 213 |
(err nil)) |
| 214 |
(dolist (i occs) |
| 215 |
(let ((n (1- i))) |
| 216 |
(if (and (<= 0 n) (< n len)) |
| 217 |
(let ((tl (nthcdr n lst))) |
| 218 |
(when (car tl) (push (car tl) sel)) |
| 219 |
(rplaca tl nil)) |
| 220 |
(push i err)))) |
| 221 |
(dolist (x lst) (when x (push x rest))) |
| 222 |
(when err |
| 223 |
(with-output-chaos-warning () |
| 224 |
(princ "found out of range in selection of subterms") |
| 225 |
(print-next) |
| 226 |
(format t "ignoring these selections : ~a" err))) |
| 227 |
(if (null rest) |
| 228 |
(values sort term) |
| 229 |
(let ((res (!make-right-associative method (nreverse sel)))) |
| 230 |
(term-replace |
| 231 |
term |
| 232 |
(!make-right-associative method (cons res (reverse rest)))) |
| 233 |
(values (car (method-arity method)) res) |
| 234 |
)))) |
| 235 |
(with-output-chaos-warning () |
| 236 |
(princ "subset selection is meaningful only for associative and commuteative operators,") |
| 237 |
(print-next) |
| 238 |
(format t "but : ") |
| 239 |
(print-chaos-object method) |
| 240 |
(princ " is not.") |
| 241 |
(print-next) |
| 242 |
(princ "ignoreing the selection and select whole subterms instead.") |
| 243 |
(values sort term))) ))) |
| 244 |
|
| 245 |
;;; compute-set-ocs |
| 246 |
;;; ("{" "1" "," "2" "," "4" "}") --> (1 2 4) |
| 247 |
;;; |
| 248 |
(defun compute-set-ocs (x) |
| 249 |
(let ((res nil) |
| 250 |
(val nil)) |
| 251 |
(setq x (cdr x)) |
| 252 |
(do ((elt x (cddr elt))) |
| 253 |
((endp elt) (nreverse res)) |
| 254 |
(setq val (str-to-int (car elt))) |
| 255 |
(pushnew val res)))) |
| 256 |
|
| 257 |
;;; top-level interface |
| 258 |
;;; ******************* |
| 259 |
(defvar *selection-target* nil) |
| 260 |
(declaim (special *selection-target*)) |
| 261 |
|
| 262 |
(defun compute-selection (tm sel) |
| 263 |
(let ((*selection-target* tm)) |
| 264 |
(!compute-selection *cosmos* |
| 265 |
tm |
| 266 |
(if (consp sel) |
| 267 |
(if (equal (car (last sel)) ".") |
| 268 |
(butlast sel) |
| 269 |
sel) |
| 270 |
sel)))) |
| 271 |
|
| 272 |
;;; !compute-selection |
| 273 |
;;; the main computing selections |
| 274 |
;;; |
| 275 |
(defun !compute-selection (sort tm sel) |
| 276 |
;; no selection |
| 277 |
(unless sel (return-from !compute-selection |
| 278 |
(if (not (term-eq $$term *selection-target*)) |
| 279 |
(values sort *selection-target*) |
| 280 |
(values sort $$term)))) |
| 281 |
;; whole term |
| 282 |
(when (memq sel '(:term :top)) |
| 283 |
(return-from !compute-selection (values sort $$term))) |
| 284 |
;; subterm |
| 285 |
(when (eq sel :subterm) |
| 286 |
(if $$subterm |
| 287 |
(return-from !compute-selection (values sort $$subterm)) |
| 288 |
(progn |
| 289 |
(with-output-chaos-warning () |
| 290 |
(format t "no subterm is specified yet, please `choose' some.") |
| 291 |
(print-next) |
| 292 |
(format t "selected the whole term.")) |
| 293 |
(values sort $$term)))) |
| 294 |
;; |
| 295 |
(!compute-selection-aux sort tm (cons (car sel) (cadr sel)))) |
| 296 |
|
| 297 |
(defun !compute-selection-aux (sort tm sel) |
| 298 |
(unless sel (return-from !compute-selection-aux |
| 299 |
(if (not (term-eq *selection-target* $$term)) |
| 300 |
(values sort *selection-target*) |
| 301 |
(values sort $$term)))) |
| 302 |
;; <Selection> of <Selection> |
| 303 |
(when (equal "of" (car sel)) |
| 304 |
(return-from !compute-selection-aux |
| 305 |
(!compute-selection-aux sort tm (cdr sel)))) |
| 306 |
;; |
| 307 |
(case-equal (caar sel) |
| 308 |
("(" |
| 309 |
;; occur selection |
| 310 |
(when (equal ")" (cadr (car sel))) |
| 311 |
(return-from !compute-selection-aux |
| 312 |
(!compute-selection-aux sort tm (cdr sel)))) |
| 313 |
(multiple-value-bind (s1 t1) |
| 314 |
(!compute-selection-aux sort tm (cdr sel)) |
| 315 |
(return-from !compute-selection-aux |
| 316 |
(compute-occur-selection s1 t1 |
| 317 |
(mapcar #'str-to-int (cadr (car sel))))))) |
| 318 |
("[" |
| 319 |
;; subseq selection { [ m .. n ] | [ m ] } |
| 320 |
(let ((i1 (str-to-int (cadr (car sel))))) |
| 321 |
(multiple-value-bind (s1 t1) |
| 322 |
(!compute-selection-aux sort tm (cdr sel)) |
| 323 |
(compute-subseq-selection s1 |
| 324 |
t1 |
| 325 |
i1 |
| 326 |
(if (equal "]" (nth 2 (car sel))) |
| 327 |
;; case [ m ] |
| 328 |
i1 |
| 329 |
;; case [ m .. n ] |
| 330 |
(str-to-int (cadr (nth 2 (car sel))))))))) |
| 331 |
("{" |
| 332 |
;; subset selection |
| 333 |
(multiple-value-bind (s1 t1) |
| 334 |
(!compute-selection-aux sort tm (cdr sel)) |
| 335 |
(compute-subset-selection s1 t1 (compute-set-ocs (car sel))))) |
| 336 |
(t (break "SNARK: !compute-selection")))) |
| 337 |
|
| 338 |
;;; ************** |
| 339 |
;;; Utils on TERMS |
| 340 |
;;; ************** |
| 341 |
|
| 342 |
(defun @copy-list-term-using-list-var (term-list list-new-var) |
| 343 |
(declare (type list term-list list-new-var) |
| 344 |
(values list list)) |
| 345 |
(let ((v-image nil) |
| 346 |
(list-copied-term nil)) |
| 347 |
(values (mapcar #'(lambda (term) |
| 348 |
(cond ((term-is-variable? term) |
| 349 |
(if (setq v-image |
| 350 |
(cdr (assoc term list-new-var |
| 351 |
:test #'variable-equal))) |
| 352 |
v-image |
| 353 |
(let ((new-var (variable-copy term))) |
| 354 |
(declare (type term new-var)) |
| 355 |
(setf list-new-var (acons term new-var |
| 356 |
list-new-var)) |
| 357 |
new-var |
| 358 |
))) |
| 359 |
((term-is-builtin-constant? term) term) |
| 360 |
((term-is-lisp-form? term) term) |
| 361 |
(t (multiple-value-setq (list-copied-term list-new-var) |
| 362 |
(@copy-list-term-using-list-var |
| 363 |
(term-subterms term) |
| 364 |
list-new-var)) |
| 365 |
(make-applform (term-sort term) |
| 366 |
(term-head term) |
| 367 |
list-copied-term)))) |
| 368 |
term-list) |
| 369 |
list-new-var))) |
| 370 |
|
| 371 |
;;; @COPY-TERM-USING-VARIABLE : term List[variable] -> term |
| 372 |
;;; |
| 373 |
(defun @copy-term-using-variable (term list-new-var) |
| 374 |
(declare (type term term) |
| 375 |
(type list list-new-var) |
| 376 |
(values term)) |
| 377 |
(multiple-value-bind (res list-new-var-res) |
| 378 |
(@copy-list-term-using-list-var (list term) list-new-var) |
| 379 |
(declare (ignore list-new-var-res) |
| 380 |
(type list res)) |
| 381 |
(car res))) |
| 382 |
|
| 383 |
#|| |
| 384 |
(defun @copy-term (term) |
| 385 |
(simple-copy-term term)) |
| 386 |
||# |
| 387 |
|
| 388 |
(defun @copy-term (term) |
| 389 |
(let ((vars (term-variables term))) |
| 390 |
;; (print vars) |
| 391 |
(if vars |
| 392 |
(@copy-term-using-variable term nil) |
| 393 |
(simple-copy-term term)))) |
| 394 |
|
| 395 |
(defun @matcher (pat term type) |
| 396 |
(if (term-is-variable? pat) |
| 397 |
(if (sort<= (term-sort term) (variable-sort pat) |
| 398 |
(module-sort-order *current-module*)) |
| 399 |
(values nil (list (cons pat term)) nil nil) |
| 400 |
(values nil nil t nil)) |
| 401 |
(if (term-is-lisp-form? pat) |
| 402 |
(values nil nil t nil) |
| 403 |
(if (eq type :match) |
| 404 |
(first-match pat term) |
| 405 |
(first-unify pat term))))) |
| 406 |
|
| 407 |
(defun @test-rule-direct (rul term type) |
| 408 |
(multiple-value-bind (gs sub no eeq) |
| 409 |
(@matcher (axiom-lhs rul) term type) |
| 410 |
(declare (ignore gs sub eeq)) |
| 411 |
(null no))) |
| 412 |
|
| 413 |
(defvar *-inside-apply-with-extensions-* nil) |
| 414 |
|
| 415 |
(defun @test-rule (rule term &optional (type :match)) |
| 416 |
(multiple-value-bind (gs sub no-match eeq) |
| 417 |
(@matcher (axiom-lhs rule) term type) |
| 418 |
(declare (ignore gs sub eeq)) |
| 419 |
(if (and no-match |
| 420 |
(and *-inside-apply-with-extensions-* |
| 421 |
(not (or (term-is-variable? term) |
| 422 |
(term-is-builtin-constant? term))) |
| 423 |
(method-is-of-same-operator (term-head (axiom-lhs rule)) |
| 424 |
(term-head term)))) |
| 425 |
(@test-rule-extensions rule term type) |
| 426 |
(null no-match)))) |
| 427 |
|
| 428 |
(defun @make-ac-pattern (top term) |
| 429 |
(let ((newvar (make-variable-term *cosmos* 'ac-pat))) |
| 430 |
(make-right-assoc-normal-form top |
| 431 |
(cons newvar |
| 432 |
(list-assoc-subterms |
| 433 |
term |
| 434 |
(term-head term)))))) |
| 435 |
(defun @make-a-patterns (top term) |
| 436 |
(let ((new-var1 (make-variable-term *cosmos* 'a-pat1)) |
| 437 |
(new-var2 (make-variable-term *cosmos* 'a-pat2))) |
| 438 |
(list (make-right-assoc-normal-form top |
| 439 |
(cons new-var1 |
| 440 |
(list-assoc-subterms |
| 441 |
term |
| 442 |
(term-head term)))) |
| 443 |
(make-right-assoc-normal-form top |
| 444 |
(append |
| 445 |
(list-assoc-subterms |
| 446 |
term |
| 447 |
(term-head term)) |
| 448 |
(list new-var1))) |
| 449 |
(make-right-assoc-normal-form top |
| 450 |
(list new-var2 |
| 451 |
term |
| 452 |
new-var1))))) |
| 453 |
|
| 454 |
(defun @pat-match (pat term &optional (type :match)) |
| 455 |
(declare (type term pat term)) |
| 456 |
(multiple-value-bind (gs sub no-match eeq) |
| 457 |
(@matcher pat term type) |
| 458 |
(declare (type global-state gs) |
| 459 |
(type substitution sub) |
| 460 |
(type (or null t) no-match eeq)) |
| 461 |
(unless no-match |
| 462 |
(return-from @pat-match (values t sub))) |
| 463 |
(if (and (term-is-application-form? term) |
| 464 |
(term-is-application-form? pat) |
| 465 |
(method-is-of-same-operator (term-head pat) |
| 466 |
(term-head term))) |
| 467 |
(let ((top (term-head pat))) |
| 468 |
(declare (type method top)) |
| 469 |
(if (method-is-associative top) |
| 470 |
(dolist (npat (if (method-is-commutative top) |
| 471 |
(list (@make-ac-pattern top pat)) |
| 472 |
(@make-a-patterns top pat)) |
| 473 |
(values nil nil)) |
| 474 |
(when (and npat |
| 475 |
(progn |
| 476 |
(multiple-value-setq (gs sub no-match eeq) |
| 477 |
(@matcher npat term type)) |
| 478 |
(null no-match))) |
| 479 |
(return (values t sub)))) |
| 480 |
(values nil nil)))))) |
| 481 |
|
| 482 |
;;; |
| 483 |
;;; FOR := |
| 484 |
;;; |
| 485 |
(declaim (special *m-pattern-subst*)) |
| 486 |
|
| 487 |
(defun match-m-pattern (pat term) |
| 488 |
(declare (type term pat term) |
| 489 |
(optimize (speed 3) (safety 0))) |
| 490 |
(multiple-value-bind (res subst) |
| 491 |
(@pat-match pat term) |
| 492 |
(when res |
| 493 |
(dolist (sub subst) |
| 494 |
(push sub *m-pattern-subst*)) |
| 495 |
(return-from match-m-pattern t)) |
| 496 |
nil)) |
| 497 |
|
| 498 |
(defun @test-rule-extensions (rule term type) |
| 499 |
(let ((top (term-head (axiom-lhs rule)))) |
| 500 |
(if (method-is-associative top) |
| 501 |
(dolist (r (if (method-is-commutative top) |
| 502 |
(compute-AC-extension rule top) |
| 503 |
(compute-A-extensions rule top)) |
| 504 |
nil) |
| 505 |
(when (and r (@test-rule-direct r term type)) |
| 506 |
(return t))) |
| 507 |
nil))) |
| 508 |
|
| 509 |
;;; ********************* |
| 510 |
;;; VARIABLE SUBSTITUTION |
| 511 |
;;; ********************* |
| 512 |
|
| 513 |
;;; COMPUTE-VARIABLE-SUBSTITUTION |
| 514 |
;;; |
| 515 |
(defun compute-variable-substitution (rule substtoks) |
| 516 |
;; rule just for vars |
| 517 |
(let ((vars (union (term-variables (axiom-lhs rule)) |
| 518 |
(union (term-variables (axiom-rhs rule)) |
| 519 |
(term-variables (axiom-condition rule))))) |
| 520 |
(sub nil) |
| 521 |
varnm |
| 522 |
trmtoks |
| 523 |
avar |
| 524 |
aterm) |
| 525 |
(with-in-module ((get-context-module)) |
| 526 |
(loop (when (null substtoks) (return)) |
| 527 |
;; <varid> = <term> |
| 528 |
(setq varnm (cadr substtoks)) |
| 529 |
(setq trmtoks (nth 3 substtoks)) |
| 530 |
(setq avar (find-if #'(lambda (x) (equal (string (variable-name x)) |
| 531 |
varnm)) |
| 532 |
vars)) |
| 533 |
(setq aterm (simple-parse *current-module* |
| 534 |
trmtoks |
| 535 |
*cosmos*)) |
| 536 |
(if (and avar (not (term-is-an-error aterm))) |
| 537 |
(progn |
| 538 |
(if (not (is-in-same-connected-component |
| 539 |
(term-sort aterm) |
| 540 |
(variable-sort avar) |
| 541 |
*current-sort-order*)) |
| 542 |
(with-output-chaos-warning () |
| 543 |
(princ "sort of term is incompatible with variable sort") |
| 544 |
(print-next) |
| 545 |
(format t "variable ~a:" (string (variable-name avar))) |
| 546 |
(print-sort-name (variable-sort avar)) |
| 547 |
(print-next) |
| 548 |
(princ "term ") |
| 549 |
(print-chaos-object aterm) |
| 550 |
(princ ":") |
| 551 |
(print-sort-name (term-sort aterm))) |
| 552 |
(push (cons avar aterm) sub))) |
| 553 |
(with-output-chaos-error ('invalid-subst) |
| 554 |
(unless avar |
| 555 |
(format t "No such variable in rule: ~s" varnm) |
| 556 |
(print-next) |
| 557 |
(princ "specified substitution contains an error") |
| 558 |
))) |
| 559 |
(setq substtoks (cddddr substtoks)))) |
| 560 |
sub)) |
| 561 |
|
| 562 |
;;; ********************** |
| 563 |
;;; FINDING MATCHING RULES |
| 564 |
;;; ********************** |
| 565 |
|
| 566 |
(defstruct found-pattern |
| 567 |
rule-num |
| 568 |
direction |
| 569 |
rule |
| 570 |
subst |
| 571 |
extra |
| 572 |
occur) |
| 573 |
|
| 574 |
(defun get-subterm-pos (term pos) |
| 575 |
(let ((cur term)) |
| 576 |
(when pos |
| 577 |
(dolist (p pos) |
| 578 |
(let ((rp (1- p))) |
| 579 |
(when (>= rp 0) |
| 580 |
(setq cur (term-arg-n cur rp))) |
| 581 |
(unless cur |
| 582 |
(with-output-panic-message () |
| 583 |
(format t "could not find subterm at pos ~d" pos) |
| 584 |
(format t "~% target was ") |
| 585 |
(term-print term) |
| 586 |
(break "wow!") |
| 587 |
(chaos-error 'panic)))))) |
| 588 |
cur)) |
| 589 |
|
| 590 |
(defun find-matching-rules-all (what target module &optional (type :match) (start-pos nil)) |
| 591 |
(with-in-module (module) |
| 592 |
(when start-pos |
| 593 |
(setq target (get-subterm-pos target start-pos))) |
| 594 |
(find-matching-rules-all* what target module type start-pos))) |
| 595 |
|
| 596 |
(defun find-matching-rules-all* (what target module type pos) |
| 597 |
(let ((result (find-matching-rules what target module type))) |
| 598 |
(dolist (r result) |
| 599 |
(setf (found-pattern-occur r) pos)) |
| 600 |
(dotimes (x (length (term-subterms target))) |
| 601 |
(let ((r (find-matching-rules-all* what |
| 602 |
(term-arg-n target x) |
| 603 |
module |
| 604 |
type |
| 605 |
(append pos (list (1+ x)))))) |
| 606 |
(when r (setq result (nconc result r))))) |
| 607 |
;; |
| 608 |
result)) |
| 609 |
|
| 610 |
(defun find-matching-rules (what target module &optional (type :match)) |
| 611 |
(with-in-module (module) |
| 612 |
(let* ((*module-all-rules-every* t) |
| 613 |
(rules (get-module-axioms *current-module* t)) |
| 614 |
(res nil)) |
| 615 |
(do* ((rls rules (cdr rls)) |
| 616 |
(rule (car rls) (car rls)) |
| 617 |
(num 1 (1+ num))) |
| 618 |
((endp rls)) |
| 619 |
(when (or (eq what :rule) (eq what :+rule)) |
| 620 |
(multiple-value-bind (match subst) |
| 621 |
(@pat-match (axiom-lhs rule) target type) |
| 622 |
(when match |
| 623 |
(push (make-found-pattern :rule-num num |
| 624 |
:direction :+rule |
| 625 |
:rule rule |
| 626 |
:subst subst |
| 627 |
:extra (compute-extra-variables |
| 628 |
rule |
| 629 |
:+rule)) |
| 630 |
res)))) |
| 631 |
(when (and (or (eq what :rule) (eq what :-rule)) |
| 632 |
(not (eq (axiom-type rule) :rule)) |
| 633 |
(not (rule-is-builtin rule))) |
| 634 |
(multiple-value-bind (match subst) |
| 635 |
(@pat-match (axiom-rhs rule) target type) |
| 636 |
(when match |
| 637 |
(push (make-found-pattern :rule-num num |
| 638 |
:direction :-rule |
| 639 |
:rule rule |
| 640 |
:subst subst |
| 641 |
:extra (compute-extra-variables |
| 642 |
rule |
| 643 |
:-rule)) |
| 644 |
res))))) |
| 645 |
(nreverse res)))) |
| 646 |
|
| 647 |
(defun compute-extra-variables (rule direction) |
| 648 |
(let ((lhs (axiom-lhs rule)) |
| 649 |
(rhs (axiom-rhs rule)) |
| 650 |
(condition (axiom-condition rule))) |
| 651 |
(when (eq direction ':-rule) |
| 652 |
(setq lhs rhs) |
| 653 |
(setq rhs lhs)) |
| 654 |
(let* ((lvars (term-variables lhs)) |
| 655 |
(rvars (union (term-variables rhs) |
| 656 |
(term-variables condition)))) |
| 657 |
(nset-difference rvars lvars)))) |
| 658 |
|
| 659 |
;;; EOF |