Debian Codegraph @

thstuff/cexec.lisp

raw
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:cexec.lisp
34 =============================================================================|#
35 #-(or :chaos-debug SBCL)
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 (declaim (special $$cexec-term)) ; the target term
42
43 ;;; *****
44 ;;; RULEs
45 ;;; *****
46
47 ;;; RULE-PAT
48 ;;; - a rule applicable to the current target
49 ;;; - POS: position matching
50 ;;; - RULE: the rule
51 ;;; - SUBST: the substitution
52 ;;;
53 (defstruct (rule-pat (:print-function print-rule-pattern))
54 (pos nil :type list) ; matched position (list of fixnum)
55 (rule nil :type rewrite-rule) ; matched rule
56 (subst nil :type substitution) ; variable substitution
57 (cond-ok t :type (or null t)) ; t iff condition part of the rule is satisfied
58 (condition nil :type (or null term)) ; resulting condition part ('if') when cond-ok = nil
59 (num 0 :type fixnum) ; sequential #, used for debugging
60 )
61
62 (declaim (type fixnum .rules-so-far.))
63 (defvar .rules-so-far. 0)
64
65 (defun print-rule-pattern (rpat &optional (stream *standard-output*) &rest ignore)
66 (declare (type rule-pat rpat)
67 (type stream stream)
68 (ignore ignore))
69 (format stream "~%-- rule pattern: ~d" (rule-pat-num rpat))
70 (format stream "~% posisition: ~a" (rule-pat-pos rpat))
71 (format stream "~& rule :")(print-chaos-object (rule-pat-rule rpat))
72 (format stream "~& subst :")(print-substitution (rule-pat-subst rpat))
73 (format stream "~& cond-ok :~a" (rule-pat-cond-ok rpat))
74 (let ((cond (rule-pat-condition rpat)))
75 (when cond
76 (format stream "~& condition :")(term-print cond))))
77
78 ;;; *****
79 ;;; STATE
80 ;;; *****
81
82 ;;; RWL-STATE
83 ;;; represents a state
84 ;;;
85 (defstruct (rwl-state
86 (:print-function pr-rwl-state))
87 (state 0 :type fixnum) ; fixnum value identifying this state
88 (term nil :type term) ; a term
89 (trans-rules nil :type list) ; applicable rules to this state
90 (rule-pat nil :type (or null rule-pat)) ; the rule-pat which derived this state
91 (subst nil :type list) ; list of substitution !!
92 (is-final nil :type (or t null)) ; t iff the state is a final state
93 (loop nil :type (or t null)) ; t iff the same state occurs more than once
94 (condition nil) ;
95 (depth 0 :type fixnum) ; nesting depth of rwl-search*
96 )
97
98 (declaim (inline state-is-valid-transition))
99 (defun state-is-valid-transition (state)
100 (declare (type rwl-state state)
101 (optimize (speed 3) (safety 0)))
102 (let ((cond (rwl-state-condition state)))
103 (and (not (rwl-state-loop state))
104 (or (null cond)
105 (is-true? cond)))))
106
107 (defun pr-rwl-state (state &optional (stream *standard-output*) &rest ignore)
108 (declare (type rwl-state state)
109 (type stream stream)
110 (ignore ignore))
111 (let ((*standard-output* stream))
112 (format t "#<rwl-state(~D):" (rwl-state-state state))
113 (term-print (rwl-state-term state))
114 (princ ", ")
115 (dolist (sub (rwl-state-subst state))
116 (print-substitution sub))
117 (when (rwl-state-is-final state)
118 (princ " ,final"))
119 (princ ">")))
120
121 (declaim (special .rwl-search-depth.)
122 (type fixnum .rwl-search-depth.))
123 (defvar .rwl-search-depth. -1)
124
125 (defun print-rwl-state (state &optional (stream *standard-output*) &rest ignore)
126 (declare (ignore ignore)
127 (type rwl-state state)
128 (type stream stream))
129 (let ((*standard-output* stream))
130 (format t "~%[state ~D-~D] " (rwl-state-depth state) (rwl-state-state state))
131 (let ((*print-indent* (+ 4 *print-indent*)))
132 (term-print-with-sort (rwl-state-term state))
133 (when *cexec-trace*
134 (format t "~& matched with the substitution "))
135 (let ((*print-indent* (+ 4 *print-indent*)))
136 (dolist (subst (rwl-state-subst state))
137 (print-next)
138 (print-substitution subst)))
139 (flush-all))))
140
141 (defun print-state-transition (state sub-states &optional (stream *standard-output*))
142 (declare (type rwl-state state)
143 (type list sub-states)
144 (type stream stream))
145 (let ((*standard-output* stream)
146 (arc-num 0))
147 (declare (type fixnum arc-num))
148 (format t "~%[state ~D-~D] " (rwl-state-depth state) (rwl-state-state state))
149 (term-print-with-sort (rwl-state-term state))
150 (dolist (sub sub-states)
151 (format t "~& arc ~D --> [state ~D-~D] " arc-num (rwl-state-depth state) (rwl-state-state sub))
152 (let ((*print-indent* (+ 4 *print-indent*)))
153 (print-next)
154 (print-axiom-brief (rule-pat-rule (rwl-state-rule-pat sub))))
155 (incf arc-num))))
156
157 ;;; ***********
158 ;;; SEARCH TREE
159 ;;; ***********
160
161 ;;; Search tree
162 ;;; - bi-directional dag (see comlib/dag.lisp)
163 ;;; - datum contains an instance of rwl-state.
164 ;;;
165 (defstruct (rwl-sch-node (:include bdag)
166 (:conc-name "SCH-NODE-")
167 (:print-function pr-rwl-sch-node))
168 (done nil :type (or null t)) ; t iff this node is checked already
169 (is-solution nil :type (or null t)) ; t iff this node found as a solution
170 )
171
172 (defmacro create-sch-node (rwl-state)
173 `(make-rwl-sch-node :datum ,rwl-state :subnodes nil :parent nil :is-solution nil))
174
175 (defun pr-rwl-sch-node (node &optional (stream *standard-output*) &rest ignore)
176 (declare (ignore ignore))
177 (let ((*standard-output* stream))
178 (format t "SCH-NODE:~A" (dag-node-datum node))))
179
180 ;;; **************
181 ;;; SEARCH CONTEXT
182 ;;; **************
183
184 ;;; RWL-SCH-CONTEXT
185 ;;;
186 (defstruct (rwl-sch-context
187 (:print-function print-sch-context))
188 (module nil :type module) ; context module
189 (term nil :type term) ; initial term
190 (pattern nil :type term) ; pattern to be matched
191 (condition nil :type (or null term)) ; =(*)=> with COND
192 (zero-trans-allowed nil :type (or null t)) ; ... =>*
193 (final-check nil :type (or null t)) ; ... =>!
194 (max-sol most-positive-fixnum :type fixnum) ; =(max-sol, )=>
195 (sol-found 0 :type fixnum) ; found solutions so far
196 (max-depth most-positive-fixnum :type fixnum)
197 ; =(, max-depth)=>
198 (cur-depth 0 :type fixnum) ; current depth
199 (root nil :type (or null rwl-sch-node)) ; root node of the search tree
200 ; (an instance of rwl-sch-node)
201 (trans-so-far 0 :type fixnum) ; # of transitions so far
202 (last-siblings nil :type list) ; nodes to be checked
203 ; initially, this contains the root.
204 (state-predicate nil) ; STATE equality predicate
205 (answers nil :type list) ; list of STATEs satisfying specified
206 ; conditions.
207 (bind nil) ; ....
208 (if nil) ;
209 (pr-out? nil :type (or null t)) ;
210 (term-hash nil :type simple-vector) ; term hash table for catching loop
211 )
212
213 (defun print-sch-context (ctxt &optional (stream *standard-output*) &rest ignore)
214 (declare (type rwl-sch-context ctxt)
215 (type stream stream)
216 (ignore ignore))
217 (let ((*standard-output* stream)
218 (mod (rwl-sch-context-module ctxt)))
219 (with-in-module (mod)
220 (format t "~%<< sch context >>")
221 (format t "~% module: ")
222 (print-chaos-object (rwl-sch-context-module ctxt))
223 (format t "~% term: ")
224 (term-print-with-sort (rwl-sch-context-term ctxt))
225 (format t "~% pattern: ")
226 (term-print-with-sort (rwl-sch-context-pattern ctxt))
227 (format t "~% condition: ")
228 (if (rwl-sch-context-condition ctxt)
229 (term-print-with-sort (rwl-sch-context-condition ctxt))
230 (princ "None."))
231 (format t "~% zero?: ~A" (rwl-sch-context-zero-trans-allowed ctxt))
232 (format t "~% final?: ~A" (rwl-sch-context-final-check ctxt))
233 (format t "~% max sol. : ~D" (rwl-sch-context-max-sol ctxt))
234 (format t "~% solutions: ~D" (rwl-sch-context-sol-found ctxt))
235 (format t "~% max depth: ~D" (rwl-sch-context-max-depth ctxt))
236 (format t "~% current depth: ~D" (rwl-sch-context-cur-depth ctxt))
237 (format t "~% root node: ~A" (rwl-sch-context-root ctxt))
238 ;; (format t "~% states: ~D" (rwl-sch-context-states-so-far ctxt))
239 (format t "~% transitions: ~D" (rwl-sch-context-trans-so-far ctxt))
240 (format t "~% last siblings: ")
241 (dolist (s (rwl-sch-context-last-siblings ctxt))
242 (format t "~% ~A" s))
243 (format t "~% answers: ")
244 (dolist (x (reverse (rwl-sch-context-answers ctxt)))
245 (term-print-with-sort (rwl-state-term x)))
246 (when (rwl-sch-context-bind ctxt)
247 (format t "~% bind pattern: ")
248 (term-print-with-sort (rwl-sch-context-bind ctxt)))
249 (when (rwl-sch-context-if ctxt)
250 (format t "~% if: ")
251 (term-print-with-sort (rwl-sch-context-if ctxt))))))
252
253
254 ;;; ******************
255 ;;; RWL-SCH-NODE utils
256 ;;; ******************
257
258 ;;; print the rule & state
259 ;;;
260 (defun show-rwl-sch-state (dag &optional (path? t) (bind-pattern nil))
261 (declare (type rwl-sch-node dag))
262 (let* ((st (dag-node-datum dag))
263 (term (rwl-state-term st))
264 (rule-pat (rwl-state-rule-pat st))
265 (rl (if rule-pat (rule-pat-rule rule-pat)
266 nil)))
267 (when (and rl path?)
268 (print-next)
269 (princ " ")
270 (let ((*print-indent* (+ 8 *print-indent*)))
271 (print-chaos-object rl) ; (print-axiom-brief rl)
272 ))
273 (format t "~%[state ~D-~D] " (rwl-state-depth st) (rwl-state-state st))
274 (term-print-with-sort term)
275 (dolist (sub (rwl-state-subst st))
276 (format t "~& ")
277 (print-substitution sub)
278 (when bind-pattern
279 (let ((bimage (substitution-image-simplifying sub bind-pattern)))
280 (normalize-term bimage)
281 (format t "~% => ")
282 (term-print-with-sort bimage))))))
283
284 ;;; print the label of a rule which derived a state
285 ;;; that denode contains.
286 ;;;
287 (defun show-rwl-sch-label (dnode)
288 (declare (type rwl-sch-node dnode))
289 (let* ((dt (dag-node-datum dnode))
290 (rl (rule-pat-rule (rwl-state-rule-pat dt)))
291 (label (car (rule-labels rl))))
292 (if label
293 (format t "~&[~a]" label)
294 (format t "~&NONE"))))
295
296
297 ;;; RULE PAT constructor
298 (defun make-rule-pat-with-check (pos rule subst sch-context)
299 (declare (type list pos)
300 (type rewrite-rule rule)
301 (type substitution subst)
302 (type rwl-sch-context sch-context)
303 (optimize (speed 3) (safety 0)))
304 (when (rule-non-exec rule)
305 ;; the rule is marked as non-executable
306 (return-from make-rule-pat-with-check nil))
307 (let ((condition (rule-condition rule)))
308 (declare (type term condition))
309 ;; pre check whether the condition part is satisfied or not
310 (when (and (is-true? condition)
311 (null (rule-id-condition rule)))
312 ;; rule is not conditional
313 (return-from make-rule-pat-with-check
314 (make-rule-pat :pos pos :rule rule :subst subst :num (incf .rules-so-far.))))
315 ;; check the condition
316 (let (($$term nil)
317 ($$cond (set-term-color (substitution-image-cp subst condition))))
318 (when *cexec-debug*
319 (format t "~%rule: cond ") (term-print-with-sort $$cond)
320 (format t "~% subst") (print-substitution subst)
321 (let ((vars (term-variables $$cond)))
322 (dolist (v vars)
323 (format t "~% var ") (term-print-with-sort v))))
324 (catch 'rule-failure
325 (if (and (or (null (rule-id-condition rule))
326 (rule-eval-id-condition subst
327 (rule-id-condition rule)
328 :slow))
329 (is-true? (progn (normalize-term $$cond) $$cond)))
330 ;; the condition is satisfied
331 (return-from make-rule-pat-with-check
332 (make-rule-pat :pos pos :rule rule :subst subst :cond-ok t :condition $$cond :num (incf .rules-so-far.)))
333 (if (rwl-sch-context-if sch-context)
334 ;; rule condition fail & there exists 'if'
335 (return-from make-rule-pat-with-check
336 (make-rule-pat :pos pos :rule rule :subst subst :cond-ok nil :condition $$cond :num (incf .rules-so-far.)))
337 (return-from make-rule-pat-with-check nil))))
338 nil)))
339
340 (defun rule-pat-equal (pat1 pat2)
341 (declare (type rule-pat pat1 pat2)
342 (optimize (speed 3) (safety 0)))
343 (and (equal (rule-pat-pos pat1) (rule-pat-pos pat2))
344 (eq (rule-pat-rule pat1) (rule-pat-rule pat2))
345 (substitution-equal (rule-pat-subst pat1) (rule-pat-subst pat2))))
346
347
348 ;;; *********************
349 ;;; SEARCH CONTEXT UTILS
350 ;;; *********************
351
352 ;;; parse-depth&state
353 ;;;
354 (defun parse-depth&state (&optional ds-string)
355 (unless ds-string
356 (return-from parse-depth&state nil))
357 (let* ((ds-list (parse-with-delimiter ds-string #\-))
358 (depth (or (and (cdr ds-list) (read-from-string (car ds-list)))
359 0))
360 (state (or (and (cdr ds-list) (read-from-string (cadr ds-list)))
361 (read-from-string (car ds-list)))))
362 (unless (and (integerp depth) (>= depth 0)
363 (integerp state) (>= state 0))
364 (with-output-chaos-error ('invalid-depth-state)
365 (format t "Invalid depth/state specifier: ~a" ds-string)))
366 (list depth state)))
367
368 ;;; show-rwl-sch-graph
369 ;;;
370 (defun show-rwl-sch-graph (&optional num)
371 (let ((c-num (if num
372 (read-from-string num)
373 0))
374 (sch-context nil))
375 (unless (integerp c-num)
376 (with-output-chaos-error ('invalid-context-number)
377 (format t "invalid search graph number ~s" num)))
378 (setq sch-context (nth c-num (reverse .rwl-context-stack.)))
379 (unless sch-context
380 (with-output-chaos-error ('no-such-context)
381 (format t "no such search graph ~d" num)))
382 (let ((mod (rwl-sch-context-module sch-context))
383 (root (rwl-sch-context-root sch-context)))
384 (unless mod
385 (with-output-chaos-error ('no-context)
386 (format t "no context module...")))
387 (unless root
388 (with-output-chaos-error ('no-root)
389 (format t "no search result exists...")))
390 (when (and *current-module*
391 (not (eq *current-module* mod)))
392 (with-output-chaos-warning ()
393 (format t "the context(module) of search graph is different from the current module.")))
394 ;;
395 (with-in-module (mod)
396 (let ((state-hash (make-hash-table)))
397 (dag-wfs root
398 #'(lambda (d)
399 (let* ((state-node (dag-node-datum d))
400 (state (rwl-state-state state-node)))
401 (unless (gethash state state-hash)
402 (setf (gethash state state-hash) t)
403 (print-state-transition
404 state-node
405 (mapcar #'(lambda (sd)
406 (dag-node-datum sd))
407 (dag-node-subnodes d))))))))))))
408
409 (defun find-rwl-sch-state (num &optional (sch-context .rwl-sch-context.))
410 (declare (type fixnum num))
411 (unless sch-context
412 (with-output-chaos-error ('no-root-node)
413 (format t "no search result exists")))
414 (let ((dag nil))
415 (setq dag
416 (catch 'dag-found
417 (dag-wfs (rwl-sch-context-root sch-context)
418 #'(lambda (d)
419 (let ((st (dag-node-datum d)))
420 (when (= (rwl-state-state st) num)
421 (throw 'dag-found d)))))
422 nil))
423 dag))
424
425 (defun find-rwl-sch-state-globally (num)
426 (declare (type fixnum num))
427 (dolist (context .rwl-context-stack.)
428 (let ((st (find-rwl-sch-state num context)))
429 (when st (return-from find-rwl-sch-state-globally (values context st))))))
430
431 (defun show-rwl-sch-path (&optional (ds-string nil)
432 (label? nil)
433 (state-only? nil))
434
435 (unless ds-string
436 (return-from show-rwl-sch-path
437 (format t "~%Nothing to be reported...")))
438 (unless .rwl-context-stack.
439 (with-output-chaos-error ('no-context)
440 (format t "~%There is no search context.")))
441 (let* ((ds-list (parse-depth&state ds-string))
442 (sch-context (or (nth (car ds-list) (reverse .rwl-context-stack.))
443 (with-output-chaos-error ('no-sch-context)
444 (format t "There is no RWL search context ~d" (car ds-list)))))
445 (dag (find-rwl-sch-state (cadr ds-list) sch-context)))
446 (unless dag
447 (with-output-chaos-error ('no-such-state)
448 (format t "There is no state ~d in context ~d" (cadr ds-list) (car ds-list))))
449 (let ((mod (rwl-sch-context-module sch-context)))
450 (when (and *current-module*
451 (not (eq *current-module* mod)))
452 (with-output-chaos-warning ()
453 (format t "the context(module) of search result is different from the current module.")))
454 (with-in-module (mod)
455 (cond (state-only? (show-rwl-sch-state dag nil (rwl-sch-context-bind sch-context)))
456 (t (let ((parents (get-bdag-parents dag)))
457 (cond (label?
458 (dolist (p (cdr parents)) ;root has no transition
459 (show-rwl-sch-label p))
460 (show-rwl-sch-label dag))
461 (t (dolist (p parents)
462 (show-rwl-sch-state p t (rwl-sch-context-bind sch-context)))
463 (show-rwl-sch-state dag t (rwl-sch-context-bind sch-context)))))))))))
464
465 ;;; ******************
466 ;;; SOME UTILs on TERM
467 ;;; ******************
468 ;;; returns a subterm at position 'pos'
469 ;;;
470 (declaim (inline get-target-subterm))
471 (defun get-target-subterm (term pos)
472 (declare (type term term)
473 (type list pos)
474 (optimize (speed 3) (safety 0)))
475 (let ((cur term))
476 (declare (type term cur))
477 (when pos
478 (dolist (p pos)
479 (declare (type fixnum p))
480 (setq cur (term-arg-n cur p))
481 (unless cur
482 (with-output-panic-message ()
483 (format t "could not find subterm at pos ~d" pos)
484 (format t "~% target was ")
485 (term-print term)
486 (break "wow!")
487 (chaos-error 'panic)))))
488 cur))
489
490 ;;; *************
491 ;;; PATTERN MATCH
492 ;;; *************
493
494 ;;; finds all transition rules possibly applicable to the given target term
495 ;;;
496 (defun find-matching-rules-for-exec (target sch-context &optional start-pos)
497 (declare (type term target)
498 (type rwl-sch-context sch-context)
499 (type list start-pos)
500 (optimize (speed 3) (safety 0)))
501 (let ((module (rwl-sch-context-module sch-context)))
502 (declare (type module module))
503 (when start-pos
504 (setq target (get-target-subterm target start-pos)))
505 (with-in-module (module)
506 (let* ((*module-all-rules-every* t)
507 (rules (get-module-axioms *current-module* t))
508 (rls nil)
509 (res nil))
510 (declare (type list res))
511 (dolist (rule rules)
512 (declare (type rewrite-rule rule))
513 (when (rule-is-rule rule)
514 (push rule rls)))
515 ;; gather rules
516 (setq res (find-matching-rules-for-exec* target rls start-pos sch-context))
517 (setq res (delete-duplicates res
518 :test #'rule-pat-equal))
519 (when *cexec-debug*
520 (format t "~%** ~D rules were found for term: "
521 (length res))
522 (term-print target)
523 (terpri)
524 (dolist (r res)
525 (print-rule-pattern r)))
526 res ))))
527
528 (defun find-matching-rules-for-exec* (target rules pos sch-context)
529 (declare (type term target)
530 (type list rules pos)
531 (type rwl-sch-context sch-context))
532 (when *cexec-debug*
533 (format t "~%find matching rules. ")
534 (term-print target)
535 (terpri))
536 (if (term-is-application-form? target)
537 (let ((res nil)
538 (rule-pat nil))
539 (do* ((rls rules (cdr rls))
540 (rule (car rls) (car rls)))
541 ((endp rls))
542 (let* ((patterns nil)
543 (lhs (rule-lhs rule))
544 (head (if (term-is-variable? lhs)
545 nil
546 (term-head lhs))))
547 (push rule patterns)
548 ;; ------- apply-rule always applies extensions
549 (when head
550 (when (method-is-associative head)
551 (if (method-is-commutative head)
552 (let ((ac-ext (give-AC-extension rule)))
553 (when ac-ext
554 (unless (car ac-ext)
555 (print ac-ext)
556 (break "Here it is!"))
557 (push (car ac-ext) patterns)))
558 ;;
559 (let ((a-exts (give-A-extensions rule)))
560 (dolist (r a-exts)
561 (when r
562 (push r patterns)))))))
563 ;; ------------
564 (dolist (pat patterns)
565 (block next
566 (unless pat (break "HANA my"))
567 ;; find all possible subst
568 (multiple-value-bind (gs sub no-match eeq)
569 (@matcher (axiom-lhs pat) target :match)
570 (declare (ignore eeq))
571 (when no-match (return-from next))
572 (setq rule-pat (make-rule-pat-with-check pos pat sub sch-context))
573 (when rule-pat
574 (push rule-pat res))
575 (loop
576 (multiple-value-setq (gs sub no-match)
577 (next-match gs))
578 (when no-match (return-from next))
579 (setq rule-pat (make-rule-pat-with-check pos pat sub sch-context))
580 (when rule-pat
581 (push rule-pat res))))))
582 )) ; done for all rules
583 ;; recursively find rules for subterms
584 (dotimes (x (length (term-subterms target)))
585 (let ((r (find-matching-rules-for-exec* (term-arg-n target x)
586 rules
587 (append pos (list x))
588 sch-context)))
589 (when r (setq res (nconc res r)))))
590 ;;
591 res)
592 nil))
593
594 ;;; ****************
595 ;;; SOLUTION CHECKER
596 ;;; ****************
597
598 (declaim (inline if-binding-should-be-printe))
599 (defun if-binding-should-be-printed (sch-context)
600 (declare (type rwl-sch-context sch-context)
601 (optimize (speed 3) (safety 0)))
602 (and (rwl-sch-context-if sch-context)
603 (<= (rwl-sch-context-cur-depth sch-context) (rwl-sch-context-max-depth sch-context))))
604
605 ;;;
606 (declaim (inline print-subst-if-binding-result))
607 (defun print-subst-if-binding-result (state sub sch-context)
608 (declare (ignore state)
609 (optimize (speed 3) (safety 0)))
610 (setf (rwl-sch-context-pr-out? sch-context) t)
611 (print-next)
612 (format t " ") (print-substitution sub)
613 (when (rwl-sch-context-bind sch-context)
614 (let ((bimg (substitution-image-simplifying sub (rwl-sch-context-bind sch-context))))
615 (normalize-term bimg)
616 (print-next)
617 (format t " --> ")
618 (if (and *grind-bool-term*
619 (sort= (term-sort bimg) *bool-sort*))
620 (let ((bt (abstract-boolean-term bimg *current-module*)))
621 (print-bterm-grinding bt))
622 (term-print-with-sort bimg)))))
623
624 ;;; rwl-sch-check-conditions (node rwl-sch-context)
625 ;;; check if the given state matches to the target pattern.
626 ;;; return t iff matches, otherwise nil.
627 ;;;
628 (defun rwl-sch-check-conditions (node sch-context)
629 (declare (type rwl-sch-node node)
630 (type rwl-sch-context sch-context)
631 (optimize (speed 3) (safety 0)))
632 (flet ((condition-check-ok (subst)
633 (let ((cond (rwl-sch-context-condition sch-context))
634 ($$term nil)
635 ($$cond nil)
636 (*rewrite-exec-mode* (if *rewrite-exec-condition*
637 *rewrite-exec-mode*
638 nil)))
639 (if (null cond)
640 (setq $$cond *bool-true*)
641 (setq $$cond (set-term-color
642 (substitution-image-cp subst cond))))
643 (when *cexec-debug*
644 (format t "~% subst :") (print-substitution subst)
645 (format t "~& suchThat :") (term-print $$cond))
646 (or (is-true? $$cond)
647 (is-true? (progn
648 (if *cexec-debug*
649 (let (($$trace-rewrite t))
650 (print-term-tree $$cond t)
651 (normalize-term $$cond))
652 (normalize-term $$cond))
653 (when *cexec-debug*
654 (format t " -C-> ") (term-print $$cond)
655 (format t "~% = ~s" (is-true? $$cond)))
656 $$cond))))))
657 ;; if checked already, we return immediately as non.
658 (when (and (sch-node-done node) (null (rwl-sch-context-if sch-context)))
659 (return-from rwl-sch-check-conditions nil))
660 ;;
661 (let* ((state (dag-node-datum node))
662 (if-var (rwl-sch-context-if sch-context))
663 (rule-pat (rwl-state-rule-pat state)))
664 (declare (type rwl-state state))
665
666 (when *chaos-verbose*
667 (format t " ~D-~D" (rwl-state-depth state) (rwl-state-state state)))
668
669 (setf (sch-node-done node) t) ; mark checked already
670
671 (when *cexec-debug*
672 (when (rwl-sch-context-condition sch-context)
673 (format t "~%** check condition ")
674 (term-print-with-sort (rwl-sch-context-condition sch-context))
675 (if rule-pat
676 (print-rule-pattern rule-pat)
677 (format t "~% no rule-pat."))))
678 ;; 0 transition?
679 (when (and (not (rwl-sch-context-zero-trans-allowed sch-context))
680 (= 0 (rwl-sch-context-trans-so-far sch-context)))
681 (when *cexec-debug*
682 (format t "~%.check condition return with 0 transition."))
683 (return-from rwl-sch-check-conditions nil))
684 ;; check with target pattern.
685 (multiple-value-bind (gs sub no-match eeq)
686 (@matcher (rwl-sch-context-pattern sch-context)
687 (rwl-state-term state)
688 :match)
689 (declare (ignore eeq))
690 (when no-match
691 (when *cexec-debug*
692 (format t "~%.check condition return with no-match."))
693 (return-from rwl-sch-check-conditions nil))
694 ;; expand subst with
695 (let ((rule-subst (and rule-pat (rule-pat-subst rule-pat))))
696 (when (car rule-subst)
697 (setq sub (append sub rule-subst))))
698 ;; additionaly expand subst 'if' part bindings
699 (when if-var
700 (setq sub (substitution-add sub if-var (or (rwl-state-condition state)
701 *bool-true*))))
702 (when (condition-check-ok sub)
703 (when (if-binding-should-be-printed sch-context) ; if-var
704 (pr-used-rule state)
705 (print-subst-if-binding-result state sub sch-context))
706 (when (state-is-valid-transition state)
707 (push sub (rwl-state-subst state))))
708 ;; try other patterns untill there's no hope
709 (loop
710 (multiple-value-setq (gs sub no-match)
711 (next-match gs))
712 (when no-match (return))
713 ;; expand subst with
714 (let ((rule-subst (and rule-pat (rule-pat-subst rule-pat))))
715 (when (car rule-subst)
716 (setq sub (append sub rule-subst))))
717 (when if-var
718 (setq sub (substitution-add sub if-var (or (rwl-state-condition state)
719 *bool-true*))))
720 (when (condition-check-ok sub)
721 (when (if-binding-should-be-printed sch-context) ; if-var
722 (when (pr-used-rule state)
723 (print-subst-if-binding-result state sub sch-context)))
724 (when (state-is-valid-transition state)
725 (push sub (rwl-state-subst state))))))
726 (not (null (rwl-state-subst state))))))
727
728 (defun pr-used-rule (state)
729 (declare (type rwl-state state))
730 (let ((rule-pat (rwl-state-rule-pat state))
731 (rule nil))
732 (unless rule-pat (return-from pr-used-rule nil))
733 (setq rule (rule-pat-rule rule-pat))
734 (unless *print-exec-rule*
735 (when (member (axiom-kind rule) .ext-rule-kinds.)
736 (return-from pr-used-rule nil)))
737 (unless *rwl-search-no-state-report*
738 (format t "~%=> ")
739 (print-axiom-brief rule))
740 t))
741
742 ;;; *********
743 ;;; TERM HASH : used for loop check
744 ;;; *********
745 (defvar .cexec-term-hash. nil)
746 ;; (deftype term-hash-key () '(unsigned-byte 29))
747 (deftype term-hash-key () 'fixnum)
748 #+(or (and :SBCL :64-BIT) (and :ALLEGRO :64BIT))
749 (defconstant term-hash-mask #x1FFFFFFFFFFFFFF)
750 #+(or (and :SBCL :32-BIT) (and :ALLEGRO :32BIT))
751 (defconstant term-hash-mask #x1FFFFFFF)
752 #-(or :SBCL :ALLEGRO)
753 (defconstant term-hash-mask #x1FFFFFFF)
754
755 (defconstant term-hash-size 9001)
756
757 (declaim (inline term-hash-equal))
758 #-CMU
759 (defun term-hash-equal (x)
760 (declare (optimize (speed 3) (safety 0)))
761 (logand term-hash-mask (sxhash x)))
762
763 #+CMU
764 (defun term-hash-equal (x)
765 (sxhash x))
766
767 (declaim (inline term-hash-eq))
768 (defun term-hash-eq (object)
769 (declare (optimize (speed 3) (safety 0)))
770 (ash (+ (the term-hash-key
771 (logand term-hash-mask
772 (the fixnum (addr-of object))))
773 3)
774 -3))
775
776 (declaim (inline term-hash-comb))
777 (defun term-hash-comb (x y)
778 (declare (optimize (speed 3) (safety 0))
779 (type fixnum x y))
780 (the term-hash-key (logand term-hash-mask (logand term-hash-mask (+ x y)))))
781
782 (defun cexec-hash-term (term)
783 (declare (type term term)
784 (optimize (speed 3) (safety 0)))
785 (cond ((term-is-applform? term)
786 (let ((res (sxhash (the symbol (method-id-symbol (term-head term))))))
787 (dolist (subterm (term-subterms term))
788 (setq res (term-hash-comb res (cexec-hash-term subterm))))
789 res))
790 ((term-is-builtin-constant? term)
791 (term-hash-comb (sxhash (the symbol (sort-id (term-sort term))))
792 (term-hash-equal (term-builtin-value term))))
793 ((term-is-variable? term) (term-hash-eq term))))
794
795 ; (defun dump-cexec-term-hash (&optional (size term-hash-size))
796 ; (let ((mod (get-context-module)))
797 ; (unless mod (return-from dump-cexec-term-hash nil))
798 ; (with-in-module (mod)
799 ; (dotimes (x size)
800 ; (let ((ent (svref .cexec-term-hash. x)))
801 ; (when ent
802 ; (format t "~%[~3d]: ~d entrie(s)" x (length ent))
803 ; (dotimes (y (length ent))
804 ; (let ((e (nth y ent)))
805 ; (format t "~%(~d)" y)
806 ; (let ((*print-indent* (+ 2 *print-indent*)))
807 ; (term-print (car e))
808 ; (print-next)
809 ; (princ "==>")
810 ; (print-next)
811 ; (term-print (cdr e)))))))))))
812
813 (defun dump-cexec-term-hash (&optional (size term-hash-size))
814 (let ((mod (get-context-module)))
815 (unless mod (return-from dump-cexec-term-hash nil))
816 (with-in-module (mod)
817 (dotimes (x size)
818 (let ((ent (svref .cexec-term-hash. x)))
819 (when ent
820 (format t "~%[~3d]: ~d entrie(s)" x (length ent))
821 (dotimes (y (length ent))
822 (let ((e (nth y ent)))
823 (format t "~%(~d) " y)
824 (term-print (car e))
825 (print-next)
826 (princ "==> ")
827 (princ (cdr e))))))))))
828
829 (declaim (inline get-sch-hashed-term))
830 (defun get-sch-hashed-term (term term-hash)
831 (declare (type term term)
832 (type simple-vector term-hash)
833 (optimize (speed 3) (safety 0)))
834 (let ((val (cexec-hash-term term)))
835 (let* ((ent (svref term-hash
836 (mod val term-hash-size)))
837 (val (cdr (assoc term ent :test #'term-equational-equal))))
838 (when val (incf (the fixnum *term-memo-hash-hit*)))
839 val)))
840
841 (declaim (inline set-sch-hashed-term))
842 (defun set-sch-hashed-term (term term-hash value)
843 (declare (type term term)
844 (type simple-vector term-hash)
845 (type fixnum value)
846 (optimize (speed 3) (safety 0)))
847 (let ((val (cexec-hash-term term)))
848 (let ((ind (mod val term-hash-size)))
849 (let ((ent (svref term-hash ind)))
850 (let ((pr (assoc term ent :test #'term-equational-equal)))
851 (if pr (rplacd pr value)
852 (setf (svref term-hash ind) (cons (cons term value) ent))))))))
853
854 (defmacro cexec-get-hashed-term (term)
855 `(get-sch-hashed-term ,term .cexec-term-hash.))
856
857 (defmacro cexec-set-hashed-term (term state-num)
858 `(set-sch-hashed-term ,term .cexec-term-hash. ,state-num))
859
860 (declaim (inline cexec-sch-check-predicate))
861 (defun cexec-sch-check-predicate (term t1 pred-pat)
862 (declare (type term term t1)
863 (type list pred-pat)
864 (optimize (speed 3) (safety 0)))
865 (let ((pred (car pred-pat))
866 (vars (cdr pred-pat))
867 (subst nil)
868 (res nil))
869 (declare (type term pred)
870 (type list vars))
871 ;; make substittion
872 (if (sort<= (term-sort term) (term-sort (the term (car vars))) *current-sort-order*)
873 (push (cons (car vars) term) subst)
874 (with-output-chaos-error ('invalid-state)
875 (format t "withStateEq: sort of term does not match with variable:")
876 (format t "~% variable: ")
877 (term-print-with-sort (car vars))
878 (format t "~% term: ")
879 (term-print-with-sort term)))
880 (if (sort<= (term-sort term) (term-sort (cadr vars)) *current-sort-order*)
881 (push (cons (cadr vars) t1) subst)
882 (with-output-chaos-error ('invalid-state)
883 (format t "withStateEq: sort of term does not match with variable:")
884 (format t "~% variable: ")
885 (term-print-with-sort (cadr vars))
886 (format t "~% term: ")
887 (term-print-with-sort t1)))
888
889 ;; apply subst with coping pred
890 ;; then reduce
891 (setq res
892 (is-true?
893 (let (($$cond (set-term-color
894 (substitution-image-cp subst pred)))
895 (*rewrite-exec-mode*
896 (if *rewrite-exec-condition*
897 *rewrite-exec-mode*
898 nil)))
899 (when *cexec-debug*
900 (format t "~%[withEQ] ")
901 (term-print $$cond))
902 (normalize-term $$cond)
903 (when *cexec-debug*
904 (format t "~% = ")
905 (term-print $$cond))
906 $$cond)))
907 (when (and res *cexec-trace*)
908 (format t "~%** state predicate returned `true'."))
909 res))
910
911 (defun cexec-loop-check (term sch-context)
912 (declare (type term)
913 (type rwl-sch-context sch-context)
914 (optimize (speed 3) (safety 0)))
915 (or (get-sch-hashed-term term .cexec-term-hash.)
916 (let ((pred-pat (rwl-sch-context-state-predicate sch-context)))
917 (if pred-pat
918 (dotimes (x term-hash-size nil)
919 (declare (fixnum x))
920 (let ((entry (svref .cexec-term-hash. x)))
921 (when entry
922 (dotimes (y (length entry))
923 (declare (type fixnum y))
924 (let ((t1 (nth y entry)))
925 (when (cexec-sch-check-predicate term (car t1) pred-pat)
926 (return-from cexec-loop-check (cdr t1))))))))
927 nil))))
928
929 ;;;
930 ;;; MAKE-RWL-STATE-WITH-HASH
931 ;;;
932 (defun make-rwl-state-with-hash (target rule-pat sch-context)
933 (declare (type term target)
934 (type rule-pat rule-pat)
935 (type rwl-sch-context sch-context)
936 (optimize (speed 3) (safety 0)))
937 (let* ((ostate-num (cexec-loop-check target sch-context))
938 (condition (rule-pat-condition rule-pat))
939 (new-state nil))
940 (declare (type (or null fixnum) ostate-num))
941 (cond (ostate-num
942 ;; this means the same state has alredy been generated
943 ;; from a node other than this node.
944 ;; we create brand new state with the same state number
945 (setq new-state (make-rwl-state :state ostate-num
946 :term target
947 :rule-pat rule-pat
948 :subst nil
949 :condition condition
950 :depth .rwl-search-depth.))
951 (when (or *cexec-trace* *chaos-verbose*)
952 (format t "~%* loop"))
953 (setf (rwl-state-loop new-state) t))
954 (t (let ((state-num (incf .rwl-states-so-far.)))
955 (setq new-state (make-rwl-state :state state-num
956 :term target
957 :rule-pat rule-pat
958 :subst nil
959 :condition condition
960 :depth .rwl-search-depth.))
961 ;; register the term
962 (when *cexec-debug*
963 (format t "~%** hashing state ~D" state-num))
964 (set-sch-hashed-term target .cexec-term-hash. state-num))))
965 ;;
966 new-state))
967
968 ;;; *******************
969 ;;; ONE STEP TRANSITION
970 ;;; *******************
971
972 ;;; RWL-STATE-SET-TRANSITION-RULES
973 ;;;
974 (defun rwl-state-set-transition-rules (state sch-context)
975 (declare (type rwl-state state)
976 (type rwl-sch-context sch-context)
977 (optimize (speed 3) (safety 0)))
978 (let ((rule-pats (find-matching-rules-for-exec (rwl-state-term state) sch-context)))
979 (setf (rwl-state-trans-rules state) rule-pats)
980 (unless rule-pats
981 (setf (rwl-state-is-final state) t))))
982
983 ;;;
984 ;;; APPLY-RULE-CEXEC: rule target -> Bool
985 ;;;
986 (defun apply-rule-cexec (rule term subst)
987 (declare (type rewrite-rule rule)
988 (term term)
989 (substitution subst))
990 (catch 'rule-failure
991 (progn
992 (term-replace-dd-simple
993 term
994 (set-term-color
995 (substitution-image-simplifying subst
996 (rule-rhs rule)
997 (rule-need-copy rule)
998 :slow)))
999 (return-from apply-rule-cexec t)))
1000 nil)
1001
1002 ;;; CEXEC-TERM-1 (state-as-dag)
1003 ;;;
1004 ;;; - compute all possible transitions from the given state(an instance of
1005 ;;; rwl-sch-node).
1006 ;;; - returns the list of substates which derived from the given state.
1007 ;;; - NOTE: term of the given state is NOT modified.
1008 ;;;
1009 (defun cexec-term-1 (dag sch-context) ; node-num ...
1010 (declare (type rwl-sch-node dag)
1011 (type rwl-sch-context sch-context)
1012 (optimize (speed 3) (safety 0)))
1013 (let* ((state (dag-node-datum dag))
1014 (term (rwl-state-term state)))
1015 (declare (type rwl-state state)
1016 (type term term))
1017 (flet ((no-more-transition ()
1018 (when (or *cexec-trace* *chaos-verbose*)
1019 (when (and (term-is-applform? term)
1020 (method-has-trans-rule (term-head term)))
1021 (with-output-simple-msg ()
1022 (format t "-- no more transitions from state ~D-~D."
1023 (rwl-state-depth state)
1024 (rwl-state-state state)))))
1025 (setf (rwl-state-is-final state) t)
1026 nil)
1027 (exec-trace-form ()
1028 (format t "=(~a,~a)=>~a"
1029 (if (= (rwl-sch-context-max-sol sch-context)
1030 most-positive-fixnum)
1031 "*"
1032 (rwl-sch-context-max-sol sch-context))
1033 (rwl-sch-context-cur-depth sch-context)
1034 (if (rwl-sch-context-final-check sch-context)
1035 "!"
1036 (if (rwl-sch-context-zero-trans-allowed sch-context)
1037 "*"
1038 "+"))))
1039 (state-is-valid? (state)
1040 (let ((cond (rwl-state-condition state)))
1041 (or (null cond) (is-true? cond)))))
1042 ;;
1043 (unless (state-is-valid? state)
1044 (return-from cexec-term-1 nil))
1045 ;;
1046 (let ((xterm term)
1047 (ptrans? (dag-node-subnodes dag)))
1048 (when *cexec-debug*
1049 (format t "~%[cexec-term-1] target = ")
1050 (let ((*fancy-print* nil))
1051 (term-print-with-sort xterm))
1052 (flush-all))
1053 ;; already computed ..
1054 (when ptrans? (return-from cexec-term-1 ptrans?))
1055 ;;
1056 ;; apply all rules found
1057 ;;
1058 (let ((rule-pats (rwl-state-trans-rules state))
1059 (*rewrite-exec-mode* t)
1060 (sub-states nil)
1061 (real-pats nil))
1062 (setq real-pats (remove-if #'(lambda (x) (not (rule-pat-cond-ok x))) rule-pats))
1063 (when *cexec-debug*
1064 (format t "~%++ ~D rule patterns for state" (length rule-pats))
1065 (pr-rwl-state state))
1066 (when *chaos-verbose*
1067 (format t "~%-- from [state ~D-~D] "
1068 (rwl-state-depth state)
1069 (rwl-state-state state))
1070 (format t "~D possible transitions....."
1071 (length real-pats)))
1072 ;;
1073 (unless rule-pats
1074 ;; no rules
1075 (no-more-transition)
1076 (return-from cexec-term-1 nil))
1077 ;;
1078 (when *cexec-trace*
1079 (flush-all)
1080 (format t "~%~%**> Step ~D from [state ~D-~D] "
1081 (rwl-sch-context-cur-depth sch-context)
1082 (rwl-state-depth state)
1083 (rwl-state-state state))
1084 (term-print-with-sort (rwl-state-term state))
1085 (flush-all))
1086 ;; apply all possible rules
1087 (do* ((rls rule-pats (cdr rls))
1088 (rule-pat (car rls) (car rls)))
1089 ((null rule-pat))
1090 (let* ((target-whole (simple-copy-term xterm))
1091 (target (get-target-subterm target-whole
1092 (rule-pat-pos rule-pat))))
1093 (declare (type term target-whole)
1094 (type term target))
1095 ;; the following should be done iff the target is NOT
1096 ;; in hash table + register target in hash.
1097 (when (apply-rule-cexec (rule-pat-rule rule-pat)
1098 target
1099 (rule-pat-subst rule-pat))
1100 (incf (rwl-sch-context-trans-so-far sch-context))
1101 (when *cexec-normalize*
1102 (when *cexec-debug*
1103 (format t "~%.. start doing normalization because cexec normalize is on.~% -- ")
1104 (term-print target-whole))
1105 (let ((*rewrite-exec-mode* nil)
1106 (xterm (if (and *cexec-trace* *chaos-verbose*)
1107 (simple-copy-term target-whole)
1108 nil)))
1109 (mark-term-as-not-lowest-parsed target-whole)
1110 (reset-reduced-flag target-whole)
1111 (rewrite* target-whole)
1112 (when *cexec-debug*
1113 (format t "~&==> ")
1114 (term-print target-whole))
1115 (when xterm
1116 (print-next)
1117 (unless (term-equational-equal xterm target-whole)
1118 (flush-all)
1119 (let ((*fancy-print* nil)
1120 (*print-indent* (+ 4 *print-indent*)))
1121 (princ "> pre normalization : ")
1122 (term-print xterm)
1123 (print-next)
1124 (princ "== ")
1125 (term-print target-whole))
1126 (flush-all)))))
1127 (let ((sub-state (make-rwl-state-with-hash target-whole
1128 rule-pat
1129 sch-context)))
1130 (declare (type rwl-state sub-state))
1131 (when sub-state
1132 (when *cexec-debug*
1133 (format t "~%** used rule pat = ~d" (rule-pat-num rule-pat)))
1134 (when *cexec-trace*
1135 (print-next)
1136 (flush-all)
1137 (let ((*fancy-print* nil)
1138 (*print-indent* (+ 4 *print-indent*)))
1139 (format t "@[~{~d~^ ~}]" (mapcar #'1+ (rule-pat-pos rule-pat)))
1140 (exec-trace-form)
1141 (format t " [state ~D-~D] " (rwl-state-depth sub-state) (rwl-state-state sub-state))
1142 (term-print-with-sort target-whole)
1143 (print-next)
1144 (print-axiom-brief (rule-pat-rule rule-pat))
1145 (print-next)
1146 (print-substitution (rule-pat-subst rule-pat))) ; ***
1147 (flush-all))
1148 (push sub-state sub-states)))))) ; done apply all rules
1149 (if sub-states
1150 (progn
1151 (when *chaos-verbose*
1152 (format t " => ~D (new) states."
1153 (length sub-states) ))
1154 sub-states)
1155 (progn
1156 ;; for some unknown reason, no real transitions happened...
1157 (no-more-transition)
1158 nil)))))))
1159
1160 ;;; RWL-STEP-FORWARD-1
1161 ;;; given a rwl-sch-context, execute one step transition for
1162 ;;; each `last-siblings' & check if derived terms match to `pattern'.
1163 ;;;
1164 (defun rwl-step-forward-1 (sch-context)
1165 (declare (type rwl-sch-context sch-context)
1166 (optimize (speed 3) (safety 0)))
1167 ;; check # of transitions
1168 (when (>= (rwl-sch-context-trans-so-far sch-context)
1169 *cexec-limit*)
1170 (return-from rwl-step-forward-1 (values :max-transitions nil)))
1171 ;;
1172 (let ((to-do (rwl-sch-context-last-siblings sch-context))
1173 (found? nil))
1174 (when *chaos-verbose*
1175 (format t "~%-- ~D state(s) to be examined --" (length to-do)))
1176 ;;
1177 ;; 1. check for each state if it satisfies the target conditions
1178 ;;
1179 (when *chaos-verbose*
1180 (format t "~&.....condition check for each state.....~%"))
1181 (loop
1182 (unless to-do (return nil)) ; done for every state
1183 (block :continue
1184 (let ((node (pop to-do)))
1185 (declare (type rwl-sch-node node))
1186 ;;
1187 (when (sch-node-done node)
1188 ;; already checked
1189 (return-from :continue nil))
1190 ;;
1191 (let ((state (dag-node-datum node)))
1192 ;; first prepare applicable rules for the next transition,
1193 ;; this also marks the state `is-final' iff there are no rules.
1194 (rwl-state-set-transition-rules state sch-context)
1195 ;; check state
1196 (when (and (rwl-sch-context-final-check sch-context)
1197 (not (rwl-state-is-final state)))
1198 (return-from :continue nil)) ; skip this node
1199 ;;
1200 (when (rwl-sch-check-conditions node sch-context)
1201 ;; register answer state
1202 (push (dag-node-datum node)
1203 (rwl-sch-context-answers sch-context))
1204 ;;
1205 (when (and (or (= (rwl-state-depth state) 0)
1206 *print-every-exec-finding*)
1207 (not *rwl-search-no-state-report*))
1208 (let ((*print-indent* (* 2 (rwl-state-depth state))))
1209 (print-next)
1210 (format t "** Found [state ~D-~D] " (rwl-state-depth state) (rwl-state-state state))
1211 (term-print-with-sort (rwl-state-term state))
1212 (print-next)
1213 (format t "-- target: ")
1214 (term-print (rwl-sch-context-pattern sch-context))
1215 (dolist (sub (rwl-state-subst state))
1216 (print-subst-if-binding-result state sub sch-context))
1217 (print-next)))
1218 (setf (sch-node-is-solution node) t) ; mark the node as solution
1219 (incf (rwl-sch-context-sol-found sch-context))
1220 (setq found? :found) ; we found at least one solution
1221 ;; check the # of solutions
1222 (when (>= (rwl-sch-context-sol-found sch-context)
1223 (rwl-sch-context-max-sol sch-context))
1224 ;; reaches to the # solutions required.
1225 ;; mesg
1226 (when (and (= 0 (rwl-state-depth state))
1227 (not *rwl-search-no-state-report*))
1228 (format t "~%-- found required number of solutions ~D."
1229 (rwl-sch-context-max-sol sch-context)))
1230 (return-from rwl-step-forward-1 (values :max-solutions nil))))))
1231 ) ; continue
1232 ) ; end loop
1233 ;;
1234 ;; 2. perform the next transitions for each node
1235 ;;
1236 (when *chaos-verbose*
1237 (format t "~%** precompute the next all states......"))
1238 ;; increment depth
1239 (incf (rwl-sch-context-cur-depth sch-context))
1240 ;;
1241 (let ((next-subs nil))
1242 (dolist (n (rwl-sch-context-last-siblings sch-context))
1243 ;; (unless n (break "wow! wow! "))
1244 (let ((subs (cexec-term-1 n sch-context))
1245 (nexts nil))
1246 (dolist (state subs)
1247 (let ((dag (create-sch-node state)))
1248 (setf (bdag-parent dag) n)
1249 (push dag (dag-node-subnodes n))
1250 (if (rwl-state-loop state)
1251 ;; mark `done' if the state is already
1252 ;; visited before..
1253 (progn
1254 (setf (sch-node-done dag) t)
1255 (when (rwl-sch-context-if sch-context)
1256 (when *cexec-debug*
1257 (format t "~%** calling check condition for reporting only."))
1258 (rwl-sch-check-conditions dag sch-context))) ; for reporting only
1259 (push dag nexts))))
1260 (setq next-subs (nconc next-subs nexts))))
1261 ;;
1262 ;; 3. lastly, set the next states as `last-siblings'
1263 ;;
1264 (setf (rwl-sch-context-last-siblings sch-context) next-subs)
1265 ;;
1266 (values found? (if next-subs
1267 nil
1268 :no-more)))))
1269
1270 ;;; *********
1271 ;;; TOP LEVEL functions
1272 ;;; *********
1273 (declaim (inline make-anything-is-ok-term))
1274 (defun make-anything-is-ok-term ()
1275 (make-variable-term *cosmos* (gensym "Univ")))
1276
1277 (defun rwl-search* (t1 t2 max-result max-depth zero? final?
1278 &optional cond
1279 pred-pat
1280 module
1281 bind
1282 if)
1283 (declare (type term t1 t2)
1284 (type (or null t) zero? final?)
1285 (optimize (speed 3) (safety 0)))
1286 (with-in-module (module)
1287 (unless t2
1288 (setq t2 (make-anything-is-ok-term)))
1289 ;; t1 and t2 must be in the same connected component
1290 (let ((t1-sort (term-sort t1))
1291 (t2-sort (term-sort t2)))
1292 (unless (is-in-same-connected-component t1-sort t2-sort *current-sort-order*)
1293 (with-output-chaos-error ('invalid-sort)
1294 (format t "Sorts of the source term and the target pattern must be in the same connected component.")
1295 (format t "~% Source term : ")
1296 (term-print-with-sort t1)
1297 (format t "~& Target pattern: ")
1298 (term-print-with-sort t2))))
1299 (let ((svars (term-variables t1)) ; variables in source
1300 (pvars (term-variables t2)) ; variables in pattern
1301 (cvars (if cond ; variables in suchThat
1302 (term-variables cond)
1303 nil))
1304 (predvars (if pred-pat ; variables in stateEq
1305 (term-variables pred-pat)
1306 nil))
1307 (ifvars (if if
1308 (term-variables if)
1309 nil))
1310 (allvars nil)
1311 (.rules-so-far. 0))
1312 (setq allvars (union svars (union pvars ifvars)))
1313 ;; check suchThat
1314 (when cvars
1315 (unless (subsetp cvars allvars)
1316 (with-output-chaos-error ('invalid-such-that)
1317 (format t "`suchThat' introduces new variable(s)."))))
1318 ;; check variables
1319 (dolist (v svars)
1320 (when (memq v pvars)
1321 (with-output-chaos-error ('subject-var-occus)
1322 (format t "Variable ")
1323 (term-print-with-sort v)
1324 ;; (format t " in subject term occurs in target pattern or coditions..")
1325 (format t " in subject term occurs in target pattern.")
1326 (format t "~& subject: ")
1327 (term-print-with-sort t1)
1328 (format t "~& pattern: ")
1329 (term-print-with-sort t2))))
1330 (dolist (v predvars)
1331 (when (or (memq v svars)
1332 (memq v pvars)
1333 (memq v cvars))
1334 (with-output-chaos-error ('invalid-stateEq)
1335 (format t "Variable ")
1336 (term-print-with-sort v)
1337 (format t " in 'stateEq' occurs in subject term or target pattern or 'suchThat'.."))))
1338 ;;
1339 (let ((sch-context (make-rwl-sch-context
1340 :module module
1341 :term t1
1342 :pattern t2
1343 :condition cond
1344 :zero-trans-allowed zero?
1345 :final-check final?
1346 :max-sol max-result
1347 :max-depth max-depth
1348 :state-predicate nil
1349 :bind bind
1350 :if if
1351 :term-hash (alloc-svec term-hash-size)))
1352 (root nil)
1353 (res nil)
1354 (no-more nil)
1355 (found? nil))
1356 (flet ((make-state-pred-pat ()
1357 (cond (pred-pat
1358 (let ((vars (term-variables pred-pat)))
1359 (declare (type list vars))
1360 (unless (sort= (term-sort pred-pat)
1361 *Bool-sort*)
1362 (with-output-chaos-error ('invalid-sort)
1363 (format t "state equality must be of a term of sort Bool.")))
1364 (unless (= 2 (the fixnum (length vars)))
1365 (with-output-chaos-error ('number-of-variables)
1366 (format t "state equality pattern must have exactly 2 different variables in it, but ~D given." (length vars))))
1367 (unless (sort= (variable-sort (car vars))
1368 (variable-sort (cadr vars)))
1369 (with-output-chaos-error ('different-variable-sort)
1370 (format t "variables in state equality pattern must be of the same sort.")))
1371 (unless (sort<= (term-sort t2) (term-sort (car vars))
1372 *current-sort-order*)
1373 (with-output-chaos-error ('invalid-variable-sort)
1374 (format t "invalid sort of variable in state equality pattern.")))
1375 (cons pred-pat vars)))
1376 (t nil))))
1377 ;;
1378 ;; initialize search context
1379 ;;
1380 (setf (rwl-sch-context-cur-depth sch-context) 0
1381 (rwl-sch-context-sol-found sch-context) 0
1382 (rwl-sch-context-trans-so-far sch-context) 0
1383 root (create-sch-node (make-rwl-state :state 0 :term t1 :depth (1+ .rwl-search-depth.))))
1384 (setf (rwl-sch-context-root sch-context) root
1385 (rwl-sch-context-last-siblings sch-context) (list root)
1386 (rwl-sch-context-answers sch-context) nil)
1387 ;; state equality predicate
1388 (setf (rwl-sch-context-state-predicate sch-context) (make-state-pred-pat))
1389 (let ((.rwl-sch-context. sch-context)
1390 (.cexec-term-hash. (rwl-sch-context-term-hash sch-context))
1391 (.rwl-search-depth. (1+ .rwl-search-depth.))
1392 (.ignore-term-id-limit. t))
1393 (declare (special .rwl-sch-context. .cexec.term-hash. .ignore-term-id-limit.))
1394 (push sch-context .rwl-context-stack.)
1395 ;; the first state is 0
1396 (set-sch-hashed-term t1 .cexec-term-hash. 0)
1397 ;;
1398 ;; do the search
1399 ;;
1400 (when *cexec-debug*
1401 (print sch-context))
1402 (loop
1403 (when *chaos-verbose*
1404 (format t "~%** << level ~D >>" (rwl-sch-context-cur-depth sch-context)))
1405 (multiple-value-setq (res no-more)
1406 (rwl-step-forward-1 sch-context))
1407 (case res
1408 (:max-transitions (return nil)) ; exit loop
1409 (:max-solutions
1410 (setq found? t)
1411 (return nil)) ; exit loop
1412 (:found
1413 (setq found? t)) ; continue..
1414 (otherwise nil))
1415 (when no-more
1416 (when (and (= 0 .rwl-search-depth.)
1417 (not *rwl-search-no-state-report*))
1418 (format t "~%** No more possible transitions."))
1419 (return nil)) ; exit if no more ...
1420 ;; one step deeper
1421 (when (> (rwl-sch-context-cur-depth sch-context)
1422 (rwl-sch-context-max-depth sch-context))
1423 (unless *rwl-search-no-state-report*
1424 (format t "~%-- reached to the specified search depth ~D."
1425 (rwl-sch-context-max-depth sch-context)))
1426 (return-from rwl-search*
1427 (if (rwl-sch-context-if sch-context)
1428 (if (rwl-sch-context-pr-out? sch-context)
1429 :found
1430 nil)
1431 (if found? :found :max-depth))))) ; end loop
1432 ;; any solution?
1433 (cond ((rwl-sch-context-if sch-context)
1434 (if (rwl-sch-context-pr-out? sch-context)
1435 :found
1436 nil))
1437 (t (if found?
1438 ;; yes
1439 :found
1440 ;; no
1441 res)))))))))
1442
1443 ;;; report-rwl-result
1444 ;;;
1445 (defun report-rwl-result (res)
1446 (case res
1447 (:max-depth
1448 ;; (format t "~&-- reached to the max depth.")
1449 )
1450 (:no-more
1451 ;; (format t "~&-- no more solutions.")
1452 )
1453 (otherwise
1454 nil))
1455 res)
1456
1457 ;;; RWL-CONTINUE
1458 ;;;
1459 (defun rwl-continue (num-tok)
1460 (multiple-value-bind (num sym)
1461 (nat*-to-max-option num-tok)
1462 (declare (ignore sym))
1463 (report-rwl-result
1464 (rwl-continue* num))))
1465
1466 (defun rwl-continue+ (num)
1467 (report-rwl-result (rwl-continue* num)))
1468
1469 (defun rwl-continue* (num)
1470 (declare (type fixnum num))
1471 (unless (and .rwl-sch-context.
1472 (or (null *current-module*)
1473 (eq *current-module*
1474 (rwl-sch-context-module .rwl-sch-context.))))
1475 (with-output-chaos-error ('invalid-context)
1476 (format t "invalid context...")))
1477 ;;
1478 (with-in-module ((rwl-sch-context-module .rwl-sch-context.))
1479 (setf (rwl-sch-context-max-sol .rwl-sch-context.) num)
1480 (setf (rwl-sch-context-sol-found .rwl-sch-context.) 0)
1481 (setf (rwl-sch-context-max-depth .rwl-sch-context.) most-positive-fixnum)
1482 ;;
1483 ;; do continue search
1484 ;;
1485 (let ((sch-context .rwl-sch-context.)
1486 (res nil)
1487 (found? nil))
1488 (loop
1489 (setq res (rwl-step-forward-1 sch-context))
1490 (case res
1491 ((:max-transitions
1492 :max-solutions
1493 :no-more)
1494 (return nil)) ; exit loop
1495 (:found
1496 (setq found? t))
1497 (otherwise nil))
1498 ;; one step deeper
1499 (incf (rwl-sch-context-cur-depth sch-context))) ; end loop
1500 (if found?
1501 :found
1502 res))))
1503
1504 ;;; RWL-SEARCH
1505 ;;;
1506 (defun rwl-search (&key term
1507 pattern
1508 (max-result most-positive-fixnum)
1509 (max-depth most-positive-fixnum)
1510 (zero? nil)
1511 (final? nil)
1512 (cond nil)
1513 (pred nil)
1514 (bind nil)
1515 ;; the followings are experimental
1516 (if nil))
1517 (let ((module (get-context-module))
1518 max-r
1519 max-d)
1520 (if (integerp max-result)
1521 (setq max-r max-result)
1522 (if (term-is-builtin-constant? max-result)
1523 (setq max-r (term-builtin-value max-result))
1524 (setq max-r most-positive-fixnum)))
1525 (if (integerp max-depth)
1526 (setq max-d max-depth)
1527 (if (term-is-builtin-constant? max-depth)
1528 (setq max-d (term-builtin-value max-depth))
1529 (setq max-d most-positive-fixnum)))
1530 (when (and if (not (term-is-variable? if)))
1531 (with-output-chaos-warning ()
1532 (format t "The `if' part is not a varible of sort BOOL, `if' binding is ignored : ")
1533 (print-next)
1534 (term-print if)
1535 (setq if nil)))
1536 (when *cexec-normalize*
1537 (let ((*rewrite-exec-mode* nil)
1538 (*clean-memo-in-normalize* nil))
1539 (rewrite* term)))
1540 ;;
1541 (when *cexec-debug*
1542 (format t "~%* CEXEC: ")
1543 (term-print-with-sort term))
1544 ;;
1545 (let ((*clean-memo-in-normalize* nil))
1546 (report-rwl-result
1547 (rwl-search* term pattern max-r max-d zero? final? cond pred module bind if)))))
1548
1549 ;;; rwl-check-one-step-reachability : term term -> { t | nil }
1550 ;;; working hourse of =>
1551 ;;;
1552 (defun rwl-check-one-step-reachability (X Y)
1553 (declare (type term X Y))
1554 (let ((*clean-memo-in-normalize* nil)
1555 (*chaos-quiet* t))
1556 (report-rwl-result
1557 (rwl-search* X Y 1 1 t nil nil nil *current-module* nil nil))))
1558
1559 ;;; rwl-sch-set-result
1560 ;;;
1561 (defun rwl-sch-set-result (raw-res)
1562 (let ((res nil))
1563 (if (eq raw-res :found)
1564 (setq res t)
1565 (setq res nil))
1566 (setq $$cexec-term (coerce-to-bool res))))
1567
1568 ;;; rwl-cont
1569 ;;;
1570 (defun rwl-cont (ast)
1571 (rwl-continue+ (%continue-num ast)))
1572
1573 ;;; for downward compatibility
1574 ;;;
1575 (defun nat*-to-max-option (term &optional (infinite most-positive-fixnum))
1576 (if (term-is-builtin-constant? term)
1577 (values (term-builtin-value term) "")
1578 (values infinite (car (method-symbol (term-head term))))))
1579
1580 (defun term-pattern-included-in-cexec (t1 t2 max-depth &optional cond)
1581 (multiple-value-bind (max sym)
1582 (nat*-to-max-option max-depth)
1583 (let ((final? nil)
1584 (zero? nil))
1585 (case-equal sym
1586 ("!" (setq final? t))
1587 ("*" (setq zero? t))
1588 (otherwise nil))
1589 (rwl-search* t1 t2 1 max zero? final? cond nil *current-module*))))
1590
1591 ;;; EOF