forked from twcamper/sicp-kindle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
book-Z-H-26.html
2400 lines (1829 loc) · 121 KB
/
book-Z-H-26.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
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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http:https://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<!-- Generated from TeX source by tex2page, v 4o,
(c) Dorai Sitaram, http:https://www.cs.rice.edu/~dorai/tex2page -->
<head>
<meta name="generator" content="HTML Tidy for Linux (vers 7 December 2008), see www.w3.org" />
<title>Structure and Interpretation of Computer Programs</title>
<link href="book-Z-C.css" title="default" rel="stylesheet" type="text/css" />
<meta name="robots" content="noindex,follow" />
</head>
<body>
<mbp:pagebreak />
<p><a name="%_sec_4.1"></a></p>
<h2><a href="book-Z-H-4.html#%_toc_%_sec_4.1">4.1 The
Metacircular Evaluator</a></h2>
<p><a name="%_idx_4210"></a> Our evaluator for Lisp will be
implemented as a Lisp program. It may seem circular to think
about evaluating Lisp programs using an evaluator that is itself
implemented in Lisp. However, evaluation is a process, so it is
appropriate to describe the evaluation process using Lisp, which,
after all, is our tool for describing processes.<a href="#footnote_Temp_510" name="call_footnote_Temp_510" id="call_footnote_Temp_510"><sup><small>3</small></sup></a> An
evaluator that is written in the same language <a name="%_idx_4212"></a><a name="%_idx_4214"></a>that it evaluates is
said to be <em>metacircular</em>.</p>
<p><a name="%_idx_4216"></a><a name="%_idx_4218"></a>The
metacircular evaluator is essentially a Scheme formulation of the
environment model of evaluation described in
section <a href="book-Z-H-21.html#%_sec_3.2">3.2</a>. Recall
that the model has two basic parts:</p>
<blockquote>
<p>1. To evaluate a combination (a compound expression other
than a special form), evaluate the subexpressions and then
apply the value of the operator subexpression to the values of
the operand subexpressions.</p>
<p>2. To apply a compound procedure to a set of arguments,
evaluate the body of the procedure in a new environment. To
construct this environment, extend the environment part of the
procedure object by a frame in which the formal parameters of
the procedure are bound to the arguments to which the procedure
is applied.</p>
</blockquote>
<p><a name="%_idx_4220"></a>These two rules describe the essence
of the evaluation process, a basic cycle in which expressions to
be evaluated in environments are reduced to procedures to be
applied to arguments, which in turn are reduced to new
expressions to be evaluated in new environments, and so on, until
we get down to symbols, whose values are looked up in the
environment, and to primitive procedures, which are applied
directly (see figure <a href="#%_fig_4.1">4.1</a>).<a href="#footnote_Temp_511" name="call_footnote_Temp_511" id="call_footnote_Temp_511"><sup><small>4</small></sup></a> This
evaluation cycle will be embodied by the interplay between the
two critical procedures in the evaluator, <tt>eval</tt> and
<tt>apply</tt>, which are described in section <a href="#%_sec_4.1.1">4.1.1</a> (see figure <a href="#%_fig_4.1">4.1</a>).</p>
<p>The implementation of the evaluator will depend upon
procedures that define the <em>syntax</em> of the expressions to
be evaluated. We will use <a name="%_idx_4224"></a>data
abstraction to make the evaluator independent of the
representation of the language. For example, rather than
committing to a choice that an assignment is to be represented by
a list beginning with the symbol <tt>set!</tt> we use an abstract
predicate <tt>assignment?</tt> to test for an assignment, and we
use abstract selectors <tt>assignment-variable</tt> and
<tt>assignment-value</tt> to access the parts of an assignment.
Implementation of expressions will be described in detail in
section <a href="#%_sec_4.1.2">4.1.2</a>. There are also
operations, described in section <a href="#%_sec_4.1.3">4.1.3</a>, that specify the representation of
procedures and environments. For example, <tt>make-procedure</tt>
constructs compound procedures, <tt>lookup-variable-value</tt>
accesses the values of variables, and
<tt>apply-primitive-procedure</tt> applies a primitive procedure
to a given list of arguments.</p>
<p><a name="%_sec_4.1.1"></a></p>
<h3><a href="book-Z-H-4.html#%_toc_%_sec_4.1.1">4.1.1 The Core of
the Evaluator</a></h3>
<p><a name="%_idx_4226"></a> <a name="%_fig_4.1"></a></p>
<div align="left">
<div align="left">
<b>Figure 4.1:</b> The
<tt>eval</tt>-<tt>apply</tt> cycle exposes the essence of a
computer language.
</div>
<table width="100%">
<tr>
<td><img src="ch4-Z-G-1.gif" border="0" /></td>
</tr>
<tr>
<td><a name="%_idx_4228"></a></td>
</tr>
</table>
</div>
<p>The evaluation process can be described as the interplay
between two procedures: <tt>eval</tt> and <tt>apply</tt>.</p>
<p><a name="%_sec_Temp_512"></a></p>
<h4><a href="book-Z-H-4.html#%_toc_%_sec_Temp_512">Eval</a></h4>
<p><a name="%_idx_4230"></a><tt>Eval</tt> takes as arguments an
expression and an environment. It classifies the expression and
directs its evaluation. <tt>Eval</tt> is structured as a case
analysis of the syntactic type of the expression to be evaluated.
In order to keep the procedure general, we express the
determination of the type of an expression abstractly, making no
commitment to any particular <a name="%_idx_4232"></a>representation for the various types of
expressions. Each type of expression has a predicate that tests
for it and an abstract means for selecting its parts. This
<a name="%_idx_4234"></a><a name="%_idx_4236"></a><em>abstract
syntax</em> makes it easy to see how we can change the syntax of
the language by using the same evaluator, but with a different
collection of syntax procedures.</p>
<p><a name="%_sec_Temp_513"></a></p>
<h5><a href="book-Z-H-4.html#%_toc_%_sec_Temp_513">Primitive
expressions</a></h5>
<ul>
<li><a name="%_idx_4238"></a><a name="%_idx_4240"></a>For
self-evaluating expressions, such as numbers, <tt>eval</tt>
returns the expression itself.</li>
<li><tt>Eval</tt> must look up variables in the environment to
find their values.</li>
</ul>
<p><a name="%_sec_Temp_514"></a></p>
<h5><a href="book-Z-H-4.html#%_toc_%_sec_Temp_514">Special
forms</a></h5>
<ul>
<li>For quoted expressions, <tt>eval</tt> returns the
expression that was quoted.</li>
<li>An assignment to (or a definition of) a variable must
recursively call <tt>eval</tt> to compute the new value to be
associated with the variable. The environment must be modified
to change (or create) the binding of the variable.</li>
<li>An <tt>if</tt> expression requires special processing of
its parts, so as to evaluate the consequent if the predicate is
true, and otherwise to evaluate the alternative.</li>
<li>A <tt>lambda</tt> expression must be transformed into an
applicable procedure by packaging together the parameters and
body specified by the <tt>lambda</tt> expression with the
environment of the evaluation.</li>
<li>A <tt>begin</tt> expression requires evaluating its
sequence of expressions in the order in which they appear.</li>
<li>A case analysis (<tt>cond</tt>) is transformed into a nest
of <tt>if</tt> expressions and then evaluated.</li>
</ul>
<p><a name="%_sec_Temp_515"></a></p>
<h5><a href="book-Z-H-4.html#%_toc_%_sec_Temp_515">Combinations</a></h5>
<ul>
<li>For a procedure application, <tt>eval</tt> must recursively
evaluate the operator part and the operands of the combination.
The resulting procedure and arguments are passed to
<tt>apply</tt>, which handles the actual procedure
application.</li>
</ul>
<p><a name="%_idx_4242"></a>Here is the definition of
<tt>eval</tt>:</p>
<p><tt>(define (eval exp env)<br />
(cond ((self-evaluating? exp) exp)<br />
((variable? exp) (lookup-variable-value exp env))<br />
((quoted? exp) (text-of-quotation exp))<br />
((assignment? exp) (eval-assignment exp env))<br />
((definition? exp) (eval-definition exp env))<br />
((if? exp) (eval-if exp env))<br />
((lambda? exp)<br />
(make-procedure (lambda-parameters exp)<br />
(lambda-body exp)<br />
env))<br />
((begin? exp) <br />
(eval-sequence (begin-actions exp) env))<br />
((cond? exp) (eval (cond->if exp) env))<br />
((application? exp)<br />
(apply (eval (operator exp) env)<br />
(list-of-values (operands exp) env)))<br />
(else<br />
(error "Unknown expression type -- EVAL" exp))))<br />
</tt></p>
<p><a name="%_idx_4244"></a><a name="%_idx_4246"></a>For clarity,
<tt>eval</tt> has been implemented as a case analysis using
<tt>cond</tt>. The disadvantage of this is that our procedure
handles only a few distinguishable types of expressions, and no
new ones can be defined without editing the definition of
<tt>eval</tt>. In most Lisp implementations, dispatching on the
type of an expression is done in a data-directed style. This
allows a user to add new types of expressions that <tt>eval</tt>
can distinguish, without modifying the definition of
<tt>eval</tt> itself. (See exercise <a href="#%_thm_4.3">4.3</a>.)</p>
<p><a name="%_sec_Temp_516"></a></p>
<h4><a href="book-Z-H-4.html#%_toc_%_sec_Temp_516">Apply</a></h4>
<p><tt>Apply</tt> takes two arguments, a procedure and a list of
arguments to which the procedure should be applied.
<tt>Apply</tt> classifies procedures into two kinds: It calls
<a name="%_idx_4248"></a><tt>apply-primitive-procedure</tt> to
apply primitives; it applies compound procedures by sequentially
evaluating the expressions that make up the body of the
procedure. The environment for the evaluation of the body of a
compound procedure is constructed by extending the base
environment carried by the procedure to include a frame that
binds the parameters of the procedure to the arguments to which
the procedure is to be applied. Here is the definition of
<tt>apply</tt>:</p>
<p><tt><a name="%_idx_4250"></a>(define (apply procedure arguments)<br />
(cond ((primitive-procedure? procedure)<br />
(apply-primitive-procedure procedure arguments))<br />
((compound-procedure? procedure)<br />
(eval-sequence<br />
(procedure-body procedure)<br />
(extend-environment<br />
(procedure-parameters procedure)<br />
arguments<br />
(procedure-environment procedure))))<br />
(else<br />
(error<br />
"Unknown procedure type -- APPLY" procedure))))<br />
</tt></p>
<p><a name="%_sec_Temp_517"></a></p>
<h4><a href="book-Z-H-4.html#%_toc_%_sec_Temp_517">Procedure
arguments</a></h4>
<p>When <tt>eval</tt> processes a procedure application, it uses
<tt>list-of-values</tt> to produce the list of arguments to which
the procedure is to be applied. <tt>List-of-values</tt> takes as
an argument the operands of the combination. It evaluates each
operand and returns a list of the corresponding values:<a href="#footnote_Temp_518" name="call_footnote_Temp_518" id="call_footnote_Temp_518"><sup><small>5</small></sup></a></p>
<p><tt><a name="%_idx_4256"></a>(define (list-of-values exps env)<br />
(if (no-operands? exps)<br />
'()<br />
(cons (eval (first-operand exps) env)<br />
(list-of-values (rest-operands exps) env))))<br />
</tt></p>
<p><a name="%_sec_Temp_519"></a></p>
<h4><a href="book-Z-H-4.html#%_toc_%_sec_Temp_519">Conditionals</a></h4>
<p><tt>Eval-if</tt> evaluates the predicate part of an
<tt>if</tt> expression in the given environment. If the result is
true, <tt>eval-if</tt> evaluates the consequent, otherwise it
evaluates the alternative:</p>
<p><tt><a name="%_idx_4258"></a>(define (eval-if exp env)<br />
(if (true? (eval (if-predicate exp) env))<br />
(eval (if-consequent exp) env)<br />
(eval (if-alternative exp) env)))<br />
</tt></p>
<p><a name="%_idx_4260"></a>The use of <tt>true?</tt> in
<tt>eval-if</tt> highlights the issue of the connection between
an implemented language and an implementation language. The
<tt>if-predicate</tt> is evaluated in the language being
implemented and thus yields a value in that language. The
interpreter predicate <tt>true?</tt> translates that value into a
value that can be tested by the <tt>if</tt> in the implementation
language: The metacircular representation of truth might not be
the same as that of the underlying Scheme.<a href="#footnote_Temp_520" name="call_footnote_Temp_520" id="call_footnote_Temp_520"><sup><small>6</small></sup></a></p>
<p><a name="%_sec_Temp_521"></a></p>
<h4><a href="book-Z-H-4.html#%_toc_%_sec_Temp_521">Sequences</a></h4>
<p><tt>Eval-sequence</tt> is used by <tt>apply</tt> to evaluate
the sequence of expressions in a procedure body and by
<tt>eval</tt> to evaluate the sequence of expressions in a
<tt>begin</tt> expression. It takes as arguments a sequence of
expressions and an environment, and evaluates the expressions in
the order in which they occur. The value returned is the value of
the final expression.</p>
<p><tt><a name="%_idx_4264"></a>(define (eval-sequence exps env)<br />
(cond ((last-exp? exps) (eval (first-exp exps) env))<br />
(else (eval (first-exp exps) env)<br />
(eval-sequence (rest-exps exps) env))))<br />
</tt></p>
<p><a name="%_sec_Temp_522"></a></p>
<h4><a href="book-Z-H-4.html#%_toc_%_sec_Temp_522">Assignments
and definitions</a></h4>
<p>The following procedure handles assignments to variables. It
calls <tt>eval</tt> to find the value to be assigned and
transmits the variable and the resulting value to
<tt>set-variable-value!</tt> to be installed in the designated
environment.</p>
<p><tt><a name="%_idx_4266"></a>(define (eval-assignment exp env)<br />
(set-variable-value! (assignment-variable exp)<br />
(eval (assignment-value exp) env)<br />
env)<br />
'ok)<br /></tt></p>
<p>Definitions of variables are handled in a similar
manner.<a href="#footnote_Temp_523" name="call_footnote_Temp_523" id="call_footnote_Temp_523"><sup><small>7</small></sup></a></p>
<p><tt><a name="%_idx_4268"></a>(define (eval-definition exp env)<br />
(define-variable! (definition-variable exp)<br />
(eval (definition-value exp) env)<br />
env)<br />
'ok)<br /></tt></p>
<p>We have chosen here to return the symbol <tt>ok</tt> as the
value of an assignment or a definition.<a href="#footnote_Temp_524" name="call_footnote_Temp_524" id="call_footnote_Temp_524"><sup><small>8</small></sup></a></p>
<p><a name="%_thm_4.1"></a> <b>Exercise
4.1.</b> <a name="%_idx_4270"></a><a name="%_idx_4272"></a>Notice that we cannot tell whether the
metacircular evaluator evaluates operands from left to right or
from right to left. Its evaluation order is inherited from the
underlying Lisp: If the arguments to <tt>cons</tt> in
<tt>list-of-values</tt> are evaluated from left to right, then
<tt>list-of-values</tt> will evaluate operands from left to
right; and if the arguments to <tt>cons</tt> are evaluated from
right to left, then <tt>list-of-values</tt> will evaluate
operands from right to left.</p>
<p>Write a version of <tt>list-of-values</tt> that evaluates
operands from left to right regardless of the order of evaluation
in the underlying Lisp. Also write a version of
<tt>list-of-values</tt> that evaluates operands from right to
left.</p>
<p><a name="%_sec_4.1.2"></a></p>
<h3><a href="book-Z-H-4.html#%_toc_%_sec_4.1.2">4.1.2 Representing
Expressions</a></h3>
<p><a name="%_idx_4274"></a><a name="%_idx_4276"></a> <a name="%_idx_4278"></a>The evaluator is reminiscent of the symbolic
differentiation program discussed in section <a href="book-Z-H-16.html#%_sec_2.3.2">2.3.2</a>. Both programs operate
on symbolic expressions. In both programs, the result of
operating on a compound expression is determined by operating
recursively on the pieces of the expression and combining the
results in a way that depends on the type of the expression. In
both programs we used <a name="%_idx_4280"></a>data abstraction
to decouple the general rules of operation from the details of
how expressions are represented. In the differentiation program
this meant that the same differentiation procedure could deal
with algebraic expressions in prefix form, in infix form, or in
some other form. For the evaluator, this means that the syntax of
the language being evaluated is determined solely by the
procedures that classify and extract pieces of expressions.</p>
<p>Here is the specification of the syntax of our language:</p>
<p>¤ The only self-evaluating items are numbers and
strings:</p>
<p><tt><a name="%_idx_4282"></a>(define (self-evaluating? exp)<br />
(cond ((number? exp) true)<br />
((string? exp) true)<br />
(else false)))<br />
</tt></p>
<p>¤ Variables are represented by symbols:</p>
<p><tt><a name="%_idx_4284"></a>(define (variable? exp) (symbol? exp))<br />
</tt></p>
<p>¤ Quotations have the form <tt>(quote
<<em>text-of-quotation</em>>)</tt>:<a href="#footnote_Temp_526" name="call_footnote_Temp_526" id="call_footnote_Temp_526"><sup><small>9</small></sup></a></p>
<p><tt><a name="%_idx_4286"></a>(define (quoted? exp)<br />
(tagged-list? exp 'quote))<br />
<br />
<a name="%_idx_4288"></a>(define (text-of-quotation exp) (cadr exp))<br />
</tt></p>
<p><tt>Quoted?</tt> is defined in terms of the procedure
<tt>tagged-list?</tt>, which identifies lists beginning with a
designated symbol:</p>
<p><tt><a name="%_idx_4290"></a>(define (tagged-list? exp tag)<br />
(if (pair? exp)<br />
(eq? (car exp) tag)<br />
false))<br /></tt></p>
<p>¤ Assignments have the form <tt>(set!
<<em>var</em>> <<em>value</em>>)</tt>:</p>
<p><tt><a name="%_idx_4292"></a>(define (assignment? exp)<br />
(tagged-list? exp 'set!))<br />
<a name="%_idx_4294"></a>(define (assignment-variable exp) (cadr exp))<br />
<a name="%_idx_4296"></a>(define (assignment-value exp) (caddr exp))<br />
</tt></p>
<p>¤ Definitions have the form</p>
<p>
<tt>(define <<em>var</em>> <<em>value</em>>)<br />
</tt></p>
<p>or the form</p>
<p>
<tt>(define (<<em>var</em>> <<em>parameter<sub>1</sub></em>>
<tt>...</tt> <<em>parameter<sub><em>n</em></sub></em>>)<br />
<<em>body</em>>)<br /></tt></p>
<p><a name="%_idx_4298"></a><a name="%_idx_4300"></a>The latter
form (standard procedure definition) is syntactic sugar for</p>
<p><tt>(define <<em>var</em>><br />
(lambda (<<em>parameter<sub>1</sub></em>>
<tt>...</tt> <<em>parameter<sub><em>n</em></sub></em>>)<br />
<<em>body</em>>))<br /></tt></p>
<p>The corresponding syntax procedures are the following:</p>
<p><tt><a name="%_idx_4302"></a>(define (definition? exp)<br />
(tagged-list? exp 'define))<br />
<a name="%_idx_4304"></a>(define (definition-variable exp)<br />
(if (symbol? (cadr exp))<br />
(cadr exp)<br />
(caadr exp)))<br />
<a name="%_idx_4306"></a>(define (definition-value exp)<br />
(if (symbol? (cadr exp))<br />
(caddr exp)<br />
(make-lambda (cdadr exp) <em>; formal parameters</em><br />
(cddr exp)))) <em>; body</em><br />
</tt></p>
<p>¤ <tt>Lambda</tt> expressions are lists that begin with
the symbol <tt>lambda</tt>:</p>
<p><tt><a name="%_idx_4308"></a>(define (lambda? exp) (tagged-list? exp 'lambda))<br />
<a name="%_idx_4310"></a>(define (lambda-parameters exp) (cadr exp))<br />
<a name="%_idx_4312"></a>(define (lambda-body exp) (cddr exp))<br />
</tt></p>
<p>We also provide a constructor for <tt>lambda</tt> expressions,
which is used by <tt>definition-value</tt>, above:</p>
<p><tt><a name="%_idx_4314"></a>(define (make-lambda parameters body)<br />
(cons 'lambda (cons parameters body)))<br />
</tt></p>
<p>¤ Conditionals begin with <tt>if</tt> and have a
predicate, a consequent, and an (optional) alternative. If the
expression has no alternative part, we provide <tt>false</tt> as
the alternative.<a href="#footnote_Temp_527" name="call_footnote_Temp_527" id="call_footnote_Temp_527"><sup><small>10</small></sup></a></p>
<p><tt><a name="%_idx_4316"></a>(define (if? exp) (tagged-list? exp 'if))<br />
<a name="%_idx_4318"></a>(define (if-predicate exp) (cadr exp))<br />
<a name="%_idx_4320"></a>(define (if-consequent exp) (caddr exp))<br />
<a name="%_idx_4322"></a>(define (if-alternative exp)<br />
(if (not (null? (cdddr exp)))<br />
(cadddr exp)<br />
'false))<br /></tt></p>
<p>We also provide a constructor for <tt>if</tt> expressions, to
be used by <tt>cond->if</tt> to transform <tt>cond</tt>
expressions into <tt>if</tt> expressions:</p>
<p><tt><a name="%_idx_4324"></a>(define (make-if predicate consequent alternative)<br />
(list 'if predicate consequent alternative))<br />
</tt></p>
<p>¤ <tt>Begin</tt> packages a sequence of expressions
into a single expression. We include syntax operations on
<tt>begin</tt> expressions to extract the actual sequence from
the <tt>begin</tt> expression, as well as selectors that return
the first expression and the rest of the expressions in the
sequence.<a href="#footnote_Temp_528" name="call_footnote_Temp_528" id="call_footnote_Temp_528"><sup><small>11</small></sup></a></p>
<p><tt><a name="%_idx_4326"></a>(define (begin? exp) (tagged-list? exp 'begin))<br />
<a name="%_idx_4328"></a>(define (begin-actions exp) (cdr exp))<br />
<a name="%_idx_4330"></a>(define (last-exp? seq) (null? (cdr seq)))<br />
<a name="%_idx_4332"></a>(define (first-exp seq) (car seq))<br />
<a name="%_idx_4334"></a>(define (rest-exps seq) (cdr seq))<br />
</tt></p>
<p>We also include a constructor <tt>sequence->exp</tt> (for
use by <tt>cond->if</tt>) that transforms a sequence into a
single expression, using <tt>begin</tt> if necessary:</p>
<p><tt><a name="%_idx_4336"></a>(define (sequence->exp seq)<br />
(cond ((null? seq) seq)<br />
((last-exp? seq) (first-exp seq))<br />
(else (make-begin seq))))<br />
<a name="%_idx_4338"></a>(define (make-begin seq) (cons 'begin seq))<br />
</tt></p>
<p>¤ A procedure application is any compound expression
that is not one of the above expression types. The <tt>car</tt>
of the expression is the operator, and the <tt>cdr</tt> is the
list of operands:</p>
<p><tt><a name="%_idx_4340"></a>(define (application? exp) (pair? exp))<br />
<a name="%_idx_4342"></a>(define (operator exp) (car exp))<br />
<a name="%_idx_4344"></a>(define (operands exp) (cdr exp))<br />
<a name="%_idx_4346"></a>(define (no-operands? ops) (null? ops))<br />
<a name="%_idx_4348"></a>(define (first-operand ops) (car ops))<br />
<a name="%_idx_4350"></a>(define (rest-operands ops) (cdr ops))<br />
</tt></p>
<p><a name="%_sec_Temp_529"></a></p>
<h4><a href="book-Z-H-4.html#%_toc_%_sec_Temp_529">Derived
expressions</a></h4>
<p><a name="%_idx_4352"></a><a name="%_idx_4354"></a><a name="%_idx_4356"></a><a name="%_idx_4358"></a> Some special forms in
our language can be defined in terms of expressions involving
other special forms, rather than being implemented directly. One
example is <tt>cond</tt>, which can be implemented as a nest of
<tt>if</tt> expressions. For example, we can reduce the problem
of evaluating the expression</p>
<p><tt>(cond ((> x 0) x)<br />
((= x 0) (display 'zero) 0)<br />
(else (- x)))<br /></tt></p>
<p>to the problem of evaluating the following expression
involving <tt>if</tt> and <tt>begin</tt> expressions:</p>
<p><tt>(if (> x 0)<br />
x<br />
(if (= x 0)<br />
(begin (display 'zero)<br />
0)<br />
(- x)))<br /></tt></p>
<p>Implementing the evaluation of <tt>cond</tt> in this way
simplifies the evaluator because it reduces the number of special
forms for which the evaluation process must be explicitly
specified.</p>
<p>We include syntax procedures that extract the parts of a
<tt>cond</tt> expression, and a procedure <tt>cond->if</tt>
that transforms <tt>cond</tt> expressions into <tt>if</tt>
expressions. A case analysis begins with <tt>cond</tt> and has a
list of predicate-action clauses. A clause is an <tt>else</tt>
clause if its predicate is the symbol <tt>else</tt>.<a href="#footnote_Temp_530" name="call_footnote_Temp_530" id="call_footnote_Temp_530"><sup><small>12</small></sup></a></p>
<p><tt><a name="%_idx_4360"></a>(define (cond? exp) (tagged-list? exp 'cond))<br />
<a name="%_idx_4362"></a>(define (cond-clauses exp) (cdr exp))<br />
<a name="%_idx_4364"></a>(define (cond-else-clause? clause)<br />
(eq? (cond-predicate clause) 'else))<br />
<a name="%_idx_4366"></a>(define (cond-predicate clause) (car clause))<br />
<a name="%_idx_4368"></a>(define (cond-actions clause) (cdr clause))<br />
<a name="%_idx_4370"></a>(define (cond->if exp)<br />
(expand-clauses (cond-clauses exp)))<br />
<br />
<a name="%_idx_4372"></a>(define (expand-clauses clauses)<br />
(if (null? clauses)<br />
'false <em>; no <tt>else</tt> clause</em><br />
(let ((first (car clauses))<br />
(rest (cdr clauses)))<br />
(if (cond-else-clause? first)<br />
(if (null? rest)<br />
(sequence->exp (cond-actions first))<br />
(error "ELSE clause isn't last -- COND->IF"<br />
clauses))<br />
(make-if (cond-predicate first)<br />
(sequence->exp (cond-actions first))<br />
(expand-clauses rest))))))<br />
</tt></p>
<p>Expressions (such as <tt>cond</tt>) that we choose to
implement as syntactic transformations are called <em>derived
expressions</em>. <tt>Let</tt> expressions are also derived
expressions (see exercise <a href="#%_thm_4.6">4.6</a>).<a href="#footnote_Temp_531" name="call_footnote_Temp_531" id="call_footnote_Temp_531"><sup><small>13</small></sup></a></p>
<p><a name="%_thm_4.2"></a> <b>Exercise
4.2.</b> <a name="%_idx_4384"></a>Louis Reasoner plans
to reorder the <tt>cond</tt> clauses in <tt>eval</tt> so that the
clause for procedure applications appears before the clause for
assignments. He argues that this will make the interpreter more
efficient: Since programs usually contain more applications than
assignments, definitions, and so on, his modified <tt>eval</tt>
will usually check fewer clauses than the original <tt>eval</tt>
before identifying the type of an expression.</p>
<p>a. What is wrong with Louis's plan? (Hint: What will Louis's
evaluator do with the expression <tt>(define x 3)</tt>?)</p>
<p><a name="%_idx_4386"></a>b. Louis is upset that his plan
didn't work. He is willing to go to any lengths to make his
evaluator recognize procedure applications before it checks for
most other kinds of expressions. Help him by changing the syntax
of the evaluated language so that procedure applications start
with <tt>call</tt>. For example, instead of <tt>(factorial
3)</tt> we will now have to write <tt>(call factorial 3)</tt> and
instead of <tt>(+ 1 2)</tt> we will have to write <tt>(call + 1
2)</tt>.</p>
<p><a name="%_thm_4.3"></a> <b>Exercise
4.3.</b> <a name="%_idx_4388"></a><a name="%_idx_4390"></a><a name="%_idx_4392"></a>Rewrite <tt>eval</tt>
so that the dispatch is done in data-directed style. Compare this
with the data-directed differentiation procedure of
exercise <a href="book-Z-H-17.html#%_thm_2.73">2.73</a>.
(You may use the <tt>car</tt> of a compound expression as the
type of the expression, as is appropriate for the syntax
implemented in this section.) .</p>
<p><a name="%_thm_4.4"></a> <b>Exercise
4.4.</b> <a name="%_idx_4394"></a><a name="%_idx_4396"></a><a name="%_idx_4398"></a>Recall the definitions
of the special forms <tt>and</tt> and <tt>or</tt> from
chapter 1:</p>
<ul>
<li><tt>and</tt>: The expressions are evaluated from left to
right. If any expression evaluates to false, false is returned;
any remaining expressions are not evaluated. If all the
expressions evaluate to true values, the value of the last
expression is returned. If there are no expressions then true
is returned.</li>
<li><tt>or</tt>: The expressions are evaluated from left to
right. If any expression evaluates to a true value, that value
is returned; any remaining expressions are not evaluated. If
all expressions evaluate to false, or if there are no
expressions, then false is returned.</li>
</ul>
<p>Install <tt>and</tt> and <tt>or</tt> as new special forms for
the evaluator by defining appropriate syntax procedures and
evaluation procedures <tt>eval-and</tt> and <tt>eval-or</tt>.
Alternatively, show how to implement <tt>and</tt> and <tt>or</tt>
as derived expressions.</p>
<p><a name="%_thm_4.5"></a> <b>Exercise
4.5.</b> <a name="%_idx_4400"></a><a name="%_idx_4402"></a><a name="%_idx_4404"></a>Scheme allows an
additional syntax for <tt>cond</tt> clauses,
<tt>(<<em>test</em>> =>
<<em>recipient</em>>)</tt>. If <<em>test</em>>
evaluates to a true value, then <<em>recipient</em>> is
evaluated. Its value must be a procedure of one argument; this
procedure is then invoked on the value of the
<<em>test</em>>, and the result is returned as the value of
the <tt>cond</tt> expression. For example</p>
<p>
<tt>(cond ((assoc 'b '((a 1) (b 2))) => cadr)<br />
(else false))<br /></tt></p>
<p>returns 2. Modify the handling of <tt>cond</tt> so that it
supports this extended syntax.</p>
<p><a name="%_thm_4.6"></a> <b>Exercise
4.6.</b> <a name="%_idx_4406"></a><tt>Let</tt>
expressions are derived expressions, because</p>
<p>
<tt>(let ((<<em>var<sub>1</sub></em>> <<em>exp<sub>1</sub></em>>) </tt>...
(<<em>var<sub><em>n</em></sub></em>> <<em>exp<sub><em>n</em></sub></em>>))<br />
<<em>body</em>>)<br /></p>
<p>is equivalent to</p>
<p>
<tt>((lambda (<<em>var<sub>1</sub></em>> </tt>...
<<em>var<sub><em>n</em></sub></em>>)<br />
<<em>body</em>>)<br />
<<em>exp<sub>1</sub></em>><br />
<img src="book-Z-G-D-18.gif" border="0" /><br />
<<em>exp<sub><em>n</em></sub></em>>)<br /></p>
<p>Implement a syntactic transformation
<tt>let->combination</tt> that reduces evaluating <tt>let</tt>
expressions to evaluating combinations of the type shown above,
and add the appropriate clause to <tt>eval</tt> to handle
<tt>let</tt> expressions.</p>
<p><a name="%_thm_4.7"></a> <b>Exercise
4.7.</b> <a name="%_idx_4408"></a><a name="%_idx_4410"></a><a name="%_idx_4412"></a><tt>Let*</tt> is
similar to <tt>let</tt>, except that the bindings of the
<tt>let</tt> variables are performed sequentially from left to
right, and each binding is made in an environment in which all of
the preceding bindings are visible. For example</p>
<p><tt>(let* ((x 3)<br />
(y (+ x 2))<br />
(z (+ x y 5)))<br />
(* x z))<br /></tt></p>
<p>returns 39. Explain how a <tt>let*</tt> expression can be
rewritten as a set of nested <tt>let</tt> expressions, and write
a procedure <tt>let*->nested-lets</tt> that performs this
transformation. If we have already implemented <tt>let</tt>
(exercise <a href="#%_thm_4.6">4.6</a>) and we want to
extend the evaluator to handle <tt>let*</tt>, is it sufficient to
add a clause to <tt>eval</tt> whose action is</p>
<p>
<tt>(eval (let*->nested-lets exp) env)<br /></tt></p>
<p>or must we explicitly expand <tt>let*</tt> in terms of
non-derived expressions?</p>
<p><a name="%_thm_4.8"></a> <b>Exercise
4.8.</b> <a name="%_idx_4414"></a><a name="%_idx_4416"></a><a name="%_idx_4418"></a><a name="%_idx_4420"></a>``Named <tt>let</tt>'' is a variant of
<tt>let</tt> that has the form</p>
<p>
<tt>(let <<em>var</em>> <<em>bindings</em>> <<em>body</em>>)<br />
</tt></p>
<p>The <<em>bindings</em>> and <<em>body</em>> are
just as in ordinary <tt>let</tt>, except that
<<em>var</em>> is bound within <<em>body</em>> to a
procedure whose body is <<em>body</em>> and whose
parameters are the variables in the <<em>bindings</em>>.
Thus, one can repeatedly execute the <<em>body</em>> by
invoking the procedure named <<em>var</em>>. For example,
the iterative Fibonacci procedure (section <a href="book-Z-H-11.html#%_sec_1.2.2">1.2.2</a>) can be rewritten using
named <tt>let</tt> as follows:</p>
<p><tt><a name="%_idx_4422"></a>(define (fib n)<br />
(let fib-iter ((a 1)<br />
(b 0)<br />
(count n))<br />
(if (= count 0)<br />
b<br />
(fib-iter (+ a b) a (- count 1)))))<br />
</tt></p>
<p>Modify <tt>let->combination</tt> of exercise <a href="#%_thm_4.6">4.6</a> to also support named <tt>let</tt>.</p>
<p><a name="%_thm_4.9"></a> <b>Exercise
4.9.</b> <a name="%_idx_4424"></a><a name="%_idx_4426"></a>Many languages support a variety of iteration
constructs, such as <tt>do</tt>, <tt>for</tt>, <tt>while</tt>,
and <tt>until</tt>. In Scheme, iterative processes can be
expressed in terms of ordinary procedure calls, so special
iteration constructs provide no essential gain in computational
power. On the other hand, such constructs are often convenient.
Design some iteration constructs, give examples of their use, and
show how to implement them as derived expressions.</p>
<p><a name="%_thm_4.10"></a> <b>Exercise
4.10.</b> <a name="%_idx_4428"></a><a name="%_idx_4430"></a>By using data abstraction, we were able to write
an <tt>eval</tt> procedure that is independent of the particular
syntax of the language to be evaluated. To illustrate this,
design and implement a new syntax for Scheme by modifying the
procedures in this section, without changing <tt>eval</tt> or
<tt>apply</tt>.</p>
<p><a name="%_sec_4.1.3"></a></p>
<h3><a href="book-Z-H-4.html#%_toc_%_sec_4.1.3">4.1.3 Evaluator
Data Structures</a></h3>
<p>In addition to defining the external syntax of expressions,
the evaluator implementation must also define the data structures
that the evaluator manipulates internally, as part of the
execution of a program, such as the representation of procedures
and environments and the representation of true and false.</p>
<p><a name="%_sec_Temp_541"></a></p>
<h4><a href="book-Z-H-4.html#%_toc_%_sec_Temp_541">Testing of
predicates</a></h4>
<p><a name="%_idx_4432"></a>For conditionals, we accept anything
to be true that is not the explicit <tt>false</tt> object.</p>
<p><tt><a name="%_idx_4434"></a>(define (true? x)<br />
(not (eq? x false)))<br />
<a name="%_idx_4436"></a>(define (false? x)<br />
(eq? x false))<br /></tt></p>
<p><a name="%_sec_Temp_542"></a></p>
<h4><a href="book-Z-H-4.html#%_toc_%_sec_Temp_542">Representing
procedures</a></h4>
<p><a name="%_idx_4438"></a> To handle primitives, we assume that
we have available the following procedures:</p>
<ul>
<li><a name="%_idx_4440"></a><tt>(apply-primitive-procedure
<<em>proc</em>> <<em>args</em>>)</tt><br />
applies the given primitive procedure to the argument values in
the list <<em>args</em>> and returns the result of the
application.</li>
<li><a name="%_idx_4442"></a><tt>(primitive-procedure?
<<em>proc</em>>)</tt><br />
tests whether <<em>proc</em>> is a primitive
procedure.</li>
</ul>
<p>These mechanisms for handling primitives are further described
in section <a href="#%_sec_4.1.4">4.1.4</a>.</p>
<p>Compound procedures are constructed from parameters, procedure
bodies, and environments using the constructor
<tt>make-procedure</tt>:</p>
<p><tt><a name="%_idx_4444"></a>(define (make-procedure parameters body env)<br />
(list 'procedure parameters body env))<br />
<a name="%_idx_4446"></a>(define (compound-procedure? p)<br />
(tagged-list? p 'procedure))<br />
<a name="%_idx_4448"></a>(define (procedure-parameters p) (cadr p))<br />
<a name="%_idx_4450"></a>(define (procedure-body p) (caddr p))<br />
<a name="%_idx_4452"></a>(define (procedure-environment p) (cadddr p))<br />
</tt></p>
<p><a name="%_sec_Temp_543"></a></p>
<h4><a href="book-Z-H-4.html#%_toc_%_sec_Temp_543">Operations on
Environments</a></h4>
<p><a name="%_idx_4454"></a> The evaluator needs operations for
manipulating environments. As explained in section <a href="book-Z-H-21.html#%_sec_3.2">3.2</a>, an environment is a
sequence of frames, where each frame is a table of bindings that
associate variables with their corresponding values. We use the
following operations for manipulating environments:</p>
<ul>
<li><tt>(lookup-variable-value <<em>var</em>>
<<em>env</em>>)</tt><br />
returns the value that is bound to the symbol
<<em>var</em>> in the environment <<em>env</em>>,
or signals an error if the variable is unbound.</li>
<li><a name="%_idx_4458"></a><tt>(extend-environment
<<em>variables</em>> <<em>values</em>>
<<em>base-env</em>>)</tt><br />
returns a new environment, consisting of a new frame in which
the symbols in the list <<em>variables</em>> are bound to
the corresponding elements in the list <<em>values</em>>,
where the enclosing environment is the environment
<<em>base-env</em>>.</li>
<li><a name="%_idx_4460"></a><tt>(define-variable!
<<em>var</em>> <<em>value</em>>
<<em>env</em>>)</tt><br />
adds to the first frame in the environment <<em>env</em>>
a new binding that associates the variable <<em>var</em>>
with the value <<em>value</em>>.</li>
<li><a name="%_idx_4462"></a><tt>(set-variable-value!
<<em>var</em>> <<em>value</em>>
<<em>env</em>>)</tt><br />
changes the binding of the variable <<em>var</em>> in the
environment <<em>env</em>> so that the variable is now
bound to the value <<em>value</em>>, or signals an error
if the variable is unbound.</li>
</ul>
<p><a name="%_idx_4464"></a>To implement these operations we
represent an environment as a list of frames. The enclosing
environment of an environment is the <tt>cdr</tt> of the list.
The empty environment is simply the empty list.</p>
<p><tt><a name="%_idx_4466"></a>(define (enclosing-environment env) (cdr env))<br />
<a name="%_idx_4468"></a>(define (first-frame env) (car env))<br />
(define the-empty-environment '())<br /></tt></p>
<p>Each frame of an environment is represented as a pair of
lists: a list of the variables bound in that frame and a list of
the associated values.<a href="#footnote_Temp_544" name="call_footnote_Temp_544" id="call_footnote_Temp_544"><sup><small>14</small></sup></a></p>
<p><tt><a name="%_idx_4470"></a>(define (make-frame variables values)<br />
(cons variables values))<br />
<a name="%_idx_4472"></a>(define (frame-variables frame) (car frame))<br />
<a name="%_idx_4474"></a>(define (frame-values frame) (cdr frame))<br />
<a name="%_idx_4476"></a>(define (add-binding-to-frame! var val frame)<br />
(set-car! frame (cons var (car frame)))<br />
(set-cdr! frame (cons val (cdr frame))))<br />
</tt></p>
<p>To extend an environment by a new frame that associates
variables with values, we make a frame consisting of the list of
variables and the list of values, and we adjoin this to the
environment. We signal an error if the number of variables does
not match the number of values.</p>