0 | 0 |
;;;-*-Mode:LISP; Package: CHAOS; Base:10; Syntax:Common-lisp -*-
|
1 | 1 |
;;;
|
2 | |
;;; Copyright (c) 2000-2015, Toshimi Sawada. All rights reserved.
|
|
2 |
;;; Copyright (c) 2000-2018, Toshimi Sawada. All rights reserved.
|
3 | 3 |
;;;
|
4 | 4 |
;;; Redistribution and use in source and binary forms, with or without
|
5 | 5 |
;;; modification, are permitted provided that the following conditions
|
|
39 | 39 |
;;;
|
40 | 40 |
(declaim (special $$cexec-term)) ; the target term
|
41 | 41 |
|
|
42 |
;;; Basic Data Structures
|
|
43 |
|
|
44 |
|
42 | 45 |
;;; *****
|
43 | 46 |
;;; RULEs
|
44 | 47 |
;;; *****
|
|
61 | 64 |
(defvar .rules-so-far. 0)
|
62 | 65 |
|
63 | 66 |
(defun print-rule-pattern (rpat &optional (stream *standard-output*) &rest ignore)
|
|
67 |
(declare (type rule-pat rpat)
|
|
68 |
(type stream stream)
|
|
69 |
(ignore ignore))
|
64 | 70 |
(format stream "~%-- rule pattern: ~d" (rule-pat-num rpat))
|
65 | 71 |
(format stream "~% posisition: ~a" (rule-pat-pos rpat))
|
66 | 72 |
(format stream "~& rule :")(print-chaos-object (rule-pat-rule rpat))
|
|
68 | 74 |
(format stream "~& cond-ok :~a" (rule-pat-cond-ok rpat))
|
69 | 75 |
(format stream "~& condition :")(term-print (rule-pat-condition rpat)))
|
70 | 76 |
|
71 | |
(defun make-rule-pat-with-check (pos rule subst sch-context)
|
72 | |
(when (rule-non-exec rule)
|
73 | |
;; the rule is marked as non-executable
|
74 | |
(return-from make-rule-pat-with-check nil))
|
75 | |
(let ((condition (rule-condition rule)))
|
76 | |
;; pre check whether the condition part is satisfied or not
|
77 | |
(when (and (is-true? condition)
|
78 | |
(null (rule-id-condition rule)))
|
79 | |
;; rule is not conditional
|
80 | |
(return-from make-rule-pat-with-check
|
81 | |
(make-rule-pat :pos pos :rule rule :subst subst :num (incf .rules-so-far.))))
|
82 | |
;; check the condition
|
83 | |
(let (($$term nil)
|
84 | |
($$cond (set-term-color (substitution-image-cp subst condition))))
|
85 | |
|
86 | |
(when *cexec-debug*
|
87 | |
(format t "~%rule: cond ") (term-print-with-sort $$cond)
|
88 | |
(format t "~% subst") (print-substitution subst)
|
89 | |
(let ((vars (term-variables $$cond)))
|
90 | |
(dolist (v vars)
|
91 | |
(format t "~% var ") (term-print-with-sort v))))
|
92 | |
|
93 | |
(catch 'rule-failure
|
94 | |
(if (and (or (null (rule-id-condition rule))
|
95 | |
(rule-eval-id-condition subst
|
96 | |
(rule-id-condition rule)
|
97 | |
:slow))
|
98 | |
(is-true? (progn (normalize-term $$cond) $$cond)))
|
99 | |
;; the condition is satisfied
|
100 | |
(return-from make-rule-pat-with-check
|
101 | |
(make-rule-pat :pos pos :rule rule :subst subst :cond-ok t :condition $$cond :num (incf .rules-so-far.)))
|
102 | |
(if (rwl-sch-context-if sch-context)
|
103 | |
;; rule condition fail & there exists 'if'
|
104 | |
(return-from make-rule-pat-with-check
|
105 | |
(make-rule-pat :pos pos :rule rule :subst subst :cond-ok nil :condition $$cond :num (incf .rules-so-far.)))
|
106 | |
(return-from make-rule-pat-with-check nil))))
|
107 | |
nil)))
|
108 | |
|
109 | |
(defun rule-pat-equal (pat1 pat2)
|
110 | |
(and (equal (rule-pat-pos pat1) (rule-pat-pos pat2))
|
111 | |
(eq (rule-pat-rule pat1) (rule-pat-rule pat2))
|
112 | |
(substitution-equal (rule-pat-subst pat1) (rule-pat-subst pat2))))
|
113 | |
|
114 | |
;;; *****
|
115 | |
;;; STATE
|
116 | |
;;; *****
|
117 | |
|
|
77 |
;;; *********
|
118 | 78 |
;;; RWL-STATE
|
|
79 |
;;; *********
|
119 | 80 |
;;; represents a state
|
120 | 81 |
;;;
|
121 | 82 |
(defstruct (rwl-state
|
122 | 83 |
(:print-function pr-rwl-state))
|
123 | |
(state 0 :type fixnum) ; fixnum value identifying this state
|
124 | |
(term nil) ; a term
|
125 | |
(trans-rules nil) ; applicable rules to this state
|
126 | |
(rule-pat nil) ; the rule-pat which derived this state
|
127 | |
(subst nil) ; list of substitution !!
|
128 | |
(is-final nil) ; t iff the state is a final state
|
129 | |
(loop nil) ; t iff the same state occurs more than once
|
130 | |
(condition nil) ;
|
131 | |
(depth 0 :type fixnum) ; nesting depth of rwl-search*
|
|
84 |
(state 0 :type fixnum) ; fixnum value identifying this state
|
|
85 |
(term nil :type (or null term)) ; a term
|
|
86 |
(trans-rules nil :type list) ; applicable rules to this state
|
|
87 |
(rule-pat nil :type (or null rule-pat)) ; the rule-pat which derived this state
|
|
88 |
(subst nil :type list) ; list of substitution !!
|
|
89 |
(is-final nil :type (or null t)) ; t iff the state is a final state
|
|
90 |
(loop nil :type (or null t)) ; t iff the same state occurs more than once
|
|
91 |
(condition nil) ;
|
|
92 |
(depth 0 :type fixnum) ; nesting depth of rwl-search*
|
132 | 93 |
)
|
133 | 94 |
|
134 | |
(defun state-is-valid-transition (state)
|
135 | |
(let ((cond (rwl-state-condition state)))
|
136 | |
(and (not (rwl-state-loop state))
|
137 | |
(or (null cond)
|
138 | |
(is-true? cond)))))
|
139 | |
|
140 | |
(defun pr-rwl-state (state &optional (stream *standard-output*) &rest ignore)
|
141 | |
(declare (ignore ignore))
|
142 | |
(let ((*standard-output* stream))
|
143 | |
(format t "#<rwl-state(~D):" (rwl-state-state state))
|
144 | |
(term-print (rwl-state-term state))
|
145 | |
(princ ", ")
|
146 | |
(dolist (sub (rwl-state-subst state))
|
147 | |
(print-substitution sub))
|
148 | |
(when (rwl-state-is-final state)
|
149 | |
(princ " ,final"))
|
150 | |
(princ ">")))
|
151 | |
|
152 | |
(declaim (special .rwl-search-depth.))
|
153 | |
(defvar .rwl-search-depth. -1)
|
154 | |
|
155 | |
(defun print-rwl-state (state &optional (stream *standard-output*) &rest ignore)
|
156 | |
(declare (ignore ignore)
|
157 | |
(type rwl-state state)
|
158 | |
(type stream stream))
|
159 | |
(let ((*standard-output* stream))
|
160 | |
(format t "~%[state ~D-~D] " (rwl-state-depth state) (rwl-state-state state))
|
161 | |
(let ((*print-indent* (+ 4 *print-indent*)))
|
162 | |
(term-print-with-sort (rwl-state-term state))
|
163 | |
(when *cexec-trace*
|
164 | |
(format t "~& matched with the substitution "))
|
165 | |
(let ((*print-indent* (+ 4 *print-indent*)))
|
166 | |
(dolist (subst (rwl-state-subst state))
|
167 | |
(print-next)
|
168 | |
(print-substitution subst)))
|
169 | |
(flush-all))))
|
170 | |
|
171 | |
(defun print-state-transition (state sub-states &optional (stream *standard-output*))
|
172 | |
(declare (type rwl-state state)
|
173 | |
(type list sub-states)
|
174 | |
(type stream stream))
|
175 | |
(let ((*standard-output* stream)
|
176 | |
(arc-num 0))
|
177 | |
(declare (type fixnum arc-num))
|
178 | |
(format t "~%[state ~D-~D] " (rwl-state-depth state) (rwl-state-state state))
|
179 | |
(term-print-with-sort (rwl-state-term state))
|
180 | |
(dolist (sub sub-states)
|
181 | |
(format t "~& arc ~D --> [state ~D-~D] " arc-num (rwl-state-depth state) (rwl-state-state sub))
|
182 | |
(let ((*print-indent* (+ 4 *print-indent*)))
|
183 | |
(print-next)
|
184 | |
(print-axiom-brief (rule-pat-rule (rwl-state-rule-pat sub))))
|
185 | |
(incf arc-num))))
|
186 | |
|
187 | 95 |
;;; ***********
|
188 | |
;;; SEARCH TREE
|
|
96 |
;;; Search tree
|
189 | 97 |
;;; ***********
|
190 | |
|
191 | |
;;; Search tree
|
192 | 98 |
;;; - bi-directional dag (see comlib/dag.lisp)
|
193 | 99 |
;;; - datum contains an instance of rwl-state.
|
194 | 100 |
;;;
|
|
205 | 111 |
(declare (ignore ignore))
|
206 | 112 |
(let ((*standard-output* stream))
|
207 | 113 |
(format t "SCH-NODE:~A" (dag-node-datum node))))
|
208 | |
|
209 | |
;;; ******************
|
210 | |
;;; RWL-SCH-NODE utils
|
211 | |
;;; ******************
|
212 | |
|
213 | |
;;; print the rule & state
|
214 | |
;;;
|
215 | |
(defun show-rwl-sch-state (dag &optional (path? t) (bind-pattern nil))
|
216 | |
(declare (type rwl-sch-node dag))
|
217 | |
(let* ((st (dag-node-datum dag))
|
218 | |
(term (rwl-state-term st))
|
219 | |
(rule-pat (rwl-state-rule-pat st))
|
220 | |
(rl (if rule-pat (rule-pat-rule rule-pat)
|
221 | |
nil)))
|
222 | |
(when (and rl path?)
|
223 | |
(print-next)
|
224 | |
(princ " ")
|
225 | |
(let ((*print-indent* (+ 8 *print-indent*)))
|
226 | |
(print-chaos-object rl) ; (print-axiom-brief rl)
|
227 | |
))
|
228 | |
(format t "~%[state ~D-~D] " (rwl-state-depth st) (rwl-state-state st))
|
229 | |
(term-print-with-sort term)
|
230 | |
(dolist (sub (rwl-state-subst st))
|
231 | |
(format t "~& ")
|
232 | |
(print-substitution sub)
|
233 | |
(when bind-pattern
|
234 | |
(let ((bimage (substitution-image-simplifying sub bind-pattern)))
|
235 | |
(normalize-term bimage)
|
236 | |
(format t "~% => ")
|
237 | |
(term-print-with-sort bimage))))))
|
238 | |
|
239 | |
;;; print the label of a rule which derived a state
|
240 | |
;;; that denode contains.
|
241 | |
;;;
|
242 | |
(defun show-rwl-sch-label (dnode)
|
243 | |
(declare (type rwl-sch-node dnode))
|
244 | |
(let* ((dt (dag-node-datum dnode))
|
245 | |
(rl (rule-pat-rule (rwl-state-rule-pat dt)))
|
246 | |
(label (car (rule-labels rl))))
|
247 | |
(if label
|
248 | |
(format t "~&[~a]" label)
|
249 | |
(format t "~&NONE"))))
|
250 | |
|
251 | |
;;; **************
|
252 | |
;;; SEARCH CONTEXT
|
253 | |
;;; **************
|
254 | 114 |
|
255 | 115 |
;;; RWL-SCH-CONTEXT
|
256 | 116 |
;;;
|
|
279 | 139 |
(bind nil) ; ....
|
280 | 140 |
(if nil) ;
|
281 | 141 |
(pr-out? nil) ;
|
282 | |
(term-hash nil :type hash-table) ; term hash table for catching loop
|
283 | 142 |
)
|
284 | 143 |
|
285 | 144 |
(defun print-sch-context (ctxt &optional (stream *standard-output*) &rest ignore)
|
|
320 | 179 |
(format t "~% if: ")
|
321 | 180 |
(term-print-with-sort (rwl-sch-context-if ctxt))))))
|
322 | 181 |
|
323 | |
;;; .RWL-SCH-CONTEXT.
|
324 | |
;;; moved to comlib/globals.lisp
|
325 | |
;;; (defvar .rwl-sch-context. nil)
|
|
182 |
;;; **********************
|
|
183 |
;;; RULE related unilities
|
|
184 |
;;; **********************
|
|
185 |
(defun make-rule-pat-with-check (pos rule subst sch-context)
|
|
186 |
(declare (type list pos)
|
|
187 |
(type axiom rule)
|
|
188 |
(type substitution subst)
|
|
189 |
(type rwl-sch-context sch-context))
|
|
190 |
(when (rule-non-exec rule)
|
|
191 |
;; the rule is marked as non-executable
|
|
192 |
(return-from make-rule-pat-with-check nil))
|
|
193 |
(let ((condition (rule-condition rule)))
|
|
194 |
;; pre check whether the condition part is satisfied or not
|
|
195 |
(when (and (is-true? condition)
|
|
196 |
(null (rule-id-condition rule)))
|
|
197 |
;; rule is not conditional
|
|
198 |
(return-from make-rule-pat-with-check
|
|
199 |
(make-rule-pat :pos pos :rule rule :subst subst :num (incf .rules-so-far.))))
|
|
200 |
;; check the condition
|
|
201 |
(let (($$term nil)
|
|
202 |
($$cond (set-term-color (substitution-image-cp subst condition))))
|
|
203 |
|
|
204 |
(when *cexec-debug*
|
|
205 |
(format t "~%rule: cond ") (term-print-with-sort $$cond)
|
|
206 |
(format t "~% subst") (print-substitution subst)
|
|
207 |
(let ((vars (term-variables $$cond)))
|
|
208 |
(dolist (v vars)
|
|
209 |
(format t "~% var ") (term-print-with-sort v))))
|
|
210 |
|
|
211 |
(catch 'rule-failure
|
|
212 |
(if (and (or (null (rule-id-condition rule))
|
|
213 |
(rule-eval-id-condition subst
|
|
214 |
(rule-id-condition rule)
|
|
215 |
:slow))
|
|
216 |
(is-true? (progn (normalize-term $$cond) $$cond)))
|
|
217 |
;; the condition is satisfied
|
|
218 |
(return-from make-rule-pat-with-check
|
|
219 |
(make-rule-pat :pos pos :rule rule :subst subst :cond-ok t :condition $$cond :num (incf .rules-so-far.)))
|
|
220 |
(if (rwl-sch-context-if sch-context)
|
|
221 |
;; rule condition fail & there exists 'if'
|
|
222 |
(return-from make-rule-pat-with-check
|
|
223 |
(make-rule-pat :pos pos :rule rule :subst subst :cond-ok nil :condition $$cond :num (incf .rules-so-far.)))
|
|
224 |
(return-from make-rule-pat-with-check nil))))
|
|
225 |
nil)))
|
|
226 |
|
|
227 |
(defun rule-pat-equal (pat1 pat2)
|
|
228 |
(and (equal (rule-pat-pos pat1) (rule-pat-pos pat2))
|
|
229 |
(eq (rule-pat-rule pat1) (rule-pat-rule pat2))
|
|
230 |
(substitution-equal (rule-pat-subst pat1) (rule-pat-subst pat2))))
|
|
231 |
|
|
232 |
;;; *****
|
|
233 |
;;; STATE utils
|
|
234 |
;;; *****
|
|
235 |
|
|
236 |
(defun state-is-valid-transition (state)
|
|
237 |
(declare (type rwl-state state))
|
|
238 |
(let ((cond (rwl-state-condition state)))
|
|
239 |
(and (not (rwl-state-loop state))
|
|
240 |
(or (null cond)
|
|
241 |
(is-true? cond)))))
|
|
242 |
|
|
243 |
(defun pr-rwl-state (state &optional (stream *standard-output*) &rest ignore)
|
|
244 |
(declare (ignore ignore)
|
|
245 |
(type rwl-state state)
|
|
246 |
(type stream stream))
|
|
247 |
(let ((*standard-output* stream))
|
|
248 |
(format t "#<rwl-state(~D):" (rwl-state-state state))
|
|
249 |
(term-print (rwl-state-term state))
|
|
250 |
(princ ", ")
|
|
251 |
(dolist (sub (rwl-state-subst state))
|
|
252 |
(print-substitution sub))
|
|
253 |
(when (rwl-state-is-final state)
|
|
254 |
(princ " ,final"))
|
|
255 |
(princ ">")))
|
|
256 |
|
|
257 |
(declaim (special .rwl-search-depth.))
|
|
258 |
(defvar .rwl-search-depth. -1)
|
|
259 |
|
|
260 |
(defun print-rwl-state (state &optional (stream *standard-output*) &rest ignore)
|
|
261 |
(declare (ignore ignore)
|
|
262 |
(type rwl-state state)
|
|
263 |
(type stream stream))
|
|
264 |
(let ((*standard-output* stream))
|
|
265 |
(format t "~%[state ~D-~D] " (rwl-state-depth state) (rwl-state-state state))
|
|
266 |
(let ((*print-indent* (+ 4 *print-indent*)))
|
|
267 |
(term-print-with-sort (rwl-state-term state))
|
|
268 |
(when *cexec-trace*
|
|
269 |
(format t "~& matched with the substitution "))
|
|
270 |
(let ((*print-indent* (+ 4 *print-indent*)))
|
|
271 |
(dolist (subst (rwl-state-subst state))
|
|
272 |
(print-next)
|
|
273 |
(print-substitution subst)))
|
|
274 |
(flush-all))))
|
|
275 |
|
|
276 |
(defun print-state-transition (state sub-states &optional (stream *standard-output*))
|
|
277 |
(declare (type rwl-state state)
|
|
278 |
(type list sub-states)
|
|
279 |
(type stream stream))
|
|
280 |
(let ((*standard-output* stream)
|
|
281 |
(arc-num 0))
|
|
282 |
(declare (type fixnum arc-num))
|
|
283 |
(format t "~%[state ~D-~D] " (rwl-state-depth state) (rwl-state-state state))
|
|
284 |
(term-print-with-sort (rwl-state-term state))
|
|
285 |
(dolist (sub sub-states)
|
|
286 |
(format t "~& arc ~D --> [state ~D-~D] " arc-num (rwl-state-depth state) (rwl-state-state sub))
|
|
287 |
(let ((*print-indent* (+ 4 *print-indent*)))
|
|
288 |
(print-next)
|
|
289 |
(print-axiom-brief (rule-pat-rule (rwl-state-rule-pat sub))))
|
|
290 |
(incf arc-num))))
|
|
291 |
|
|
292 |
;;; ******************
|
|
293 |
;;; RWL-SCH-NODE utils
|
|
294 |
;;; ******************
|
|
295 |
|
|
296 |
;;; print the rule & state
|
|
297 |
;;;
|
|
298 |
(defun show-rwl-state (dag &optional (path? t) (bind-pattern nil))
|
|
299 |
(declare (type rwl-sch-node dag)
|
|
300 |
(type (or null t) path? bind-pattern))
|
|
301 |
(let* ((st (dag-node-datum dag))
|
|
302 |
(term (rwl-state-term st))
|
|
303 |
(rule-pat (rwl-state-rule-pat st))
|
|
304 |
(rl (if rule-pat (rule-pat-rule rule-pat)
|
|
305 |
nil)))
|
|
306 |
(when (and rl path?)
|
|
307 |
(print-next)
|
|
308 |
(princ " ")
|
|
309 |
(let ((*print-indent* (+ 8 *print-indent*)))
|
|
310 |
(print-chaos-object rl) ; (print-axiom-brief rl)
|
|
311 |
))
|
|
312 |
(format t "~%[state ~D-~D] " (rwl-state-depth st) (rwl-state-state st))
|
|
313 |
(term-print-with-sort term)
|
|
314 |
(dolist (sub (rwl-state-subst st))
|
|
315 |
(format t "~& ")
|
|
316 |
(print-substitution sub)
|
|
317 |
(when bind-pattern
|
|
318 |
(let ((bimage (substitution-image-simplifying sub bind-pattern)))
|
|
319 |
(normalize-term bimage)
|
|
320 |
(format t "~% => ")
|
|
321 |
(term-print-with-sort bimage))))))
|
|
322 |
|
|
323 |
;;; print the label of a rule which derived a state
|
|
324 |
;;; that denode contains.
|
|
325 |
;;;
|
|
326 |
(defun show-rwl-sch-label (dnode)
|
|
327 |
(declare (type rwl-sch-node dnode))
|
|
328 |
(let* ((dt (dag-node-datum dnode))
|
|
329 |
(rl (rule-pat-rule (rwl-state-rule-pat dt)))
|
|
330 |
(label (car (rule-labels rl))))
|
|
331 |
(if label
|
|
332 |
(format t "~&[~a]" label)
|
|
333 |
(format t "~&NONE"))))
|
326 | 334 |
|
327 | 335 |
;;; *********************
|
328 | 336 |
;;; SEARCH CONTEXT UTILS
|
|
369 | 377 |
(dag-node-datum sd))
|
370 | 378 |
(dag-node-subnodes d))))))))))))
|
371 | 379 |
|
372 | |
(defun find-rwl-sch-state (num &optional (sch-context .rwl-sch-context.))
|
373 | |
(declare (type fixnum num))
|
|
380 |
(defun find-rwl-state (num &optional (sch-context .rwl-sch-context.))
|
|
381 |
(declare (type fixnum num)
|
|
382 |
(type (or null rwl-sch-context) sch-context))
|
374 | 383 |
(unless sch-context
|
375 | 384 |
(with-output-chaos-error ('no-root-node)
|
376 | 385 |
(format t "no search result exists")))
|
|
379 | 388 |
(catch 'dag-found
|
380 | 389 |
(dag-wfs (rwl-sch-context-root sch-context)
|
381 | 390 |
#'(lambda (d)
|
|
391 |
(declare (type dag-node d))
|
382 | 392 |
(let ((st (dag-node-datum d)))
|
383 | 393 |
(when (= (rwl-state-state st) num)
|
384 | 394 |
(throw 'dag-found d)))))
|
385 | 395 |
nil))
|
386 | 396 |
dag))
|
387 | 397 |
|
388 | |
(defun find-rwl-sch-state-globally (num)
|
|
398 |
(defun find-rwl-state-globally (num)
|
389 | 399 |
(declare (type fixnum num))
|
390 | 400 |
(dolist (context .rwl-context-stack.)
|
391 | |
(let ((st (find-rwl-sch-state num context)))
|
392 | |
(when st (return-from find-rwl-sch-state-globally (values context st))))))
|
|
401 |
(declare (type rwl-sch-context context))
|
|
402 |
(let ((st (find-rwl-state num context)))
|
|
403 |
(when st (return-from find-rwl-state-globally (values context st))))))
|
393 | 404 |
|
394 | 405 |
(defun show-rwl-sch-path (num-tok &optional (label? nil)
|
395 | 406 |
(sch-context .rwl-sch-context.)
|
|
405 | 416 |
(with-output-chaos-error ()
|
406 | 417 |
(format t "state must be a positive integer value.")))
|
407 | 418 |
(multiple-value-bind (sch-context dag)
|
408 | |
(find-rwl-sch-state-globally num)
|
|
419 |
(find-rwl-state-globally num)
|
409 | 420 |
(unless dag
|
410 | 421 |
(with-output-chaos-error ('no-state)
|
411 | 422 |
(format t "no such state ~D" num)))
|
|
415 | 426 |
(with-output-chaos-warning ()
|
416 | 427 |
(format t "the context(module) of search result is different from the current module.")))
|
417 | 428 |
(with-in-module (mod)
|
418 | |
(cond (state-only? (show-rwl-sch-state dag nil (rwl-sch-context-bind sch-context)))
|
|
429 |
(cond (state-only? (show-rwl-state dag nil (rwl-sch-context-bind sch-context)))
|
419 | 430 |
(t (let ((parents (get-bdag-parents dag)))
|
420 | 431 |
(cond (label?
|
421 | 432 |
(dolist (p (cdr parents)) ;root has no transition
|
422 | 433 |
(show-rwl-sch-label p))
|
423 | 434 |
(show-rwl-sch-label dag))
|
424 | 435 |
(t (dolist (p parents)
|
425 | |
(show-rwl-sch-state p t (rwl-sch-context-bind sch-context)))
|
426 | |
(show-rwl-sch-state dag t (rwl-sch-context-bind sch-context))))))))))))
|
|
436 |
(show-rwl-state p t (rwl-sch-context-bind sch-context)))
|
|
437 |
(show-rwl-state dag t (rwl-sch-context-bind sch-context))))))))))))
|
427 | 438 |
|
428 | 439 |
|
429 | 440 |
;;; *************
|
|
433 | 444 |
;;; finds all transition rules possibly applicable to the given target term
|
434 | 445 |
;;;
|
435 | 446 |
(defun find-matching-rules-for-exec (target sch-context &optional start-pos)
|
|
447 |
(declare (type term target)
|
|
448 |
(type rwl-sch-context sch-context)
|
|
449 |
(type (or null fixnum) start-pos))
|
436 | 450 |
(let ((module (rwl-sch-context-module sch-context)))
|
|
451 |
(declare (type module module))
|
437 | 452 |
(when start-pos
|
438 | 453 |
(setq target (get-target-subterm target start-pos)))
|
439 | 454 |
(with-in-module (module)
|
|
441 | 456 |
(rules (get-module-axioms *current-module* t))
|
442 | 457 |
(rls nil)
|
443 | 458 |
(res nil))
|
|
459 |
(declare (type list rules rls res))
|
444 | 460 |
(dolist (rule rules)
|
445 | 461 |
(when (rule-is-rule rule)
|
446 | 462 |
(push rule rls)))
|
|
458 | 474 |
res ))))
|
459 | 475 |
|
460 | 476 |
(defun find-matching-rules-for-exec* (target rules pos sch-context)
|
|
477 |
(declare (type term target)
|
|
478 |
(type list rules pos)
|
|
479 |
(type rwl-sch-context sch-context))
|
461 | 480 |
(when *cexec-debug*
|
462 | 481 |
(format t "~%find matching rules. ")
|
463 | 482 |
(term-print target)
|
|
525 | 544 |
;;; ****************
|
526 | 545 |
|
527 | 546 |
(defun if-binding-should-be-printed (sch-context)
|
|
547 |
(declare (type rwl-sch-context sch-context))
|
528 | 548 |
(and (rwl-sch-context-if sch-context)
|
529 | 549 |
;; (not *rwl-search-no-state-report*)
|
530 | 550 |
(<= (rwl-sch-context-cur-depth sch-context) (rwl-sch-context-max-depth sch-context))))
|
|
537 | 557 |
(declare (type rwl-sch-node node)
|
538 | 558 |
(type rwl-sch-context sch-context))
|
539 | 559 |
(flet ((condition-check-ok (subst)
|
|
560 |
(declare (type substitution subst))
|
540 | 561 |
(let ((cond (rwl-sch-context-condition sch-context))
|
541 | 562 |
($$term nil)
|
542 | 563 |
($$cond nil)
|
|
633 | 654 |
(not (null (rwl-state-subst state))))))
|
634 | 655 |
|
635 | 656 |
(defun pr-used-rule (state)
|
|
657 |
(declare (type rwl-state state))
|
636 | 658 |
(let ((rule-pat (rwl-state-rule-pat state))
|
637 | 659 |
(rule nil))
|
638 | 660 |
(unless rule-pat (return-from pr-used-rule nil))
|
|
646 | 668 |
t))
|
647 | 669 |
|
648 | 670 |
(defun print-subst-if-binding-result (state sub sch-context)
|
649 | |
(declare (ignore state))
|
|
671 |
(declare (ignore state)
|
|
672 |
(type substitution sub)
|
|
673 |
(type rwl-sch-context sch-context))
|
650 | 674 |
(setf (rwl-sch-context-pr-out? sch-context) t)
|
651 | 675 |
(format t "~% ") (print-substitution sub)
|
652 | 676 |
(when (rwl-sch-context-bind sch-context)
|
|
665 | 689 |
;;; returns a subterm at position 'pos'
|
666 | 690 |
;;;
|
667 | 691 |
(defun get-target-subterm (term pos)
|
|
692 |
(declare (type term term)
|
|
693 |
(type list pos))
|
668 | 694 |
(let ((cur term))
|
|
695 |
(declare (type term cur))
|
669 | 696 |
(when pos
|
670 | 697 |
(dolist (p pos)
|
671 | 698 |
(setq cur (term-arg-n cur p))
|
|
679 | 706 |
cur))
|
680 | 707 |
|
681 | 708 |
;;; *********
|
682 | |
;;; TERM HASH
|
|
709 |
;;; TERM HASH : used for loop check
|
683 | 710 |
;;; *********
|
684 | |
(declaim (special .cexec-term-hash.))
|
|
711 |
(deftype term-hash-key () '(unsigned-byte 29))
|
|
712 |
(defconstant term-hash-mask #x1FFFFFFF)
|
|
713 |
(defconstant term-hash-size 9001)
|
|
714 |
|
|
715 |
(declaim (inline term-hash-equal))
|
|
716 |
#-CMU
|
|
717 |
(defun term-hash-equal (x)
|
|
718 |
(declare (optimize (speed 3) (safety 0)))
|
|
719 |
(logand term-hash-mask (sxhash x)))
|
|
720 |
|
|
721 |
#+CMU
|
|
722 |
(defun term-hash-equal (x)
|
|
723 |
(sxhash x))
|
|
724 |
|
|
725 |
(declaim (inline term-hash-eq))
|
|
726 |
(defun term-hash-eq (object)
|
|
727 |
(declare (optimize (speed 3) (safety 0)))
|
|
728 |
(ash (+ (the term-hash-key
|
|
729 |
(logand term-hash-mask
|
|
730 |
(the (unsigned-byte 32) (addr-of object))))
|
|
731 |
3)
|
|
732 |
-3))
|
|
733 |
|
|
734 |
(declaim (inline term-hash-comb))
|
|
735 |
(defun term-hash-comb (x y)
|
|
736 |
(declare (optimize (speed 3) (safety 0)))
|
|
737 |
(the term-hash-key (logand term-hash-mask (logand term-hash-mask (+ x y)))))
|
|
738 |
|
|
739 |
(defun cexec-hash-term (term)
|
|
740 |
(declare (type term term)
|
|
741 |
(optimize (speed 3) (safety 0)))
|
|
742 |
(cond ((term-is-applform? term)
|
|
743 |
(let ((res (sxhash (the symbol (method-id-symbol (term-head term))))))
|
|
744 |
(dolist (subterm (term-subterms term))
|
|
745 |
(setq res (term-hash-comb res (cexec-hash-term subterm))))
|
|
746 |
res))
|
|
747 |
((term-is-builtin-constant? term)
|
|
748 |
(term-hash-comb (sxhash (the symbol (sort-id (term-sort term))))
|
|
749 |
(term-hash-equal (term-builtin-value term))))
|
|
750 |
((term-is-variable? term) (term-hash-eq term))))
|
|
751 |
|
|
752 |
(defun dump-cexec-term-hash (term-hash &optional (size term-hash-size))
|
|
753 |
(dotimes (x size)
|
|
754 |
(let ((ent (svref term-hash x)))
|
|
755 |
(when ent
|
|
756 |
(format t "~%[~3d]: ~d entrie(s)" x (length ent))
|
|
757 |
(dotimes (y (length ent))
|
|
758 |
(let ((e (nth y ent)))
|
|
759 |
(format t "~%(~d)" y)
|
|
760 |
(let ((*print-indent* (+ 2 *print-indent*)))
|
|
761 |
(term-print (car e))
|
|
762 |
(print-next)
|
|
763 |
(princ "==>")
|
|
764 |
(print-next)
|
|
765 |
(term-print (cdr e)))))))))
|
|
766 |
|
685 | 767 |
(defvar .cexec-term-hash. nil)
|
686 | 768 |
|
|
769 |
(declaim (inline init-rwl-term-hash))
|
|
770 |
(defun init-rwl-term-hash (depth)
|
|
771 |
(declare (type fixnum depth)
|
|
772 |
(optimize (speed 3) (safety 0)))
|
|
773 |
(unless .cexec-term-hash.
|
|
774 |
(setq .cexec-term-hash. (alloc-svec term-hash-size)))
|
|
775 |
(when (zerop depth)
|
|
776 |
(dotimes (x term-hash-size)
|
|
777 |
(setf (svref .cexec-term-hash. x) nil))))
|
|
778 |
|
687 | 779 |
(declaim (inline get-sch-hashed-term))
|
688 | |
|
689 | |
(defun get-sch-hashed-term (term-id term-hash)
|
690 | |
(if term-id
|
691 | |
(gethash term-id term-hash)
|
692 | |
nil))
|
|
780 |
(defun get-sch-hashed-term (term term-hash)
|
|
781 |
(declare (type term term)
|
|
782 |
(type simple-vector term-hash)
|
|
783 |
(optimize (speed 3) (safety 0)))
|
|
784 |
(let ((val (cexec-hash-term term)))
|
|
785 |
(let* ((ent (svref term-hash
|
|
786 |
(mod val term-hash-size)))
|
|
787 |
(val (cdr (assoc term ent :test #'term-equational-equal))))
|
|
788 |
(when val (incf (the fixnum *term-memo-hash-hit*)))
|
|
789 |
val)))
|
693 | 790 |
|
694 | 791 |
(declaim (inline set-sch-hashed-term))
|
695 | |
|
696 | |
(defun set-sch-hashed-term (term-id term-hash value)
|
697 | |
(when term-id
|
698 | |
(setf (gethash term-id term-hash) value)))
|
|
792 |
(defun set-sch-hashed-term (term term-hash value)
|
|
793 |
(declare (type term term)
|
|
794 |
(type simple-vector term-hash)
|
|
795 |
(type fixnum value)
|
|
796 |
(optimize (speed 3) (safety 0)))
|
|
797 |
(let ((val (cexec-hash-term term)))
|
|
798 |
(let ((ind (mod val term-hash-size)))
|
|
799 |
(let ((ent (svref term-hash ind)))
|
|
800 |
(let ((pr (assoc term ent :test #'term-equational-equal)))
|
|
801 |
(if pr (rplacd pr value)
|
|
802 |
(setf (svref term-hash ind) (cons (cons term value) ent))))))))
|
|
803 |
|
|
804 |
(defmacro cexec-get-hashed-term (term)
|
|
805 |
`(get-sch-hashed-term ,term .cexec-term-hash.))
|
|
806 |
|
|
807 |
(defmacro cexec-set-hashed-term (term state-num)
|
|
808 |
`(set-sch-hashed-term ,term .cexec-term-hash. ,state-num))
|
699 | 809 |
|
700 | 810 |
(defun cexec-sch-check-predicate (term t1 pred-pat)
|
|
811 |
(declare (type term term t1)
|
|
812 |
(type list pred-pat)
|
|
813 |
(optimize (speed 3) (safety 0)))
|
701 | 814 |
(let ((pred (car pred-pat))
|
702 | 815 |
(vars (cdr pred-pat))
|
703 | 816 |
(subst nil)
|
|
736 | 849 |
(format t "~%** state predicate returned `true'."))
|
737 | 850 |
res))
|
738 | 851 |
|
739 | |
(defun cexec-loop-check (term-id term sch-context)
|
740 | |
(or (get-sch-hashed-term term-id .cexec-term-hash.)
|
|
852 |
(declaim (inline cexec-loop-check))
|
|
853 |
(defun cexec-loop-check (term sch-context)
|
|
854 |
(declare (type term term)
|
|
855 |
(type rwl-sch-context sch-context)
|
|
856 |
(optimize (safety 0) (speed 3)))
|
|
857 |
(or (cexec-get-hashed-term term)
|
741 | 858 |
(let ((pred-pat (rwl-sch-context-state-predicate sch-context)))
|
742 | 859 |
(if pred-pat
|
743 | |
(maphash #'(lambda (key e)
|
744 | |
(declare (ignore key))
|
745 | |
(let ((t1 (car e)))
|
746 | |
(when (cexec-sch-check-predicate term t1 pred-pat)
|
747 | |
(return-from cexec-loop-check (cdr e)))))
|
748 | |
.cexec-term-hash.)
|
|
860 |
(dotimes (x term-hash-size nil)
|
|
861 |
(let ((ent (svref .cexec-term-hash. x)))
|
|
862 |
(dolist (e ent)
|
|
863 |
(let ((t1 (car e)))
|
|
864 |
(when (cexec-sch-check-predicate term t1 pred-pat)
|
|
865 |
(return-from cexec-loop-check (cdr e)))))))
|
749 | 866 |
nil))))
|
750 | 867 |
|
751 | 868 |
;;;
|
752 | 869 |
;;; MAKE-RWL-STATE-WITH-HASH
|
753 | 870 |
;;;
|
754 | 871 |
(defun make-rwl-state-with-hash (target rule-pat sch-context)
|
755 | |
(let* ((term-id (term-id target))
|
756 | |
(ostate-num (cexec-loop-check term-id target sch-context))
|
|
872 |
(declare (type term target)
|
|
873 |
(type rule-pat rule-pat)
|
|
874 |
(type rwl-sch-context sch-context)
|
|
875 |
(optimize (speed 3) (safety 0)))
|
|
876 |
(let* ((ostate-num (cexec-loop-check target sch-context))
|
757 | 877 |
(condition (rule-pat-condition rule-pat))
|
758 | 878 |
(new-state nil))
|
759 | 879 |
(cond (ostate-num
|
|
764 | 884 |
:term target
|
765 | 885 |
:rule-pat rule-pat
|
766 | 886 |
:subst nil
|
767 | |
:condition condition
|
768 | |
:depth .rwl-search-depth.))
|
|
887 |
:condition condition))
|
769 | 888 |
(when (or *cexec-trace* *chaos-verbose*)
|
770 | 889 |
(format t "~%* loop"))
|
771 | 890 |
(setf (rwl-state-loop new-state) t))
|
|
774 | 893 |
:term target
|
775 | 894 |
:rule-pat rule-pat
|
776 | 895 |
:subst nil
|
777 | |
:condition condition
|
778 | |
:depth .rwl-search-depth.))
|
|
896 |
:condition condition))
|
779 | 897 |
;; register the term
|
780 | 898 |
(when *cexec-debug*
|
781 | 899 |
(format t "~%** hashing state ~D" state-num))
|
782 | |
(set-sch-hashed-term term-id .cexec-term-hash. state-num))))
|
|
900 |
(cexec-set-hashed-term target state-num))))
|
783 | 901 |
;;
|
784 | 902 |
new-state))
|
|
903 |
|
785 | 904 |
|
786 | 905 |
;;; *******************
|
787 | 906 |
;;; ONE STEP TRANSITION
|
|
790 | 909 |
;;; RWL-STATE-SET-TRANSITION-RULES
|
791 | 910 |
;;;
|
792 | 911 |
(defun rwl-state-set-transition-rules (state sch-context)
|
|
912 |
(declare (type rwl-state state)
|
|
913 |
(type rwl-sch-context sch-context))
|
793 | 914 |
(let ((rule-pats (find-matching-rules-for-exec (rwl-state-term state) sch-context)))
|
794 | 915 |
(setf (rwl-state-trans-rules state) rule-pats)
|
795 | 916 |
(unless rule-pats
|
|
799 | 920 |
;;; APPLY-RULE-CEXEC: rule target -> Bool
|
800 | 921 |
;;;
|
801 | 922 |
(defun apply-rule-cexec (rule term subst)
|
|
923 |
(declare (type rewrite-rule rule)
|
|
924 |
(type term term)
|
|
925 |
(type substitution subst))
|
802 | 926 |
(catch 'rule-failure
|
803 | 927 |
(progn
|
804 | 928 |
(term-replace-dd-simple
|
|
818 | 942 |
;;; - returns the list of substates which derived from the given state.
|
819 | 943 |
;;; - NOTE: term of the given state is NOT modified.
|
820 | 944 |
;;;
|
821 | |
(defun cexec-term-1 (dag sch-context) ; node-num ...
|
|
945 |
(defun cexec-term-1 (dag sch-context)
|
822 | 946 |
(declare (type rwl-sch-node dag)
|
823 | 947 |
(type rwl-sch-context sch-context)
|
824 | |
; (type fixnum node-num)
|
825 | |
)
|
826 | |
;;
|
|
948 |
(optimize (speed 3) (safety 0)))
|
827 | 949 |
(let* ((state (dag-node-datum dag))
|
828 | 950 |
(term (rwl-state-term state)))
|
829 | 951 |
(flet ((no-more-transition ()
|
|
974 | 1096 |
;;; each `last-siblings' & check if derived terms match to `pattern'.
|
975 | 1097 |
;;;
|
976 | 1098 |
(defun rwl-step-forward-1 (sch-context)
|
977 | |
(declare (type rwl-sch-context sch-context))
|
|
1099 |
(declare (type rwl-sch-context sch-context)
|
|
1100 |
(optimize (speed 3) (safety 0)))
|
978 | 1101 |
;; check # of transitions
|
979 | 1102 |
(when (>= (rwl-sch-context-trans-so-far sch-context)
|
980 | 1103 |
*cexec-limit*)
|
|
1075 | 1198 |
;;; *********
|
1076 | 1199 |
;;; TOP LEVEL functions
|
1077 | 1200 |
;;; *********
|
|
1201 |
(declaim (inline make-anything-is-ok-term))
|
1078 | 1202 |
(defun make-anything-is-ok-term ()
|
1079 | 1203 |
(make-variable-term *cosmos* (gensym "Univ")))
|
1080 | 1204 |
|
|
1084 | 1208 |
module
|
1085 | 1209 |
bind
|
1086 | 1210 |
if)
|
|
1211 |
(declare (type term t1 t2)
|
|
1212 |
(type fixnum max-result max-depth)
|
|
1213 |
(type (or null t) zero?))
|
1087 | 1214 |
(with-in-module (module)
|
1088 | 1215 |
(unless t2
|
1089 | 1216 |
(setq t2 (make-anything-is-ok-term)))
|
|
1148 | 1275 |
:max-depth max-depth
|
1149 | 1276 |
:state-predicate nil
|
1150 | 1277 |
:bind bind
|
1151 | |
:if if
|
1152 | |
:term-hash (make-hash-table :test #'equal
|
1153 | |
:rehash-size 1.5
|
1154 | |
:rehash-threshold 0.7)))
|
|
1278 |
:if if))
|
1155 | 1279 |
(root nil)
|
1156 | 1280 |
(res nil)
|
1157 | 1281 |
(no-more nil)
|
|
1189 | 1313 |
;; state equality predicate
|
1190 | 1314 |
(setf (rwl-sch-context-state-predicate sch-context) (make-state-pred-pat))
|
1191 | 1315 |
(let ((.rwl-sch-context. sch-context)
|
1192 | |
(.cexec-term-hash. (rwl-sch-context-term-hash sch-context))
|
1193 | 1316 |
(.rwl-search-depth. (1+ .rwl-search-depth.))
|
1194 | 1317 |
(.ignore-term-id-limit. t))
|
1195 | 1318 |
(declare (special .rwl-sch-context. .cexec.term-hash. .ignore-term-id-limit.))
|
1196 | 1319 |
(push sch-context .rwl-context-stack.)
|
|
1320 |
(init-rwl-term-hash .rwl-search-depth.)
|
1197 | 1321 |
;; the first state is 0
|
1198 | |
(set-sch-hashed-term (term-id t1) .cexec-term-hash. 0)
|
|
1322 |
(set-sch-hashed-term t1 .cexec-term-hash. 0)
|
1199 | 1323 |
;;
|
1200 | 1324 |
;; do the search
|
1201 | 1325 |
;;
|
|
1316 | 1440 |
(bind nil)
|
1317 | 1441 |
;; the followings are experimental
|
1318 | 1442 |
(if nil))
|
|
1443 |
(declare (type term term pattern)
|
|
1444 |
(type fixnum max-result max-depth)
|
|
1445 |
(type (or null t) zero? final?))
|
1319 | 1446 |
(let ((module (get-context-module))
|
1320 | 1447 |
max-r
|
1321 | 1448 |
max-d)
|
|
1449 |
(declare (type module module))
|
1322 | 1450 |
(if (integerp max-result)
|
1323 | 1451 |
(setq max-r max-result)
|
1324 | 1452 |
(if (term-is-builtin-constant? max-result)
|