| 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: eval-match.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 |
;;; DESCRIPTION ---------------------------------------------------------------- |
| 41 |
;;; evaluators of match, find commands. |
| 42 |
;;; *NOTE* `find <what>' is just the alias of `match it to <what>' . |
| 43 |
;;; |
| 44 |
|
| 45 |
;;; ****** |
| 46 |
;;; MATCH |
| 47 |
;;; ****** |
| 48 |
|
| 49 |
(defun eval-match-command (ast) |
| 50 |
(let ((type (%match-type ast)) |
| 51 |
(target (case (%match-target ast) |
| 52 |
(:top $$term) |
| 53 |
(:subterm $$subterm) |
| 54 |
(:it (if $$subterm |
| 55 |
$$subterm |
| 56 |
$$term)) |
| 57 |
(t (let* ((*parse-variables* nil) |
| 58 |
(parsed (with-in-module ((get-context-module)) |
| 59 |
(prepare-for-parsing *current-module*) |
| 60 |
(simple-parse *current-module* |
| 61 |
(%match-target ast) |
| 62 |
*cosmos*)))) |
| 63 |
(if (sort<= (term-sort parsed) |
| 64 |
*syntax-err-sort* |
| 65 |
*chaos-sort-order*) |
| 66 |
nil |
| 67 |
parsed)))))) |
| 68 |
;; |
| 69 |
(when (or (null target) (eq target 'void)) |
| 70 |
(if (symbolp (%match-target ast)) |
| 71 |
(with-output-chaos-warning () |
| 72 |
(princ "no target term is specified, please `start' or `choose'."))) |
| 73 |
(return-from eval-match-command nil)) |
| 74 |
;; |
| 75 |
(if (symbolp (%match-pattern ast)) |
| 76 |
(find-rewrite-rules target (%match-pattern ast) type) |
| 77 |
(perform-match target (%match-pattern ast) type)))) |
| 78 |
|
| 79 |
(defun find-rewrite-rules (target what &optional (type :match)) |
| 80 |
(if *find-all-rules* |
| 81 |
(find-rewrite-rules-all target what type) |
| 82 |
(find-rewrite-rules-top target what type))) |
| 83 |
|
| 84 |
(defun find-rewrite-rules-top (target what &optional (type :match)) |
| 85 |
(let* ((real-target (supply-pconstants target)) |
| 86 |
(patterns (find-matching-rules what real-target (get-context-module) type))) |
| 87 |
(unless patterns |
| 88 |
(with-in-module ((get-context-module)) |
| 89 |
(format t "~%no rules found for term : ") |
| 90 |
(term-print target)) |
| 91 |
(return-from find-rewrite-rules-top nil)) |
| 92 |
;; report the result |
| 93 |
(format t "~%== matching rules to term : ") |
| 94 |
(with-in-module ((get-context-module)) |
| 95 |
(let ((*fancy-print* nil)) |
| 96 |
(term-print target)) |
| 97 |
(dolist (pat patterns) |
| 98 |
(let ((num (found-pattern-rule-num pat)) |
| 99 |
(direction (found-pattern-direction pat)) |
| 100 |
(rule (found-pattern-rule pat)) |
| 101 |
(subst (found-pattern-subst pat)) |
| 102 |
(extra (found-pattern-extra pat))) |
| 103 |
(print-next) |
| 104 |
(if (eq direction :-rule) |
| 105 |
(princ "-") |
| 106 |
(princ "+")) |
| 107 |
(print-mod-name *current-module*) |
| 108 |
(if (rule-labels rule) |
| 109 |
(format t ".{ ~D " num) |
| 110 |
(format t ".~D" num)) |
| 111 |
(dolist (label (rule-labels rule)) |
| 112 |
(print-check) |
| 113 |
(princ "| ") |
| 114 |
(princ (string label))) |
| 115 |
(when (rule-labels rule) |
| 116 |
(princ " }")) |
| 117 |
(princ " is ") |
| 118 |
(print-axiom-brief rule *standard-output* nil t) |
| 119 |
(format t "~% substitution : ") |
| 120 |
(let ((*print-indent* (+ *print-indent* 4))) |
| 121 |
(print-substitution subst)) |
| 122 |
(when extra |
| 123 |
(format t "~% extra variables : ") |
| 124 |
(format t "~{~a~^ ~}" (mapcar #'(lambda (x) (string (variable-name x))) |
| 125 |
extra)))))))) |
| 126 |
|
| 127 |
(defun find-rewrite-rules-all (target what &optional (type :match)) |
| 128 |
(let* ((real-target (supply-pconstants target)) |
| 129 |
(patterns (find-matching-rules-all what real-target (get-context-module) type))) |
| 130 |
(unless patterns |
| 131 |
(with-in-module ((get-context-module)) |
| 132 |
(format t "~%no rules found for term : ") |
| 133 |
(term-print target)) |
| 134 |
(return-from find-rewrite-rules-all nil)) |
| 135 |
;; report the result |
| 136 |
(format t "~%== matching rules to term : ") |
| 137 |
(with-in-module ((get-context-module)) |
| 138 |
(let ((*fancy-print* nil)) |
| 139 |
(term-print target)) |
| 140 |
(dolist (pat patterns) |
| 141 |
(let ((num (found-pattern-rule-num pat)) |
| 142 |
(direction (found-pattern-direction pat)) |
| 143 |
(rule (found-pattern-rule pat)) |
| 144 |
(subst (found-pattern-subst pat)) |
| 145 |
(extra (found-pattern-extra pat)) |
| 146 |
(pos (found-pattern-occur pat))) |
| 147 |
(print-next) |
| 148 |
(princ "* at posotion:") |
| 149 |
(format t "(~{~d~^ ~}), " pos) |
| 150 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 151 |
;; |
| 152 |
(if (eq direction :-rule) |
| 153 |
(princ "-") |
| 154 |
(princ "+")) |
| 155 |
(print-mod-name *current-module*) |
| 156 |
(if (rule-labels rule) |
| 157 |
(format t ".{ ~D " num) |
| 158 |
(format t ".~D" num)) |
| 159 |
(dolist (label (rule-labels rule)) |
| 160 |
(print-check) |
| 161 |
(princ "| ") |
| 162 |
(princ (string label))) |
| 163 |
(when (rule-labels rule) |
| 164 |
(princ " }")) |
| 165 |
;; |
| 166 |
(princ " is") |
| 167 |
(print-next) |
| 168 |
(print-axiom-brief rule *standard-output* nil t) |
| 169 |
(format t "~& substitution : ") |
| 170 |
(let ((*print-indent* (+ *print-indent* 4))) |
| 171 |
(print-substitution subst)) |
| 172 |
(when extra |
| 173 |
(format t "~& extra variables : ") |
| 174 |
(format t "~{~a~^ ~}" (mapcar #'(lambda (x) (string (variable-name x))) |
| 175 |
extra))))))))) |
| 176 |
|
| 177 |
;;; moved to 'meta.lisp' |
| 178 |
;;; (defvar *use-choose-match* nil) |
| 179 |
|
| 180 |
(defun perform-match (target pre-pattern &optional (type :match)) |
| 181 |
(let ((real-target (if (eq type :match) |
| 182 |
(supply-pconstants target) |
| 183 |
target))) |
| 184 |
(let ((first-match-meth (if (eq type :match) |
| 185 |
(if *use-choose-match* |
| 186 |
nil |
| 187 |
'@matcher) |
| 188 |
'first-unify)) |
| 189 |
(next-match-meth (if (eq type :match) |
| 190 |
(if *use-choose-match* |
| 191 |
nil |
| 192 |
'next-match) |
| 193 |
'next-unify))) |
| 194 |
(with-in-module ((get-context-module)) |
| 195 |
(let* ((*parse-variables* (mapcar #'(lambda (x) |
| 196 |
(cons (variable-name x) x)) |
| 197 |
(term-variables target))) |
| 198 |
(pattern (simple-parse *current-module* |
| 199 |
pre-pattern |
| 200 |
*cosmos*))) |
| 201 |
(when (sort<= (term-sort pattern) |
| 202 |
*syntax-err-sort* *chaos-sort-order*) |
| 203 |
(return-from perform-match nil)) |
| 204 |
;; |
| 205 |
(when (and *use-choose-match* |
| 206 |
(eq type :match)) |
| 207 |
(let ((meth (choose-match-method real-target *bool-true* nil))) |
| 208 |
(setf first-match-meth (car meth)) |
| 209 |
(setf next-match-meth (cdr meth)))) |
| 210 |
;; ---- first match |
| 211 |
(multiple-value-bind (global-state subst no-match e-equal) |
| 212 |
(funcall first-match-meth pattern real-target) |
| 213 |
(when no-match |
| 214 |
(if (eq type :match) |
| 215 |
(format t "~%-- no match") |
| 216 |
(format t "~%-- no unify")) |
| 217 |
(return-from perform-match nil)) |
| 218 |
(if (eq type :match) |
| 219 |
(format t "~%-- match success.") |
| 220 |
(format t "~%-- unify success.")) |
| 221 |
(when e-equal |
| 222 |
(format t "~%-- given terms are equational equal.") |
| 223 |
(return-from perform-match nil)) |
| 224 |
(format t "~% substitution : ") |
| 225 |
(let ((*print-indent* (+ *print-indent* 4))) |
| 226 |
(print-substitution subst)) |
| 227 |
;; ---- next matches |
| 228 |
(block end |
| 229 |
(multiple-value-setq (global-state subst no-match) |
| 230 |
(funcall next-match-meth global-state)) |
| 231 |
(while (not no-match) |
| 232 |
(cond ((y-or-n-p-wait #\y 20 ">> More? [y/n] : ") |
| 233 |
(if (eq type :match) |
| 234 |
(format t "~%-- match success : ") |
| 235 |
(format t "~%-- unify success : ")) |
| 236 |
(let ((*print-indent* (+ 4 *print-indent*))) |
| 237 |
(format t "~% * substitution : ") |
| 238 |
(print-substitution subst)) |
| 239 |
(print-next)) |
| 240 |
(t (return-from end))) |
| 241 |
(multiple-value-setq (global-state subst no-match) |
| 242 |
(funcall next-match-meth global-state))) |
| 243 |
(if (eq type :match) |
| 244 |
(format t "~%-- No more match") |
| 245 |
(format t "~%-- No more unify"))))))))) |
| 246 |
|
| 247 |
;;; EOF |