-
Notifications
You must be signed in to change notification settings - Fork 117
/
subsection1.xml
2345 lines (2245 loc) · 81.6 KB
/
subsection1.xml
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
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<SUBSECTION>
<NAME>
Deductive Information Retrieval
</NAME>
<LABEL NAME="sec:deductive-info-retrieval"/>
<INDEX>query language<OPEN/></INDEX>
<TEXT>
Logic programming excels in providing interfaces to
<INDEX>data base<SUBINDEX>logic programming and</SUBINDEX></INDEX>
data bases for
information retrieval. The query language we shall implement in this
chapter is designed to be used in this way.
</TEXT>
<TEXT>
In order to illustrate what the query system does, we will show how it
can be used to manage the data base of personnel records for
<INDEX>Gargle</INDEX>
Gargle, a thriving high-technology company in the
Boston area. The language provides pattern-directed access to
personnel information and can also take advantage of general rules in
order to make logical deductions.
</TEXT>
<SUBHEADING>
<NAME>A sample data base</NAME>
</SUBHEADING>
<INDEX>query language<SUBINDEX>data base<OPEN/></SUBINDEX></INDEX>
<TEXT>
<INDEX>data base<SUBINDEX>Gargle personnel<OPEN/></SUBINDEX></INDEX>
The personnel data base for Gargle
contains
<INDEX>assertion</INDEX>
<EM>assertions</EM> about company personnel. Here is the
information about Ben Bitdiddle, the resident computer wizard:
<SNIPPET>
<NAME>sample_data_base_1</NAME>
<REQUIRES>process_query</REQUIRES>
<EXAMPLE>sample_data_base_1_example</EXAMPLE>
<SCHEME>
(address (Bitdiddle Ben) (Slumerville (Ridge Road) 10))
(job (Bitdiddle Ben) (computer wizard))
(salary (Bitdiddle Ben) 60000)
</SCHEME>
<JAVASCRIPT>
address(list("Bitdiddle", "Ben"),
list("Slumerville", list("Ridge", "Road"), 10))
job(list("Bitdiddle", "Ben"), list("computer", "wizard"))
salary(list("Bitdiddle", "Ben"), 122000)
</JAVASCRIPT>
<JAVASCRIPT_RUN>
process_query(`assert(address(list("Bitdiddle", "Ben"),
list("Slumerville", list("Ridge", "Road"), 10)))`);
process_query(`assert(job(list("Bitdiddle", "Ben"),
list("computer", "wizard")))`);
process_query(`assert(salary(list("Bitdiddle", "Ben"), 122000))`);
</JAVASCRIPT_RUN>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>sample_data_base_1_example</NAME>
<REQUIRES>sample_data_base_1</REQUIRES>
<REQUIRES>process_query</REQUIRES>
<EXPECTED>'salary(list("Bitdiddle", "Ben"), 122000)'</EXPECTED>
<JAVASCRIPT>
process_query('salary(list("Bitdiddle", "Ben"), $x)');
</JAVASCRIPT>
<JAVASCRIPT_TEST>
first_answer('salary(list("Bitdiddle", "Ben"), $x)');
</JAVASCRIPT_TEST>
</SNIPPET>
<COMMENT>
Another option for syntactically representing facts in the data base
would have been to use simply lists, also for the top level
<QUOTE>predicates</QUOTE>, as in the original book. We did not resist the
temptation to syntactically introduce <QUOTE>predicate symbols</QUOTE>,
for the following reasons:
<UL>
<LI>
In JavaScript, facts in a datalog data base look much more natural
when rendered as <JAVASCRIPTINLINE>p(x, y)</JAVASCRIPTINLINE> rather
than as <JAVASCRIPTINLINE>list("p", x, y)</JAVASCRIPTINLINE>.
</LI>
<LI>
The analogy between logic programming and functional programming
comes out much clearer; the predicate is in familiar position
and the reader is reminded of function declarations when reading:
<JAVASCRIPTINLINE>append_to_form(list("a", "b"), y, list("a", "b", "c", "d"))</JAVASCRIPTINLINE> rather than
<JAVASCRIPTINLINE>list("append_to_form", list("a", "b"), y, list("a", "b", "c", "d"))</JAVASCRIPTINLINE>.
</LI>
<LI>
The most decisive argument is that in this treatment, function
symbols (as in the predicate calculus) come for free: We can write
predicates such as <JAVASCRIPTINLINE>plus</JAVASCRIPTINLINE> without
any additional effort in the interpreter:
<JAVASCRIPTINLINE>rule(plus($x, 0, $x)</JAVASCRIPTINLINE> and
<JAVASCRIPTINLINE>rule(plus($x, s($y), s($z)), plus($x, $y, $z))</JAVASCRIPTINLINE>.
</LI>
</UL>
</COMMENT>
<SPLIT>
<SCHEME>
Each assertion is a list (in this case a triple) whose elements can
themselves be lists.
</SCHEME>
<JAVASCRIPT>
Assertions look just like function applications in JavaScript, but
they actually represent information in the data base. The first
symbols<EMDASH/>here <JAVASCRIPTINLINE>address</JAVASCRIPTINLINE>,
<JAVASCRIPTINLINE>job</JAVASCRIPTINLINE> and
<JAVASCRIPTINLINE>salary</JAVASCRIPTINLINE><EMDASH/>describe the
<EM>kind of information</EM> contained in the respective assertion, and
the <QUOTE>arguments</QUOTE> are lists or primitive values such as strings
and numbers. The first symbols do not need to be declared, as do constants
or variables in JavaScript; their scope is global.
<COMMENT>
We shy away from calling these symbols
<QUOTE>predicate symbols</QUOTE> instead of
<QUOTE>kind of information</QUOTE>
because the term
<QUOTE>predicate</QUOTE> is already used, also in this section,
for JavaScript expressions and functions that return boolean values.
</COMMENT>
</JAVASCRIPT>
</SPLIT>
</TEXT>
<TEXT>
As resident wizard, Ben is in charge of the company<APOS/>s computer
division, and he supervises two programmers and one technician. Here
is the information about them:
<SNIPPET>
<NAME>sample_data_base_2</NAME>
<REQUIRES>process_query</REQUIRES>
<REQUIRES>sample_data_base_1</REQUIRES>
<EXPECTED>'supervisor(list("Tweakit", "Lem", "E"), list("Bitdiddle", "Ben"))'</EXPECTED>
<EXAMPLE>sample_data_base_2_example</EXAMPLE>
<SCHEME>
(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))
</SCHEME>
<JAVASCRIPT>
address(list("Hacker", "Alyssa", "P"),
list("Cambridge", list("Mass", "Ave"), 78))
job(list("Hacker", "Alyssa", "P"), list("computer", "programmer"))
salary(list("Hacker", "Alyssa", "P"), 81000)
supervisor(list("Hacker", "Alyssa", "P"), list("Bitdiddle", "Ben"))
address(list("Fect", "Cy", "D"),
list("Cambridge", list("Ames", "Street"), 3))
job(list("Fect", "Cy", "D"), list("computer", "programmer"))
salary(list("Fect", "Cy", "D"), 70000)
supervisor(list("Fect", "Cy", "D"), list("Bitdiddle", "Ben"))
address(list("Tweakit", "Lem", "E"),
list("Boston", list("Bay", "State", "Road"), 22))
job(list("Tweakit", "Lem", "E"), list("computer", "technician"))
salary(list("Tweakit", "Lem", "E"), 51000)
supervisor(list("Tweakit", "Lem", "E"), list("Bitdiddle", "Ben"))
</JAVASCRIPT>
<JAVASCRIPT_RUN>
process_query('assert(address(list("Hacker", "Alyssa", "P"), list("Cambridge", list("Mass", "Ave"), 78)))');
process_query('assert(job(list("Hacker", "Alyssa", "P"), list("computer", "programmer")))');
process_query('assert(salary(list("Hacker", "Alyssa", "P"), 81000))');
process_query('assert(supervisor(list("Hacker", "Alyssa", "P"), list("Bitdiddle", "Ben")))');
process_query('assert(address(list("Fect", "Cy", "D"), list("Cambridge", list("Ames", "Street"), 3)))');
process_query('assert(job(list("Fect", "Cy", "D"), list("computer", "programmer")))');
process_query('assert(salary(list("Fect", "Cy", "D"), 70000))');
process_query('assert(supervisor(list("Fect", "Cy", "D"), list("Bitdiddle", "Ben")))');
process_query('assert(address(list("Tweakit", "Lem", "E"), list("Boston", list("Bay", "State", "Road"), 22)))');
process_query('assert(job(list("Tweakit", "Lem", "E"), list("computer", "technician")))');
process_query('assert(salary(list("Tweakit", "Lem", "E"), 51000))');
process_query('assert(supervisor(list("Tweakit", "Lem", "E"), list("Bitdiddle", "Ben")))');
</JAVASCRIPT_RUN>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>sample_data_base_2_example</NAME>
<JAVASCRIPT>
process_query('supervisor($x, list("Bitdiddle", "Ben"))');
</JAVASCRIPT>
<JAVASCRIPT_TEST>
first_answer('supervisor($x, list("Bitdiddle", "Ben"))');
</JAVASCRIPT_TEST>
</SNIPPET>
<PDF_ONLY>\newpage\noindent </PDF_ONLY>There is also a programmer trainee, who is supervised by Alyssa:
<SNIPPET>
<NAME>sample_data_base_3</NAME>
<REQUIRES>process_query</REQUIRES>
<REQUIRES>sample_data_base_2</REQUIRES>
<EXPECTED>'supervisor(list("Reasoner", "Louis"), list("Hacker", "Alyssa", "P"))'</EXPECTED>
<EXAMPLE>sample_data_base_3_example</EXAMPLE>
<SCHEME>
(address (Reasoner Louis) (Slumerville (Pine Tree Road) 80))
(job (Reasoner Louis) (computer programmer trainee))
(salary (Reasoner Louis) 30000)
(supervisor (Reasoner Louis) (Hacker Alyssa P))
</SCHEME>
<JAVASCRIPT>
address(list("Reasoner", "Louis"),
list("Slumerville", list("Pine", "Tree", "Road"), 80))
job(list("Reasoner", "Louis"),
list("computer", "programmer", "trainee"))
salary(list("Reasoner", "Louis"), 62000)
supervisor(list("Reasoner", "Louis"), list("Hacker", "Alyssa", "P"))
</JAVASCRIPT>
<JAVASCRIPT_RUN>
process_query('assert(address(list("Reasoner", "Louis"), list("Slumerville", list("Pine", "Tree", "Road"), 80)))');
process_query('assert(job(list("Reasoner", "Louis"), list("computer", "programmer", "trainee")))');
process_query('assert(salary(list("Reasoner", "Louis"), 62000))');
process_query('assert(supervisor(list("Reasoner", "Louis"), list("Hacker", "Alyssa", "P")))');
</JAVASCRIPT_RUN>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>sample_data_base_3_example</NAME>
<JAVASCRIPT>
process_query('supervisor(list("Reasoner", $x), $y)');
</JAVASCRIPT>
<JAVASCRIPT_TEST>
first_answer('supervisor(list("Reasoner", $x), $y)');
</JAVASCRIPT_TEST>
</SNIPPET>
All these people are in the computer division, as indicated by the
word
<SPLITINLINE>
<SCHEME>
<SCHEMEINLINE>computer</SCHEMEINLINE>
</SCHEME>
<JAVASCRIPT>
<JAVASCRIPTINLINE>"computer"</JAVASCRIPTINLINE>
</JAVASCRIPT>
</SPLITINLINE>
as the first item in their job
descriptions.
</TEXT>
<TEXT>
Ben is a high-level employee. His supervisor is the company<APOS/>s big
wheel himself:
<SNIPPET>
<NAME>sample_data_base_4</NAME>
<REQUIRES>process_query</REQUIRES>
<REQUIRES>sample_data_base_3</REQUIRES>
<EXPECTED>'address(list("Warbucks", "Oliver"), list("Swellesley", list("Top", "Heap", "Road")))'</EXPECTED>
<EXAMPLE>sample_data_base_4_example</EXAMPLE>
<SCHEME>
(supervisor (Bitdiddle Ben) (Warbucks Oliver))
(address (Warbucks Oliver) (Swellesley (Top Heap Road)))
(job (Warbucks Oliver) (administration big wheel))
(salary (Warbucks Oliver) 150000)
</SCHEME>
<JAVASCRIPT>
supervisor(list("Bitdiddle", "Ben"), list("Warbucks", "Oliver"))
address(list("Warbucks", "Oliver"),
list("Swellesley", list("Top", "Heap", "Road")))
job(list("Warbucks", "Oliver"), list("administration", "big", "wheel"))
salary(list("Warbucks", "Oliver"), 314159)
</JAVASCRIPT>
<JAVASCRIPT_RUN>
process_query('assert(supervisor(list("Bitdiddle", "Ben"), list("Warbucks", "Oliver")))');
process_query('assert(address(list("Warbucks", "Oliver"), list("Swellesley", list("Top", "Heap", "Road"))))');
process_query('assert(job(list("Warbucks", "Oliver"), list("administration", "big", "wheel")))');
process_query('assert(salary(list("Warbucks", "Oliver"), 314159))');
</JAVASCRIPT_RUN>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>sample_data_base_4_example</NAME>
<JAVASCRIPT>
process_query('address($x, list("Swellesley", $y))');
</JAVASCRIPT>
<JAVASCRIPT_TEST>
first_answer('address($x, list("Swellesley", $y))');
</JAVASCRIPT_TEST>
</SNIPPET>
</TEXT>
<TEXT>
Besides the computer division supervised by Ben, the company has an
accounting division, consisting of a chief accountant and his
assistant:
<SNIPPET>
<NAME>sample_data_base_5</NAME>
<REQUIRES>process_query</REQUIRES>
<REQUIRES>sample_data_base_4</REQUIRES>
<EXPECTED>'job(list("Cratchit", "Robert"), list("accounting", "scrivener"))'</EXPECTED>
<EXAMPLE>sample_data_base_5_example</EXAMPLE>
<SCHEME>
(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))
</SCHEME>
<JAVASCRIPT>
address(list("Scrooge", "Eben"),
list("Weston", list("Shady", "Lane"), 10))
job(list("Scrooge", "Eben"), list("accounting", "chief", "accountant"))
salary(list("Scrooge", "Eben"), 141421)
supervisor(list("Scrooge", "Eben"), list("Warbucks", "Oliver"))
address(list("Cratchit", "Robert"),
list("Allston", list("N", "Harvard", "Street"), 16))
job(list("Cratchit", "Robert"), list("accounting", "scrivener"))
salary(list("Cratchit", "Robert"), 26100)
supervisor(list("Cratchit", "Robert"), list("Scrooge", "Eben"))
</JAVASCRIPT>
<JAVASCRIPT_RUN>
process_query('assert(address(list("Scrooge", "Eben"), list("Weston", list("Shady", "Lane"), 10)))');
process_query('assert(job(list("Scrooge", "Eben"), list("accounting", "chief", "accountant")))');
process_query('assert(salary(list("Scrooge", "Eben"), 141421))');
process_query('assert(supervisor(list("Scrooge", "Eben"), list("Warbucks", "Oliver")))');
process_query('assert(address(list("Cratchit", "Robert"), list("Allston", list("N", "Harvard", "Street"), 16)))');
process_query('assert(job(list("Cratchit", "Robert"), list("accounting", "scrivener")))');
process_query('assert(salary(list("Cratchit", "Robert"), 26100))');
process_query('assert(supervisor(list("Cratchit", "Robert"), list("Scrooge", "Eben")))');
</JAVASCRIPT_RUN>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>sample_data_base_5_example</NAME>
<JAVASCRIPT>
process_query('job($x, pair("accounting", $y))');
</JAVASCRIPT>
<JAVASCRIPT_TEST>
first_answer('job($x, pair("accounting", $y))');
</JAVASCRIPT_TEST>
</SNIPPET>
There is also
<SPLITINLINE>
<SCHEME>
a secretary
</SCHEME>
<JAVASCRIPT>
an administrative assistant
</JAVASCRIPT>
</SPLITINLINE>
for the big wheel:
<SNIPPET>
<NAME>sample_data_base_6</NAME>
<REQUIRES>process_query</REQUIRES>
<REQUIRES>sample_data_base_5</REQUIRES>
<EXPECTED>'address(list("Aull", "DeWitt"), list("Slumerville", list("Onion", "Square"), 5))'</EXPECTED>
<EXAMPLE>sample_data_base_6_example</EXAMPLE>
<SCHEME>
(address (Aull DeWitt) (Slumerville (Onion Square) 5))
(job (Aull DeWitt) (administration secretary))
(salary (Aull DeWitt) 25000)
(supervisor (Aull DeWitt) (Warbucks Oliver))
</SCHEME>
<JAVASCRIPT>
address(list("Aull", "DeWitt"),
list("Slumerville", list("Onion", "Square"), 5))
job(list("Aull", "DeWitt"), list("administration", "assistant"))
salary(list("Aull", "DeWitt"), 42195)
supervisor(list("Aull", "DeWitt"), list("Warbucks", "Oliver"))
</JAVASCRIPT>
<JAVASCRIPT_RUN>
process_query('assert(address(list("Aull", "DeWitt"), list("Slumerville", list("Onion", "Square"), 5)))');
process_query('assert(job(list("Aull", "DeWitt"), list("administration", "assistant")))');
process_query('assert(salary(list("Aull", "DeWitt"), 42195))');
process_query('assert(supervisor(list("Aull", "DeWitt"), list("Warbucks", "Oliver")))');
</JAVASCRIPT_RUN>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>sample_data_base_6_example</NAME>
<JAVASCRIPT>
process_query('address($x, list(y, list("Onion", "Square"), $n))');
</JAVASCRIPT>
<JAVASCRIPT_TEST>
first_answer('address($x, list(y, list("Onion", "Square"), $n))');
</JAVASCRIPT_TEST>
</SNIPPET>
</TEXT>
<TEXT>
The data base also contains assertions about which kinds of jobs can
be done by people holding other kinds of jobs. For instance, a
computer wizard can do the jobs of both a computer programmer and a
computer technician:
<SNIPPET>
<NAME>sample_data_base_7</NAME>
<REQUIRES>process_query</REQUIRES>
<REQUIRES>sample_data_base_6</REQUIRES>
<EXPECTED>'can_do_job(list("computer", "wizard"), list("computer", "technician"))'</EXPECTED>
<EXAMPLE>sample_data_base_7_example</EXAMPLE>
<SCHEME>
(can-do-job (computer wizard) (computer programmer))
(can-do-job (computer wizard) (computer technician))
</SCHEME>
<JAVASCRIPT>
can_do_job(list("computer", "wizard"),
list("computer", "programmer"))
can_do_job(list("computer", "wizard"),
list("computer", "technician"))
</JAVASCRIPT>
<JAVASCRIPT_RUN>
process_query('assert(can_do_job(list("computer", "wizard"), list("computer", "programmer")))');
process_query('assert(can_do_job(list("computer", "wizard"), list("computer", "technician")))');
</JAVASCRIPT_RUN>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>sample_data_base_7_example</NAME>
<JAVASCRIPT>
process_query('can_do_job($x, list("computer", $y))');
</JAVASCRIPT>
<JAVASCRIPT_TEST>
first_answer('can_do_job($x, list("computer", $y))');
</JAVASCRIPT_TEST>
</SNIPPET>
A computer programmer could fill in for a trainee:
<SNIPPET>
<NAME>sample_data_base_8</NAME>
<REQUIRES>process_query</REQUIRES>
<REQUIRES>sample_data_base_7</REQUIRES>
<EXPECTED>'can_do_job(list("computer", "programmer"), list("computer", "programmer", "trainee"))'</EXPECTED>
<EXAMPLE>sample_data_base_8_example</EXAMPLE>
<SCHEME>
(can-do-job (computer programmer)
(computer programmer trainee))
</SCHEME>
<JAVASCRIPT>
can_do_job(list("computer", "programmer"),
list("computer", "programmer", "trainee"))
</JAVASCRIPT>
<JAVASCRIPT_RUN>
process_query('assert(can_do_job(list("computer", "programmer"), list("computer", "programmer", "trainee")))');
</JAVASCRIPT_RUN>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>sample_data_base_8_example</NAME>
<JAVASCRIPT>
process_query('can_do_job($x, pair("computer", $y))');
</JAVASCRIPT>
<JAVASCRIPT_TEST>
first_answer('can_do_job($x, pair("computer", $y))');
</JAVASCRIPT_TEST>
</SNIPPET>
<SPLITINLINE>
<SCHEME>
<INDEX>secretary, importance of</INDEX>
</SCHEME>
<JAVASCRIPT>
<INDEX>administrative assistant, importance of</INDEX>
</JAVASCRIPT>
</SPLITINLINE>
Also, as is well known,
<SNIPPET POSTPADDING="no">
<NAME>sample_data_base_9</NAME>
<REQUIRES>process_query</REQUIRES>
<REQUIRES>sample_data_base_8</REQUIRES>
<EXPECTED>'can_do_job(list("administration", "assistant"), list("administration", "big", "wheel"))'</EXPECTED>
<EXAMPLE>sample_data_base_9_example</EXAMPLE>
<SCHEME>
(can-do-job (administration secretary)
(administration big wheel))
</SCHEME>
<JAVASCRIPT>
can_do_job(list("administration", "assistant"),
list("administration", "big", "wheel"))
</JAVASCRIPT>
<JAVASCRIPT_RUN>
process_query('assert(can_do_job(list("administration", "assistant"), list("administration", "big", "wheel")))');
</JAVASCRIPT_RUN>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>sample_data_base_9_example</NAME>
<JAVASCRIPT>
process_query('can_do_job(list($x, "assistant"), $y)');
</JAVASCRIPT>
<JAVASCRIPT_TEST>
first_answer('can_do_job(list($x, "assistant"), $y)');
</JAVASCRIPT_TEST>
</SNIPPET>
</TEXT>
<INDEX>data base<SUBINDEX>Gargle personnel<CLOSE/></SUBINDEX></INDEX>
<INDEX>query language<SUBINDEX>data base<CLOSE/></SUBINDEX></INDEX>
<SUBHEADING>
<NAME>Simple queries</NAME>
</SUBHEADING>
<INDEX>simple query<OPEN/></INDEX>
<TEXT>
The query language allows users to retrieve information from the data
base by posing queries in response to the system<APOS/>s prompt.
For example, to find all computer programmers one can say
<SNIPPET EVAL="no" HIDE="yes">
<NAME>simple_queries_input</NAME>
<SCHEMEOUTPUT>
;;; Query input:
</SCHEMEOUTPUT>
</SNIPPET>
<SNIPPET>
<NAME>simple_queries_1</NAME>
<REQUIRES>process_query</REQUIRES>
<REQUIRES>sample_data_base_9</REQUIRES>
<REQUIRES>query_driver_loop</REQUIRES>
<EXPECTED>'job(list("Fect", "Cy", "D"), list("computer", "programmer"))'</EXPECTED>
<EXAMPLE>simple_queries_1_example</EXAMPLE>
<SCHEME>
(job ?x (computer programmer))
</SCHEME>
<JAVASCRIPT_PROMPT>
Query input:
</JAVASCRIPT_PROMPT>
<JAVASCRIPT>
job($x, list("computer", "programmer"))
</JAVASCRIPT>
<JAVASCRIPT_RUN>
</JAVASCRIPT_RUN>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>simple_queries_1_example</NAME>
<JAVASCRIPT_TEST>
first_answer('job($x, list("computer", "programmer"))');
</JAVASCRIPT_TEST>
<JAVASCRIPT>
process_query('job($x, list("computer", "programmer"))');
// Query input:
// job($x, list("computer", "programmer"))
// Query results:
// job(list("Hacker", "Alyssa", "P"), list("computer", "programmer"))
// job(list("Fect", "Cy", "D"), list("computer", "programmer"))
</JAVASCRIPT>
</SNIPPET>
The system will respond with the following items:
<SNIPPET EVAL="no">
<SCHEMEOUTPUT>
;;; Query results:
(job (Hacker Alyssa P) (computer programmer))
(job (Fect Cy D) (computer programmer))
</SCHEMEOUTPUT>
<JAVASCRIPT_OUTPUT>
Query results:
job(list("Hacker", "Alyssa", "P"), list("computer", "programmer"))
job(list("Fect", "Cy", "D"), list("computer", "programmer"))
</JAVASCRIPT_OUTPUT>
</SNIPPET>
</TEXT>
<TEXT>
The input query specifies that we are looking for entries in the data
base that match a certain
<INDEX>pattern<OPEN/></INDEX>
<EM>pattern</EM>.
<SPLIT>
<SCHEME>
In this example, the pattern
specifies entries consisting of three items, of which the first is the
literal symbol <SCHEMEINLINE>job</SCHEMEINLINE>, the second can be
anything, and the third is the literal list
<SCHEMEINLINE>(computer programmer)</SCHEMEINLINE>.
The <QUOTE>anything</QUOTE> that can be the second item in the matching
list is specified by a
<INDEX>pattern variable</INDEX>
<EM>pattern variable</EM>,
<SCHEMEINLINE>?x</SCHEMEINLINE>.
The general form of a pattern variable is a symbol, taken to be the name
of the variable, preceded by a question mark. We will see below why it
is useful to specify names for pattern variables rather than just putting
<SCHEMEINLINE>?</SCHEMEINLINE>
into patterns to represent <QUOTE>anything.</QUOTE>
</SCHEME>
<JAVASCRIPT>
In this example, the pattern
specifies <SCHEMEINLINE>job</SCHEMEINLINE> as the kind of information
that we are looking for. The first item can be
anything, and the second is the literal list
<JAVASCRIPTINLINE>list("computer", "programmer")</JAVASCRIPTINLINE>.
The <QUOTE>anything</QUOTE> that can be the first item in the matching
assertion is specified by a
<INDEX>pattern variable</INDEX>
<EM>pattern variable</EM>,
<JAVASCRIPTINLINE>$x</JAVASCRIPTINLINE>. As pattern variables, we use
<INDEX>naming conventions<SUBINDEX><ORDER>;x</ORDER>$\$$ for pattern variables</SUBINDEX><FRAGILE/></INDEX>
<INDEX><ORDER>0a4</ORDER>$\$$, pattern variables starting with<FRAGILE/></INDEX>
JavaScript names that start with a dollar sign.
We will see below why
it is useful to specify names for pattern variables rather than just
putting a single symbol such as <SCHEMEINLINE>$</SCHEMEINLINE>
into patterns to represent <QUOTE>anything.</QUOTE>
</JAVASCRIPT>
</SPLIT>
The system responds to a simple query by showing all entries in the data
base that match the specified pattern.
</TEXT>
<TEXT>
A pattern can have more than one variable. For example, the query
<SNIPPET>
<NAME>simple_queries_2</NAME>
<REQUIRES>process_query</REQUIRES>
<REQUIRES>sample_data_base_9</REQUIRES>
<REQUIRES>query_driver_loop</REQUIRES>
<EXPECTED>'address(list("Aull", "DeWitt"), list("Slumerville", list("Onion", "Square"), 5))'</EXPECTED>
<EXAMPLE>simple_queries_2_example</EXAMPLE>
<SCHEME>
(address ?x ?y)
</SCHEME>
<JAVASCRIPT>
address($x, $y)
</JAVASCRIPT>
<JAVASCRIPT_RUN>
</JAVASCRIPT_RUN>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>simple_queries_2_example</NAME>
<JAVASCRIPT_TEST>
first_answer('address($x, $y)');
</JAVASCRIPT_TEST>
<JAVASCRIPT>
process_query('address($x, $y)');
// query input:
// address($x, $y)
// query results:
// all employees' addresses
</JAVASCRIPT>
</SNIPPET>
will list all the employees<APOS/> addresses.
</TEXT>
<TEXT>
A pattern can have no variables, in which case the query simply
determines whether that pattern is an entry in the data base. If so,
there will be one match; if not, there will be no matches.
</TEXT>
<TEXT>
The same pattern variable can appear more than once in a query,
specifying that the same <QUOTE>anything</QUOTE> must appear in each
position. This is why variables have names. For example,
<SNIPPET>
<NAME>simple_queries_3</NAME>
<REQUIRES>process_query</REQUIRES>
<REQUIRES>sample_data_base_9</REQUIRES>
<REQUIRES>query_driver_loop</REQUIRES>
<EXPECTED>'supervisor(list("Julius", "Caesar"), list("Julius", "Caesar"))'</EXPECTED>
<EXAMPLE>simple_queries_3_example</EXAMPLE>
<SCHEME>
(supervisor ?x ?x)
</SCHEME>
<JAVASCRIPT>
supervisor($x, $x)
</JAVASCRIPT>
<JAVASCRIPT_RUN>
</JAVASCRIPT_RUN>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>simple_queries_3_example</NAME>
<JAVASCRIPT_TEST>
process_query('assert(supervisor(list("Julius", "Caesar"), list("Julius", "Caesar")))');
first_answer('supervisor($x, $x)');
</JAVASCRIPT_TEST>
<JAVASCRIPT>
process_query('supervisor($x, $x)');
// query input:
// supervisor($x, $x)
// query results:
// none
</JAVASCRIPT>
</SNIPPET>
finds all people who supervise themselves (though there are no
such assertions in our sample data base).
</TEXT>
<TEXT>
The query
<SNIPPET>
<NAME>simple_queries_4</NAME>
<REQUIRES>process_query</REQUIRES>
<REQUIRES>sample_data_base_9</REQUIRES>
<REQUIRES>query_driver_loop</REQUIRES>
<EXPECTED>'job(list("Tweakit", "Lem", "E"), list("computer", "technician"))'</EXPECTED>
<EXAMPLE>simple_queries_4_example</EXAMPLE>
<SCHEME>
(job ?x (computer ?type))
</SCHEME>
<JAVASCRIPT>
job($x, list("computer", $type))
</JAVASCRIPT>
<JAVASCRIPT_RUN>
</JAVASCRIPT_RUN>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>simple_queries_4_example</NAME>
<JAVASCRIPT_TEST>
first_answer('job($x, list("computer", $type))');
</JAVASCRIPT_TEST>
<JAVASCRIPT>
process_query('job($x, list("computer", $type))');
// query input:
// job($x, list("computer", $type))
// query results:
// job(list("Bitdiddle", "Ben"), list("computer", "wizard"))
// job(list("Hacker", "Alyssa", "P"), list("computer", "programmer"))
// job(list("Fect", "Cy", "D"), list("computer", "programmer"))
// job(list("Tweakit", "Lem", "E"), list("computer", "technician"))
</JAVASCRIPT>
</SNIPPET>
matches all job entries whose
<SPLITINLINE>
<SCHEME>
third
</SCHEME>
<JAVASCRIPT>
second
</JAVASCRIPT>
</SPLITINLINE>
item is a two-element list whose
first item is
<SPLITINLINE>
<SCHEME>
<SCHEMEINLINE>computer</SCHEMEINLINE>:
</SCHEME>
<JAVASCRIPT>
<JAVASCRIPTINLINE>"computer"</JAVASCRIPTINLINE>:
</JAVASCRIPT>
</SPLITINLINE>
<SNIPPET EVAL="no">
<SCHEME>
(job (Bitdiddle Ben) (computer wizard))
(job (Hacker Alyssa P) (computer programmer))
(job (Fect Cy D) (computer programmer))
(job (Tweakit Lem E) (computer technician))
</SCHEME>
<JAVASCRIPT>
job(list("Bitdiddle", "Ben"), list("computer", "wizard"))
job(list("Hacker", "Alyssa", "P"), list("computer", "programmer"))
job(list("Fect", "Cy", "D"), list("computer", "programmer"))
job(list("Tweakit", "Lem", "E"), list("computer", "technician"))
</JAVASCRIPT>
</SNIPPET>
<SPLIT>
<SCHEME>
This same pattern does <EM>not</EM> match
<SNIPPET EVAL="no">
<SCHEME>
(job (Reasoner Louis) (computer programmer trainee))
</SCHEME>
</SNIPPET>
because the third item in the entry is a list of three elements, and
the pattern<APOS/>s third item specifies that there should be two
elements. If we wanted to change the pattern so that the third item
could be any
<INDEX>dotted-tail notation<SUBINDEX><ORDER>query</ORDER>in query pattern</SUBINDEX></INDEX>
list beginning with
<SCHEMEINLINE>computer</SCHEMEINLINE>,
we could
specify<SPLITINLINE><SCHEME><FOOTNOTE>This uses the dotted-tail
notation introduced in
exercise<SPACE/><REF NAME="ex:dotted-arglist"/>.</FOOTNOTE>
</SCHEME></SPLITINLINE>
</SCHEME>
<JAVASCRIPT>
This same pattern does <EM>not</EM> match
<SNIPPET EVAL="no">
<JAVASCRIPT>
job(list("Reasoner", "Louis"),
list("computer", "programmer", "trainee"))
</JAVASCRIPT>
</SNIPPET>
because the second item in the assertion is a list of three
elements, and the pattern<APOS/>s second item specifies that there
should be two elements. If we wanted to change the pattern so that the
second item could be any
list beginning with
<JAVASCRIPTINLINE>"computer"</JAVASCRIPTINLINE>,
we could
specify
<COMMENT>
There is no need for <QUOTE>dotted-tail notation</QUOTE> in the
JavaScript edition: We simply use
<JAVASCRIPTINLINE>pair</JAVASCRIPTINLINE> instead of
<JAVASCRIPTINLINE>list</JAVASCRIPTINLINE> where needed.
</COMMENT>
</JAVASCRIPT>
</SPLIT>
<SNIPPET>
<NAME>simple_queries_5</NAME>
<REQUIRES>process_query</REQUIRES>
<REQUIRES>sample_data_base_9</REQUIRES>
<REQUIRES>query_driver_loop</REQUIRES>
<EXPECTED>'job(list("Reasoner", "Louis"), list("computer", "programmer", "trainee"))'</EXPECTED>
<EXAMPLE>simple_queries_5_example</EXAMPLE>
<SCHEME>
(job ?x (computer . ?type))
</SCHEME>
<JAVASCRIPT>
job($x, pair("computer", $type))
</JAVASCRIPT>
<JAVASCRIPT_RUN>
</JAVASCRIPT_RUN>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>simple_queries_5_example</NAME>
<JAVASCRIPT_TEST>
first_answer('job($x, pair("computer", $type))');
</JAVASCRIPT_TEST>
<JAVASCRIPT>
process_query('job($x, pair("computer", $type))');
// query input:
// job($x, pair("computer", $type))
// query results:
// job(list("Bitdiddle", "Ben"), list("computer", "wizard"))
// job(list("Hacker", "Alyssa", "P"), list("computer", "programmer"))
// job(list("Fect", "Cy", "D"), list("computer", "programmer"))
// job(list("Tweakit", "Lem", "E"), list("computer", "technician"))
// job(list("Reasoner", "Louis"), list("computer", "programmer", "trainee"))
</JAVASCRIPT>
</SNIPPET>
For example,
<SNIPPET EVAL="no">
<SCHEME>
(computer . ?type)
</SCHEME>
<JAVASCRIPT>
pair("computer", $type)
</JAVASCRIPT>
</SNIPPET>
matches the data
<SNIPPET EVAL="no">
<SCHEME>
(computer programmer trainee)
</SCHEME>
<JAVASCRIPT>
list("computer", "programmer", "trainee")
</JAVASCRIPT>
</SNIPPET>
with
<SPLITINLINE>
<SCHEME>
<SCHEMEINLINE>?type</SCHEMEINLINE>
</SCHEME>
<JAVASCRIPT>
<JAVASCRIPTINLINE>$type</JAVASCRIPTINLINE>
</JAVASCRIPT>
</SPLITINLINE>
as
<SPLITINLINE>
<SCHEME>the list <SCHEMEINLINE>(programmer trainee)</SCHEMEINLINE>.</SCHEME>
<JAVASCRIPT>
<JAVASCRIPTINLINE>list("programmer", "trainee")</JAVASCRIPTINLINE>.
</JAVASCRIPT>
</SPLITINLINE>
It also matches the data
<SNIPPET EVAL="no">
<SCHEME>
(computer programmer)
</SCHEME>
<JAVASCRIPT>
list("computer", "programmer")
</JAVASCRIPT>
</SNIPPET>
with
<SPLITINLINE>
<SCHEME>
<SCHEMEINLINE>?type</SCHEMEINLINE>
</SCHEME>
<JAVASCRIPT><JAVASCRIPTINLINE>$type</JAVASCRIPTINLINE>
</JAVASCRIPT>
</SPLITINLINE>
as
<SPLITINLINE>
<SCHEME>the list <SCHEMEINLINE>(programmer)</SCHEMEINLINE>,</SCHEME>
<JAVASCRIPT><JAVASCRIPTINLINE>list("programmer")</JAVASCRIPTINLINE>,
</JAVASCRIPT>
</SPLITINLINE>
and matches the data
<SNIPPET EVAL="no">
<SCHEME>
(computer)
</SCHEME>
<JAVASCRIPT>
list("computer")
</JAVASCRIPT>
</SNIPPET>
with
<SPLITINLINE>
<SCHEME>
<SCHEMEINLINE>?type</SCHEMEINLINE>
</SCHEME>
<JAVASCRIPT><JAVASCRIPTINLINE>$type</JAVASCRIPTINLINE>
</JAVASCRIPT>
</SPLITINLINE>
as the empty
<SPLITINLINE>
<SCHEME>list <SCHEMEINLINE>()</SCHEMEINLINE>.</SCHEME>
<JAVASCRIPT>list, <JAVASCRIPTINLINE>null</JAVASCRIPTINLINE>.</JAVASCRIPT>
</SPLITINLINE>
</TEXT>
<INDEX>pattern<CLOSE/></INDEX>
<TEXT>
We can describe the query language<APOS/>s processing of simple queries as
follows:
<UL>
<LI>
The system finds all assignments to variables in the query
pattern that
<INDEX>satisfy a pattern (simple query)</INDEX>
<EM>satisfy</EM> the pattern<EMDASH/>that is, all sets
of values for the variables such that if the pattern variables are
<INDEX>instantiate a pattern</INDEX>
<EM>instantiated with</EM> (replaced by) the values, the result is
in the data base.
</LI>
<LI>
The system responds to the query by listing all instantiations of the
query pattern with the variable assignments that satisfy it.
</LI>
</UL>
Note that if the pattern has no variables, the query reduces to a
determination of whether that pattern is in the data base. If so, the
empty assignment, which assigns no values to variables, satisfies that
pattern for that data base.
</TEXT>
<EXERCISE>
Give simple queries that retrieve the following information from the
data base:
<OL>
<LI>
all people supervised by Ben Bitdiddle;
</LI>
<LI>
the names and jobs of all people in the accounting division;
</LI>
<LI>
the names and addresses of all people who live
in Slumerville.
</LI>
</OL>
<LABEL NAME="ex:4_53"/>
</EXERCISE>
<INDEX>simple query<CLOSE/></INDEX>
<SUBHEADING>
<NAME>Compound queries</NAME>
</SUBHEADING>
<INDEX>compound query<OPEN/></INDEX>
<TEXT>
Simple queries form the primitive operations of the query language.
In order to form compound operations, the query language provides
means of combination. One thing that makes the query language a logic
programming language is that the means of combination mirror the means
of combination used in forming logical expressions:
<SCHEMEINLINE>and</SCHEMEINLINE>, <SCHEMEINLINE>or</SCHEMEINLINE>, and
<SCHEMEINLINE>not</SCHEMEINLINE>.
<SPLITINLINE>
<SCHEME>
(Here <SCHEMEINLINE>and</SCHEMEINLINE>, <SCHEMEINLINE>or</SCHEMEINLINE>,
and <SCHEMEINLINE>not</SCHEMEINLINE> are not the Lisp primitives, but
rather operations built into the query language.)
</SCHEME>
</SPLITINLINE>
</TEXT>
<TEXT>
We can use
<INDEX><USE>and</USE> (query language)</INDEX>
<SCHEMEINLINE>and</SCHEMEINLINE> as follows to find the addresses
of all the computer programmers:
<SNIPPET>