-
Notifications
You must be signed in to change notification settings - Fork 0
/
paip-krep1.el
417 lines (359 loc) · 14.5 KB
/
paip-krep1.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
;;; paip-kere1.el
;; Copyright (C) 2014
;; Yosuke Funahashi <yosuke@funahashi.cc>
;;
;; This file is part of paip-el.
;;
;; paip-el is free software: you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; paip-el is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Foobar. If not, see <http://www.gnu.org/licenses/>.
;; [YF] I will comment out original text, keep them as is, and make
;; comments with [YF] marks.
;; [YF] This is the copyright description about the original code.
;; ;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*-
;; ;;; Code from Paradigms of Artificial Intelligence Programming
;; ;;; Copyright (c) 1991 Peter Norvig
;; ;;; krep1.lisp: Knowledge representation code; first version.
;; [YF] I use not paip-krep1 but paip-krep as the prefix for this
;; file, paip-krep2.el, and paip-kerep.el. This is because krep2 is an
;; update for krep1 and krep is an update for krep2. I'm not sure
;; about what this incosistency of a file name and prefix will cause
;; for ELM system. Let's see.
(eval-when-compile
(require 'cl-lib))
;; (requires "prolog")
(require 'paip-prolog)
;; ;;; ==============================
;; ;; An nlist is implemented as a (count . elements) pair:
;; (defun make-empty-nlist ()
;; "Create a new, empty nlist."
;; (cons 0 nil))
(defun paip-krep-make-empty-nlist ()
"Create a new, empty nlist."
(cons 0 nil))
;; (defun nlist-n (x) "The number of elements in an nlist." (car x))
;; (defun nlist-list (x) "The elements in an nlist." (cdr x))
(defun paip-krep-nlist-n (x)
"The number of elements in an nlist."
(car x))
(defun paip-krep-nlist-list (x)
"The elements in an nlist."
(cdr x))
;; (defun nlist-push (item nlist)
;; "Add a new element to an nlist."
;; (incf (car nlist))
;; (push item (cdr nlist))
;; nlist)
(defun paip-krep-nlist-push (item nlist)
"Add a new element to an nlist."
(cl-incf (car nlist))
(push item (cdr nlist))
nlist)
;; ;;; ==============================
;; (defstruct (dtree (:type vector))
;; (first nil) (rest nil) (atoms nil) (var (make-empty-nlist)))
(cl-defstruct (paip-krep-dtree (:type vector))
(first nil) (rest nil) (atoms nil) (var (paip-krep-make-empty-nlist)))
;; ;;; ==============================
;; ;; Not all Lisps handle the closure properly, so change the local PREDICATES
;; ;; to a global *predicates* - norvig Jun 11 1996
;; (defvar *predicates* nil)
(defvar paip-krep-*predicates* nil)
;; (defun get-dtree (predicate)
;; "Fetch (or make) the dtree for this predicate."
;; (cond ((get predicate 'dtree))
;; (t (push predicate *predicates*)
;; (setf (get predicate 'dtree) (make-dtree)))))
(defun paip-krep-get-dtree (predicate)
"Fetch (or make) the dtree for this predicate."
(cond ((get predicate 'dtree))
(t (push predicate paip-krep-*predicates*)
(setf (get predicate 'dtree)
(make-paip-krep-dtree)))))
;; (defun clear-dtrees ()
;; "Remove all the dtrees for all the predicates."
;; (dolist (predicate *predicates*)
;; (setf (get predicate 'dtree) nil))
;; (setf *predicates* nil))
(defun paip-krep-clear-dtrees ()
"Remove all the dtrees for all the predicates."
(dolist (predicate paip-krep-*predicates*)
(setf (get predicate 'dtree) nil))
(setf paip-krep-*predicates* nil))
;; ;;; ==============================
;; (defun index (key)
;; "Store key in a dtree node. Key must be (predicate . args);
;; it is stored in the predicate's dtree."
;; (dtree-index key key (get-dtree (predicate key))))
(defun paip-krep-index (key)
"Store key in a dtree node. Key must be (predicate . args);
it is stored in the predicate's dtree."
(paip-krep-dtree-index key key
(paip-krep-get-dtree
(paip-prolog-predicate key))))
;; (defun dtree-index (key value dtree)
;; "Index value under all atoms of key in dtree."
;; (cond
;; ((consp key) ; index on both first and rest
;; (dtree-index (first key) value
;; (or (dtree-first dtree)
;; (setf (dtree-first dtree) (make-dtree))))
;; (dtree-index (rest key) value
;; (or (dtree-rest dtree)
;; (setf (dtree-rest dtree) (make-dtree)))))
;; ((null key)) ; don't index on nil
;; ((variable-p key) ; index a variable
;; (nlist-push value (dtree-var dtree)))
;; (t ;; Make sure there is an nlist for this atom, and add to it
;; (nlist-push value (lookup-atom key dtree)))))
(defun paip-krep-dtree-index (key value dtree)
"Index value under all atoms of key in dtree."
(cond
((consp key) ; index on both first and rest
(paip-krep-dtree-index
(first key) value
(or (paip-krep-dtree-first dtree)
(setf (paip-krep-dtree-first dtree)
(make-paip-krep-dtree))))
(paip-krep-dtree-index
(rest key) value
(or (paip-krep-dtree-rest dtree)
(setf (paip-krep-dtree-rest dtree)
(make-paip-krep-dtree)))))
((null key)) ; don't index on nil
((paip-variable-p key) ; index a variable
(paip-krep-nlist-push
value (paip-krep-dtree-var dtree)))
(t ;; Make sure there is an nlist for this atom, and add to it
(paip-krep-nlist-push value
(paip-krep-lookup-atom key dtree)))))
;; (defun lookup-atom (atom dtree)
;; "Return (or create) the nlist for this atom in dtree."
;; (or (lookup atom (dtree-atoms dtree))
;; (let ((new (make-empty-nlist)))
;; (push (cons atom new) (dtree-atoms dtree))
;; new)))
(defun paip-krep-lookup-atom (atom dtree)
"Return (or create) the nlist for this atom in dtree."
(or (paip-lookup atom
(paip-krep-dtree-atoms dtree))
(lexical-let
((new (paip-krep-make-empty-nlist)))
(push (cons atom new)
(paip-krep-dtree-atoms dtree))
new)))
;; ;;; ==============================
;; (defun test-index ()
;; (let ((props '((p a b) (p a c) (p a ?x) (p b c)
;; (p b (f c)) (p a (f . ?x)))))
;; (clear-dtrees)
;; (mapc #'index props)
;; (write (list props (get-dtree 'p))
;; :circle t :array t :pretty t)
;; (values)))
(defun paip-krep-test-index ()
(let ((props '((p a b) (p a c) (p a \?x) (p b c)
(p b (f c)) (p a (f . \?x)))))
(paip-krep-clear-dtrees)
(mapc 'paip-krep-index props)
(let ((print-circle t))
(paipx-message
(pp (list props (paip-krep-get-dtree 'p)))))
(cl-values)))
;; ;;; ==============================
;; (defun fetch (query)
;; "Return a list of buckets potentially matching the query,
;; which must be a relation of form (predicate . args)."
;; (dtree-fetch query (get-dtree (predicate query))
;; nil 0 nil most-positive-fixnum))
(defun paip-krep-fetch (query)
"Return a list of buckets potentially matching the query,
which must be a relation of form (predicate . args)."
(paip-krep-dtree-fetch query
(paip-krep-get-dtree (paip-prolog-predicate query))
nil 0 nil most-positive-fixnum))
;; ;;; ==============================
;; (defun dtree-fetch (pat dtree var-list-in var-n-in best-list best-n)
;; "Return two values: a list-of-lists of possible matches to pat,
;; and the number of elements in the list-of-lists."
;; (if (or (null dtree) (null pat) (variable-p pat))
;; (values best-list best-n)
;; (let* ((var-nlist (dtree-var dtree))
;; (var-n (+ var-n-in (nlist-n var-nlist)))
;; (var-list (if (null (nlist-list var-nlist))
;; var-list-in
;; (cons (nlist-list var-nlist)
;; var-list-in))))
;; (cond
;; ((>= var-n best-n) (values best-list best-n))
;; ((atom pat) (dtree-atom-fetch pat dtree var-list var-n
;; best-list best-n))
;; (t (multiple-value-bind (list1 n1)
;; (dtree-fetch (first pat) (dtree-first dtree)
;; var-list var-n best-list best-n)
;; (dtree-fetch (rest pat) (dtree-rest dtree)
;; var-list var-n list1 n1)))))))
(defun paip-krep-dtree-fetch (pat dtree var-list-in var-n-in best-list best-n)
"Return two values: a list-of-lists of possible matches to pat,
and the number of elements in the list-of-lists."
(paipx-message
(format "\nfetch:\n pat=%s\n dtree=%s\n var-list-n=%s\n var-n-in=%s\n best-list=%s\n best-n=%s\n"
pat dtree var-list-in var-n-in best-list best-n))
(lexical-let ((pat pat)
(dtree dtree)
(var-list-in var-list-in)
(var-n-in var-n-in)
(best-list best-list)
(best-n best-n))
(if (or (null dtree) (null pat) (paip-variable-p pat))
(progn
(paipx-message (format "\nfetch-return1: (%s %s)\n" best-list best-n))
(cl-values best-list best-n))
(lexical-let* ((var-nlist (paip-krep-dtree-var dtree))
(var-n (+ var-n-in (paip-krep-nlist-n var-nlist)))
(var-list (if (null (paip-krep-nlist-list var-nlist))
var-list-in
(cons (paip-krep-nlist-list var-nlist)
var-list-in))))
(cond
((>= var-n best-n)
(progn
(paipx-message (format "\nfetch-return2: (%s %s)\n" best-list best-n))
(cl-values best-list best-n)))
((atom pat) (paip-krep-dtree-atom-fetch
pat dtree var-list var-n
best-list best-n))
(t (cl-multiple-value-bind (list1 n1)
(paip-krep-dtree-fetch
(first pat) (paip-krep-dtree-first dtree)
var-list var-n best-list best-n)
(paip-krep-dtree-fetch
(rest pat) (paip-krep-dtree-rest dtree)
var-list var-n list1 n1))))))))
;; (defun dtree-atom-fetch (atom dtree var-list var-n best-list best-n)
;; "Return the answers indexed at this atom (along with the vars),
;; or return the previous best answer, if it is better."
;; (let ((atom-nlist (lookup atom (dtree-atoms dtree))))
;; (cond
;; ((or (null atom-nlist) (null (nlist-list atom-nlist)))
;; (values var-list var-n))
;; ((and atom-nlist (< (incf var-n (nlist-n atom-nlist)) best-n))
;; (values (cons (nlist-list atom-nlist) var-list) var-n))
;; (t (values best-list best-n)))))
(defun paip-krep-dtree-atom-fetch (atom dtree var-list var-n best-list best-n)
"Return the answers indexed at this atom (along with the vars),
or return the previous best answer, if it is better."
(paipx-message
(format "\nfetch-atom:\n atom=%s\n dtree=%s\n var-list=%s\n var-n=%s\n best-list=%s\n best-n=%s\n"
atom dtree var-list var-n best-list best-n))
(lexical-let
((atom atom)
(dtree dtree)
(var-list var-list)
(var-n var-n)
(best-list best-list)
(best-n best-n))
(lexical-let ((atom-nlist (paip-lookup
atom
(paip-krep-dtree-atoms dtree))))
(cond
((or (null atom-nlist) (null (paip-krep-nlist-list atom-nlist)))
(paipx-message (format "\nfetch-atom-return1: var-list=%s, var-n=%s)\n" var-list var-n))
(cl-values var-list var-n))
((and atom-nlist
(< (cl-incf var-n (paip-krep-nlist-n atom-nlist)) best-n))
(lexical-let ((new-var-list
(cons (paip-krep-nlist-list atom-nlist) var-list)))
(paipx-message (format "\nfetch-atom-return2: var-list=%s, var-n=%s)\n" new-var-list var-n))
(cl-values new-var-list var-n)))
(t
(paipx-message (format "\nfetch-atom-return3: best-list=%s, best-n=%s)\n" best-list best-n))
(cl-values best-list best-n))))))
;; ;;; ==============================
;; (proclaim '(inline mapc-retrieve))
(cl-proclaim '(inline paip-krep-mapc-retrieve))
;; (defun mapc-retrieve (fn query)
;; "For every fact that matches the query,
;; apply the function to the binding list."
;; (dolist (bucket (fetch query))
;; (dolist (answer bucket)
;; (let ((bindings (unify query answer)))
;; (unless (eq bindings fail)
;; (funcall fn bindings))))))
(defun paip-krep-mapc-retrieve (fn query)
"For every fact that matches the query,
apply the function to the binding list."
(cl-dolist (bucket (car (paip-krep-fetch query))) ; [YF] fetch
; returns pseudo
; multiple value.
(cl-dolist (answer bucket)
(paipx-message (format "\nmapc-retrieve: answer=%s" answer))
(let ((bindings (paip-unify-unify query answer)))
(unless (eq bindings paip-fail)
(funcall fn bindings))))))
;; ;;; ==============================
;; (defun retrieve (query)
;; "Find all facts that match query. Return a list of bindings."
;; (let ((answers nil))
;; (mapc-retrieve #'(lambda (bindings) (push bindings answers))
;; query)
;; answers))
(defun paip-krep-retrieve (query)
"Find all facts that match query. Return a list of bindings."
(let ((answers nil))
(paip-krep-mapc-retrieve
(lambda (bindings)
(push bindings answers))
query)
answers))
;; (defun retrieve-matches (query)
;; "Find all facts that match query.
;; Return a list of expressions that match the query."
;; (mapcar #'(lambda (bindings) (subst-bindings bindings query))
;; (retrieve query)))
(defun paip-krep-retrieve-matches (query)
"Find all facts that match query.
Return a list of expressions that match the query."
(mapcar (lambda (bindings)
(paip-unify-subst-bindings bindings query))
(paip-krep-retrieve query)))
;; ;;; ==============================
;; (defmacro query-bind (variables query &body body)
;; "Execute the body for each match to the query.
;; Within the body, bind each variable."
;; (let* ((bindings (gensym "BINDINGS"))
;; (vars-and-vals
;; (mapcar
;; #'(lambda (var)
;; (list var `(subst-bindings ,bindings ',var)))
;; variables)))
;; `(mapc-retrieve
;; #'(lambda (,bindings)
;; (let ,vars-and-vals
;; ,@body))
;; ,query)))
(cl-defmacro paip-krep-query-bind (variables query &body body)
"Execute the body for each match to the query.
Within the body, bind each variable."
(let* ((bindings (cl-gensym "BINDINGS"))
(vars-and-vals
(mapcar
(lambda (var)
(list var `(paip-unify-subst-bindings ,bindings ',var)))
variables)))
`(paip-krep-mapc-retrieve
(lambda (,bindings)
(let ,vars-and-vals
,@body))
,query)))
(provide 'paip-krep1)
;;; paip-krep1.el ends here