forked from ldct/isicp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
5-4-explicit-control.html
836 lines (674 loc) · 45.3 KB
/
5-4-explicit-control.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="web-worker-interpreter/deps/codemirror/lib/codemirror.css" />
<link rel="stylesheet" type="text/css" href="css/isicp.css" />
<link rel="stylesheet" type="text/css" href="css/footnotes.css" />
<link rel="stylesheet" type="text/css" href="css/theme.css" />
<script src="js/helper.js"></script>
<script src="js/jquery.min.js"></script>
<script src="web-worker-interpreter/deps/codemirror/lib/codemirror.js"></script>
<script src="web-worker-interpreter/deps/codemirror/mode/scheme/scheme.js"></script>
<script src="web-worker-interpreter/coding.js"> </script>
<script>
set_interpreter_path("web-worker-interpreter/");
set_language("scheme");
</script>
<script src="js/footnotes.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {inlineMath: [['$','$']]}
});
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<title> iSICP 2.4 - Multiple Representations for Abstract Data </title>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-36868476-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div id="sidebox">
<div class="tab"></div>
<div class="content">
<p>
<a href="index.html" class="navlink"> <img src='images/home.svg' width=32 height=32> </a>
<span id="toc-link" class="navlink"> <img src='images/list.svg' width=32 height=32> </span>
<span id="currently-editing-link" class="navlink"> <img src='images/file-edit.svg' width=32 height=32> </span>
<script src="http://cdn.jotfor.ms/static/feedback2.js?3.2.310" type="text/javascript">
new JotformFeedback({
formId:'40222623177447',
base:'http://jotform.me/',
windowTitle:'Notify Me',
background:'#FFA500',
fontColor:'#FFFFFF',
type:false,
height:500,
width:700
});
</script>
<a class="lightbox-40222623177447" style="cursor:pointer;color:blue;text-decoration:underline;"><img src='images/envelope.svg' width=32 height=32></a>
<p>
<div id="currently-editing"> </div>
<script>
function hideAll() {
$("#currently-editing").hide();
$("#toc").hide();
}
$("#currently-editing-link").click(function() {
hideAll();
$("#currently-editing").show();
});
$("#toc-link").click(function() {
hideAll();
$("#toc").show();
});
</script>
<div id="toc"> </div>
<p style='font-size:12px'> (Click on the left edge of this green box to hide it!)
<script>
hideAll();
$("#toc").show();
</script>
</div>
</div>
<script>
$('#sidebox .tab').toggle(function(){
$('#sidebox').animate({'right':'0%'});
}, function(){
$('#sidebox').animate({'right':'-30%'});
});
$(document).ready(createTOC);
</script>
<div id="main">
<h2> The Explicit-Control Evaluator </h2>
<p> In section 5-1 we saw how to transform simple Scheme programs into descriptions of register machines. We will now perform this transformation on a more complex program, the metacircular evaluator of sections 4-1-1-4-1-4, which shows how the behavior of a Scheme interpreter can be described in terms of the procedures <tt>eval</tt> and <tt>apply</tt>. The <tt>explicit-control evaluator</tt> that we develop in this section shows how the underlying procedure-calling and argument-passing mechanisms used in the evaluation process can be described in terms of operations on registers and stacks. In addition, the explicit-control evaluator can serve as an implementation of a Scheme interpreter, written in a language that is very similar to the native machine language of conventional computers. The evaluator can be executed by the register-machine simulator of section 5-2. Alternatively, it can be used as a starting point for building a machine-language implementation of a Scheme evaluator, or even a special-purpose machine for evaluating Scheme expressions. Figure 5-16 shows such a hardware implementation: a silicon chip that acts as an evaluator for Scheme. The chip designers started with the data-path and controller specifications for a register machine similar to the evaluator described in this section and used design automation programs to construct the integrated-circuit layout.@footnote{See Batali et al. 1982 for more information on the chip and the method by which it was designed.}
<h4> Registers and operations </h4>
<p> In designing the explicit-control evaluator, we must specify the operations to be used in our register machine. We described the metacircular evaluator in terms of abstract syntax, using procedures such as <tt>quoted?</tt> and <tt>make-procedure</tt>. In implementing the register machine, we could expand these procedures into sequences of elementary list-structure memory operations, and implement these operations on our register machine. However, this would make our evaluator very long, obscuring the basic structure with details. To clarify the presentation, we will include as primitive operations of the register machine the syntax procedures given in section 4-1-2 and the procedures for representing environments and other run-time data given in sections 4-1-3 and 4-1-4. In order to completely specify an evaluator that could be programmed in a low-level machine language or implemented in hardware, we would replace these operations by more elementary operations, using the list-structure implementation we described in section 5-3.
<div class="exercise">
<b>Figure 5.16:</b> A silicon-chip implementation of an
evaluator for Scheme.
[This figure is missing.]
</div>
<p> Our Scheme evaluator register machine includes a stack and seven registers: <tt>exp</tt>, <tt>env</tt>, <tt>val</tt>, <tt>continue</tt>, <tt>proc</tt>, <tt>argl</tt>, and <tt>unev</tt>. <tt>Exp</tt> is used to hold the expression to be evaluated, and <tt>env</tt> contains the environment in which the evaluation is to be performed. At the end of an evaluation, <tt>val</tt> contains the value obtained by evaluating the expression in the designated environment. The <tt>continue</tt> register is used to implement recursion, as explained in section 5-1-4. (The evaluator needs to call itself recursively, since evaluating an expression requires evaluating its subexpressions.) The registers <tt>proc</tt>, <tt>argl</tt>, and <tt>unev</tt> are used in evaluating combinations.
<p> We will not provide a data-path diagram to show how the registers and operations of the evaluator are connected, nor will we give the complete list of machine operations. These are implicit in the evaluator's controller, which will be presented in detail.
<h3> The Core of the Explicit-Control Evaluator </h3>
<p> The central element in the evaluator is the sequence of instructions beginning at <tt>eval-dispatch</tt>. This corresponds to the <tt>eval</tt> procedure of the metacircular evaluator described in section 4-1-1. When the controller starts at <tt>eval-dispatch</tt>, it evaluates the expression specified by <tt>exp</tt> in the environment specified by <tt>env</tt>. When evaluation is complete, the controller will go to the entry point stored in <tt>continue</tt>, and the <tt>val</tt> register will hold the value of the expression. As with the metacircular <tt>eval</tt>, the structure of <tt>eval-dispatch</tt> is a case analysis on the syntactic type of the expression to be evaluated.@footnote{In our controller, the dispatch is written as a sequence of <tt>test</tt> and <tt>branch</tt> instructions. Alternatively, it could have been written in a data-directed style (and in a real system it probably would have been) to avoid the need to perform sequential tests and to facilitate the definition of new expression types. A machine designed to run Lisp would probably include a <tt>dispatch-on-type</tt> instruction that would efficiently execute such data-directed dispatches.}
<div id="">
eval-dispatch
(test (op self-evaluating?) (reg exp))
(branch (label ev-self-eval))
(test (op variable?) (reg exp))
(branch (label ev-variable))
(test (op quoted?) (reg exp))
(branch (label ev-quoted))
(test (op assignment?) (reg exp))
(branch (label ev-assignment))
(test (op definition?) (reg exp))
(branch (label ev-definition))
(test (op if?) (reg exp))
(branch (label ev-if))
(test (op lambda?) (reg exp))
(branch (label ev-lambda))
(test (op begin?) (reg exp))
(branch (label ev-begin))
(test (op application?) (reg exp))
(branch (label ev-application))
(goto (label unknown-expression-type))
</div>
<script>
prompt();
</script>
<h4> Evaluating simple expressions </h4>
<p> Numbers and strings (which are self-evaluating), variables, quotations, and <tt>lambda</tt> expressions have no subexpressions to be evaluated. For these, the evaluator simply places the correct value in the <tt>val</tt> register and continues execution at the entry point specified by <tt>continue</tt>. Evaluation of simple expressions is performed by the following controller code:
<div id="">
ev-self-eval
(assign val (reg exp))
(goto (reg continue))
ev-variable
(assign val (op lookup-variable-value) (reg exp) (reg env))
(goto (reg continue))
ev-quoted
(assign val (op text-of-quotation) (reg exp))
(goto (reg continue))
ev-lambda
(assign unev (op lambda-parameters) (reg exp))
(assign exp (op lambda-body) (reg exp))
(assign val (op make-procedure)
(reg unev) (reg exp) (reg env))
(goto (reg continue))
</div>
<script>
prompt();
</script>
<p> Observe how <tt>ev-lambda</tt> uses the <tt>unev</tt> and <tt>exp</tt> registers to hold the parameters and body of the lambda expression so that they can be passed to the <tt>make-procedure</tt> operation, along with the environment in <tt>env</tt>.
<h4> Evaluating procedure applications </h4>
<p> A procedure application is specified by a combination containing an operator and operands. The operator is a subexpression whose value is a procedure, and the operands are subexpressions whose values are the arguments to which the procedure should be applied. The metacircular <tt>eval</tt> handles applications by calling itself recursively to evaluate each element of the combination, and then passing the results to <tt>apply</tt>, which performs the actual procedure application. The explicit-control evaluator does the same thing; these recursive calls are implemented by <tt>goto</tt> instructions, together with use of the stack to save registers that will be restored after the recursive call returns. Before each call we will be careful to identify which registers must be saved (because their values will be needed later).@footnote{This is an important but subtle point in translating algorithms from a procedural language, such as Lisp, to a register-machine language. As an alternative to saving only what is needed, we could save all the registers (except <tt>val</tt>) before each recursive call. This is called a <tt>framed-stack</tt> discipline. This would work but might save more registers than necessary; this could be an important consideration in a system where stack operations are expensive. Saving registers whose contents will not be needed later may also hold onto useless data that could otherwise be garbage-collected, freeing space to be reused.}
<p> We begin the evaluation of an application by evaluating the operator to produce a procedure, which will later be applied to the evaluated operands. To evaluate the operator, we move it to the <tt>exp</tt> register and go to <tt>eval-dispatch</tt>. The environment in the <tt>env</tt> register is already the correct one in which to evaluate the operator. However, we save <tt>env</tt> because we will need it later to evaluate the operands. We also extract the operands into <tt>unev</tt> and save this on the stack. We set up <tt>continue</tt> so that <tt>eval-dispatch</tt> will resume at <tt>ev-appl-did-operator</tt> after the operator has been evaluated. First, however, we save the old value of <tt>continue</tt>, which tells the controller where to continue after the application.
<div id="">
ev-application
(save continue)
(save env)
(assign unev (op operands) (reg exp))
(save unev)
(assign exp (op operator) (reg exp))
(assign continue (label ev-appl-did-operator))
(goto (label eval-dispatch))
</div>
<script>
prompt();
</script>
<p> Upon returning from evaluating the operator subexpression, we proceed to evaluate the operands of the combination and to accumulate the resulting arguments in a list, held in <tt>argl</tt>. First we restore the unevaluated operands and the environment. We initialize <tt>argl</tt> to an empty list. Then we assign to the <tt>proc</tt> register the procedure that was produced by evaluating the operator. If there are no operands, we go directly to <tt>apply-dispatch</tt>. Otherwise we save <tt>proc</tt> on the stack and start the argument-evaluation loop:@footnote{We add to the evaluator data-structure procedures in section 4-1-3 the following two procedures for manipulating argument lists:
<div id="">
(define (empty-arglist) '())
(define (adjoin-arg arg arglist)
(append arglist (list arg)))
</div>
<script>
prompt();
</script>
<p> We also use an additional syntax procedure to test for the last operand in a combination:
<div id="">
(define (last-operand? ops)
(null? (cdr ops)))
</div>
<script>
prompt();
</script>
}
<div id="">
ev-appl-did-operator
(restore unev) ; the operands
(restore env)
(assign argl (op empty-arglist))
(assign proc (reg val)) ; the operator
(test (op no-operands?) (reg unev))
(branch (label apply-dispatch))
(save proc)
</div>
<script>
prompt();
</script>
<p> Each cycle of the argument-evaluation loop evaluates an operand from the list in <tt>unev</tt> and accumulates the result into <tt>argl</tt>. To evaluate an operand, we place it in the <tt>exp</tt> register and go to <tt>eval-dispatch</tt>, after setting <tt>continue</tt> so that execution will resume with the argument-accumulation phase. But first we save the arguments accumulated so far (held in <tt>argl</tt>), the environment (held in <tt>env</tt>), and the remaining operands to be evaluated (held in <tt>unev</tt>). A special case is made for the evaluation of the last operand, which is handled at <tt>ev-appl-last-arg</tt>.
<div id="">
ev-appl-operand-loop
(save argl)
(assign exp (op first-operand) (reg unev))
(test (op last-operand?) (reg unev))
(branch (label ev-appl-last-arg))
(save env)
(save unev)
(assign continue (label ev-appl-accumulate-arg))
(goto (label eval-dispatch))
</div>
<script>
prompt();
</script>
<p> When an operand has been evaluated, the value is accumulated into the list held in <tt>argl</tt>. The operand is then removed from the list of unevaluated operands in <tt>unev</tt>, and the argument-evaluation continues.
<div id="">
ev-appl-accumulate-arg
(restore unev)
(restore env)
(restore argl)
(assign argl (op adjoin-arg) (reg val) (reg argl))
(assign unev (op rest-operands) (reg unev))
(goto (label ev-appl-operand-loop))
</div>
<script>
prompt();
</script>
<p> Evaluation of the last argument is handled differently. There is no need to save the environment or the list of unevaluated operands before going to <tt>eval-dispatch</tt>, since they will not be required after the last operand is evaluated. Thus, we return from the evaluation to a special entry point <tt>ev-appl-accum-last-arg</tt>, which restores the argument list, accumulates the new argument, restores the saved procedure, and goes off to perform the application.@footnote{The optimization of treating the last operand specially is known as <tt>evlis tail recursion</tt> (see Wand 1980). We could be somewhat more efficient in the argument evaluation loop if we made evaluation of the first operand a special case too. This would permit us to postpone initializing <tt>argl</tt> until after evaluating the first operand, so as to avoid saving <tt>argl</tt> in this case. The compiler in section 5-5 performs this optimization. (Compare the <tt>construct-arglist</tt> procedure of section 5-5-3.)}
<div id="">
ev-appl-last-arg
(assign continue (label ev-appl-accum-last-arg))
(goto (label eval-dispatch))
ev-appl-accum-last-arg
(restore argl)
(assign argl (op adjoin-arg) (reg val) (reg argl))
(restore proc)
(goto (label apply-dispatch))
</div>
<script>
prompt();
</script>
<p> The details of the argument-evaluation loop determine the order in which the interpreter evaluates the operands of a combination (e.g., left to right or right to left---see Exercise 3-8). This order is not determined by the metacircular evaluator, which inherits its control structure from the underlying Scheme in which it is implemented.@footnote{The order of operand evaluation in the metacircular evaluator is determined by the order of evaluation of the arguments to <tt>cons</tt> in the procedure <tt>list-of-values</tt> of section 4-1-1 (see Exercise 4-1).} Because the <tt>first-operand</tt> selector (used in <tt>ev-appl-operand-loop</tt> to extract successive operands from <tt>unev</tt>) is implemented as <tt>car</tt> and the <tt>rest-operands</tt> selector is implemented as <tt>cdr</tt>, the explicit-control evaluator will evaluate the operands of a combination in left-to-right order.
<h4> Procedure application </h4>
<p> The entry point <tt>apply-dispatch</tt> corresponds to the <tt>apply</tt> procedure of the metacircular evaluator. By the time we get to <tt>apply-dispatch</tt>, the <tt>proc</tt> register contains the procedure to apply and <tt>argl</tt> contains the list of evaluated arguments to which it must be applied. The saved value of <tt>continue</tt> (originally passed to <tt>eval-dispatch</tt> and saved at <tt>ev-application</tt>), which tells where to return with the result of the procedure application, is on the stack. When the application is complete, the controller transfers to the entry point specified by the saved <tt>continue</tt>, with the result of the application in <tt>val</tt>. As with the metacircular <tt>apply</tt>, there are two cases to consider. Either the procedure to be applied is a primitive or it is a compound procedure.
<div id="">
apply-dispatch
(test (op primitive-procedure?) (reg proc))
(branch (label primitive-apply))
(test (op compound-procedure?) (reg proc))
(branch (label compound-apply))
(goto (label unknown-procedure-type))
</div>
<script>
prompt();
</script>
<p> We assume that each primitive is implemented so as to obtain its arguments from <tt>argl</tt> and place its result in <tt>val</tt>. To specify how the machine handles primitives, we would have to provide a sequence of controller instructions to implement each primitive and arrange for <tt>primitive-apply</tt> to dispatch to the instructions for the primitive identified by the contents of <tt>proc</tt>. Since we are interested in the structure of the evaluation process rather than the details of the primitives, we will instead just use an <tt>apply-primitive-procedure</tt> operation that applies the procedure in <tt>proc</tt> to the arguments in <tt>argl</tt>. For the purpose of simulating the evaluator with the simulator of section 5-2 we use the procedure <tt>apply-primitive-procedure</tt>, which calls on the underlying Scheme system to perform the application, just as we did for the metacircular evaluator in section 4-1-4. After computing the value of the primitive application, we restore <tt>continue</tt> and go to the designated entry point.
<div id="">
primitive-apply
(assign val (op apply-primitive-procedure)
(reg proc)
(reg argl))
(restore continue)
(goto (reg continue))
</div>
<script>
prompt();
</script>
<p> To apply a compound procedure, we proceed just as with the metacircular evaluator. We construct a frame that binds the procedure's parameters to the arguments, use this frame to extend the environment carried by the procedure, and evaluate in this extended environment the sequence of expressions that forms the body of the procedure. <tt>Ev-sequence</tt>, described below in section 5-4-2, handles the evaluation of the sequence.
<div id="">
compound-apply
(assign unev (op procedure-parameters) (reg proc))
(assign env (op procedure-environment) (reg proc))
(assign env (op extend-environment)
(reg unev) (reg argl) (reg env))
(assign unev (op procedure-body) (reg proc))
(goto (label ev-sequence))
</div>
<script>
prompt();
</script>
<p> <tt>Compound-apply</tt> is the only place in the interpreter where the <tt>env</tt> register is ever assigned a new value. Just as in the metacircular evaluator, the new environment is constructed from the environment carried by the procedure, together with the argument list and the corresponding list of variables to be bound.
<h3> Sequence Evaluation and Tail Recursion </h3>
<p> The portion of the explicit-control evaluator at <tt>ev-sequence</tt> is analogous to the metacircular evaluator's <tt>eval-sequence</tt> procedure. It handles sequences of expressions in procedure bodies or in explicit <tt>begin</tt> expressions.
<p> Explicit <tt>begin</tt> expressions are evaluated by placing the sequence of expressions to be evaluated in <tt>unev</tt>, saving <tt>continue</tt> on the stack, and jumping to <tt>ev-sequence</tt>.
<div id="">
ev-begin
(assign unev (op begin-actions) (reg exp))
(save continue)
(goto (label ev-sequence))
</div>
<script>
prompt();
</script>
<p> The implicit sequences in procedure bodies are handled by jumping to <tt>ev-sequence</tt> from <tt>compound-apply</tt>, at which point <tt>continue</tt> is already on the stack, having been saved at <tt>ev-application</tt>.
<p> The entries at <tt>ev-sequence</tt> and <tt>ev-sequence-continue</tt> form a loop that successively evaluates each expression in a sequence. The list of unevaluated expressions is kept in <tt>unev</tt>. Before evaluating each expression, we check to see if there are additional expressions to be evaluated in the sequence. If so, we save the rest of the unevaluated expressions (held in <tt>unev</tt>) and the environment in which these must be evaluated (held in <tt>env</tt>) and call <tt>eval-dispatch</tt> to evaluate the expression. The two saved registers are restored upon the return from this evaluation, at <tt>ev-sequence-continue</tt>.
<p> The final expression in the sequence is handled differently, at the entry point <tt>ev-sequence-last-exp</tt>. Since there are no more expressions to be evaluated after this one, we need not save <tt>unev</tt> or <tt>env</tt> before going to <tt>eval-dispatch</tt>. The value of the whole sequence is the value of the last expression, so after the evaluation of the last expression there is nothing left to do except continue at the entry point currently held on the stack (which was saved by <tt>ev-application</tt> or <tt>ev-begin</tt>.) Rather than setting up <tt>continue</tt> to arrange for <tt>eval-dispatch</tt> to return here and then restoring <tt>continue</tt> from the stack and continuing at that entry point, we restore <tt>continue</tt> from the stack before going to <tt>eval-dispatch</tt>, so that <tt>eval-dispatch</tt> will continue at that entry point after evaluating the expression.
<div id="">
ev-sequence
(assign exp (op first-exp) (reg unev))
(test (op last-exp?) (reg unev))
(branch (label ev-sequence-last-exp))
(save unev)
(save env)
(assign continue (label ev-sequence-continue))
(goto (label eval-dispatch))
ev-sequence-continue
(restore env)
(restore unev)
(assign unev (op rest-exps) (reg unev))
(goto (label ev-sequence))
ev-sequence-last-exp
(restore continue)
(goto (label eval-dispatch))
</div>
<script>
prompt();
</script>
<h4> Tail recursion </h4>
<p> In Chapter 1 we said that the process described by a procedure such as
<div id="">
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
</div>
<script>
prompt();
</script>
<p> is an iterative process. Even though the procedure is syntactically recursive (defined in terms of itself), it is not logically necessary for an evaluator to save information in passing from one call to <tt>sqrt-iter</tt> to the next.@footnote{We saw in section 5-1 how to implement such a process with a register machine that had no stack; the state of the process was stored in a fixed set of registers.} An evaluator that can execute a procedure such as <tt>sqrt-iter</tt> without requiring increasing storage as the procedure continues to call itself is called a <tt>tail-recursive</tt> evaluator. The metacircular implementation of the evaluator in Chapter 4 does not specify whether the evaluator is tail-recursive, because that evaluator inherits its mechanism for saving state from the underlying Scheme. With the explicit-control evaluator, however, we can trace through the evaluation process to see when procedure calls cause a net accumulation of information on the stack.
<p> Our evaluator is tail-recursive, because in order to evaluate the final expression of a sequence we transfer directly to <tt>eval-dispatch</tt> without saving any information on the stack. Hence, evaluating the final expression in a sequence---even if it is a procedure call (as in <tt>sqrt-iter</tt>, where the <tt>if</tt> expression, which is the last expression in the procedure body, reduces to a call to <tt>sqrt-iter</tt>)---will not cause any information to be accumulated on the stack.@footnote{This implementation of tail recursion in <tt>ev-sequence</tt> is one variety of a well-known optimization technique used by many compilers. In compiling a procedure that ends with a procedure call, one can replace the call by a jump to the called procedure's entry point. Building this strategy into the interpreter, as we have done in this section, provides the optimization uniformly throughout the language.}
<p> If we did not think to take advantage of the fact that it was unnecessary to save information in this case, we might have implemented <tt>eval-sequence</tt> by treating all the expressions in a sequence in the same way---saving the registers, evaluating the expression, returning to restore the registers, and repeating this until all the expressions have been evaluated:@footnote{We can define <tt>no-more-exps?</tt> as follows:
<div id="">
(define (no-more-exps? seq) (null? seq))
</div>
<script>
prompt();
</script>
}
<div id="">
ev-sequence
(test (op no-more-exps?) (reg unev))
(branch (label ev-sequence-end))
(assign exp (op first-exp) (reg unev))
(save unev)
(save env)
(assign continue (label ev-sequence-continue))
(goto (label eval-dispatch))
ev-sequence-continue
(restore env)
(restore unev)
(assign unev (op rest-exps) (reg unev))
(goto (label ev-sequence))
ev-sequence-end
(restore continue)
(goto (reg continue))
</div>
<script>
prompt();
</script>
<p> This may seem like a minor change to our previous code for evaluation of a sequence: The only difference is that we go through the save-restore cycle for the last expression in a sequence as well as for the others. The interpreter will still give the same value for any expression. But this change is fatal to the tail-recursive implementation, because we must now return after evaluating the final expression in a sequence in order to undo the (useless) register saves. These extra saves will accumulate during a nest of procedure calls. Consequently, processes such as <tt>sqrt-iter</tt> will require space proportional to the number of iterations rather than requiring constant space. This difference can be significant. For example, with tail recursion, an infinite loop can be expressed using only the procedure-call mechanism:
<div id="">
(define (count n)
(newline)
(display n)
(count (+ n 1)))
</div>
<script>
prompt();
</script>
<p> Without tail recursion, such a procedure would eventually run out of stack space, and expressing a true iteration would require some control mechanism other than procedure call.
<h3> Conditionals, Assignments, and Definitions </h3>
<p> As with the metacircular evaluator, special forms are handled by selectively evaluating fragments of the expression. For an <tt>if</tt> expression, we must evaluate the predicate and decide, based on the value of predicate, whether to evaluate the consequent or the alternative.
<p> Before evaluating the predicate, we save the <tt>if</tt> expression itself so that we can later extract the consequent or alternative. We also save the environment, which we will need later in order to evaluate the consequent or the alternative, and we save <tt>continue</tt>, which we will need later in order to return to the evaluation of the expression that is waiting for the value of the <tt>if</tt>.
<div id="">
ev-if
(save exp) ; save expression for later
(save env)
(save continue)
(assign continue (label ev-if-decide))
(assign exp (op if-predicate) (reg exp))
(goto (label eval-dispatch)) ; evaluate the predicate
</div>
<script>
prompt();
</script>
<p> When we return from evaluating the predicate, we test whether it was true or false and, depending on the result, place either the consequent or the alternative in <tt>exp</tt> before going to <tt>eval-dispatch</tt>. Notice that restoring <tt>env</tt> and <tt>continue</tt> here sets up <tt>eval-dispatch</tt> to have the correct environment and to continue at the right place to receive the value of the <tt>if</tt> expression.
<div id="">
ev-if-decide
(restore continue)
(restore env)
(restore exp)
(test (op true?) (reg val))
(branch (label ev-if-consequent))
ev-if-alternative
(assign exp (op if-alternative) (reg exp))
(goto (label eval-dispatch))
ev-if-consequent
(assign exp (op if-consequent) (reg exp))
(goto (label eval-dispatch))
</div>
<script>
prompt();
</script>
<h4> Assignments and definitions </h4>
<p> Assignments are handled by <tt>ev-assignment</tt>, which is reached from <tt>eval-dispatch</tt> with the assignment expression in <tt>exp</tt>. The code at <tt>ev-assignment</tt> first evaluates the value part of the expression and then installs the new value in the environment. <tt>Set-variable-value!</tt> is assumed to be available as a machine operation.
<div id="">
ev-assignment
(assign unev (op assignment-variable) (reg exp))
(save unev) ; save variable for later
(assign exp (op assignment-value) (reg exp))
(save env)
(save continue)
(assign continue (label ev-assignment-1))
(goto (label eval-dispatch)) ; evaluate the assignment value
ev-assignment-1
(restore continue)
(restore env)
(restore unev)
(perform
(op set-variable-value!) (reg unev) (reg val) (reg env))
(assign val (const ok))
(goto (reg continue))
</div>
<script>
prompt();
</script>
<p> Definitions are handled in a similar way:
<div id="">
ev-definition
(assign unev (op definition-variable) (reg exp))
(save unev) ; save variable for later
(assign exp (op definition-value) (reg exp))
(save env)
(save continue)
(assign continue (label ev-definition-1))
(goto (label eval-dispatch)) ; evaluate the definition value
ev-definition-1
(restore continue)
(restore env)
(restore unev)
(perform
(op define-variable!) (reg unev) (reg val) (reg env))
(assign val (const ok))
(goto (reg continue))
</div>
<script>
prompt();
</script>
<div class="exercise">
<p> <b>Exercise 5.23:</b> Extend the evaluator to handle derived expressions such as <tt>cond</tt>, <tt>let</tt>, and so on (section 4-1-2). You may ``cheat'' and assume that the syntax transformers such as <tt>cond->if</tt> are available as machine operations.@footnote{This isn't really cheating. In an actual implementation built from scratch, we would use our explicit-control evaluator to interpret a Scheme program that performs source-level transformations like <tt>cond->if</tt> in a syntax phase that runs before execution.}
</div>
<div class="exercise">
<p> <b>Exercise 5.24:</b> Implement <tt>cond</tt> as a new basic special form without reducing it to <tt>if</tt>. You will have to construct a loop that tests the predicates of successive <tt>cond</tt> clauses until you find one that is true, and then use <tt>ev-sequence</tt> to evaluate the actions of the clause.
</div>
<div class="exercise">
<p> <b>Exercise 5.25:</b> Modify the evaluator so that it uses normal-order evaluation, based on the lazy evaluator of section 4-2. </div>
<h3> Running the Evaluator </h3>
<p> With the implementation of the explicit-control evaluator we come to the end of a development, begun in Chapter 1, in which we have explored successively more precise models of the evaluation process. We started with the relatively informal substitution model, then extended this in Chapter 3 to the environment model, which enabled us to deal with state and change. In the metacircular evaluator of Chapter 4, we used Scheme itself as a language for making more explicit the environment structure constructed during evaluation of an expression. Now, with register machines, we have taken a close look at the evaluator's mechanisms for storage management, argument passing, and control. At each new level of description, we have had to raise issues and resolve ambiguities that were not apparent at the previous, less precise treatment of evaluation. To understand the behavior of the explicit-control evaluator, we can simulate it and monitor its performance.
<p> We will install a driver loop in our evaluator machine. This plays the role of the <tt>driver-loop</tt> procedure of section 4-1-4. The evaluator will repeatedly print a prompt, read an expression, evaluate the expression by going to <tt>eval-dispatch</tt>, and print the result. The following instructions form the beginning of the explicit-control evaluator's controller sequence:@footnote{We assume here that <tt>read</tt> and the various printing operations are available as primitive machine operations, which is useful for our simulation, but completely unrealistic in practice. These are actually extremely complex operations. In practice, they would be implemented using low-level input-output operations such as transferring single characters to and from a device.
<p> To support the <tt>get-global-environment</tt> operation we define
<div id="">
(define the-global-environment (setup-environment))
(define (get-global-environment)
the-global-environment)
</div>
<script>
prompt();
</script>
}
<div id="">
read-eval-print-loop
(perform (op initialize-stack))
(perform
(op prompt-for-input) (const ";;; EC-Eval input:"))
(assign exp (op read))
(assign env (op get-global-environment))
(assign continue (label print-result))
(goto (label eval-dispatch))
print-result
(perform
(op announce-output) (const ";;; EC-Eval value:"))
(perform (op user-print) (reg val))
(goto (label read-eval-print-loop))
</div>
<script>
prompt();
</script>
<p> When we encounter an error in a procedure (such as the ``unknown procedure type error'' indicated at <tt>apply-dispatch</tt>), we print an error message and return to the driver loop.@footnote{There are other errors that we would like the interpreter to handle, but these are not so simple. See Exercise 5-30.}
<div id="">
unknown-expression-type
(assign val (const unknown-expression-type-error))
(goto (label signal-error))
unknown-procedure-type
(restore continue) ; clean up stack (from <tt>apply-dispatch</tt>)
(assign val (const unknown-procedure-type-error))
(goto (label signal-error))
signal-error
(perform (op user-print) (reg val))
(goto (label read-eval-print-loop))
</div>
<script>
prompt();
</script>
<p> For the purposes of the simulation, we initialize the stack each time through the driver loop, since it might not be empty after an error (such as an undefined variable) interrupts an evaluation.@footnote{We could perform the stack initialization only after errors, but doing it in the driver loop will be convenient for monitoring the evaluator's performance, as described below.}
<p> If we combine all the code fragments presented in sections 5-4-1-5-4-4, we can create an evaluator machine model that we can run using the register-machine simulator of section 5-2.
<div id="">
(define eceval
(make-machine
'(exp env val proc argl continue unev)
eceval-operations
'(
read-eval-print-loop
<<em>entire machine controller as given above</em>>
)))
</div>
<script>
prompt();
</script>
<p> We must define Scheme procedures to simulate the operations used as primitives by the evaluator. These are the same procedures we used for the metacircular evaluator in section 4-1, together with the few additional ones defined in footnotes throughout section 5-4.
<div id="">
(define eceval-operations
(list (list 'self-evaluating? self-evaluating)
<<em>complete list of operations for eceval machine</em>>))
</div>
<script>
prompt();
</script>
<p> Finally, we can initialize the global environment and run the evaluator:
<div id="">
(define the-global-environment (setup-environment))
(start eceval)
;;; EC-Eval input:
(define (append x y)
(if (null? x)
y
(cons (car x)
(append (cdr x) y))))
;;; EC-Eval value:
ok
;;; EC-Eval input:
(append '(a b c) '(d e f))
;;; EC-Eval value:
(a b c d e f)
</div>
<script>
prompt();
</script>
<p> Of course, evaluating expressions in this way will take much longer than if we had directly typed them into Scheme, because of the multiple levels of simulation involved. Our expressions are evaluated by the explicit-control-evaluator machine, which is being simulated by a Scheme program, which is itself being evaluated by the Scheme interpreter.
<h4> Monitoring the performance of the evaluator </h4>
<p> Simulation can be a powerful tool to guide the implementation of evaluators. Simulations make it easy not only to explore variations of the register-machine design but also to monitor the performance of the simulated evaluator. For example, one important factor in performance is how efficiently the evaluator uses the stack. We can observe the number of stack operations required to evaluate various expressions by defining the evaluator register machine with the version of the simulator that collects statistics on stack use (section 5-2-4), and adding an instruction at the evaluator's <tt>print-result</tt> entry point to print the statistics:
<div id="">
print-result
(perform (op print-stack-statistics)); added instruction
(perform
(op announce-output) (const ";;; EC-Eval value:"))
... ; same as before
</div>
<script>
prompt();
</script>
<p> Interactions with the evaluator now look like this:
<div id="">
;;; EC-Eval input:
(define (factorial n)
(if (= n 1)
1
(* (factorial (- n 1)) n)))
(total-pushes = 3 maximum-depth = 3)
;;; EC-Eval value:
ok
;;; EC-Eval input:
(factorial 5)
(total-pushes = 144 maximum-depth = 28)
;;; EC-Eval value:
120
</div>
<script>
prompt();
</script>
<p> Note that the driver loop of the evaluator reinitializes the stack at the start of each interaction, so that the statistics printed will refer only to stack operations used to evaluate the previous expression.
<div class="exercise">
<b>Exercise 5.26:</b> Use the monitored stack to
explore the tail-recursive property of the evaluator (section 5-4-2).
Start the evaluator and define the iterative <tt>factorial</tt> procedure from
section 1-2-1:
<div id="">
(define (factorial n)
(define (iter product counter)
(if (> counter n)
product
(iter (* counter product)
(+ counter 1))))
(iter 1 1))
</div>
<script>
prompt();
</script>
<p> Run the procedure with some small values of n. Record the maximum stack depth and the number of pushes required to compute n! for each of these values.
<ul>
<li>
You will find that the maximum depth required to evaluate n! is independent of n. What is that depth?
</li>
<li>
Determine from your data a formula in terms of n for the total number of push operations used in evaluating n! for any n >= 1. Note that the number of operations used is a linear function of n and is thus determined by two constants.
</li>
</ul>
</div>
<div class="exercise">
<p> <b>Exercise 5.27:</b> For comparison with Exercise 5-26, explore the behavior of the following procedure for computing factorials recursively:
<div id="">
(define (factorial n)
(if (= n 1)
1
(* (factorial (- n 1)) n)))
</div>
<script>
prompt();
</script>
<p> By running this procedure with the monitored stack, determine, as a function of n, the maximum depth of the stack and the total number of pushes used in evaluating n! for n >= 1. (Again, these functions will be linear.) Summarize your experiments by filling in the following table with the appropriate expressions in terms of n:
<pre>
Maximum depth Number of pushes
Recursive
factorial
Iterative
factorial
</pre>
<p> The maximum depth is a measure of the amount of space used by the evaluator in carrying out the computation, and the number of pushes correlates well with the time required.
</div>
<div class="exercise">
<p> <b>Exercise 5.28:</b> Modify the definition of the evaluator by changing <tt>eval-sequence</tt> as described in section 5-4-2 so that the evaluator is no longer tail-recursive. Rerun your experiments from Exercise 5-26 and Exercise 5-27 to demonstrate that both versions of the <tt>factorial</tt> procedure now require space that grows linearly with their input.
</div>
<div class="exercise">
<p> <b>Exercise 5.29:</b> Monitor the stack operations in the tree-recursive Fibonacci computation:
<div id="">
(define (fib n)
(if (< n 2)
n
(+ (fib (- n 1)) (fib (- n 2)))))
</div>
<script>
prompt();
</script>
<ul>
<li>
Give a formula in terms of n for the maximum depth of the stack required to compute <em>Fib</em>(n) for n >= 2. Hint: In section 1-2-2 we argued that the space used by this process grows linearly with n.
</li>
<li>
Give a formula for the total number of pushes used to compute <em>Fib</em>(n) for n >= 2. You should find that the number of pushes (which correlates well with the time used) grows exponentially with n. Hint: Let S(n) be the number of pushes used in computing <em>Fib</em>(n). You should be able to argue that there is a formula that expresses S(n) in terms of S(n - 1), S(n - 2), and some fixed ``overhead''constant k that is independent of n. Give the formula, and say what k is. Then show that S(n) can be expressed as a <em>Fib</em>(n + 1) + b and give the values of a and b.
</li>
</ul>
</div>
<div class="exercise">
<p> <b>Exercise 5.30:</b> Our evaluator currently catches and signals only two kinds of errors---unknown expression types and unknown procedure types. Other errors will take us out of the evaluator read-eval-print loop. When we run the evaluator using the register-machine simulator, these errors are caught by the underlying Scheme system. This is analogous to the computer crashing when a user program makes an error.@footnote{Regrettably, this is the normal state of affairs in conventional compiler-based language systems such as C. In @acronym{UNIX}(tm) the system ``dumps core,'' and in @acronym{DOS}/Windows(tm) it becomes catatonic. The Macintosh(tm) displays a picture of an exploding bomb and offers you the opportunity to reboot the computer---if you're lucky.} It is a large project to make a real error system work, but it is well worth the effort to understand what is involved here.
<ul>
<li>
Errors that occur in the evaluation process, such as an attempt to access an unbound variable, could be caught by changing the lookup operation to make it return a distinguished condition code, which cannot be a possible value of any user variable. The evaluator can test for this condition code and then do what is necessary to go to <tt>signal-error</tt>. Find all of the places in the evaluator where such a change is necessary and fix them. This is lots of work.
</li>
<li>
Much worse is the problem of handling errors that are signaled by applying primitive procedures, such as an attempt to divide by zero or an attempt to extract the <tt>car</tt> of a symbol. In a professionally written high-quality system, each primitive application is checked for safety as part of the primitive. For example, every call to <tt>car</tt> could first check that the argument is a pair. If the argument is not a pair, the application would return a distinguished condition code to the evaluator, which would then report the failure. We could arrange for this in our register-machine simulator by making each primitive procedure check for applicability and returning an appropriate distinguished condition code on failure. Then the <tt>primitive-apply</tt> code in the evaluator can check for the condition code and go to <tt>signal-error</tt> if necessary. Build this structure and make it work. This is a major project.
</li>
</ul>
</div>
<br>
<br>
<hr>
<div id="footnotes">
<h3 id='Notes'> Notes </h3>
</div>
<hr>
<p> <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/deed.en_US" target="_blank"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" /></a><br /> Based on Structure and Interpretation of Computer Programs, a work at <a xmlns:dct="http://purl.org/dc/terms/" href="http://mitpress.mit.edu/sicp/" rel="dct:source" target="_blank">http://mitpress.mit.edu/sicp/</a>.
</div>
<a href="https://github.com/zodiac/isicp" target="_blank"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_green_007200.png" alt="Fork me on GitHub"></a>
</body>
</html>