forked from DeathKing/Learning-SICP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec9b.eng.srt
2600 lines (1950 loc) · 72.5 KB
/
lec9b.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:15,840 --> 00:00:29,730
PROFESSOR: Well, I hope you appreciate that we have inducted you into some real magic, the magic of building languages, really building new languages.
2
00:00:29,730 --> 00:00:30,430
What have we looked at?
3
00:00:30,430 --> 00:00:42,360
We've looked at an Escher picture language: this language invented by Peter Henderson.
4
00:00:42,360 --> 00:00:46,260
We looked at digital logic language.
5
00:00:53,260 --> 00:00:53,570
Let's see.
6
00:00:53,570 --> 00:00:55,360
We've looked at the query language.
7
00:00:59,700 --> 00:01:08,250
And the thing you should realize is, even though these were toy examples, they really are the kernels of really useful things.
8
00:01:08,250 --> 00:01:23,300
So, for instance, the Escher picture language was taken by Henry Wu, who's a student at MIT, and developed into a real language for laying out PC boards based just on extending those structures.
9
00:01:23,300 --> 00:01:33,460
And the digital logic language, Jerry mentioned when he showed it to you, was really extended to be used as the basis for a simulator that was used to design a real computer.
10
00:01:33,460 --> 00:01:37,510
And the query language, of course, is kind of the germ of prologue.
11
00:01:37,510 --> 00:01:41,080
So we built all of these languages, they're all based on LISP.
12
00:01:43,630 --> 00:01:48,820
A lot of people ask what particular problems is LISP good for solving for?
13
00:01:48,820 --> 00:02:01,470
The answer is LISP is not good for solving any particular problems. What LISP is good for is constructing within it the right language to solve the problems you want to solve, and that's how you should think about it.
14
00:02:01,470 --> 00:02:04,326
So all of these languages were based on LISP.
15
00:02:04,326 --> 00:02:07,270
Now, what's LISP based on?
16
00:02:07,270 --> 00:02:07,920
Where's that come from?
17
00:02:07,920 --> 00:02:09,400
Well, we looked at that too.
18
00:02:12,740 --> 00:02:25,810
We looked at the meta-circular evaluator and said well, LISP is based on LISP.
19
00:02:25,810 --> 00:02:29,950
And when we start looking at that, we've got to do some real magic, right?
20
00:02:29,950 --> 00:02:31,660
So what does that mean, right?
21
00:02:31,660 --> 00:02:47,470
Why operators, and fixed points, and the idea that what this means is that LISP is somehow the fixed-point equation for this funny set of things which are defined in terms of themselves.
22
00:02:47,470 --> 00:02:49,070
Now, it's real magic.
23
00:02:49,070 --> 00:02:54,250
Well, today, for a final piece of magic, we're going to make all the magic go away.
24
00:03:06,430 --> 00:03:09,770
We already know how to do that.
25
00:03:09,770 --> 00:03:15,500
The idea is, we're going to take the register machine architecture and show how to implement LISP on terms of that.
26
00:03:15,500 --> 00:03:24,800
And, remember, the idea of the register machine is that there's a fixed and finite part of the machine.
27
00:03:24,800 --> 00:03:30,510
There's a finite-state controller, which does some particular thing with a particular amount of hardware.
28
00:03:30,510 --> 00:03:33,550
There are particular data paths: the operation the machine does.
29
00:03:33,550 --> 00:03:42,060
And then, in order to implement recursion and sustain the illusion of infinity, there's some large amount of memory, which is the stack.
30
00:03:42,060 --> 00:03:49,850
So, if we implement LISP in terms of a register machine, then everything ought to become, at this point, completely concrete.
31
00:03:49,850 --> 00:03:51,650
All the magic should go away.
32
00:03:51,650 --> 00:04:04,720
And, by the end of this talk, I want you get the feeling that, as opposed to this very mysterious meta-circular evaluator, that a LISP evaluator really is something that's concrete enough that you can hold in the palm of your hand.
33
00:04:04,720 --> 00:04:09,546
You should be able to imagine holding a LISP interpreter there.
34
00:04:09,546 --> 00:04:10,950
All right, how are we going to do this?
35
00:04:10,950 --> 00:04:13,960
We already have all the ingredients.
36
00:04:13,960 --> 00:04:28,210
See, what you learned last time from Jerry is how to take any particular couple of LISP procedures and hand-translate them into something that runs on a register machine.
37
00:04:28,210 --> 00:04:39,120
So, to implement all of LISP on a register machine, all we have to do is take the particular procedures that are the meta-circular evaluator and hand-translate them for a register machine.
38
00:04:39,120 --> 00:04:42,320
And that does all of LISP, right?
39
00:04:42,320 --> 00:04:45,380
So, in principle, we already know how to do this.
40
00:04:45,380 --> 00:04:54,670
And, indeed, it's going to be no different, in kind, from translating, say, recursive factorial or recursive Fibonacci.
41
00:04:54,670 --> 00:04:56,840
It's just bigger and there's more of it.
42
00:04:56,840 --> 00:05:01,730
So it'd just be more details, but nothing really conceptually new.
43
00:05:01,730 --> 00:05:14,810
All right, also, when we've done that, and the thing is completely explicit, and we see how to implement LISP in terms of the actual sequential register operations, that's going to be our final most explicit model of LISP in this course.
44
00:05:14,810 --> 00:05:16,950
And, remember, that's a progression through this course.
45
00:05:16,950 --> 00:05:20,370
We started out with substitution, which is sort of like algebra.
46
00:05:20,370 --> 00:05:26,390
And then we went to the environment model, which talked about the actual frames and how they got linked together.
47
00:05:26,390 --> 00:05:31,080
And then we made that more concrete in the meta-circular evaluator.
48
00:05:31,080 --> 00:05:34,360
There are things the meta-circular evaluator doesn't tell us.
49
00:05:34,360 --> 00:05:36,090
You should realize that.
50
00:05:36,090 --> 00:05:47,210
For instance, it left unanswered the question of how a procedure, like recursive factorial here , somehow takes space that grows.
51
00:05:47,210 --> 00:05:56,760
On the other hand, a procedure which also looks syntactically recursive, called fact-iter, somehow doesn't take space.
52
00:05:56,760 --> 00:06:01,960
We justify that it doesn't need to take space by showing the substitution model.
53
00:06:01,960 --> 00:06:12,520
But we didn't really say how it happens that the machine manages to do that, that that has to do with the details of how arguments are passed to procedures.
54
00:06:12,520 --> 00:06:23,510
And that's the thing we didn't see in the meta-circular evaluator precisely because the way arguments got passed to procedures in this LISP depended on the way arguments got passed to procedures in this LISP.
55
00:06:26,070 --> 00:06:30,740
But, now, that's going to become extremely explicit.
56
00:06:30,740 --> 00:06:31,230
OK.
57
00:06:31,230 --> 00:06:43,250
Well, before going on to the evaluator, let me just give you a sense of what a whole LISP system looks like so you can see the parts we're going to talk about and the parts we're not going to talk about.
58
00:06:43,250 --> 00:06:52,525
Let's see, over here is a happy LISP user, and the LISP user is talking to something called the reader.
59
00:07:00,360 --> 00:07:19,210
The reader's job in life is to take characters from the user and turn them into data structures in something called a list structure memory.
60
00:07:29,783 --> 00:07:42,340
All right, so the reader is going to take symbols, parentheses, and A's and B's, and ones and threes that you type in, and turn these into actual list structure: pairs, and pointers, and things.
61
00:07:42,340 --> 00:07:45,850
And so, by the time evaluator is going, there are no characters in the world.
62
00:07:45,850 --> 00:07:56,280
And, of course, in more modern list systems, there's sort of a big morass here that might sit between the user and the reader: Windows systems, and top levels, and mice, and all kinds of things.
63
00:07:56,280 --> 00:07:59,590
But conceptually, characters are coming in.
64
00:07:59,590 --> 00:08:17,090
All right, the reader transforms these into pointers to stuff in this memory, and that's what the evaluator sees, OK?
65
00:08:17,090 --> 00:08:19,780
The evaluator has a bunch of helpers.
66
00:08:19,780 --> 00:08:23,080
It has all possible primitive operators you might want.
67
00:08:23,080 --> 00:08:35,960
So there's a completely separate box, a floating point unit, or all sorts of things, which do the primitive operators.
68
00:08:35,960 --> 00:08:42,080
And, if you want more special primitives, you build more primitive operators, but they're separate from the evaluator.
69
00:08:42,080 --> 00:08:47,400
The evaluator finally gets an answer and communicates that to the printer.
70
00:08:50,780 --> 00:09:05,540
And now, the printer's job in life is to take this list structure coming from the evaluator, and turn it back into characters, and communicate them to the user through whatever interface there is.
71
00:09:08,050 --> 00:09:08,810
OK.
72
00:09:08,810 --> 00:09:12,670
Well, today, what we're going to talk about is this evaluator.
73
00:09:12,670 --> 00:09:19,440
The primitive operators have nothing particular to do with LISP, they're however you like to implement primitive operations.
74
00:09:19,440 --> 00:09:24,420
The reader and printer are actually complicated, but we're not going to talk about them.
75
00:09:24,420 --> 00:09:29,900
They sort of have to do with details of how you might build up list structure from characters.
76
00:09:29,900 --> 00:09:32,490
So that is a long story, but we're not going to talk about it.
77
00:09:32,490 --> 00:09:36,930
The list structure memory, we'll talk about next time.
78
00:09:36,930 --> 00:09:46,295
So, pretty much, except for the details of reading and printing, the only mystery that's going to be left after you see the evaluator is how you build list structure on conventional memories.
79
00:09:46,295 --> 00:09:50,580
But we'll worry about that next time too.
80
00:09:50,580 --> 00:09:51,830
OK.
81
00:09:53,350 --> 00:09:56,110
Well, let's start talking about the evaluator.
82
00:09:56,110 --> 00:10:01,120
The one that we're going to show you, of course, is not, I think, nothing special about it.
83
00:10:01,120 --> 00:10:04,810
It's just a particular register machine that runs LISP.
84
00:10:04,810 --> 00:10:09,890
And it has seven registers, and here are the seven registers.
85
00:10:09,890 --> 00:10:18,370
There's a register, called EXP, and its job is to hold the expression to be evaluated.
86
00:10:18,370 --> 00:10:26,550
And by that, I mean it's going to hold a pointer to someplace in list structure memory that holds the expression to be evaluated.
87
00:10:26,550 --> 00:10:34,070
There's a register, called ENV, which holds the environment in which this expression is to be evaluated.
88
00:10:34,070 --> 00:10:34,940
And, again, I made a pointer.
89
00:10:34,940 --> 00:10:38,240
The environment is some data structure.
90
00:10:38,240 --> 00:10:44,630
There's a register, called FUN, which will hold the procedure to be applied when you go to apply a procedure.
91
00:10:44,630 --> 00:10:50,630
A register, called ARGL, which wants the list of evaluated arguments.
92
00:10:50,630 --> 00:10:53,140
What you can start seeing here is the basic structure of the evaluator.
93
00:10:53,140 --> 00:10:54,490
Remember how evaluators work.
94
00:10:54,490 --> 00:11:03,480
There's a piece that takes expressions and environments, and there's a piece that takes functions, or procedures and arguments.
95
00:11:03,480 --> 00:11:07,740
And going back and forth around here is the eval/apply loop.
96
00:11:07,740 --> 00:11:10,270
So those are the basic pieces of the eval and apply.
97
00:11:10,270 --> 00:11:11,610
Then there's some other things, there's continue.
98
00:11:11,610 --> 00:11:19,000
You just saw before how the continue register is used to implement recursion and stack discipline.
99
00:11:19,000 --> 00:11:24,190
There's a register that's going to hold the result of some evaluation.
100
00:11:24,190 --> 00:11:37,150
And then, besides that, there's one temporary register, called UNEV, which typically, in the evaluator, is going to be used to hold temporary pieces of the expression you're working on, which you haven't gotten around to evaluate yet, right?
101
00:11:37,150 --> 00:11:40,646
So there's my machine: a seven-register machine.
102
00:11:40,646 --> 00:11:48,480
And, of course, you might want to make a machine with a lot more registers to get better performance, but this is just a tiny, minimal one.
103
00:11:48,480 --> 00:11:49,780
Well, how about the data paths?
104
00:11:49,780 --> 00:11:55,100
This machine has a lot of special operations for LISP.
105
00:11:55,100 --> 00:12:00,120
So, here are some typical data paths.
106
00:12:00,120 --> 00:12:06,710
A typical one might be, oh, assign to the VAL register the contents of the EXP register.
107
00:12:06,710 --> 00:12:11,900
In terms of those diagrams you saw, that's a little button on some arrow.
108
00:12:11,900 --> 00:12:14,040
Here's a more complicated one.
109
00:12:14,040 --> 00:12:23,850
It says branch, if the thing in the expression register is a conditional to some label here, called the ev-conditional.
110
00:12:23,850 --> 00:12:26,230
And you can imagine this implemented in a lot of different ways.
111
00:12:26,230 --> 00:12:36,610
You might imagine this conditional test as a special purpose sub-routine, and conditional might be represented as some data abstraction that you don't care about at this level of detail.
112
00:12:36,610 --> 00:12:37,980
So that might be done as a sub-routine.
113
00:12:37,980 --> 00:12:45,350
This might be a machine with hardware-types, and conditional might be testing some bits for a particular code.
114
00:12:45,350 --> 00:12:50,190
There are all sorts of ways that's beneath the level of abstraction we're looking at.
115
00:12:50,190 --> 00:12:56,840
Another kind of operation, and there are a lot of different operations assigned to EXP, the first clause of what's in EXP.
116
00:12:56,840 --> 00:12:59,260
This might be part of processing a conditional.
117
00:12:59,260 --> 00:13:04,470
And, again, first clause is some selector whose details we don't care about.
118
00:13:04,470 --> 00:13:12,170
And you can, again, imagine that as a sub-routine which'll do some list operations, or you can imagine that as something that's built directly into hardware.
119
00:13:12,170 --> 00:13:19,740
The reason I keep saying you can imagine it built directly into hardware is even though there are a lot of operations, there are still a fixed number of them.
120
00:13:19,740 --> 00:13:22,370
I forget how many, maybe 150.
121
00:13:22,370 --> 00:13:26,400
So, it's plausible to think of building these directly into hardware.
122
00:13:26,400 --> 00:13:28,500
Here's a more complicated one.
123
00:13:28,500 --> 00:13:31,710
You can see this has to do with looking up the values of variables.
124
00:13:31,710 --> 00:13:42,850
It says assign to the VAL register the result of looking up the variable value of some particular expression, which, in this case, is supposed to be a variable in some environment.
125
00:13:42,850 --> 00:13:52,240
And this'll be some operation that searches through the environment structure, however it is represented, and goes and looks up that variable.
126
00:13:52,240 --> 00:13:55,790
And, again, that's below the level of detail that we're thinking about.
127
00:13:55,790 --> 00:14:00,380
This has to do with the details of the data structures for representing environments.
128
00:14:00,380 --> 00:14:05,940
But, anyway, there is this fixed and finite number of operations in the register machine.
129
00:14:08,500 --> 00:14:11,720
Well, what's its overall structure?
130
00:14:11,720 --> 00:14:14,930
Those are some typical operations.
131
00:14:14,930 --> 00:14:22,890
Remember what we have to do, we have to take the meta-circular evaluator-- and here's a piece of the meta-circular evaluator.
132
00:14:22,890 --> 00:14:28,310
This is the one using abstract syntax that's in the book.
133
00:14:28,310 --> 00:14:33,500
It's a little bit different from the one that Jerry shows you.
134
00:14:33,500 --> 00:14:48,560
And the main thing to remember about the evaluator is that it's doing some sort of case analysis on the kinds of expressions: so if it's either self-evaluated, or quoted, or whatever else.
135
00:14:48,560 --> 00:14:55,750
And then, in the general case where the expression it's looking at is an application, there's some tricky recursions going on.
136
00:14:55,750 --> 00:15:05,880
First of all, eval has to call itself both to evaluate the operator and to evaluate all the operands.
137
00:15:05,880 --> 00:15:12,270
So there's this sort of red recursion of values walking down the tree that's really the easy recursion.
138
00:15:12,270 --> 00:15:14,750
That's just a val walking down this tree of expressions.
139
00:15:14,750 --> 00:15:16,600
Then, in the evaluator, there's a hard recursion.
140
00:15:16,600 --> 00:15:18,200
There's the red to green.
141
00:15:18,200 --> 00:15:19,450
Eval calls apply.
142
00:15:22,470 --> 00:15:30,370
That's the case where evaluating a procedure or argument reduces to applying the procedure to the list of arguments.
143
00:15:30,370 --> 00:15:31,700
And then, apply comes over here.
144
00:15:34,770 --> 00:15:44,560
Apply takes a procedure and arguments and, in the general case where there's a compound procedure, apply goes around and green calls red.
145
00:15:44,560 --> 00:15:48,170
Apply comes around and calls eval again.
146
00:15:48,170 --> 00:15:56,605
Eval's the body of the procedure in the result of extending the environment with the parameters of the procedure by binding the arguments.
147
00:15:59,620 --> 00:16:05,980
Except in the primitive case, where it just calls something else primitive-apply, which is not really the business of the evaluator.
148
00:16:05,980 --> 00:16:17,186
So this sort of red to green, to red to green, that's the eval/apply loop, and that's the thing that we're going to want to see in the evaluator.
149
00:16:19,840 --> 00:16:19,970
All right.
150
00:16:19,970 --> 00:16:27,470
Well, it won't surprise you at all that the two big pieces of this evaluator correspond to eval and apply.
151
00:16:27,470 --> 00:16:32,110
There's a piece called eval-dispatch, and a piece called apply-dispatch.
152
00:16:32,110 --> 00:16:41,870
And, before we get into the details of the code, the way to understand this is to think, again, in terms of these pieces of the evaluator having contracts with the rest of the world.
153
00:16:41,870 --> 00:16:45,780
What do they do from the outside before getting into the grungy details?
154
00:16:45,780 --> 00:16:51,300
Well, the contract for eval-dispatch-- remember, it corresponds to eval.
155
00:16:51,300 --> 00:16:54,100
It's got to evaluate an expression in an environment.
156
00:16:54,100 --> 00:17:03,640
So, in particular, what this one is going to do, eval-dispatch will assume that, when you call it, that the expression you want to evaluate is in the EXP register.
157
00:17:03,640 --> 00:17:09,569
The environment in which you want the evaluation to take place is in the ENV register.
158
00:17:09,569 --> 00:17:13,880
And continue tells you the place where the machine should go next when the evaluation is done.
159
00:17:17,440 --> 00:17:26,619
Eval-dispatch's contract is that it'll actually perform that evaluation, and, at the end of which, it'll end up at the place specified by continue.
160
00:17:26,619 --> 00:17:29,950
The result of the evaluation will be in the VAL register.
161
00:17:29,950 --> 00:17:35,230
And it just warns you, it makes no promises about what happens to the registers.
162
00:17:35,230 --> 00:17:37,490
All other registers might be destroyed.
163
00:17:37,490 --> 00:17:41,790
So, there's one piece, OK?
164
00:17:41,790 --> 00:17:54,540
Together, the pieces, apply-dispatch that corresponds to apply, it's got to apply a procedure to some arguments, so it assumes that this register, ARGL, contains a list of the evaluated arguments.
165
00:17:54,540 --> 00:17:57,220
FUN contains the procedure.
166
00:17:57,220 --> 00:18:01,055
Those correspond to the arguments to the apply procedure in the meta-circular evaluator.
167
00:18:03,970 --> 00:18:21,840
And apply, in this particular evaluator, we're going to use a discipline which says the place the machine should go to next when apply is done is, at the moment apply-dispatch is called at the top of the stack, that's just discipline for the way this particular machine's organized.
168
00:18:21,840 --> 00:18:23,950
And now apply's contract is given all that.
169
00:18:23,950 --> 00:18:25,540
It'll perform the application.
170
00:18:25,540 --> 00:18:28,890
The result of that application will end up in VAL.
171
00:18:28,890 --> 00:18:31,120
The stack will be popped.
172
00:18:31,120 --> 00:18:35,110
And, again, the contents of all the other registers may be destroyed, all right?
173
00:18:35,110 --> 00:18:39,760
So that's the basic organization of this machine.
174
00:18:39,760 --> 00:18:42,700
Let's break for a little bit and see if there are any questions, and then we'll do a real example.
175
00:19:47,850 --> 00:20:03,400
Well, let's take the register machine now, and actually step through, and really, in real detail, so you see completely concrete how some expressions are evaluated, all right?
176
00:20:03,400 --> 00:20:06,435
So, let's start with a very simple expression.
177
00:20:09,620 --> 00:20:13,320
Let's evaluate the expression 1.
178
00:20:18,880 --> 00:20:23,085
And we need an environment, so let's imagine that somewhere there's an environment, we'll call it E,0.
179
00:20:30,260 --> 00:20:38,360
And just, since we'll use these later, we obviously don't really need anything to evaluate 1.
180
00:20:38,360 --> 00:20:49,140
But, just for reference later, let's assume that E,0 has in it an X that's bound to 3 and a Y that's bound to 4, OK?
181
00:20:49,140 --> 00:21:03,560
And now what we're going to do is we're going to evaluate 1 in this environment, and so the ENV register has a pointer to this environment, E,0, all right?
182
00:21:03,560 --> 00:21:05,650
So let's watch that thing go.
183
00:21:05,650 --> 00:21:08,260
What I'm going to do is step through the code.
184
00:21:08,260 --> 00:21:10,080
And, let's see, I'll be the controller.
185
00:21:10,080 --> 00:21:16,830
And now what I need, since this gets rather complicated, is a very little execution unit.
186
00:21:16,830 --> 00:21:22,624
So here's the execution unit, OK?
187
00:21:22,624 --> 00:21:23,874
OK.
188
00:21:27,088 --> 00:21:28,590
OK.
189
00:21:28,590 --> 00:21:30,690
All right, now we're going to start.
190
00:21:30,690 --> 00:21:33,660
We're going to start the machine at eval-dispatch, right?
191
00:21:33,660 --> 00:21:36,120
That's the beginning of this.
192
00:21:36,120 --> 00:21:42,010
Eval-dispatch is going to look at the expression in dispatch, just like eval where we look at the very first thing.
193
00:21:42,010 --> 00:21:47,950
We branch on whether or not this expression is self-evaluating.
194
00:21:47,950 --> 00:21:57,040
Self-evaluating is some abstraction we put into the machine-- it's going to be true for numbers-- to a place called ev-self-eval, right?
195
00:21:57,040 --> 00:22:02,780
So me, being the controller, looks at ev-self-eval, so we'll go over to there.
196
00:22:02,780 --> 00:22:15,220
Ev-self-eval says fine, assign to val whatever is in the expression unit, OK?
197
00:22:15,220 --> 00:22:32,050
And I have a bug because what I didn't do when I initialized this machine is also say what's supposed to happen when it's done, so I should have started out the machine with done being in the continue register, OK?
198
00:22:32,050 --> 00:22:33,640
So we assign to VAL.
199
00:22:33,640 --> 00:22:40,000
And now go to fetch of continue, and now change-- OK.
200
00:22:40,000 --> 00:22:42,160
OK, let's try something harder.
201
00:22:42,160 --> 00:22:56,710
Let's reset the machine here, and we'll put in the expression register, X, OK?
202
00:22:56,710 --> 00:22:59,610
Start again at eval-dispatch.
203
00:22:59,610 --> 00:23:01,690
Check, is it self-evaluating?
204
00:23:01,690 --> 00:23:02,650
No.
205
00:23:02,650 --> 00:23:04,630
Is it a variable?
206
00:23:04,630 --> 00:23:05,560
Yes.
207
00:23:05,560 --> 00:23:08,380
We go off to ev-variable.
208
00:23:08,380 --> 00:23:21,620
It says assign to VAL, look up the variable value in the expression register, OK?
209
00:23:21,620 --> 00:23:23,625
Go to fetch of continue.
210
00:23:23,625 --> 00:23:24,875
PROFESSOR: Done.
211
00:23:27,252 --> 00:23:28,950
PROFESSOR: OK.
212
00:23:28,950 --> 00:23:29,430
All right.
213
00:23:29,430 --> 00:23:31,330
Well, that's the basic idea.
214
00:23:31,330 --> 00:23:32,920
That's a simple operation of the machine.
215
00:23:32,920 --> 00:23:36,070
Now, let's actually do something a little bit more interesting.
216
00:23:36,070 --> 00:23:49,678
Let's look at the expression the sum of x and y.
217
00:23:49,678 --> 00:23:50,130
OK.
218
00:23:50,130 --> 00:23:57,100
And now we'll see how you start unrolling these expression trees, OK?
219
00:23:57,100 --> 00:24:00,645
Well, start again at eval-dispatch, all right?
220
00:24:04,610 --> 00:24:06,060
Self-evaluating?
221
00:24:06,060 --> 00:24:06,810
No.
222
00:24:06,810 --> 00:24:07,280
Variable?
223
00:24:07,280 --> 00:24:07,850
No.
224
00:24:07,850 --> 00:24:13,260
All the other special forms which I didn't write down, like quote, and lambda, and set, and whatever, it's none of those.
225
00:24:13,260 --> 00:24:19,970
It turns out to be an application, so we go off to ev-application, OK?
226
00:24:19,970 --> 00:24:25,580
Ev-application, remember what it's going to do overall.
227
00:24:25,580 --> 00:24:28,310
It is going to evaluate the operator.
228
00:24:28,310 --> 00:24:35,060
It's going to evaluate the arguments, and then it's going to go apply them.
229
00:24:35,060 --> 00:24:55,340
So, before we start, since we're being very literal, we'd better remember that, somewhere in this environment, it's linked to another environment in which plus is bound to the primitive procedure plus before we get an unknown variable in our machine.
230
00:24:55,340 --> 00:24:56,590
OK, so we're at ev-application.
231
00:24:59,850 --> 00:25:07,920
OK, assign to UNEV the operands of what's in the expression register, OK?
232
00:25:07,920 --> 00:25:09,230
Those are the operands.
233
00:25:09,230 --> 00:25:12,916
UNEV's a temporary register where we're going to save them.
234
00:25:12,916 --> 00:25:13,860
PROFESSOR: I'm assigning.
235
00:25:13,860 --> 00:25:18,070
PROFESSOR: Assign to x the operator.
236
00:25:18,070 --> 00:25:25,820
Now, notice we've destroyed that expression in x, but the piece that we need is now in UNEV. OK.
237
00:25:25,820 --> 00:25:28,750
Now, we're going to get set up to recursively evaluate the operator.
238
00:25:28,750 --> 00:25:31,565
Save the continue register on the stack.
239
00:25:34,870 --> 00:25:36,120
Save the environment.
240
00:25:40,520 --> 00:25:54,460
Save UNEV. OK, assign to continue a label called eval-args.
241
00:26:01,400 --> 00:26:01,980
Now, what have we done?
242
00:26:01,980 --> 00:26:04,380
We've set up for a recursive call.
243
00:26:04,380 --> 00:26:06,280
We're about to go to eval-dispatch.
244
00:26:06,280 --> 00:26:10,230
We've set up for a recursive call to eval-dispatch.
245
00:26:10,230 --> 00:26:11,020
What did we do?
246
00:26:11,020 --> 00:26:27,120
We took the things we're going to need later, those operands that were in UNEV; the environment in which we're going to eventually have to, maybe, evaluate those operands; the place we eventually want to go to, which, in this case, was done; we've saved them on the stack.
247
00:26:27,120 --> 00:26:33,550
The reason we saved them on the stack is because eval-dispatch makes no promises about what registers it may destroy.
248
00:26:33,550 --> 00:26:35,020
So all that stuff is saved on the stack.
249
00:26:35,020 --> 00:26:37,380
Now, we've set up eval-dispatch's contract.
250
00:26:37,380 --> 00:26:47,600
There's a new expression, which is the operator plus; a new environment, although, in this case, it's the same one; and a new place to go to when you're done, which is eval-args.