-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Exercise_4_72.rkt
420 lines (370 loc) · 12.8 KB
/
Exercise_4_72.rkt
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
418
419
420
#lang racket/base
(require racket/stream)
(require racket/promise) ;; delay, force
(require racket/string) ;; string-append*(4.60)
;; for Exercise 4.60
;; https://docs.racket-lang.org/guide/eval.html#%28part._namespaces%29
(define-namespace-anchor a)
(define user-initial-environment (namespace-anchor->namespace a))
;; or use (make-base-namespace) as user-initial-environmen for `execute`
(define table (make-hash))
(define (put op type item)
(hash-set! table (list op type) item))
(define (get op type)
(if (hash-has-key? table (list op type))
(hash-ref table (list op type))
#f))
(define (prompt-for-input string)
(newline) (newline) (display string) (newline))
(define (announce-output string)
(newline) (display string) (newline))
(define input-prompt ";;; Query input:")
(define output-prompt ";;; Query results:")
(define (query-driver-loop)
(prompt-for-input input-prompt)
(let ([q (query-syntax-process (read))])
(cond [(assertion-to-be-added? q)
(add-rule-or-assertion! (add-assertion-body q))
(newline)
(display "Assertion added to data base.")
(query-driver-loop)]
[else
(announce-output output-prompt)
(display-stream
(stream-map
(lambda (frame)
(instantiate
q
frame
(lambda (v f)
(contract-question-mark v))))
(qeval q (singleton-stream '()))))
(query-driver-loop)])))
(define (instantiate exp frame unbound-var-handler)
(define (copy exp)
(cond [(var? exp)
(let ([binding (binding-in-frame exp frame)])
(if binding
(copy (binding-value binding))
(unbound-var-handler exp frame)))]
[(pair? exp)
(cons (copy (car exp)) (copy (cdr exp)))]
[else exp]))
(copy exp))
(define (qeval query frame-stream)
(let ([qproc (get (type query) 'qeval)])
(if qproc
(qproc (contents query) frame-stream)
(simple-query query frame-stream))))
(define (simple-query query-pattern frame-stream)
(stream-flatmap
(lambda (frame)
(stream-append-delayed
(find-assertions query-pattern frame)
(delay (apply-rules query-pattern frame))))
frame-stream))
(define (conjoin conjuncts frame-stream)
(if (empty-conjunction? conjuncts)
frame-stream
(conjoin (rest-conjuncts conjuncts)
(qeval (first-conjunct conjuncts) frame-stream))))
(put 'and 'qeval conjoin)
(define (disjoin disjuncts frame-stream)
(if (empty-disjunction? disjuncts)
empty-stream
(interleave-delayed
(qeval (first-disjunct disjuncts) frame-stream)
(delay (disjoin (rest-disjuncts disjuncts) frame-stream)))))
(put 'or 'qeval disjoin)
(define (negate operands frame-stream)
(stream-flatmap
(lambda (frame)
(if (stream-empty?
(qeval (negated-query operands)
(singleton-stream frame)))
(singleton-stream frame)
empty-stream))
frame-stream))
(put 'not 'qeval negate)
(define (lisp-value call frame-stream)
(stream-flatmap
(lambda (frame)
(if (execute
(instantiate
call
frame
(lambda (v f)
(error "Unknown pat var: LISP-VALUE" v))))
(singleton-stream frame)
empty-stream))
frame-stream))
(put 'lisp-value 'qeval lisp-value)
(define (execute exp)
(apply (eval (predicate exp) user-initial-environment)
(args exp)))
(define (always-true ignore frame-stream) frame-stream)
(put 'always-true 'qeval always-true)
(define (find-assertions pattern frame)
(stream-flatmap
(lambda (datum) (check-an-assertion datum pattern frame))
(fetch-assertions pattern frame)))
(define (check-an-assertion assertion query-pat query-frame)
(let ([match-result
(pattern-match query-pat assertion query-frame)])
(if (eq? match-result 'failed)
empty-stream
(singleton-stream match-result))))
(define (pattern-match pat dat frame)
(cond [(eq? frame 'failed) 'failed]
[(equal? pat dat) frame]
[(var? pat) (extend-if-consistent pat dat frame)]
[(and (pair? pat) (pair? dat))
(pattern-match
(cdr pat)
(cdr dat)
(pattern-match (car pat) (car dat) frame))]
[else 'failed]))
(define (extend-if-consistent var dat frame)
(let ([binding (binding-in-frame var frame)])
(if binding
(pattern-match (binding-value binding) dat frame)
(extend var dat frame))))
(define (apply-rules pattern frame)
(stream-flatmap (lambda (rule)
(apply-a-rule rule pattern frame))
(fetch-rules pattern frame)))
(define (apply-a-rule rule query-pattern query-frame)
(let ([clean-rule (rename-variables-in rule)])
(let ([unify-result (unify-match query-pattern
(conclusion clean-rule)
query-frame)])
(if (eq? unify-result 'failed)
empty-stream
(qeval (rule-body clean-rule)
(singleton-stream unify-result))))))
(define (rename-variables-in rule)
(let ([rule-application-id (new-rule-application-id)])
(define (tree-walk exp)
(cond [(var? exp)
(make-new-variable exp rule-application-id)]
[(pair? exp)
(cons (tree-walk (car exp))
(tree-walk (cdr exp)))]
[else exp]))
(tree-walk rule)))
(define (unify-match p1 p2 frame)
(cond [(eq? frame 'failed) 'failed]
[(equal? p1 p2) frame]
[(var? p1) (extend-if-possible p1 p2 frame)]
[(var? p2) (extend-if-possible p2 p1 frame)] ;; ***
[(and (pair? p1) (pair? p2))
(unify-match (cdr p1)
(cdr p2)
(unify-match (car p1)
(car p2)
frame))]
[else 'failed]))
(define (extend-if-possible var val frame)
(let ([binding (binding-in-frame var frame)])
(cond [binding
(unify-match (binding-value binding) val frame)]
[(var? val) ;; ***
(let ([binding (binding-in-frame val frame)])
(if binding
(unify-match
var (binding-value binding) frame)
(extend var val frame)))]
[(depends-on? val var frame) ;; ***
'failed]
[else (extend var val frame)])))
(define (depends-on? exp var frame)
(define (tree-walk e)
(cond [(var? e)
(if (equal? var e)
#t
(let ([b (binding-in-frame e frame)])
(if b
(tree-walk (binding-value b))
#f)))]
[(pair? e)
(or (tree-walk (car e))
(tree-walk (cdr e)))]
[else #f]))
(tree-walk exp))
(define THE-ASSERTIONS empty-stream)
(define (fetch-assertions pattern frame)
(if (use-index? pattern)
(get-indexed-assertions pattern)
(get-all-assertions)))
(define (get-all-assertions) THE-ASSERTIONS)
(define (get-indexed-assertions pattern)
(get-stream (index-key-of pattern) 'assertion-stream))
(define (get-stream key1 key2)
(let ([s (get key1 key2)])
(if s s empty-stream)))
(define THE-RULES empty-stream)
(define (fetch-rules pattern frame)
(if (use-index? pattern)
(get-indexed-rules pattern)
(get-all-rules)))
(define (get-all-rules) THE-RULES)
(define (get-indexed-rules pattern)
(stream-append
(get-stream (index-key-of pattern) 'rule-stream)
(get-stream '? 'rule-stream)))
(define (add-rule-or-assertion! assertion)
(if (rule? assertion)
(add-rule! assertion)
(add-assertion! assertion)))
(define (add-assertion! assertion)
(store-assertion-in-index assertion)
(let ([old-assertions THE-ASSERTIONS])
(set! THE-ASSERTIONS
(stream-cons assertion old-assertions))
'ok))
(define (add-rule! rule)
(store-rule-in-index rule)
(let ([old-rules THE-RULES])
(set! THE-RULES (stream-cons rule old-rules))
'ok))
(define (store-assertion-in-index assertion)
(when (indexable? assertion)
(let ([key (index-key-of assertion)])
(let ([current-assertion-stream
(get-stream key 'assertion-stream)])
(put key
'assertion-stream
(stream-cons
assertion
current-assertion-stream))))))
(define (store-rule-in-index rule)
(let ([pattern (conclusion rule)])
(when (indexable? pattern)
(let ([key (index-key-of pattern)])
(let ([current-rule-stream
(get-stream key 'rule-stream)])
(put key
'rule-stream
(stream-cons rule
current-rule-stream)))))))
(define (indexable? pat)
(or (constant-symbol? (car pat))
(var? (car pat))))
(define (index-key-of pat)
(let ([key (car pat)])
(if (var? key) '? key)))
(define (use-index? pat) (constant-symbol? (car pat)))
(define (stream-append-delayed s1 delayed-s2)
(if (stream-empty? s1)
(force delayed-s2)
(stream-cons
(stream-first s1)
(stream-append-delayed
(stream-rest s1)
delayed-s2))))
(define (interleave-delayed s1 delayed-s2)
(if (stream-empty? s1)
(force delayed-s2)
(stream-cons
(stream-first s1)
(interleave-delayed
(force delayed-s2)
(delay (stream-rest s1))))))
(define (stream-flatmap proc s)
(flatten-stream (stream-map proc s)))
(define (flatten-stream stream)
(if (stream-empty? stream)
empty-stream
(interleave-delayed
(stream-first stream)
(delay (flatten-stream (stream-rest stream))))))
(define (singleton-stream x)
(stream-cons x empty-stream))
(define (type exp)
(if (pair? exp)
(car exp)
(error "Unknown expression TYPE" exp)))
(define (contents exp)
(if (pair? exp)
(cdr exp)
(error "Unknown expression CONTENTS" exp)))
(define (assertion-to-be-added? exp)
(eq? (type exp) 'assert!))
(define (add-assertion-body exp)
(car (contents exp)))
(define (empty-conjunction? exps) (null? exps))
(define (first-conjunct exps) (car exps))
(define (rest-conjuncts exps) (cdr exps))
(define (empty-disjunction? exps) (null? exps))
(define (first-disjunct exps) (car exps))
(define (rest-disjuncts exps) (cdr exps))
(define (negated-query exps) (car exps))
(define (predicate exps) (car exps))
(define (args exps) (cdr exps))
(define (tagged-list? exp tag)
(if (pair? exp)
(eq? (car exp) tag)
#f))
(define (rule? statement)
(tagged-list? statement 'rule))
(define (conclusion rule) (cadr rule))
(define (rule-body rule)
(if (null? (cddr rule)) '(always-true) (caddr rule)))
(define (query-syntax-process exp)
(map-over-symbols expand-question-mark exp))
(define (map-over-symbols proc exp)
(cond [(pair? exp)
(cons (map-over-symbols proc (car exp))
(map-over-symbols proc (cdr exp)))]
[(symbol? exp) (proc exp)]
[else exp]))
(define (expand-question-mark symbol)
(let ([chars (symbol->string symbol)])
(if (string=? (substring chars 0 1) "?")
(list '?
(string->symbol
(substring chars 1 (string-length chars))))
symbol)))
(define (var? exp) (tagged-list? exp '?))
(define (constant-symbol? exp) (symbol? exp))
(define rule-counter 0)
(define (new-rule-application-id)
(set! rule-counter (+ 1 rule-counter))
rule-counter)
(define (make-new-variable var rule-application-id)
(cons '? (cons rule-application-id (cdr var))))
(define (contract-question-mark variable)
(string->symbol
(string-append "?"
(if (number? (cadr variable))
(string-append (symbol->string (caddr variable))
"-"
(number->string (cadr variable)))
(symbol->string (cadr variable))))))
(define (make-binding variable value)
(cons variable value))
(define (binding-variable binding) (car binding))
(define (binding-value binding) (cdr binding))
(define (binding-in-frame variable frame)
(assoc variable frame))
(define (extend variable value frame)
(cons (make-binding variable value) frame))
(define (display-stream s)
(stream-for-each displayln s))
(query-driver-loop)
;; If the first stream is infinite, append both streams
;; will only display the first stream.
(assert! (killed They Kenny))
(assert! (killed Randy Pooh))
(assert! (rule (killed ?x ?y) (killed ?y ?x)))
(or (killed Kenny ?who)
(killed Pooh ?who))
;; using interleave-delayed:
;;; Query results:
(or (killed Kenny They) (killed Pooh They))
(or (killed Kenny Randy) (killed Pooh Randy))
;; repeat infinitely
;; using stream-append
;;; Query results:
(or (killed Kenny They) (killed Pooh They))
;; repeat infinitely