Chapter 4, Metalinguistic Abstraction

Exercise 4.77


Interesting but it took almost the whole day!

Few points to understand the solution:

  • Implementing only for the queries like not and lisp-value. As suggested in problem we can call them filters.
    • A filter always need all the values to be bounded(like not or lisp-value, unlike unique)
    • The result of a filter, when applied to a frame, never adds anything to the resulting frame. The filter either returns the frame without any change or it returns 'failed.
    • Ofcourse filter internally might add items in the frame but the returned result won’t contain any change.
  • Modified the frame to contain promise as well as bindings. Added selectors for the same.
  • Modifying frame might look a big change but abstraction of frame(via selectors) enabled me to easily carry out this task by modifying the frame selectors.
  • There can be more than one promises(because of multiple not or multiple lisp-value in the query).
  • The order in which the promised not or lisp-value are evaluated does not effect the result. It may change the order of result but number of results and individual items won’t change.
  • My implementation does not guarantee any order in which promises for not or lisp-value would be evaluated.
  • Since, the requirement is to evaluate the promise as soon as possible. The best place to try to evaluate a promise is when we extend a frame by a new variable!
  • A promise is a pair of two things
    1. list of variables in the query which is delayed/promised.
    2. procedure to execute when the above variables become available to check the filter condition. Note that this procedure is just a predicate.
  • keeping list of variables in a promise is a small optimization so that we need not to extract variables from the pattern every time we check if the all variables are bounded or not.
  • The bounded? procedure is implemented to work for any pattern and thus can work for list of variables. Note that we need to recursively check for bounds for cases like ?x bound to (a ?y) where ?y might not be bounded.
  • When evaluating a promise, make sure the frame does not contain any old promises else we can go into a loop of evaluating promises. Check comment in procedure execute-promises. This took me quite some time to debug.
  • When printing force evaluate all the promises.
  • My changes are on top of the changes done in loop-detector.

Code

