forked from DeathKing/Learning-SICP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec8b_512kb.mp4.srt
5871 lines (4534 loc) · 92.6 KB
/
lec8b_512kb.mp4.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
0
00:00:00,000 --> 00:00:18,910
1
00:00:18,910 --> 00:00:20,900
PROFESSOR: All right, well,
we've seen how the query
2
00:00:20,900 --> 00:00:22,502
language works.
3
00:00:22,502 --> 00:00:26,280
Now, let's talk about how
it's implemented.
4
00:00:26,280 --> 00:00:29,470
You already pretty much can
guess what's going on there.
5
00:00:29,470 --> 00:00:32,810
At the bottom of it, there's
a pattern matcher.
6
00:00:32,810 --> 00:00:35,180
And we looked at a pattern
matcher when we did the
7
00:00:35,180 --> 00:00:38,110
rule-based control language.
8
00:00:38,110 --> 00:00:41,520
Just to remind you, here are
some sample patterns.
9
00:00:41,520 --> 00:00:45,010
This is a pattern that will
match any list of three things
10
00:00:45,010 --> 00:00:48,930
of which the first is a and the
second is c and the middle
11
00:00:48,930 --> 00:00:50,650
one can be anything.
12
00:00:50,650 --> 00:00:52,310
So in this little
pattern-matching syntax,
13
00:00:52,310 --> 00:00:54,050
there's only one distinction
you make.
14
00:00:54,050 --> 00:00:57,830
There's either literal things
or variables, and variables
15
00:00:57,830 --> 00:00:59,080
begin with question mark.
16
00:00:59,080 --> 00:01:01,370
17
00:01:01,370 --> 00:01:04,900
So this matches any list of
three things of which the
18
00:01:04,900 --> 00:01:06,500
first is a and the
second is c.
19
00:01:06,500 --> 00:01:11,010
This one matches any list of
three things of which the
20
00:01:11,010 --> 00:01:12,530
first is the symbol job.
21
00:01:12,530 --> 00:01:14,210
The second can be anything.
22
00:01:14,210 --> 00:01:16,750
And the third is a list of two
things of which the first is
23
00:01:16,750 --> 00:01:20,480
the symbol computer and the
second can be anything.
24
00:01:20,480 --> 00:01:25,100
And this one, this next one
matches any list of three
25
00:01:25,100 --> 00:01:29,120
things, and the only difference
is, here, the third
26
00:01:29,120 --> 00:01:32,280
list, the first is the symbol
computer, and then there's
27
00:01:32,280 --> 00:01:36,430
some rest of the list. So this
means two elements and this
28
00:01:36,430 --> 00:01:37,860
means arbitrary number.
29
00:01:37,860 --> 00:01:39,996
And our language implementation
isn't even
30
00:01:39,996 --> 00:01:42,310
going to have to worry about
implementing this dot because
31
00:01:42,310 --> 00:01:44,050
that's automatically done
by Lisp's reader.
32
00:01:44,050 --> 00:01:48,340
33
00:01:48,340 --> 00:01:50,310
Remember matchers also have
some consistency in them.
34
00:01:50,310 --> 00:01:53,010
This match is a list of
three things of which
35
00:01:53,010 --> 00:01:54,430
the first is a.
36
00:01:54,430 --> 00:01:56,280
And the second and third can be
anything, but they have to
37
00:01:56,280 --> 00:01:57,940
be the same thing.
38
00:01:57,940 --> 00:01:59,600
They're both called x.
39
00:01:59,600 --> 00:02:02,730
And this matches a list of four
things of which the first
40
00:02:02,730 --> 00:02:05,590
is the fourth and the second
is the same as the third.
41
00:02:05,590 --> 00:02:09,685
And this last one matches any
list that begins with a.
42
00:02:09,685 --> 00:02:14,040
The first thing is a, and the
rest can be anything.
43
00:02:14,040 --> 00:02:16,750
So that's just a review of
pattern matcher syntax that
44
00:02:16,750 --> 00:02:18,780
you've already seen.
45
00:02:18,780 --> 00:02:21,490
And remember, that's implemented
by some procedure
46
00:02:21,490 --> 00:02:22,740
called match.
47
00:02:22,740 --> 00:02:24,870
48
00:02:24,870 --> 00:02:35,695
And match takes a pattern and
some data and a dictionary.
49
00:02:35,695 --> 00:02:43,200
50
00:02:43,200 --> 00:02:50,470
And match asks the question is
there any way to match this
51
00:02:50,470 --> 00:02:55,170
pattern against this data object
subject to the bindings
52
00:02:55,170 --> 00:02:58,160
that are already in
this dictionary?
53
00:02:58,160 --> 00:03:03,200
So, for instance, if we're going
to match the pattern x,
54
00:03:03,200 --> 00:03:18,080
y, y, x against the data a, b,
b, a subject to a dictionary,
55
00:03:18,080 --> 00:03:22,010
that says x equals a.
56
00:03:22,010 --> 00:03:25,260
Then the matcher would say,
yes, that's consistent.
57
00:03:25,260 --> 00:03:28,410
These match, and it's consistent
with what's in the
58
00:03:28,410 --> 00:03:30,320
dictionary to say
that x equals a.
59
00:03:30,320 --> 00:03:34,810
And the result of the match is
the extended dictionary that
60
00:03:34,810 --> 00:03:39,490
says x equals a and
y equals b.
61
00:03:39,490 --> 00:03:42,860
So a matcher takes in pattern
data dictionary, puts out an
62
00:03:42,860 --> 00:03:45,590
extended dictionary if it
matches, or if it doesn't
63
00:03:45,590 --> 00:03:46,840
match, says that it fails.
64
00:03:46,840 --> 00:03:51,620
So, for example, if I use the
same pattern here, if I say
65
00:03:51,620 --> 00:04:02,450
this x, y, y, x match a, b, b, a
with the dictionary y equals
66
00:04:02,450 --> 00:04:06,665
a, then the matcher would
put out fail.
67
00:04:06,665 --> 00:04:12,150
68
00:04:12,150 --> 00:04:15,100
Well, you've already seen the
code for a pattern matcher so
69
00:04:15,100 --> 00:04:19,040
I'm not going to go over it, but
it's the same thing we've
70
00:04:19,040 --> 00:04:21,190
been doing before.
71
00:04:21,190 --> 00:04:23,220
You saw that in the system
on rule-based control.
72
00:04:23,220 --> 00:04:24,950
It's essentially the
same matcher.
73
00:04:24,950 --> 00:04:28,415
In fact, I think the syntax is
a little bit simpler because
74
00:04:28,415 --> 00:04:30,490
we're not worrying about
arbitrary constants and
75
00:04:30,490 --> 00:04:31,400
expressions and things.
76
00:04:31,400 --> 00:04:32,690
There's just variables
and constants.
77
00:04:32,690 --> 00:04:35,790
78
00:04:35,790 --> 00:04:39,610
OK, well, given that, what's
a primitive query?
79
00:04:39,610 --> 00:04:42,970
80
00:04:42,970 --> 00:04:46,720
Primitive query is going to be
a rather complicated thing.
81
00:04:46,720 --> 00:04:48,100
It's going to be--
82
00:04:48,100 --> 00:05:03,490
let's think about the query
job of x is d dot y.
83
00:05:03,490 --> 00:05:06,850
84
00:05:06,850 --> 00:05:09,400
That's a query we
might type in.
85
00:05:09,400 --> 00:05:11,095
That's going to be implemented
in the system.
86
00:05:11,095 --> 00:05:14,270
87
00:05:14,270 --> 00:05:15,700
We'll think of it as
this little box.
88
00:05:15,700 --> 00:05:18,880
Here's the primitive query.
89
00:05:18,880 --> 00:05:32,070
What this little box is going
to do is take in two streams
90
00:05:32,070 --> 00:05:34,030
and put out a stream.
91
00:05:34,030 --> 00:05:37,310
So the shape of a primitive
query is that it's a thing
92
00:05:37,310 --> 00:05:41,120
where two streams come in
and one stream goes out.
93
00:05:41,120 --> 00:05:43,240
What these streams are
going to be is
94
00:05:43,240 --> 00:05:45,925
down here is the database.
95
00:05:45,925 --> 00:05:51,600
96
00:05:51,600 --> 00:05:56,180
So we imagine all the things
in the database sort of
97
00:05:56,180 --> 00:06:00,330
sitting there in a stream and
this thing sucks on them.
98
00:06:00,330 --> 00:06:02,800
So what are some things that
might be in the database?
99
00:06:02,800 --> 00:06:22,440
Oh, job of Alyssa is
something and some
100
00:06:22,440 --> 00:06:25,770
other job is something.
101
00:06:25,770 --> 00:06:29,800
So imagine all of the facts in
the database sitting there in
102
00:06:29,800 --> 00:06:32,040
the stream.
103
00:06:32,040 --> 00:06:33,400
That's what comes in here.
104
00:06:33,400 --> 00:06:38,510
What comes in here is a stream
of dictionaries.
105
00:06:38,510 --> 00:06:48,855
So one particular dictionary
might say y equals programmer.
106
00:06:48,855 --> 00:06:55,470
107
00:06:55,470 --> 00:06:59,170
Now, what the query does when
it gets in a dictionary from
108
00:06:59,170 --> 00:07:06,090
this stream, it finds all
possible ways of matching the
109
00:07:06,090 --> 00:07:11,390
query against whatever is coming
in from the database.
110
00:07:11,390 --> 00:07:15,420
It looks at the query as a
pattern, matches it against
111
00:07:15,420 --> 00:07:20,870
any fact from the database or
all possible ways of finding
112
00:07:20,870 --> 00:07:24,830
and matching the database with
respect to this dictionary
113
00:07:24,830 --> 00:07:27,550
that's coming in.
114
00:07:27,550 --> 00:07:30,940
So for each fact in the
database, it calls the matcher
115
00:07:30,940 --> 00:07:35,110
using the pattern, fact,
and dictionary.
116
00:07:35,110 --> 00:07:38,950
And every time it gets a good
match, it puts out the
117
00:07:38,950 --> 00:07:40,420
extended dictionary.
118
00:07:40,420 --> 00:07:44,610
So, for example, if this one
comes in and it finds a match,
119
00:07:44,610 --> 00:07:48,710
out will come a dictionary that
in this case will have y
120
00:07:48,710 --> 00:07:52,970
equals programmer and
x equals something.
121
00:07:52,970 --> 00:07:56,740
122
00:07:56,740 --> 00:07:59,410
y is programmer, x is
something, and d
123
00:07:59,410 --> 00:08:01,430
is whatever it found.
124
00:08:01,430 --> 00:08:03,520
And that's all.
125
00:08:03,520 --> 00:08:07,240
And, of course, it's going to
try this for every fact in the
126
00:08:07,240 --> 00:08:07,980
dictionary.
127
00:08:07,980 --> 00:08:09,250
So it might find lots of them.
128
00:08:09,250 --> 00:08:14,110
It might find another one that
says y equals programmer and x
129
00:08:14,110 --> 00:08:16,355
equals, and d equals.
130
00:08:16,355 --> 00:08:20,040
131
00:08:20,040 --> 00:08:22,750
So for one frame coming
in, it might put out--
132
00:08:22,750 --> 00:08:24,600
for one dictionary coming in,
it might put out a lot of
133
00:08:24,600 --> 00:08:30,470
dictionaries, or it might
put out none.
134
00:08:30,470 --> 00:08:34,620
It might have something
that wouldn't match
135
00:08:34,620 --> 00:08:39,320
like x equals FOO.
136
00:08:39,320 --> 00:08:42,730
This one might not match
anything in which case nothing
137
00:08:42,730 --> 00:08:47,510
will go into this stream
corresponding to this frame.
138
00:08:47,510 --> 00:08:53,560
Or what you might do is put in
an empty frame, and an empty
139
00:08:53,560 --> 00:08:55,905
frame says try matching
all ways--
140
00:08:55,905 --> 00:08:59,930
141
00:08:59,930 --> 00:09:02,880
find all possible ways of
matching the query against
142
00:09:02,880 --> 00:09:05,470
something in the database
subject to no previous
143
00:09:05,470 --> 00:09:07,570
restrictions.
144
00:09:07,570 --> 00:09:10,620
And if you think about what that
means, that's just the
145
00:09:10,620 --> 00:09:13,980
computation that's done when you
type in a query right off.
146
00:09:13,980 --> 00:09:16,650
It tries to find all matches.
147
00:09:16,650 --> 00:09:19,370
So a primitive query sets
up this mechanism.
148
00:09:19,370 --> 00:09:23,920
And what the language does, when
you type in the query at
149
00:09:23,920 --> 00:09:27,440
the top level, it takes this
mechanism, feeds in one single
150
00:09:27,440 --> 00:09:33,130
empty dictionary, and then for
each thing that comes out
151
00:09:33,130 --> 00:09:39,330
takes the original query and
instantiates the result with
152
00:09:39,330 --> 00:09:41,810
all the different dictionaries,
producing a new
153
00:09:41,810 --> 00:09:44,990
stream of instantiated
patterns here.
154
00:09:44,990 --> 00:09:48,170
And that's what gets printed
on the terminal.
155
00:09:48,170 --> 00:09:53,510
That's the basic mechanism
going on there.
156
00:09:53,510 --> 00:09:56,870
Well, why is that
so complicated?
157
00:09:56,870 --> 00:10:00,310
You probably can think of a lot
simpler ways to arrange
158
00:10:00,310 --> 00:10:03,010
this match for a primitive query
rather than having all
159
00:10:03,010 --> 00:10:04,725
of these streams floating
around.
160
00:10:04,725 --> 00:10:07,290
And the answer is--
161
00:10:07,290 --> 00:10:10,860
you probably guess already.
162
00:10:10,860 --> 00:10:15,660
The answer is this thing extends
elegantly to implement
163
00:10:15,660 --> 00:10:17,790
the means of combination.
164
00:10:17,790 --> 00:10:22,470
So, for instance, suppose I
don't only want to do this.
165
00:10:22,470 --> 00:10:27,230
I don't want to say who to be
everybody's job description.
166
00:10:27,230 --> 00:10:39,140
Suppose I want to say AND the
job of x is d dot y and the
167
00:10:39,140 --> 00:10:48,800
supervisor of x is z.
168
00:10:48,800 --> 00:10:52,550
Now, supervisor of x is z is
going to be another primitive
169
00:10:52,550 --> 00:10:57,830
query that has the same shape
to take in a stream of data
170
00:10:57,830 --> 00:11:02,570
objects, a stream of initial
dictionaries, which are the
171
00:11:02,570 --> 00:11:05,930
restrictions to try and use when
you match, and it's going
172
00:11:05,930 --> 00:11:08,700
to put out a stream
of dictionaries.
173
00:11:08,700 --> 00:11:11,680
So that's what this primitive
query looks like.
174
00:11:11,680 --> 00:11:12,910
And how do I implement
the AND?
175
00:11:12,910 --> 00:11:13,450
Well, it's simple.
176
00:11:13,450 --> 00:11:14,880
I just hook them together.
177
00:11:14,880 --> 00:11:17,790
I take the output of this one,
and I put that to the
178
00:11:17,790 --> 00:11:19,830
input of that one.
179
00:11:19,830 --> 00:11:21,545
And I take the dictionary
here and I fan it out.
180
00:11:21,545 --> 00:11:26,570
181
00:11:26,570 --> 00:11:29,610
And then you see how that's
going to work, because what's
182
00:11:29,610 --> 00:11:32,820
going to happen is a frame will
now come in here, which
183
00:11:32,820 --> 00:11:37,920
has a binding for x, y, and d.
184
00:11:37,920 --> 00:11:40,030
And then when this one gets
it, it'll say, oh, gee,
185
00:11:40,030 --> 00:11:45,530
subject to these restrictions,
which now already have values
186
00:11:45,530 --> 00:11:52,340
in the dictionary for y and
x and d, it looks in the
187
00:11:52,340 --> 00:11:56,080
database and says, gee, can I
find any supervisor facts?
188
00:11:56,080 --> 00:12:00,120
And if it finds any, out will
come dictionaries which have
189
00:12:00,120 --> 00:12:09,340
bindings for y and x
and d and z now.
190
00:12:09,340 --> 00:12:12,070
191
00:12:12,070 --> 00:12:16,430
And then notice that because the
frames coming in here have
192
00:12:16,430 --> 00:12:19,440
these restrictions, that's the
thing that assures that when
193
00:12:19,440 --> 00:12:26,470
you do the AND, this x will mean
the same thing as that x.
194
00:12:26,470 --> 00:12:30,520
Because by the time something
comes floating in here, x has
195
00:12:30,520 --> 00:12:34,460
a value that you have to match
against consistently.
196
00:12:34,460 --> 00:12:36,250
And then you remember from the
code from the matcher, there
197
00:12:36,250 --> 00:12:38,570
was something in the way the
matcher did dictionaries that
198
00:12:38,570 --> 00:12:40,710
arrange consistent matches.
199
00:12:40,710 --> 00:12:44,260
So there's AND.
200
00:12:44,260 --> 00:12:48,570
The important point to notice
is the general shape.
201
00:12:48,570 --> 00:12:52,600
Look at what happened: the AND
of two queries, say, P and Q.
202
00:12:52,600 --> 00:13:00,465
Here's P and Q. The AND
of two queries, well,
203
00:13:00,465 --> 00:13:01,190
it looks like this.
204
00:13:01,190 --> 00:13:05,120
Each query takes in a stream
from the database, a stream of
205
00:13:05,120 --> 00:13:10,230
inputs, and puts out a
stream of outputs.
206
00:13:10,230 --> 00:13:14,320
And the important point to
notice is that if I draw a box
207
00:13:14,320 --> 00:13:26,500
around this thing and say this
is AND of P and Q, then that
208
00:13:26,500 --> 00:13:32,360
box has exactly the same
overall shape.
209
00:13:32,360 --> 00:13:34,200
It's something that takes in
a stream from the database.
210
00:13:34,200 --> 00:13:37,020
Here it's going to get fanned
out inside, but from the
211
00:13:37,020 --> 00:13:38,160
outside you don't see that.
212
00:13:38,160 --> 00:13:42,230