| 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: eval-apply.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 |
;;; START |
| 42 |
;;; ***** |
| 43 |
|
| 44 |
(defun eval-start-command (ast) |
| 45 |
(do-eval-start-th (%start-target ast) (get-context-module))) |
| 46 |
|
| 47 |
(defun do-eval-start-th (pre-term &optional context) |
| 48 |
(catch 'apply-context-error |
| 49 |
(let ((mod (if context |
| 50 |
(eval-modexp context) |
| 51 |
(get-context-module)))) |
| 52 |
(if (or (null mod) (modexp-is-error mod)) |
| 53 |
(if (null mod) |
| 54 |
(with-output-chaos-error ('invalid-module) |
| 55 |
(princ "no module expression provided and no current module.")) |
| 56 |
(with-output-chaos-error ('invalid-module) |
| 57 |
(format t "incorrect module expression: ~a" context))) |
| 58 |
(if pre-term |
| 59 |
(with-in-module (mod) |
| 60 |
(prepare-for-parsing *current-module*) |
| 61 |
(cond ((or (equal pre-term '("$$term")) |
| 62 |
(equal pre-term '("$$subterm"))) |
| 63 |
(let ((target nil)) |
| 64 |
(catch 'term-context-error |
| 65 |
(setq target (get-bound-value (car pre-term)))) |
| 66 |
(unless target |
| 67 |
(return-from do-eval-start-th nil)) |
| 68 |
(setq $$action-stack nil) |
| 69 |
(reset-reduced-flag target) |
| 70 |
(reset-target-term target *current-module* mod))) |
| 71 |
(t |
| 72 |
(let ((*parse-variables* nil)) |
| 73 |
(let ((res (simple-parse *current-module* |
| 74 |
pre-term |
| 75 |
*cosmos*))) |
| 76 |
(when (term-is-an-error res) |
| 77 |
(return-from do-eval-start-th nil)) |
| 78 |
(setq $$action-stack nil) |
| 79 |
(reset-target-term res *current-module* mod)))))) |
| 80 |
;; try use $$term |
| 81 |
(progn |
| 82 |
(when (or (null $$term) (eq 'void $$term)) |
| 83 |
(with-output-chaos-warning () |
| 84 |
(format t "no target term is given!") |
| 85 |
(return-from do-eval-start-th nil))) |
| 86 |
(check-apply-context mod) |
| 87 |
(setq $$action-stack nil) |
| 88 |
(reset-reduced-flag $$term) |
| 89 |
(reset-target-term $$term (get-context-module) mod)))) |
| 90 |
(when (command-final) (command-display)) |
| 91 |
t))) |
| 92 |
|
| 93 |
;;; ****** |
| 94 |
;;; CHOOSE |
| 95 |
;;; ****** |
| 96 |
|
| 97 |
(defun eval-choose-command (ast) |
| 98 |
(unless $$subterm (setq $$subterm $$term)) |
| 99 |
(unless $$subterm |
| 100 |
(with-output-chaos-warning () |
| 101 |
(format t "no target term is specified yet.") |
| 102 |
(print-next) |
| 103 |
(princ "please set the target term by `start' command.") |
| 104 |
(return-from eval-choose-command nil))) |
| 105 |
(let ((selectors (%choose-selectors ast))) |
| 106 |
(when (eq selectors ':top) |
| 107 |
(setq $$subterm $$term) |
| 108 |
(setq $$selection-stack nil) |
| 109 |
(return-from eval-choose-command nil)) |
| 110 |
(with-in-module ((get-context-module)) |
| 111 |
(multiple-value-bind (subterm-sort subterm) |
| 112 |
(compute-selection $$subterm selectors) |
| 113 |
(declare (ignore subterm-sort)) |
| 114 |
(push selectors $$selection-stack) |
| 115 |
(setq $$subterm subterm))))) |
| 116 |
|
| 117 |
;;; ************* |
| 118 |
;;; INSPECT-TERM |
| 119 |
;;; ************* |
| 120 |
(defun eval-inspect-term-command (&optional ast) |
| 121 |
(declare (ignore ast)) |
| 122 |
(unless $$subterm (setq $$subterm $$term)) |
| 123 |
(unless $$subterm |
| 124 |
(with-output-chaos-warning () |
| 125 |
(format t "no target term is specified yet.") |
| 126 |
(print-next) |
| 127 |
(princ "please set the target term by `choose' command.") |
| 128 |
(return-from eval-inspect-term-command nil))) |
| 129 |
(inspect-term $$subterm)) |
| 130 |
|
| 131 |
;;; ***** |
| 132 |
;;; APPLY |
| 133 |
;;; ***** |
| 134 |
|
| 135 |
(defvar *-applied-* nil) |
| 136 |
(defvar *-inside-apply-all-* nil) |
| 137 |
|
| 138 |
;;; top-level evaluator |
| 139 |
|
| 140 |
(defun eval-apply-command (ast) |
| 141 |
(let ((action (%apply-action ast)) |
| 142 |
(rule-spec (%apply-rule-spec ast)) |
| 143 |
(substitution (%apply-substitution ast)) |
| 144 |
(where-spec (%apply-where-spec ast)) |
| 145 |
(selectors (%apply-selectors ast))) |
| 146 |
(catch 'apply-context-error |
| 147 |
(when (eq action :help) |
| 148 |
(apply-help) |
| 149 |
(return-from eval-apply-command nil)) |
| 150 |
;; check some evaluation env |
| 151 |
(when (or (null $$term) (eq 'void $$term)) |
| 152 |
(with-output-chaos-error ('invalid-term) |
| 153 |
(princ "term to be applied is not defined."))) |
| 154 |
;; real work begins here ------------------------------ |
| 155 |
(with-in-module ((get-context-module)) |
| 156 |
(multiple-value-bind (subterm-sort subterm) |
| 157 |
(compute-selection $$term selectors) |
| 158 |
(setq *-applied-* t) |
| 159 |
(case action |
| 160 |
(:reduce ; full reduction on selections. |
| 161 |
(!setup-reduction *current-module*) |
| 162 |
(let ((*rewrite-semantic-reduce* |
| 163 |
(module-has-behavioural-axioms *current-module*)) |
| 164 |
(*rewrite-exec-mode* nil)) |
| 165 |
(term-replace subterm (@copy-term subterm)) |
| 166 |
(reset-reduced-flag subterm) |
| 167 |
(rewrite subterm *current-module*))) |
| 168 |
(:breduce |
| 169 |
(!setup-reduction *current-module*) |
| 170 |
(let ((*rewrite-semantic-reduce* nil) |
| 171 |
(*rewrite-exec-mode* nil)) |
| 172 |
(term-replace subterm (@copy-term subterm)) |
| 173 |
(reset-reduced-flag subterm) |
| 174 |
(rewrite subterm *current-module*))) |
| 175 |
(:exec |
| 176 |
(!setup-reduction *current-module*) |
| 177 |
(let ((*rewrite-semantic-reduce* |
| 178 |
(module-has-behavioural-axioms *current-module*)) |
| 179 |
(*rewrite-exec-mode* t)) |
| 180 |
(term-replace subterm (@copy-term subterm)) |
| 181 |
(reset-reduced-flag subterm) |
| 182 |
(rewrite subterm *current-module*))) |
| 183 |
;; |
| 184 |
(:print ; print selections. |
| 185 |
(format t "~%term ") |
| 186 |
(disp-term subterm) |
| 187 |
(format t "~&tree form") |
| 188 |
(print-term-tree subterm)) |
| 189 |
(:apply ; apply specified rule. |
| 190 |
(setq *-applied-* nil) |
| 191 |
(let* ((actrule (compute-action-rule rule-spec |
| 192 |
substitution |
| 193 |
selectors)) |
| 194 |
(*-inside-apply-with-extensions-* |
| 195 |
(and |
| 196 |
(let ((arlhs (rule-lhs actrule))) |
| 197 |
(and (term-is-application-form? arlhs) |
| 198 |
(method-is-associative (term-head arlhs))))))) |
| 199 |
(if (eq :within where-spec) |
| 200 |
(let ((*-inside-apply-all-* t)) |
| 201 |
(catch 'apply-all-quit |
| 202 |
(@apply-all actrule subterm-sort subterm))) |
| 203 |
(@apply-rule actrule subterm-sort subterm))) |
| 204 |
(when *-applied-* |
| 205 |
(update-lowest-parse $$term) |
| 206 |
(when (nth 2 rule-spec) ; reverse order |
| 207 |
(setq $$term (@copy-term $$term))) |
| 208 |
(reset-target-term $$term *current-module* *current-module*))) ; end :apply |
| 209 |
(t (with-output-panic-message () |
| 210 |
(format t "unknown apply action : ~a" action) |
| 211 |
(chaos-error 'unknown-action)))) |
| 212 |
;; |
| 213 |
(unless *-applied-* |
| 214 |
(with-output-chaos-warning () |
| 215 |
(princ "rule not applied"))) |
| 216 |
;; |
| 217 |
(command-final) |
| 218 |
(command-display)))))) |
| 219 |
|
| 220 |
(defvar *copy-conditions*) |
| 221 |
(declaim (special *copy-conditons*)) |
| 222 |
(defvar *apply-debug* nil) |
| 223 |
|
| 224 |
(defun @apply-one-rule (rule sort term) |
| 225 |
(when *apply-debug* |
| 226 |
(princ "* @apply-one-rule : rule = ") |
| 227 |
(print-chaos-object rule) |
| 228 |
(format t "~%- sort = ") (print-sort-name sort) |
| 229 |
(format t "~&- term = ") (term-print term)) |
| 230 |
(let ((*self* term)) |
| 231 |
(let ((cond (rule-condition rule))) |
| 232 |
(if (or *reduce-conditions* (is-true? cond)) |
| 233 |
(let ((lhs (rule-lhs rule))) |
| 234 |
(if (term-is-variable? lhs) |
| 235 |
(multiple-value-bind (gs sub no eeq) |
| 236 |
(@matcher lhs (@copy-term term) :match) ; why? |
| 237 |
(declare (ignore gs)) |
| 238 |
(when eeq (setq sub nil)) |
| 239 |
(unless (or no |
| 240 |
(and (not (is-true? cond)) |
| 241 |
(not (is-true? |
| 242 |
(!normalize-term |
| 243 |
(@copy-term |
| 244 |
(substitution-image! sub cond))))))) |
| 245 |
(setq *-applied-* t) |
| 246 |
(term-replace-dd-simple term |
| 247 |
(@copy-term |
| 248 |
(substitution-image! sub |
| 249 |
(rule-rhs rule)))))) |
| 250 |
(let ((*copy-conditions* t)) |
| 251 |
(let ((res (apply-one-rule-no-simplify rule term))) |
| 252 |
(when res |
| 253 |
(term-replace-dd-simple term |
| 254 |
(@copy-term term)) |
| 255 |
(setq *-applied-* t)) |
| 256 |
))) |
| 257 |
term) |
| 258 |
;; "recurse" on condition |
| 259 |
(let ((lhs (rule-lhs rule)) |
| 260 |
(rhs (rule-rhs rule))) |
| 261 |
(multiple-value-bind (gs sub no eeq) |
| 262 |
(@matcher lhs (@copy-term term) :match) |
| 263 |
(declare (ignore gs)) |
| 264 |
(when eeq (setq sub nil)) |
| 265 |
(unless no |
| 266 |
(setq *-applied-* t) |
| 267 |
(format t "~%shifting focus to condition") |
| 268 |
(force-output) |
| 269 |
(let ((cond-inst (@copy-term (substitution-image! sub cond))) |
| 270 |
(rhs-inst |
| 271 |
(@copy-term (substitution-image! sub rhs)))) |
| 272 |
(setq $$action-stack |
| 273 |
(cons |
| 274 |
(list $$term term rule cond-inst rhs-inst sort) |
| 275 |
$$action-stack)) |
| 276 |
(setq $$term cond-inst) |
| 277 |
(when *-inside-apply-all-* |
| 278 |
(format t "~%-- applying rule only at first position found: ") |
| 279 |
(term-print term) |
| 280 |
(force-output) |
| 281 |
(throw 'apply-all-quit nil)))))))))) |
| 282 |
|
| 283 |
;;; APPLY-ONE-RULE-NO-SIMPLIFY (rule term) |
| 284 |
;;; |
| 285 |
|
| 286 |
(defun apply-one-rule-no-simplify (rule term) |
| 287 |
(when *apply-debug* |
| 288 |
(with-output-simple-msg () |
| 289 |
(princ "[apply]: rule = ") |
| 290 |
(print-chaos-object rule) |
| 291 |
(print-next) |
| 292 |
(princ " term = ") |
| 293 |
(print-chaos-object term))) |
| 294 |
(block the-end |
| 295 |
(let ((condition nil) |
| 296 |
next-match-method |
| 297 |
;; (*do-unify* t) |
| 298 |
(*self* term)) |
| 299 |
(multiple-value-bind (global-state subst nomatch Eequal) |
| 300 |
(funcall (rule-first-match-method rule) (rule-lhs rule) term) |
| 301 |
(when nomatch (return-from the-end nil)) |
| 302 |
(when *apply-debug* |
| 303 |
(format t "~%[apply-one-rule] : ") |
| 304 |
(format t "~% subst = ") |
| 305 |
(print-substitution subst) |
| 306 |
(format t "~% Eequal = ~a" eequal)) |
| 307 |
;; technical assignation related to substitution$image |
| 308 |
(when Eequal (setq subst nil)) |
| 309 |
;; the condition must be checked |
| 310 |
(block try-rule |
| 311 |
(catch 'rule-failure |
| 312 |
(when (is-true? (setq condition (rule-condition rule))) |
| 313 |
;; there is no condition |
| 314 |
(term-replace-dd-simple term |
| 315 |
(if (rule-is-builtin rule) |
| 316 |
(multiple-value-bind (newterm success) |
| 317 |
(funcall (lisp-form-function |
| 318 |
(rule-rhs rule)) subst) |
| 319 |
(if success |
| 320 |
newterm |
| 321 |
(return-from try-rule nil))) |
| 322 |
;; note that the computation of the substitution |
| 323 |
;; made a copy of the rhs. |
| 324 |
(substitution-image! |
| 325 |
subst |
| 326 |
(rule-rhs rule)))) |
| 327 |
(return-from the-end t)))) |
| 328 |
;; if the condition is not trivial, we enter in a loop |
| 329 |
;; where one try to find a match such that the condition |
| 330 |
;; is satisfied |
| 331 |
(setf next-match-method (rule-next-match-method rule)) |
| 332 |
(loop |
| 333 |
(when nomatch (return)) ; exit from loop |
| 334 |
(block try-rule |
| 335 |
(catch 'rule-failure |
| 336 |
(when (is-true? |
| 337 |
(let (($$cond (substitution-image |
| 338 |
;; want to avoid recopying the whole |
| 339 |
(if *copy-conditions* |
| 340 |
(mapcar #'(lambda (x) |
| 341 |
(cons (car x) |
| 342 |
(@copy-term (cdr x)))) |
| 343 |
subst) |
| 344 |
subst) |
| 345 |
condition))) |
| 346 |
(!normalize-term $$cond))) |
| 347 |
;; the condition is satisfied |
| 348 |
(term-replace-dd-simple |
| 349 |
term |
| 350 |
(if (rule-is-builtin rule) |
| 351 |
(multiple-value-bind (newterm success) |
| 352 |
(funcall (lisp-form-function (rule-rhs rule)) subst) |
| 353 |
(if success |
| 354 |
newterm |
| 355 |
(return-from try-rule nil))) |
| 356 |
(substitution-image! subst (rule-rhs rule)))) |
| 357 |
(return-from the-end t))) |
| 358 |
) ; block try-rule |
| 359 |
;; else, try another ... |
| 360 |
(multiple-value-setq (global-state subst nomatch) |
| 361 |
(funcall next-match-method global-state)) |
| 362 |
) ; end loop |
| 363 |
;; In this case there is no match at all and the rule does not apply |
| 364 |
(return-from the-end nil))))) |
| 365 |
|
| 366 |
(defun @apply-rule (rule sort term) |
| 367 |
(if (and *-inside-apply-with-extensions-* |
| 368 |
(term-is-application-form? term) |
| 369 |
(method-is-of-same-operator (term-head (rule-lhs rule)) |
| 370 |
(term-head term))) |
| 371 |
(@apply-rule-with-extensions rule sort term) |
| 372 |
(@apply-one-rule rule sort term))) |
| 373 |
|
| 374 |
(defun @apply-rule-with-extensions (rule sort term) |
| 375 |
(let ((top (term-head (rule-lhs rule)))) |
| 376 |
(if (method-is-associative top) |
| 377 |
(let ((is-applied nil) |
| 378 |
(is-AC (method-is-commutative top))) |
| 379 |
(when (@test-rule rule term) |
| 380 |
(@apply-one-rule rule sort term) |
| 381 |
(setq is-applied *-applied-*)) |
| 382 |
(unless is-applied |
| 383 |
(dolist (r (if is-AC |
| 384 |
(compute-AC-extension rule top) |
| 385 |
(compute-A-extensions rule top))) |
| 386 |
(when (and r (@test-rule r term)) |
| 387 |
(@apply-one-rule r sort term) |
| 388 |
(setq is-applied *-applied-*) |
| 389 |
(return))))) |
| 390 |
;; only hit this case if top of rule lhs wasn't associative |
| 391 |
(@apply-one-rule rule sort term))) |
| 392 |
nil) |
| 393 |
|
| 394 |
;;; |
| 395 |
;;; @apply-all |
| 396 |
;;; |
| 397 |
(defun @apply-all (rule sort term) |
| 398 |
(if (term-is-variable? term) |
| 399 |
(when (@test-rule rule term) (@apply-rule rule sort term)) |
| 400 |
(if (@test-rule rule term) |
| 401 |
(@apply-rule rule sort term) |
| 402 |
(if (term-is-application-form? term) |
| 403 |
(mapc #'(lambda (s x) (@apply-all rule s x)) |
| 404 |
(method-arity (term-head term)) |
| 405 |
(term-subterms term))))) |
| 406 |
nil) |
| 407 |
|
| 408 |
;;; |
| 409 |
;;; apply-print-rule |
| 410 |
;;; |
| 411 |
(defun apply-print-rule (x) |
| 412 |
(unless x |
| 413 |
(format t "~%That dosen't make sense as a rule specification.") |
| 414 |
(return-from apply-print-rule t)) |
| 415 |
(let* ((act (get-apply-action x)) |
| 416 |
(rule-spec (if (eq act :apply) |
| 417 |
(parse-rule-spec x)))) |
| 418 |
;; |
| 419 |
(if (eq :reduce act) |
| 420 |
(format t "~%special rule for reduction of a selected subterm.") |
| 421 |
(if (eq :print act) |
| 422 |
(format t "~%special rule to print-the selected subterm.") |
| 423 |
(progn |
| 424 |
(when (or (eq :error rule-spec) (null rule-spec)) |
| 425 |
(format t "~%That doesn't make sense as a rule specification.") |
| 426 |
(return-from apply-print-rule t)) |
| 427 |
(let ((num (cadr rule-spec)) |
| 428 |
(mod (car rule-spec)) |
| 429 |
(rev (caddr rule-spec))) |
| 430 |
(format t "~&rule ~a" num) |
| 431 |
(when rev (format t " (reversed)")) |
| 432 |
(if (equal "" mod) |
| 433 |
(format t " of the last module") |
| 434 |
(format t " of module ~a" mod)) |
| 435 |
(multiple-value-bind (rule module) |
| 436 |
(compute-action-rule rule-spec nil) |
| 437 |
(with-in-module (module) |
| 438 |
(format t "~& ") |
| 439 |
(print-chaos-object rule) |
| 440 |
(when (and rev (or (rule-is-builtin rule) |
| 441 |
(eq (axiom-type rule) :rule))) |
| 442 |
(format t "~%This rule cannot be applied reversed.")) |
| 443 |
(when (and (get-context-module t) |
| 444 |
(not (rule-is-builtin rule))) |
| 445 |
(format t "~%(This rule rewrites up.)")))))))) |
| 446 |
t)) |
| 447 |
|
| 448 |
;;; EOF |