Here are the main changes/additions for this exercise. Complete code is presented at the bottom of this page.

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
(define (variables pattern)
  (cond ((null? pattern) '())
		((var? pattern) (cons pattern '()))
		((pair? pattern) (append (variables (car pattern))
								 (variables (cdr pattern))))
		(else '())))

(define (bounded? pattern frame)
  (cond ((var? pattern)
		 (let ((binding (binding-in-frame pattern frame)))
		   (and binding
				(bounded? (binding-value binding)
						  frame))))
		((pair? pattern)
		 (and (bounded? (car pattern) frame)
			  (bounded? (cdr pattern) frame)))
		(else #t)))

(define (execute-promises forced frame)
  (let ((frame-without-promises (make-frame (frame-bindings frame) '())))
	(define (iter promises delayed-promises)
	  (if (null? promises)
		  (make-frame (frame-bindings frame)
					  delayed-promises)
		  (let ((promise (first-promise promises))
				(rem-promises (rest-promises promises)))
			(if (or forced
					(bounded? (promise-variables promise) frame))
				(let ((pred? (promise-predicate promise)))
				  ;;evaluate pred? with frame without promises
				  ;;else it will try again to evaluate the promise again
				  ;;and again in a loop.
				  (if (pred? frame-without-promises)
					  (iter rem-promises delayed-promises)
					  'failed))
				(iter rem-promises
					  (extend-promises promise delayed-promises))))))
  
  (iter (frame-promises frame) '())))

(define (stream-apply-promises forced frame-stream)
  (simple-stream-flatmap
   (lambda (frame)
	 (let ((result-frame (execute-promises forced frame)))
	   (if (eq? 'failed result-frame)
		   the-empty-stream
		   (singleton-stream result-frame))))
   frame-stream))

(define (stream-add-or-apply-promise promise frame-stream)
  (stream-apply-promises
   #f
   (stream-map (lambda(frame)
				 (make-frame (frame-bindings frame)
							 (extend-promises promise
											  (frame-promises frame))))
			   frame-stream)))

(define (negate operands frame-stream history)
  (stream-add-or-apply-promise
   (make-promise
	(variables operands)
	(lambda (frame)
	  (stream-null? (qeval (negated-query operands)
						   (singleton-stream frame)
						   history))
	  ))
   frame-stream))

;;(put 'not 'qeval negate)

(define (lisp-value call frame-stream history)
  (stream-add-or-apply-promise
   (make-promise
	(variables call)
	(lambda (frame)
      (execute (instantiate
				call
				frame
				(lambda (v f)
				  (error "Unknown pat var -- LISP-VALUE" v))))))
   frame-stream))

;;;Frames and bindings

(define (make-frame bindings promises)
  (cons bindings promises))

(define (frame-bindings frame) (car frame))

(define (frame-promises frame) (cdr frame))

(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-bindings frame)))

(define (extend-bindings var val bindings)
  (cons (make-binding var val) bindings))

(define (make-promise vars pred?) (cons vars pred?))

(define (promise-variables promise) (car promise))

(define (promise-predicate promise) (cdr promise))

(define (extend-promises promise promises)
  (cons promise promises))

(define (first-promise promises) (car promises))

(define (rest-promises promises) (cdr promises))

(define (extend variable value frame)
  (execute-promises #f
					(make-frame (extend-bindings variable
												 value
												 (frame-bindings frame))
								(frame-promises frame))))

Test/Example

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
;;; Example from book where the problems in `not` were discussed.
;;; Query input:
(and (not (job ?x (computer programmer)))
     (supervisor ?x ?y))

;;; Query results:
(and (not (job (aull dewitt) (computer programmer))) (supervisor (aull dewitt) (warbucks oliver)))
(and (not (job (cratchet robert) (computer programmer))) (supervisor (cratchet robert) (scrooge eben)))
(and (not (job (scrooge eben) (computer programmer))) (supervisor (scrooge eben) (warbucks oliver)))
(and (not (job (bitdiddle ben) (computer programmer))) (supervisor (bitdiddle ben) (warbucks oliver)))
(and (not (job (reasoner louis) (computer programmer))) (supervisor (reasoner louis) (hacker alyssa p)))
(and (not (job (tweakit lem e) (computer programmer))) (supervisor (tweakit lem e) (bitdiddle ben)))


;; Taken from ex-4.57. Moved 'not' to the beginning for testing.

;;; Query input:

(assert!
 (rule (can-replace ?per1 ?per2)
	   (and (not (same ?per1 ?per2))
			(job ?per1 ?job1)
			(job ?per2 ?job2)
			(or (same ?job1 ?job2)
				(can-do-job ?job1 ?job2)))))

Assertion added to data base.

;;; Query input:
(can-replace ?per1 (Fect Cy D))

;;; Query results:
(can-replace (hacker alyssa p) (fect cy d))
(can-replace (bitdiddle ben) (fect cy d))



;;this example taken from ex-4.56
;;Changing position of lisp-value (i) first in the normal position and then (ii) in the first clause
;;; Query input:
(and (salary (Bitdiddle Ben) ?ben-sal) (and (salary ?name ?sal) (lisp-value < ?sal ?ben-sal)))


;;; Query results:
(and (salary (bitdiddle ben) 60000) (and (salary (aull dewitt) 25000) (lisp-value < 25000 60000)))
(and (salary (bitdiddle ben) 60000) (and (salary (cratchet robert) 18000) (lisp-value < 18000 60000)))
(and (salary (bitdiddle ben) 60000) (and (salary (reasoner louis) 30000) (lisp-value < 30000 60000)))
(and (salary (bitdiddle ben) 60000) (and (salary (tweakit lem e) 25000) (lisp-value < 25000 60000)))
(and (salary (bitdiddle ben) 60000) (and (salary (fect cy d) 35000) (lisp-value < 35000 60000)))
(and (salary (bitdiddle ben) 60000) (and (salary (hacker alyssa p) 40000) (lisp-value < 40000 60000)))

;;Changed the position of lisp-value from the previous example.
;;; Query input:

(and (salary (Bitdiddle Ben) ?ben-sal) (and (lisp-value < ?sal ?ben-sal) (salary ?name ?sal)))

;;; Query results:
(and (salary (bitdiddle ben) 60000) (and (lisp-value < 25000 60000) (salary (aull dewitt) 25000)))
(and (salary (bitdiddle ben) 60000) (and (lisp-value < 18000 60000) (salary (cratchet robert) 18000)))
(and (salary (bitdiddle ben) 60000) (and (lisp-value < 30000 60000) (salary (reasoner louis) 30000)))
(and (salary (bitdiddle ben) 60000) (and (lisp-value < 25000 60000) (salary (tweakit lem e) 25000)))
(and (salary (bitdiddle ben) 60000) (and (lisp-value < 35000 60000) (salary (fect cy d) 35000)))
(and (salary (bitdiddle ben) 60000) (and (lisp-value < 40000 60000) (salary (hacker alyssa p) 40000)))


Complete code

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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
;tom;;;QUERY SYSTEM FROM SECTION 4.4.4 OF
;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS

;;;;Matches code in ch4.scm
;;;;Includes:
;;;;  -- supporting code from 4.1, chapter 3, and instructor's manual
;;;;  -- data base from Section 4.4.1 -- see microshaft-data-base below

;;;;This file can be loaded into Scheme as a whole.
;;;;In order to run the query system, the Scheme must support streams.

;;;;NB. PUT's are commented out and no top-level table is set up.
;;;;Instead use initialize-data-base (from manual), supplied in this file.


;;;SECTION 4.4.4.1
;;;The Driver Loop and Instantiation

(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
           (newline)
           (display output-prompt)
           ;; [extra newline at end] (announce-output output-prompt)
           (display-stream
            (stream-map
             (lambda (frame)
               (instantiate q
                            frame
                            (lambda (v f)
                              (contract-question-mark v))))
             (stream-apply-promises
			  #t
			  (qeval q (singleton-stream (make-frame '() '())) (make-empty-history)))))
           (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))


;;;SECTION 4.4.4.2
;;;The Evaluator

(define (qeval query frame-stream history)
  (let ((qproc (get (type query) 'qeval)))
    (if qproc
        (qproc (contents query) frame-stream history)
        (simple-query query frame-stream history))))

;;;Simple queries

(define (simple-query query-pattern frame-stream history)
  (stream-flatmap
   (lambda (frame)
     (stream-append-delayed
      (find-assertions query-pattern frame)
      (delay (apply-rules query-pattern frame history))))
   frame-stream))

;;;Compound queries

(define (conjoin conjuncts frame-stream history)
  (if (empty-conjunction? conjuncts)
      frame-stream
      (conjoin (rest-conjuncts conjuncts)
               (qeval (first-conjunct conjuncts)
                      frame-stream
					  history)
			   history)))

;; (define (optimized-conjoin conjuncts frame-stream)
;;   (define (iter conjuncts merged-stream)
;; 	(if (empty-conjunction? conjuncts)
;; 		merged-stream
;; 		(iter (rest-conjuncts conjuncts)
;; 			  (merge-frame-stream
;; 			   (qeval (first-conjunct conjuncts)
;; 					  frame-stream)
;; 			   merged-stream))))
  
;;   (stream-filter
;;    (lambda (frame) (not (eq? frame 'failed)))
;;    (iter conjuncts frame-stream)))

;; (define (merge-frame-stream stream1 stream2)
;;   (stream-flatmap (lambda (frame1)
;; 					(stream-map (lambda (frame2)
;; 								  (merge-frames frame1 frame2))
;; 								stream2))
;; 				  stream1))

;; (define (merge-frames frame1 frame2)
;;   (cond ((or (eq? 'failed frame1)
;; 			 (eq? 'failed frame2))
;; 		 'failed)
;; 		((null? frame1) frame2) 
;; 		(else (merge-frames (cdr frame1)
;; 								   (extend-if-possible (caar frame1)
;; 													   (cdar frame1)
;; 													   frame2)))))
;;(put 'and 'qeval conjoin)


(define (disjoin disjuncts frame-stream history)
  (if (empty-disjunction? disjuncts)
      the-empty-stream
      (interleave-delayed
       (qeval (first-disjunct disjuncts) frame-stream history)
       (delay (disjoin (rest-disjuncts disjuncts)
                       frame-stream
					   history)))))

;;(put 'or 'qeval disjoin)

;;;Filters

(define (unique-query exps) (car exps))

(define (uniquely-asserted operands frame-stream history)
  (simple-stream-flatmap
   (lambda (frame)
	 (let ((matched-frame-stream (qeval (unique-query operands)
										(singleton-stream frame)
										history)))
	   (if (or (stream-null? matched-frame-stream)
			   (not (stream-null? (stream-cdr matched-frame-stream))))
		   the-empty-stream
		   matched-frame-stream)))
   frame-stream))

;; (put 'unique 'qeval uniquely-asserted)

;; ex-4.77

(define (variables pattern)
  (cond ((null? pattern) '())
		((var? pattern) (cons pattern '()))
		((pair? pattern) (append (variables (car pattern))
								 (variables (cdr pattern))))
		(else '())))

(define (bounded? pattern frame)
  (cond ((var? pattern)
		 (let ((binding (binding-in-frame pattern frame)))
		   (and binding
				(bounded? (binding-value binding)
						  frame))))
		((pair? pattern)
		 (and (bounded? (car pattern) frame)
			  (bounded? (cdr pattern) frame)))
		(else #t)))

(define (execute-promises forced frame)
  (let ((frame-without-promises (make-frame (frame-bindings frame) '())))
	(define (iter promises delayed-promises)
	  (if (null? promises)
		  (make-frame (frame-bindings frame)
					  delayed-promises)
		  (let ((promise (first-promise promises))
				(rem-promises (rest-promises promises)))
			(if (or forced
					(bounded? (promise-variables promise) frame))
				(let ((pred? (promise-predicate promise)))
				  ;;evaluate pred? with frame without promises
				  ;;else it will try again to evaluate the promise again
				  ;;and again in a loop.
				  (if (pred? frame-without-promises)
					  (iter rem-promises delayed-promises)
					  'failed))
				(iter rem-promises
					  (extend-promises promise delayed-promises))))))
  
  (iter (frame-promises frame) '())))

(define (stream-apply-promises forced frame-stream)
  (simple-stream-flatmap
   (lambda (frame)
	 (let ((result-frame (execute-promises forced frame)))
	   (if (eq? 'failed result-frame)
		   the-empty-stream
		   (singleton-stream result-frame))))
   frame-stream))

(define (stream-add-or-apply-promise promise frame-stream)
  (stream-apply-promises
   #f
   (stream-map (lambda(frame)
				 (make-frame (frame-bindings frame)
							 (extend-promises promise
											  (frame-promises frame))))
			   frame-stream)))

(define (negate operands frame-stream history)
  (stream-add-or-apply-promise
   (make-promise
	(variables operands)
	(lambda (frame)
	  (stream-null? (qeval (negated-query operands)
						   (singleton-stream frame)
						   history))
	  ))
   frame-stream))

;;(put 'not 'qeval negate)

(define (lisp-value call frame-stream history)
  (stream-add-or-apply-promise
   (make-promise
	(variables call)
	(lambda (frame)
      (execute (instantiate
				call
				frame
				(lambda (v f)
				  (error "Unknown pat var -- LISP-VALUE" v))))))
   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 history) frame-stream)

;;(put 'always-true 'qeval always-true)

;;;SECTION 4.4.4.3
;;;Finding Assertions by Pattern Matching

(define (find-assertions pattern frame)
  (simple-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)
        the-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))))

;;;history maintenance for loop detector
;;;in ex-4.67

(define (make-empty-history) '())

(define (make-history-item pattern frame)
  (instantiate pattern
			   frame
			   (lambda (v f) '??)))

(define (in-history? item history)
  (if (null? history)
	  #f
	  (if (equal? item (car history))
		  #t
		  (in-history? item (cdr history)))))

(define (add-item-in-history item history)
  (cons item history))

(define (print-items . items)
  (newline)
  (display items))

;; (define (add-in-history! pattern frame history)
;;   (let ((history-item (make-history-item pattern frame)))
;; 	(let ((pattern (car history-item))
;; 		  (val (cdr history-item)))
;; 	  (let ((cons-containing-pattern (assoc pattern history)))
;; 		(if cons-containing-pattern
;; 			(let ((instantiated-values (cdr cons-containing-pattern)))
;; 			  (if (memq val instantiated-values)
;; 				  #f
;; 				  (begin
;; 					(set-cdr! cons-containing-pattern
;; 							  (cons val instantiated-values))
;; 					history)))
;; 			(cons
;; 			 (cons pattern
;; 				   (cons val '()))
;; 			 history))))))

;;end of history maintenance

;;;SECTION 4.4.4.4
;;;Rules and Unification

(define (apply-rules pattern frame history)
  (stream-flatmap (lambda (rule)
                    (apply-a-rule rule pattern frame history))
                  (fetch-rules pattern frame)))

(define (apply-a-rule rule query-pattern query-frame history)
  (let ((clean-rule (rename-variables-in rule)))
    (let ((unify-result
           (unify-match query-pattern
                        (conclusion clean-rule)
                        query-frame)))
      (if (eq? unify-result 'failed)
          the-empty-stream
		  (let ((new-history-item
				 (make-history-item (conclusion clean-rule)
									unify-result)))
			(if (in-history? new-history-item history)
				(begin
				  (print-items "Loop Detected!" " the history item " new-history-item " is repeated" history)
				  the-empty-stream)
				(qeval (rule-body clean-rule)
					   (singleton-stream unify-result)
					   (add-item-in-history new-history-item history))))))))

(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)) ; {\em ; ***}
        ((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)                     ; {\em ; ***}
           (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)    ; {\em ; ***}
           'failed)
          (else (extend var val frame)))))

(define (depends-on? exp var frame)
  (define (tree-walk e)
    (cond ((var? e)
           (if (equal? var e)
               true
               (let ((b (binding-in-frame e frame)))
                 (if b
                     (tree-walk (binding-value b))
                     false))))
          ((pair? e)
           (or (tree-walk (car e))
               (tree-walk (cdr e))))
          (else false)))
  (tree-walk exp))

;;;SECTION 4.4.4.5
;;;Maintaining the Data Base

(define THE-ASSERTIONS the-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 the-empty-stream)))

(define THE-RULES the-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
          (cons-stream assertion old-assertions))
    'ok))

(define (add-rule! rule)
  (store-rule-in-index rule)
  (let ((old-rules THE-RULES))
    (set! THE-RULES (cons-stream rule old-rules))
    'ok))

(define (store-assertion-in-index assertion)
  (if (indexable? assertion)
      (let ((key (index-key-of assertion)))
        (let ((current-assertion-stream
               (get-stream key 'assertion-stream)))
          (put key
               'assertion-stream
               (cons-stream assertion
                            current-assertion-stream))))))

(define (store-rule-in-index rule)
  (let ((pattern (conclusion rule)))
    (if (indexable? pattern)
        (let ((key (index-key-of pattern)))
          (let ((current-rule-stream
                 (get-stream key 'rule-stream)))
            (put key
                 'rule-stream
                 (cons-stream 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)))

;;;SECTION 4.4.4.6
;;;Stream operations

(define (stream-append-delayed s1 delayed-s2)
  (if (stream-null? s1)
      (force delayed-s2)
      (cons-stream
       (stream-car s1)
       (stream-append-delayed (stream-cdr s1) delayed-s2))))

(define (interleave-delayed s1 delayed-s2)
  (if (stream-null? s1)
      (force delayed-s2)
      (cons-stream
       (stream-car s1)
       (interleave-delayed (force delayed-s2)
                           (delay (stream-cdr s1))))))

(define (stream-flatmap proc s)
  (flatten-stream (stream-map proc s)))

(define (flatten-stream stream)
  (if (stream-null? stream)
      the-empty-stream
      (interleave-delayed
       (stream-car stream)
       (delay (flatten-stream (stream-cdr stream))))))

(define simple-stream-flatmap stream-flatmap)

;; (define (simple-stream-flatmap proc s)
;;   (simple-flatten (stream-map proc s)))

;; (define (simple-flatten stream)
;;   (stream-map stream-car
;;               (stream-filter (lambda (s)
;;                                (not (stream-null? s)))
;;                              stream)))

(define (singleton-stream x)
  (cons-stream x the-empty-stream))


;;;SECTION 4.4.4.7
;;;Query syntax procedures

(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 (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))))))


;;;SECTION 4.4.4.8
;;;Frames and bindings

(define (make-frame bindings promises)
  (cons bindings promises))

(define (frame-bindings frame) (car frame))

(define (frame-promises frame) (cdr frame))

(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-bindings frame)))

(define (extend-bindings var val bindings)
  (cons (make-binding var val) bindings))

(define (make-promise vars pred?) (cons vars pred?))

(define (promise-variables promise) (car promise))

(define (promise-predicate promise) (cdr promise))

(define (extend-promises promise promises)
  (cons promise promises))

(define (first-promise promises) (car promises))

(define (rest-promises promises) (cdr promises))

(define (extend variable value frame)
  (execute-promises #f
					(make-frame (extend-bindings variable
												 value
												 (frame-bindings frame))
								(frame-promises frame))))



;;;;From Section 4.1

(define (tagged-list? exp tag)
  (if (pair? exp)
      (eq? (car exp) tag)
      false))

(define (prompt-for-input string)
  (newline) (newline) (display string) (newline))


;;;;Stream support from Chapter 3

(define (stream-map proc s)
  (if (stream-null? s)
      the-empty-stream
      (cons-stream (proc (stream-car s))
                   (stream-map proc (stream-cdr s)))))

(define (stream-for-each proc s)
  (if (stream-null? s)
      'done
      (begin (proc (stream-car s))
             (stream-for-each proc (stream-cdr s)))))

(define (display-stream s)
  (stream-for-each display-line s))
(define (display-line x)
  (newline)
  (display x))

(define (stream-filter pred stream)
  (cond ((stream-null? stream) the-empty-stream)
        ((pred (stream-car stream))
         (cons-stream (stream-car stream)
                      (stream-filter pred
                                     (stream-cdr stream))))
        (else (stream-filter pred (stream-cdr stream)))))

(define (stream-append s1 s2)
  (if (stream-null? s1)
      s2
      (cons-stream (stream-car s1)
                   (stream-append (stream-cdr s1) s2))))

(define (interleave s1 s2)
  (if (stream-null? s1)
      s2
      (cons-stream (stream-car s1)
                   (interleave s2 (stream-cdr s1)))))

;;;;Table support from Chapter 3, Section 3.3.3 (local tables)

(define (make-table)
  (let ((local-table (list '*table*)))
    (define (lookup key-1 key-2)
      (let ((subtable (assoc key-1 (cdr local-table))))
        (if subtable
            (let ((record (assoc key-2 (cdr subtable))))
              (if record
                  (cdr record)
                  false))
            false)))
    (define (insert! key-1 key-2 value)
      (let ((subtable (assoc key-1 (cdr local-table))))
        (if subtable
            (let ((record (assoc key-2 (cdr subtable))))
              (if record
                  (set-cdr! record value)
                  (set-cdr! subtable
                            (cons (cons key-2 value)
                                  (cdr subtable)))))
            (set-cdr! local-table
                      (cons (list key-1
                                  (cons key-2 value))
                            (cdr local-table)))))
      'ok)    
    (define (dispatch m)
      (cond ((eq? m 'lookup-proc) lookup)
            ((eq? m 'insert-proc!) insert!)
            (else (error "Unknown operation -- TABLE" m))))
    dispatch))

;;;; From instructor's manual

(define get '())

(define put '())

(define (initialize-data-base rules-and-assertions)
  (define (deal-out r-and-a rules assertions)
    (cond ((null? r-and-a)
           (set! THE-ASSERTIONS (list->stream assertions))
           (set! THE-RULES (list->stream rules))
           'done)
          (else
           (let ((s (query-syntax-process (car r-and-a))))
             (cond ((rule? s)
                    (store-rule-in-index s)
                    (deal-out (cdr r-and-a)
                              (cons s rules)
                              assertions))
                   (else
                    (store-assertion-in-index s)
                    (deal-out (cdr r-and-a)
                              rules
                              (cons s assertions))))))))
  (let ((operation-table (make-table)))
    (set! get (operation-table 'lookup-proc))
    (set! put (operation-table 'insert-proc!)))
  (put 'and 'qeval conjoin)
  (put 'or 'qeval disjoin)
  (put 'not 'qeval negate)
  (put 'unique 'qeval uniquely-asserted)
  (put 'lisp-value 'qeval lisp-value)
  (put 'always-true 'qeval always-true)
  (deal-out rules-and-assertions '() '())) 

;; Do following to reinit the data base from microshaft-data-base
;;  in Scheme (not in the query driver loop)
;; (initialize-data-base microshaft-data-base)

(define microshaft-data-base
  '(
;; from section 4.4.1
(address (Bitdiddle Ben) (Slumerville (Ridge Road) 10))
(job (Bitdiddle Ben) (computer wizard))
(salary (Bitdiddle Ben) 60000)

(address (Hacker Alyssa P) (Cambridge (Mass Ave) 78))
(job (Hacker Alyssa P) (computer programmer))
(salary (Hacker Alyssa P) 40000)
(supervisor (Hacker Alyssa P) (Bitdiddle Ben))

(address (Fect Cy D) (Cambridge (Ames Street) 3))
(job (Fect Cy D) (computer programmer))
(salary (Fect Cy D) 35000)
(supervisor (Fect Cy D) (Bitdiddle Ben))

(address (Tweakit Lem E) (Boston (Bay State Road) 22))
(job (Tweakit Lem E) (computer technician))
(salary (Tweakit Lem E) 25000)
(supervisor (Tweakit Lem E) (Bitdiddle Ben))

(address (Reasoner Louis) (Slumerville (Pine Tree Road) 80))
(job (Reasoner Louis) (computer programmer trainee))
(salary (Reasoner Louis) 30000)
(supervisor (Reasoner Louis) (Hacker Alyssa P))

(supervisor (Bitdiddle Ben) (Warbucks Oliver))

(address (Warbucks Oliver) (Swellesley (Top Heap Road)))
(job (Warbucks Oliver) (administration big wheel))
(salary (Warbucks Oliver) 150000)

(address (Scrooge Eben) (Weston (Shady Lane) 10))
(job (Scrooge Eben) (accounting chief accountant))
(salary (Scrooge Eben) 75000)
(supervisor (Scrooge Eben) (Warbucks Oliver))

(address (Cratchet Robert) (Allston (N Harvard Street) 16))
(job (Cratchet Robert) (accounting scrivener))
(salary (Cratchet Robert) 18000)
(supervisor (Cratchet Robert) (Scrooge Eben))

(address (Aull DeWitt) (Slumerville (Onion Square) 5))
(job (Aull DeWitt) (administration secretary))
(salary (Aull DeWitt) 25000)
(supervisor (Aull DeWitt) (Warbucks Oliver))

(can-do-job (computer wizard) (computer programmer))
(can-do-job (computer wizard) (computer technician))

(can-do-job (computer programmer)
            (computer programmer trainee))

(can-do-job (administration secretary)
            (administration big wheel))

(rule (lives-near ?person-1 ?person-2)
      (and (address ?person-1 (?town . ?rest-1))
           (address ?person-2 (?town . ?rest-2))
           (not (same ?person-1 ?person-2))))

(rule (same ?x ?x))

(rule (wheel ?person)
      (and (supervisor ?middle-manager ?person)
           (supervisor ?x ?middle-manager)))

(rule (outranked-by ?staff-person ?boss)
      (or (supervisor ?staff-person ?boss)
          (and (supervisor ?staff-person ?middle-manager)
               (outranked-by ?middle-manager ?boss))))
))