forked from DeathKing/Learning-SICP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec8b.eng.srt
2404 lines (1803 loc) · 70.1 KB
/
lec8b.eng.srt
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
1
00:00:18,910 --> 00:00:22,502
PROFESSOR: All right, well, we've seen how the query language works.
2
00:00:22,502 --> 00:00:26,280
Now, let's talk about how it's implemented.
3
00:00:26,280 --> 00:00:29,470
You already pretty much can guess what's going on there.
4
00:00:29,470 --> 00:00:32,810
At the bottom of it, there's a pattern matcher.
5
00:00:32,810 --> 00:00:38,110
And we looked at a pattern matcher when we did the rule-based control language.
6
00:00:38,110 --> 00:00:41,520
Just to remind you, here are some sample patterns.
7
00:00:41,520 --> 00:00:50,650
This is a pattern that will match any list of three things of which the first is a and the second is c and the middle one can be anything.
8
00:00:50,650 --> 00:00:54,050
So in this little pattern-matching syntax, there's only one distinction you make.
9
00:00:54,050 --> 00:00:59,080
There's either literal things or variables, and variables begin with question mark.
10
00:01:01,370 --> 00:01:06,500
So this matches any list of three things of which the first is a and the second is c.
11
00:01:06,500 --> 00:01:12,530
This one matches any list of three things of which the first is the symbol job.
12
00:01:12,530 --> 00:01:14,210
The second can be anything.
13
00:01:14,210 --> 00:01:20,480
And the third is a list of two things of which the first is the symbol computer and the second can be anything.
14
00:01:20,480 --> 00:01:37,860
And this one, this next one matches any list of three things, and the only difference is, here, the third list, the first is the symbol computer, and then there's some rest of the list. So this means two elements and this means arbitrary number.
15
00:01:37,860 --> 00:01:44,050
And our language implementation isn't even going to have to worry about implementing this dot because that's automatically done by Lisp's reader.
16
00:01:48,340 --> 00:01:50,310
Remember matchers also have some consistency in them.
17
00:01:50,310 --> 00:01:54,430
This match is a list of three things of which the first is a.
18
00:01:54,430 --> 00:01:57,940
And the second and third can be anything, but they have to be the same thing.
19
00:01:57,940 --> 00:01:59,600
They're both called x.
20
00:01:59,600 --> 00:02:05,590
And this matches a list of four things of which the first is the fourth and the second is the same as the third.
21
00:02:05,590 --> 00:02:09,685
And this last one matches any list that begins with a.
22
00:02:09,685 --> 00:02:14,040
The first thing is a, and the rest can be anything.
23
00:02:14,040 --> 00:02:18,780
So that's just a review of pattern matcher syntax that you've already seen.
24
00:02:18,780 --> 00:02:22,740
And remember, that's implemented by some procedure called match.
25
00:02:24,870 --> 00:02:35,695
And match takes a pattern and some data and a dictionary.
26
00:02:43,200 --> 00:02:58,160
And match asks the question is there any way to match this pattern against this data object subject to the bindings that are already in this dictionary?
27
00:02:58,160 --> 00:03:22,010
So, for instance, if we're going to match the pattern x, y, y, x against the data a, b, b, a subject to a dictionary, that says x equals a.
28
00:03:22,010 --> 00:03:25,260
Then the matcher would say, yes, that's consistent.
29
00:03:25,260 --> 00:03:30,320
These match, and it's consistent with what's in the dictionary to say that x equals a.
30
00:03:30,320 --> 00:03:39,490
And the result of the match is the extended dictionary that says x equals a and y equals b.
31
00:03:39,490 --> 00:03:46,840
So a matcher takes in pattern data dictionary, puts out an extended dictionary if it matches, or if it doesn't match, says that it fails.
32
00:03:46,840 --> 00:04:06,665
So, for example, if I use the same pattern here, if I say this x, y, y, x match a, b, b, a with the dictionary y equals a, then the matcher would put out fail.
33
00:04:12,150 --> 00:04:21,190
Well, you've already seen the code for a pattern matcher so I'm not going to go over it, but it's the same thing we've been doing before.
34
00:04:21,190 --> 00:04:23,220
You saw that in the system on rule-based control.
35
00:04:23,220 --> 00:04:24,950
It's essentially the same matcher.
36
00:04:24,950 --> 00:04:31,400
In fact, I think the syntax is a little bit simpler because we're not worrying about arbitrary constants and expressions and things.
37
00:04:31,400 --> 00:04:32,690
There's just variables and constants.
38
00:04:35,790 --> 00:04:39,610
OK, well, given that, what's a primitive query?
39
00:04:42,970 --> 00:04:46,720
Primitive query is going to be a rather complicated thing.
40
00:04:46,720 --> 00:05:03,490
It's going to be-- let's think about the query job of x is d dot y.
41
00:05:06,850 --> 00:05:09,400
That's a query we might type in.
42
00:05:09,400 --> 00:05:11,095
That's going to be implemented in the system.
43
00:05:14,270 --> 00:05:15,700
We'll think of it as this little box.
44
00:05:15,700 --> 00:05:18,880
Here's the primitive query.
45
00:05:18,880 --> 00:05:34,030
What this little box is going to do is take in two streams and put out a stream.
46
00:05:34,030 --> 00:05:41,120
So the shape of a primitive query is that it's a thing where two streams come in and one stream goes out.
47
00:05:41,120 --> 00:05:45,925
What these streams are going to be is down here is the database.
48
00:05:51,600 --> 00:06:00,330
So we imagine all the things in the database sort of sitting there in a stream and this thing sucks on them.
49
00:06:00,330 --> 00:06:02,800
So what are some things that might be in the database?
50
00:06:02,800 --> 00:06:25,770
Oh, job of Alyssa is something and some other job is something.
51
00:06:25,770 --> 00:06:32,040
So imagine all of the facts in the database sitting there in the stream.
52
00:06:32,040 --> 00:06:33,400
That's what comes in here.
53
00:06:33,400 --> 00:06:38,510
What comes in here is a stream of dictionaries.
54
00:06:38,510 --> 00:06:48,855
So one particular dictionary might say y equals programmer.
55
00:06:55,470 --> 00:07:11,390
Now, what the query does when it gets in a dictionary from this stream, it finds all possible ways of matching the query against whatever is coming in from the database.
56
00:07:11,390 --> 00:07:27,550
It looks at the query as a pattern, matches it against any fact from the database or all possible ways of finding and matching the database with respect to this dictionary that's coming in.
57
00:07:27,550 --> 00:07:35,110
So for each fact in the database, it calls the matcher using the pattern, fact, and dictionary.
58
00:07:35,110 --> 00:07:40,420
And every time it gets a good match, it puts out the extended dictionary.
59
00:07:40,420 --> 00:07:52,970
So, for example, if this one comes in and it finds a match, out will come a dictionary that in this case will have y equals programmer and x equals something.
60
00:07:56,740 --> 00:08:01,430
y is programmer, x is something, and d is whatever it found.
61
00:08:01,430 --> 00:08:03,520
And that's all.
62
00:08:03,520 --> 00:08:07,980
And, of course, it's going to try this for every fact in the dictionary.
63
00:08:07,980 --> 00:08:09,250
So it might find lots of them.
64
00:08:09,250 --> 00:08:16,355
It might find another one that says y equals programmer and x equals, and d equals.
65
00:08:20,040 --> 00:08:30,470
So for one frame coming in, it might put out-- for one dictionary coming in, it might put out a lot of dictionaries, or it might put out none.
66
00:08:30,470 --> 00:08:39,320
It might have something that wouldn't match like x equals FOO.
67
00:08:39,320 --> 00:08:47,510
This one might not match anything in which case nothing will go into this stream corresponding to this frame.
68
00:08:47,510 --> 00:09:07,570
Or what you might do is put in an empty frame, and an empty frame says try matching all ways-- find all possible ways of matching the query against something in the database subject to no previous restrictions.
69
00:09:07,570 --> 00:09:13,980
And if you think about what that means, that's just the computation that's done when you type in a query right off.
70
00:09:13,980 --> 00:09:16,650
It tries to find all matches.
71
00:09:16,650 --> 00:09:19,370
So a primitive query sets up this mechanism.
72
00:09:19,370 --> 00:09:44,990
And what the language does, when you type in the query at the top level, it takes this mechanism, feeds in one single empty dictionary, and then for each thing that comes out takes the original query and instantiates the result with all the different dictionaries, producing a new stream of instantiated patterns here.
73
00:09:44,990 --> 00:09:48,170
And that's what gets printed on the terminal.
74
00:09:48,170 --> 00:09:53,510
That's the basic mechanism going on there.
75
00:09:53,510 --> 00:09:56,870
Well, why is that so complicated?
76
00:09:56,870 --> 00:10:04,725
You probably can think of a lot simpler ways to arrange this match for a primitive query rather than having all of these streams floating around.
77
00:10:04,725 --> 00:10:10,860
And the answer is-- you probably guess already.
78
00:10:10,860 --> 00:10:17,790
The answer is this thing extends elegantly to implement the means of combination.
79
00:10:17,790 --> 00:10:22,470
So, for instance, suppose I don't only want to do this.
80
00:10:22,470 --> 00:10:27,230
I don't want to say who to be everybody's job description.
81
00:10:27,230 --> 00:10:48,800
Suppose I want to say AND the job of x is d dot y and the supervisor of x is z.
82
00:10:48,800 --> 00:11:08,700
Now, supervisor of x is z is going to be another primitive query that has the same shape to take in a stream of data objects, a stream of initial dictionaries, which are the restrictions to try and use when you match, and it's going to put out a stream of dictionaries.
83
00:11:08,700 --> 00:11:11,680
So that's what this primitive query looks like.
84
00:11:11,680 --> 00:11:12,910
And how do I implement the AND?
85
00:11:12,910 --> 00:11:13,450
Well, it's simple.
86
00:11:13,450 --> 00:11:14,880
I just hook them together.
87
00:11:14,880 --> 00:11:19,830
I take the output of this one, and I put that to the input of that one.
88
00:11:19,830 --> 00:11:21,545
And I take the dictionary here and I fan it out.
89
00:11:26,570 --> 00:11:37,920
And then you see how that's going to work, because what's going to happen is a frame will now come in here, which has a binding for x, y, and d.
90
00:11:37,920 --> 00:11:56,080
And then when this one gets it, it'll say, oh, gee, subject to these restrictions, which now already have values in the dictionary for y and x and d, it looks in the database and says, gee, can I find any supervisor facts?
91
00:11:56,080 --> 00:12:09,340
And if it finds any, out will come dictionaries which have bindings for y and x and d and z now.
92
00:12:12,070 --> 00:12:26,470
And then notice that because the frames coming in here have these restrictions, that's the thing that assures that when you do the AND, this x will mean the same thing as that x.
93
00:12:26,470 --> 00:12:34,460
Because by the time something comes floating in here, x has a value that you have to match against consistently.
94
00:12:34,460 --> 00:12:40,710
And then you remember from the code from the matcher, there was something in the way the matcher did dictionaries that arrange consistent matches.
95
00:12:40,710 --> 00:12:44,260
So there's AND.
96
00:12:44,260 --> 00:12:48,570
The important point to notice is the general shape.
97
00:12:48,570 --> 00:12:52,600
Look at what happened: the AND of two queries, say, P and Q.
98
00:12:52,600 --> 00:13:01,190
Here's P and Q. The AND of two queries, well, it looks like this.
99
00:13:01,190 --> 00:13:10,230
Each query takes in a stream from the database, a stream of inputs, and puts out a stream of outputs.
100
00:13:10,230 --> 00:13:32,360
And the important point to notice is that if I draw a box around this thing and say this is AND of P and Q, then that box has exactly the same overall shape.
101
00:13:32,360 --> 00:13:34,200
It's something that takes in a stream from the database.
102
00:13:34,200 --> 00:13:38,160
Here it's going to get fanned out inside, but from the outside you don't see that.
103
00:13:38,160 --> 00:13:42,230
It takes an input stream and puts out an output stream.
104
00:13:42,230 --> 00:13:43,570
So this is AND.
105
00:13:43,570 --> 00:13:46,020
And then similarly, OR would look like this.
106
00:13:46,020 --> 00:13:49,840
OR would-- although I didn't show you examples of OR.
107
00:13:49,840 --> 00:13:58,070
OR would say can I find all ways of matching P or Q. So I have P and Q. Each will have their shape.
108
00:14:04,460 --> 00:14:12,500
And the way OR is implemented is I'll take my database stream.
109
00:14:12,500 --> 00:14:13,490
I'll fan it out.
110
00:14:13,490 --> 00:14:21,980
I'll put one into P and one into Q. I'll take my initial query stream coming in and fan it out.
111
00:14:26,750 --> 00:14:41,080
So I'll look at all the answers I might get from P and all the answers I might get from Q, and I'll put them through some sort of thing that appends them or merges the result into one stream, and that's what will come out.
112
00:14:41,080 --> 00:14:48,240
And this whole thing from the outside is OR.
113
00:14:52,350 --> 00:14:56,790
And again, you see it has the same overall shape when looked at from the outside.
114
00:15:01,000 --> 00:15:02,020
What's NOT?
115
00:15:02,020 --> 00:15:04,310
NOT works kind of the same way.
116
00:15:04,310 --> 00:15:14,690
If I have some query P, I take the primitive query for P.
117
00:15:14,690 --> 00:15:20,720
Here, I'm going to implement NOT P. And NOT's just going to act as a filter.
118
00:15:20,720 --> 00:15:39,020
I'll take in the database and my original stream of dictionaries coming in, and what NOT P will do is it will filter these guys.
119
00:15:39,020 --> 00:15:47,460
And the way it will filter it, it will say when I get in a dictionary here, I'll find all the matches, and if I find any, I'll throw it away.
120
00:15:47,460 --> 00:15:55,560
And if I don't find any matches to something coming in here, I'll just pass that through, so NOT is a pure filter.
121
00:15:55,560 --> 00:15:59,980
So AND is-- think of these sort of electoral resistors or something.
122
00:15:59,980 --> 00:16:04,960
AND is series combination and OR is parallel combination.
123
00:16:04,960 --> 00:16:07,460
And then NOT is not going to extend any dictionaries at all.
124
00:16:07,460 --> 00:16:08,750
It's just going to filter it.
125
00:16:08,750 --> 00:16:12,640
It's going to throw away the ones for which it finds a way to match.
126
00:16:12,640 --> 00:16:14,540
And list value is sort of the same way.
127
00:16:14,540 --> 00:16:16,600
The filter's a little more complicated.
128
00:16:16,600 --> 00:16:19,640
It applies to predicate.
129
00:16:19,640 --> 00:16:24,980
The major point to notice here, and it's a major point we've looked at before, is this idea of closure.
130
00:16:28,490 --> 00:16:39,750
The things that we build as a means of combination have the same overall structure as the primitive things that we're combining.
131
00:16:39,750 --> 00:16:44,630
So the AND of two things when looked at from the outside has the same shape.
132
00:16:44,630 --> 00:16:54,950
And what that means is that this box here could be an AND or an OR or a NOT or something because it has the same shape to interface to the larger things.
133
00:16:54,950 --> 00:17:04,170
It's the same thing that allowed us to get complexity in the Escher picture language or allows you to immediately build up these complicated structures just out of pairs.
134
00:17:04,170 --> 00:17:06,280
It's closure.
135
00:17:06,280 --> 00:17:19,260
And that's the thing that allowed me to do what by now you took for granted when I said, gee, there's a query which is AND of job and salary, and I said, oh, there's another one, which is AND of job, a NOT of something.
136
00:17:19,260 --> 00:17:25,230
The fact that I can do that is a direct consequence of this closure principle.
137
00:17:25,230 --> 00:17:29,520
OK, let's break and then we'll go on.
138
00:17:29,520 --> 00:17:30,710
AUDIENCE: Where does the dictionary come from?
139
00:17:30,710 --> 00:17:36,030
PROFESSOR: The dictionary comes initially from what you type in.
140
00:17:36,030 --> 00:17:41,090
So when you start this up, the first thing it does is set up this whole structure.
141
00:17:41,090 --> 00:17:45,000
It puts in one empty dictionary.
142
00:17:45,000 --> 00:17:52,310
And if all you have is one primitive query, then what will come out is a bunch of dictionaries with things filled in.
143
00:17:52,310 --> 00:17:59,710
The general situation that I have here is when this is in the middle of some nest of combined things.
144
00:18:02,380 --> 00:18:03,790
Let's look at the picture over here.
145
00:18:03,790 --> 00:18:06,730
This supervisor query gets in some dictionary.
146
00:18:06,730 --> 00:18:08,730
Where did this one come from?
147
00:18:08,730 --> 00:18:16,260
This dictionary came from the fact that I'm looking at the output of this primitive query.
148
00:18:16,260 --> 00:18:31,770
So maybe to be very specific, if I literally typed in just this query at the top level, this AND, what would actually happen is it would build this structure and start up this whole thing with one empty dictionary.
149
00:18:31,770 --> 00:18:38,640
And now this one would process, and a whole bunch of dictionaries would come out with x, y's and d's in them.
150
00:18:38,640 --> 00:18:40,190
Run it through this one.
151
00:18:40,190 --> 00:18:42,160
So now that's the input to this one.
152
00:18:42,160 --> 00:18:45,040
This one would now put out some other stuff.
153
00:18:45,040 --> 00:18:56,110
And if this itself were buried in some larger thing, like an OR of something, then that would go feed into the next one.
154
00:18:58,560 --> 00:19:07,660
So you initially get only one empty dictionary when you start it, but as you're in the middle of processing these compounds things, that's where these cascades of dictionaries start getting generated.
155
00:19:07,660 --> 00:19:12,280
AUDIENCE: Dictionaries only come about as a result of using the queries?
156
00:19:15,120 --> 00:19:23,220
Or do they become-- do they stay someplace in space like the database does?
157
00:19:23,220 --> 00:19:24,980
Are these temporary items?
158
00:19:24,980 --> 00:19:28,030
PROFESSOR: They're created temporarily in the matcher.
159
00:19:28,030 --> 00:19:29,880
Really, they're someplace in storage.
160
00:19:29,880 --> 00:19:40,950
Initially, someone creates a thing called the empty dictionary that gets initially fed to this match procedure, and then the match procedure builds some dictionaries, and they get passed on and on.
161
00:19:40,950 --> 00:19:43,526
AUDIENCE: OK, so they'll go way after the match?
162
00:19:43,526 --> 00:19:45,930
PROFESSOR: They'll go away when no one needs them again, yeah.
163
00:19:51,900 --> 00:19:56,050
AUDIENCE: It appears that the AND performs some redundant searches of the database.
164
00:19:56,050 --> 00:20:06,700
If the first clause matched, let's say, the third element and not on the first two elements, the second clause is going to look at those first two elements again, discarding them because they don't match.
165
00:20:06,700 --> 00:20:10,000
The match is already in the dictionary.
166
00:20:10,000 --> 00:20:14,450
Would it makes sense to carry the data element from the database along with the dictionary?
167
00:20:17,120 --> 00:20:21,740
PROFESSOR: Well, in general, there are other ways to arrange this search, and there's some analysis that you can do.
168
00:20:21,740 --> 00:20:29,850
I think there's a problem in the book, which talks about a different way that you can cascade AND to eliminate various kinds of redundancies.
169
00:20:29,850 --> 00:20:34,650
This one is meant to be-- was mainly meant to be very simple so you can see how they fit together.
170
00:20:34,650 --> 00:20:35,380
But you're quite right.
171
00:20:35,380 --> 00:20:38,370
There are redundancies here that you can get rid of.
172
00:20:38,370 --> 00:20:41,190
That's another reason why this language is somewhat slow.
173
00:20:41,190 --> 00:20:42,930
There are a lot smarter things you can do.
174
00:20:42,930 --> 00:20:46,840
We're just trying to show you a very simple, in principle, implementation.
175
00:20:51,220 --> 00:20:55,150
AUDIENCE: Did you model this language on Prolog, or did it just come out looking like Prolog?
176
00:21:04,960 --> 00:21:16,120
PROFESSOR: Well, Jerry insulted a whole bunch of people yesterday, so I might as well say that the MIT attitude towards Prolog is something that people did in about 1971 and decided that it wasn't really the right thing and stopped.
177
00:21:16,120 --> 00:21:37,330
So we modeled this on the sort of natural way that this thing was done in about 1971, except at that point, we didn't do it with streams. After we were using it for about six months, we discovered that it had all these problems, some of which I'll talk about later.
178
00:21:37,330 --> 00:21:41,250
And we said, gee, Prolog must have fixed those, and then we found out that it didn't.
179
00:21:41,250 --> 00:21:43,460
So this does about the same thing as Prolog.
180
00:21:43,460 --> 00:21:44,950
AUDIENCE: Does Prolog use streams?
181
00:21:44,950 --> 00:21:46,200
PROFESSOR: No.
182
00:21:48,540 --> 00:21:51,040
In how it behaves, it behaves a lot like Prolog.
183
00:21:51,040 --> 00:21:53,800
Prolog uses a backtracking strategy.
184
00:21:53,800 --> 00:22:20,850
But the other thing that's really good about Prolog that makes it a usable thing is that there's a really very, very well-engineered compiler technology that makes it run fast. So although you saw the merge spitting out these answers very, very slowly, a real Prolog will run very, very fast. Because even though it's sort of doing this, the real work that went into Prolog is a very, very excellent compiler effort.
185
00:22:24,460 --> 00:22:25,710
Let's take a break.
186
00:23:16,650 --> 00:23:26,950
We've looked at the primitive queries and the ways that streams are used to implement the means of combination: AND and OR and NOT.
187
00:23:26,950 --> 00:23:29,580
Now, let go on to the means of abstraction.
188
00:23:29,580 --> 00:23:32,570
Remember, the means of abstraction in this language are rules.
189
00:23:35,150 --> 00:23:48,900
So z is a boss in division d if there's some x who has a job in division d and z is the supervisor of x.
190
00:23:48,900 --> 00:23:52,260
That's what it means for someone to be a boss.
191
00:23:52,260 --> 00:24:33,900
And in effect, if you think about what we're doing with relation to this, there's the query we wrote-- the job of x is in d and the supervisor of x is z-- what we in effect want to do is take this whole mess and draw a box around it and say this whole thing inside the box is boss of z in division d.
192
00:24:33,900 --> 00:24:35,250
That's in effect what we want to do.
193
00:24:38,720 --> 00:25:33,045
So, for instance, if we've done that, and we want to check whether or not it's true that Ben Bitdiddle is a boss in the computer division, so if I want to say boss of Ben Bitdiddle in the computer division, imagine typing that in as query to the system, in effect what we want to do is set up a dictionary here, which has z to Ben Bitdiddle and d to computer.
194
00:25:37,340 --> 00:25:38,720
Where did that dictionary come from?
195
00:25:38,720 --> 00:25:40,710
Let's look at the slide for one second.
196
00:25:40,710 --> 00:25:51,650
That dictionary came from matching the query that said boss of Ben Bitdiddle and computer onto the conclusion of the rule: boss of z and d.
197
00:25:51,650 --> 00:25:54,190
So we match the query to the conclusion of the rule.
198
00:25:54,190 --> 00:26:06,670
That gives us a dictionary, and that's the thing that we would now like to put into this whole big thing and process and see if anything comes out the other side.
199
00:26:06,670 --> 00:26:11,330
If anything comes out, it'll be true.
200
00:26:11,330 --> 00:26:12,370
That's the basic idea.
201
00:26:12,370 --> 00:26:23,580
So in general, the way we implement a rule is we match the conclusion of the rule against something we might want to check it's true.
202
00:26:23,580 --> 00:26:36,470
That match gives us a dictionary, and with respect to that dictionary, we process the body of the rule.
203
00:26:36,470 --> 00:26:43,070
Well, that's really all there is, except for two technical points.
204
00:26:43,070 --> 00:26:47,510
The first technical point is that I might have said something else.
205
00:26:47,510 --> 00:26:52,490
I might have said who's the boss in the computer division?
206
00:26:52,490 --> 00:26:56,270
So I might say boss of who in computer division.
207
00:27:00,329 --> 00:27:18,620
And if I did that, what I would really like to do in effect is start up this dictionary with a match that sort of says, well, d is computer and z is whatever who is.
208
00:27:21,700 --> 00:27:23,220
And our matcher won't quite do that.
209
00:27:23,220 --> 00:27:28,580
That's not quite matching a pattern against data.
210
00:27:28,580 --> 00:27:33,480
It's matching two patterns and saying are they consistent or not or what ways make them consistent.
211
00:27:33,480 --> 00:27:39,740
In other words, what we need is not quite a pattern matcher, but something a little bit more general called a unifier.
212
00:27:44,420 --> 00:27:49,530
And a unifier is a slight generalization of a pattern matcher.
213
00:27:49,530 --> 00:28:05,680
What a unifier does is take two patterns and say what's the most general thing you can substitute for the variables in those two patterns to make them satisfy the pattern simultaneously?
214
00:28:05,680 --> 00:28:08,900
Let me give you an example.
215
00:28:08,900 --> 00:28:43,440
If I have the pattern two-element list, which is x and x, so I have a two-element list where both elements are the same and otherwise I don't care what they are, and I unify that against the pattern that says there's a two-element list, and the first one is a and something in c and the second one is a and b and z, then what the unifier should tell me is, oh yeah, in that dictionary, x has to be a, b, c, and y has to be d and z has to be c.
216
00:28:43,440 --> 00:28:55,420
Those are the restrictions I'd have to put on the values of x, y, and z to make these two unify, or in other words, to make this match x and make this match x.
217
00:28:55,420 --> 00:28:58,540
The unifier should be able to deduce that.
218
00:28:58,540 --> 00:29:01,080
But the unifier may-- there are more complicated things.
219
00:29:01,080 --> 00:29:03,810
I might have said something a little bit more complicated.
220
00:29:03,810 --> 00:29:12,650
I might have said there's a list with two elements, and they're both the same, and they should unify against something of this form.
221
00:29:12,650 --> 00:29:16,890
And the unifier should be able to deduce from that.
222
00:29:16,890 --> 00:29:19,570
Like that y would have to be b. y would have to be b.
223
00:29:19,570 --> 00:29:24,340
Because these two are the same, so y's got to be b.
224
00:29:24,340 --> 00:29:28,940
And v here would have to be a.
225
00:29:28,940 --> 00:29:32,700
And z and w can be anything, but they have to be the same thing.
226
00:29:35,710 --> 00:29:44,680
And x would have to be b, followed by a, followed by whatever w is or whatever z is, which is the same.
227
00:29:44,680 --> 00:29:50,880
So you see, the unifier somehow has to deduce things to unify these patterns.
228
00:29:50,880 --> 00:29:55,850
So you might think there's some kind of magic deduction going on, but there's not.
229
00:29:55,850 --> 00:30:00,150
A unifier is basically a very simple modification of a pattern matcher.
230
00:30:00,150 --> 00:30:08,280
And if you look in the book, you'll see something like three or four lines of code added to the pattern matcher you just saw to handle the symmetric case.
231
00:30:08,280 --> 00:30:14,980
Remember, the pattern matcher has a place where it says is this variable matching a constant.
232
00:30:14,980 --> 00:30:16,420
And if so, it checks in the dictionary.
233
00:30:16,420 --> 00:30:27,030
There's only one other clause in the unifier, which says is this variable matching a variable, in which case you go look in the dictionary and see if that's consistent with what's in the dictionary.
234
00:30:27,030 --> 00:30:45,260
So all the, quote, deduction that's in this language, if you sort of look at it, sort of sits in the rule applications, which, if you look at that, sits in the unifier, which, if you look at that under a microscope, sits essentially in the pattern matcher.
235
00:30:45,260 --> 00:30:47,410
There's no magic at all going on in there.
236
00:30:47,410 --> 00:30:56,030
And the, quote, deduction that you see is just the fact that there's this recursion, which is unwinding the matches bit by bit.
237
00:30:56,030 --> 00:31:02,140
So it looks like this thing is being very clever, but in fact, it's not being very clever at all.
238
00:31:02,140 --> 00:31:04,880
There are cases where a unifier might have to be clever.
239
00:31:04,880 --> 00:31:06,130
Let me show you one more.
240
00:31:11,070 --> 00:31:24,370
Suppose I want to unify a list of two elements, x and x, with a thing that says it's y followed by a dot y.
241
00:31:24,370 --> 00:31:37,330
Now, if you think of what that would have to mean, it would have to mean that x had better be the same as y, but also x had better be the same as a list whose first element is a and whose rest is y.
242
00:31:37,330 --> 00:31:44,710
And if you think about what that would have to mean, it would have to mean that y is the infinite list of a's.
243
00:31:47,500 --> 00:32:01,840
In some sense, in order to do that unification, I have to solve the fixed-point equation cons of a to y is equal to y.
244
00:32:04,570 --> 00:32:07,290
And in general, I wrote a very simple one.
245
00:32:07,290 --> 00:32:15,530
Really doing unification might have to solve an arbitrary fixed-point equation: f of y equals y.
246
00:32:15,530 --> 00:32:20,570
And basically, you can't do that and make the thing finite all the time.
247
00:32:20,570 --> 00:32:25,140
So how does the logic language handle that?
248
00:32:25,140 --> 00:32:26,850
The answer is it doesn't.
249
00:32:26,850 --> 00:32:28,730
It just punts.
250
00:32:28,730 --> 00:32:38,650
And there's a little check in the unifier, which says, oh, is this one of the hard cases which when I go to match things would involve solving a fixed-point equation?