forked from ldct/isicp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-5-generic.html
1032 lines (793 loc) · 73.6 KB
/
2-5-generic.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 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.5 - Systems with Generic Operations</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">
<a href='index.html' style="float:left"> <img src='images/chevron-up.svg' height=64 width=64> </a>
<span style="float:right">
<a href='2-4-representation.html'> <img src='images/chevron-left.svg' height=64 width=64> </a>
<a href='3-1-assignment.html'> <img src='images/chevron-right.svg' height=64 width=64> </a>
</span>
<br> <br>
<h2> Systems with Generic Operations </h2>
<p> In the previous section, we saw how to design systems in which data objects can be represented in more than one way. The key idea is to link the code that specifies the data operations to the several representations by means of generic interface procedures. Now we will see how to use this same idea not only to define operations that are generic over different representations but also to define operations that are generic over different kinds of arguments. We have already seen several different packages of arithmetic operations: the primitive arithmetic (<tt>+</tt>, <tt>-</tt>, <tt>*</tt>, <tt>/</tt>) built into our language, the rational-number arithmetic (<tt>add-rat</tt>, <tt>sub-rat</tt>, <tt>mul-rat</tt>, <tt>div-rat</tt>) of section 2-1-1, and the complex-number arithmetic that we implemented in section 2-4-3. We will now use data-directed techniques to construct a package of arithmetic operations that incorporates all the arithmetic packages we have already constructed.
<p> Figure 2-23 shows the structure of the system we shall build. Notice the abstraction barriers. From the perspective of someone using ``numbers,'' there is a single procedure <tt>add</tt> that operates on whatever numbers are supplied. <tt>Add</tt> is part of a generic interface that allows the separate ordinary-arithmetic, rational-arithmetic, and complex-arithmetic packages to be accessed uniformly by programs that use numbers. Any individual arithmetic package (such as the complex package) may itself be accessed through generic procedures (such as <tt>add-complex</tt>) that combine packages designed for different representations (such as rectangular and polar). Moreover, the structure of the system is additive, so that one can design the individual arithmetic packages separately and combine them to produce a generic arithmetic system.
<div class="figure">
<img src="http://mitpress.mit.edu/sicp/full-text/book/ch2-Z-G-64.gif">
<p> <b>Figure 2.23:</b> Generic arithmetic system.
</div>
<h3> Generic Arithmetic Operations </h3>
<p> The task of designing generic arithmetic operations is analogous to that of designing the generic complex-number operations. We would like, for instance, to have a generic addition procedure <tt>add</tt> that acts like ordinary primitive addition <tt>+</tt> on ordinary numbers, like <tt>add-rat</tt> on rational numbers, and like <tt>add-complex</tt> on complex numbers. We can implement <tt>add</tt>, and the other generic arithmetic operations, by following the same strategy we used in section 2-4-3 to implement the generic selectors for complex numbers. We will attach a type tag to each kind of number and cause the generic procedure to dispatch to an appropriate package according to the data type of its arguments.
<p> The generic arithmetic procedures are defined as follows:
<div id="scheme-define-generic-arith">
(define (add x y) (apply-generic 'add x y))
(define (sub x y) (apply-generic 'sub x y))
(define (mul x y) (apply-generic 'mul x y))
(define (div x y) (apply-generic 'div x y))
</div>
<script>
prompt("scheme-define-generic-arith");
</script>
<p> We begin by installing a package for handling <tt>ordinary</tt> numbers, that is, the primitive numbers of our language. We will tag these with the symbol <tt>scheme-number</tt>. The arithmetic operations in this package are the primitive arithmetic procedures (so there is no need to define extra procedures to handle the untagged numbers). Since these operations each take two arguments, they are installed in the table keyed by the list <tt>(scheme-number scheme-number)</tt>:
<div id="scheme-define-install-scheme-number-package">
(define (install-scheme-number-package)
(define (tag x)
(attach-tag 'scheme-number x))
(put 'add '(scheme-number scheme-number)
(lambda (x y) (tag (+ x y))))
(put 'sub '(scheme-number scheme-number)
(lambda (x y) (tag (- x y))))
(put 'mul '(scheme-number scheme-number)
(lambda (x y) (tag (* x y))))
(put 'div '(scheme-number scheme-number)
(lambda (x y) (tag (/ x y))))
(put 'make 'scheme-number
(lambda (x) (tag x)))
'done)
</div>
<script>
prompt("scheme-define-install-scheme-number-package");
</script>
<p> Users of the Scheme-number package will create (tagged) ordinary numbers by means of the procedure:
<div id="scheme-define-make-scheme-number">
(define (make-scheme-number n)
((get 'make 'scheme-number) n))
</div>
<script>
prompt("scheme-define-make-scheme-number");
</script>
<p> Now that the framework of the generic arithmetic system is in place, we can readily include new kinds of numbers. Here is a package that performs rational arithmetic. Notice that, as a benefit of additivity, we can use without modification the rational-number code from section 2-1-1 as the internal procedures in the package:
<div id="scheme-define-install-rational-package">
(define (install-rational-package)
;; internal procedures
(define (numer x) (car x))
(define (denom x) (cdr x))
(define (make-rat n d)
(let ((g (gcd n d)))
(cons (/ n g) (/ d g))))
(define (add-rat x y)
(make-rat (+ (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (sub-rat x y)
(make-rat (- (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (mul-rat x y)
(make-rat (* (numer x) (numer y))
(* (denom x) (denom y))))
(define (div-rat x y)
(make-rat (* (numer x) (denom y))
(* (denom x) (numer y))))
;; interface to rest of the system
(define (tag x) (attach-tag 'rational x))
(put 'add '(rational rational)
(lambda (x y) (tag (add-rat x y))))
(put 'sub '(rational rational)
(lambda (x y) (tag (sub-rat x y))))
(put 'mul '(rational rational)
(lambda (x y) (tag (mul-rat x y))))
(put 'div '(rational rational)
(lambda (x y) (tag (div-rat x y))))
(put 'make 'rational
(lambda (n d) (tag (make-rat n d))))
'done)
(define (make-rational n d)
((get 'make 'rational) n d))
</div>
<script>
prompt("scheme-define-install-rational-package");
</script>
<p> We can install a similar package to handle complex numbers, using the tag <tt>complex</tt>. In creating the package, we extract from the table the operations <tt>make-from-real-imag</tt> and <tt>make-from-mag-ang</tt> that were defined by the rectangular and polar packages. Additivity permits us to use, as the internal operations, the same <tt>add-complex</tt>, <tt>sub-complex</tt>, <tt>mul-complex</tt>, and <tt>div-complex</tt> procedures from section 2-4-1.
<div id="scheme-define-install-complex-package">
(define (install-complex-package)
;; imported procedures from rectangular and polar packages
(define (make-from-real-imag x y)
((get 'make-from-real-imag 'rectangular) x y))
(define (make-from-mag-ang r a)
((get 'make-from-mag-ang 'polar) r a))
;; internal procedures
(define (add-complex z1 z2)
(make-from-real-imag (+ (real-part z1) (real-part z2))
(+ (imag-part z1) (imag-part z2))))
(define (sub-complex z1 z2)
(make-from-real-imag (- (real-part z1) (real-part z2))
(- (imag-part z1) (imag-part z2))))
(define (mul-complex z1 z2)
(make-from-mag-ang (* (magnitude z1) (magnitude z2))
(+ (angle z1) (angle z2))))
(define (div-complex z1 z2)
(make-from-mag-ang (/ (magnitude z1) (magnitude z2))
(- (angle z1) (angle z2))))
;; interface to rest of the system
(define (tag z) (attach-tag 'complex z))
(put 'add '(complex complex)
(lambda (z1 z2) (tag (add-complex z1 z2))))
(put 'sub '(complex complex)
(lambda (z1 z2) (tag (sub-complex z1 z2))))
(put 'mul '(complex complex)
(lambda (z1 z2) (tag (mul-complex z1 z2))))
(put 'div '(complex complex)
(lambda (z1 z2) (tag (div-complex z1 z2))))
(put 'make-from-real-imag 'complex
(lambda (x y) (tag (make-from-real-imag x y))))
(put 'make-from-mag-ang 'complex
(lambda (r a) (tag (make-from-mag-ang r a))))
'done)
</div>
<script>
prompt("scheme-define-install-complex-package");
</script>
<p> Programs outside the complex-number package can construct complex numbers either from real and imaginary parts or from magnitudes and angles. Notice how the underlying procedures, originally defined in the rectangular and polar packages, are exported to the complex package, and exported from there to the outside world.
<div id="scheme-define-make-complex">
(define (make-complex-from-real-imag x y)
((get 'make-from-real-imag 'complex) x y))
(define (make-complex-from-mag-ang r a)
((get 'make-from-mag-ang 'complex) r a))
</div>
<script>
prompt("scheme-define-make-complex");
</script>
<p> What we have here is a two-level tag system. A typical complex number, such as 3 + 4i in rectangular form, would be represented as shown in Figure 2-24. The outer tag (<tt>complex</tt>) is used to direct the number to the complex package. Once within the complex package, the next tag (<tt>rectangular</tt>) is used to direct the number to the rectangular package. In a large and complicated system there might be many levels, each interfaced with the next by means of generic operations. As a data object is passed ``downward,'' the outer tag that is used to direct it to the appropriate package is stripped off (by applying <tt>contents</tt>) and the next level of tag (if any) becomes visible to be used for further dispatching.
<div class="figure">
<img src="http://mitpress.mit.edu/sicp/full-text/book/ch2-Z-G-65.gif">
<p> <b>Figure 2.24:</b> Representation of 3 + 4i in rectangular form.
</div>
<p> In the above packages, we used <tt>add-rat</tt>, <tt>add-complex</tt>, and the other arithmetic procedures exactly as originally written. Once these definitions are internal to different installation procedures, however, they no longer need names that are distinct from each other: we could simply name them <tt>add</tt>, <tt>sub</tt>, <tt>mul</tt>, and <tt>div</tt> in both packages.
<div class="exercise">
<p> <b>Exercise 2.77:</b> Louis Reasoner tries to evaluate the expression <tt>(magnitude z)</tt> where <tt>z</tt> is the object shown in Figure 2-24. To his surprise, instead of the answer $5$ he gets an error message from <tt>apply-generic</tt>, saying there is no method for the operation <tt>magnitude</tt> on the types <tt>(complex)</tt>. He shows this interaction to Alyssa P. Hacker, who says "The problem is that the complex-number selectors were never defined for <tt>complex</tt> numbers, just for <tt>polar</tt> and <tt>rectangular</tt> numbers. All you have to do to make this work is add the following to the <tt>complex</tt> package:"
<div id="scheme-complex-selectors-for-complex">
(put 'real-part '(complex) real-part)
(put 'imag-part '(complex) imag-part)
(put 'magnitude '(complex) magnitude)
(put 'angle '(complex) angle)
</div>
<script>
no_output_frozen_prompt("scheme-complex-selectors-for-complex");
</script>
<p> Describe in detail why this works. As an example, trace through all the procedures called in evaluating the expression <tt>(magnitude z)</tt> where <tt>z</tt> is the object shown in Figure 2-24. In particular, how many times is <tt>apply-generic</tt> invoked? What procedure is dispatched to in each case?
</div>
<p>
<div class="exercise">
<p> <b>Exercise 2.78:</b> The internal procedures in the <tt>scheme-number</tt> package are essentially nothing more than calls to the primitive procedures <tt>+</tt>, <tt>-</tt>, etc. It was not possible to use the primitives of the language directly because our type-tag system requires that each data object have a type attached to it. In fact, however, all Lisp implementations do have a type system, which they use internally. Primitive predicates such as <tt>symbol?</tt> and <tt>number?</tt> determine whether data objects have particular types. Modify the definitions of <tt>type-tag</tt>, <tt>contents</tt>, and <tt>attach-tag</tt> from section 2-4-2 so that our generic system takes advantage of Scheme's internal type system. That is to say, the system should work as before except that ordinary numbers should be represented simply as Scheme numbers rather than as pairs whose <tt>car</tt> is the symbol <tt>scheme-number</tt>.
</div>
<p>
<div class="exercise">
<p> <b>Exercise 2.79:</b> Define a generic equality predicate <tt>equ?</tt> that tests the equality of two numbers, and install it in the generic arithmetic package. This operation should work for ordinary numbers, rational numbers, and complex numbers.
</div>
<p>
<div class="exercise">
<p> <b>Exercise 2.80:</b> Define a generic predicate <tt>=zero?</tt> that tests if its argument is zero, and install it in the generic arithmetic package. This operation should work for ordinary numbers, rational numbers, and complex numbers.
</div>
<h3> Combining Data of Different Types </h3>
<p> We have seen how to define a unified arithmetic system that encompasses ordinary numbers, complex numbers, rational numbers, and any other type of number we might decide to invent, but we have ignored an important issue. The operations we have defined so far treat the different data types as being completely independent. Thus, there are separate packages for adding, say, two ordinary numbers, or two complex numbers. What we have not yet considered is the fact that it is meaningful to define operations that cross the type boundaries, such as the addition of a complex number to an ordinary number. We have gone to great pains to introduce barriers between parts of our programs so that they can be developed and understood separately. We would like to introduce the cross-type operations in some carefully controlled way, so that we can support them without seriously violating our module boundaries.
<p> One way to handle cross-type operations is to design a different procedure for each possible combination of types for which the operation is valid. For example, we could extend the complex-number package so that it provides a procedure for adding complex numbers to ordinary numbers and installs this in the table using the tag <tt>(complex scheme-number)</tt><a name="footnote_link_2-49" class="footnote_link" href="#footnote_2-49">49</a>
<div id="scheme-define-add-complex-to-schemenum">
;; to be included in the complex package
(define (add-complex-to-schemenum z x)
(make-from-real-imag (+ (real-part z) x)
(imag-part z)))
(put 'add '(complex scheme-number)
(lambda (z x) (tag (add-complex-to-schemenum z x))))
</div>
<script>
prompt("scheme-define-add-complex-to-schemenum");
</script>
<p> This technique works, but it is cumbersome. With such a system, the cost of introducing a new type is not just the construction of the package of procedures for that type but also the construction and installation of the procedures that implement the cross-type operations. This can easily be much more code than is needed to define the operations on the type itself. The method also undermines our ability to combine separate packages additively, or least to limit the extent to which the implementors of the individual packages need to take account of other packages. For instance, in the example above, it seems reasonable that handling mixed operations on complex numbers and ordinary numbers should be the responsibility of the complex-number package. Combining rational numbers and complex numbers, however, might be done by the complex package, by the rational package, or by some third package that uses operations extracted from these two packages. Formulating coherent policies on the division of responsibility among packages can be an overwhelming task in designing systems with many packages and many cross-type operations.
<h4> Coercion </h4>
<p> In the general situation of completely unrelated operations acting on completely unrelated types, implementing explicit cross-type operations, cumbersome though it may be, is the best that one can hope for. Fortunately, we can usually do better by taking advantage of additional structure that may be latent in our type system. Often the different data types are not completely independent, and there may be ways by which objects of one type may be viewed as being of another type. This process is called <tt>coercion</tt> . For example, if we are asked to arithmetically combine an ordinary number with a complex number, we can view the ordinary number as a complex number whose imaginary part is zero. This transforms the problem to that of combining two complex numbers, which can be handled in the ordinary way by the complex-arithmetic package.
<p> In general, we can implement this idea by designing coercion procedures that transform an object of one type into an equivalent object of another type. Here is a typical coercion procedure, which transforms a given ordinary number to a complex number with that real part and zero imaginary part:
<div id="scheme-define-scheme-number-to-complex">
(define (scheme-number->complex n)
(make-complex-from-real-imag (contents n) 0))
</div>
<script>
prompt("scheme-define-scheme-number-to-complex");
</script>
<p> We install these coercion procedures in a special coercion table, indexed under the names of the two types:
<div id="scheme-put-coercion">
(put-coercion 'scheme-number 'complex scheme-number->complex)
</div>
<script>
prompt("scheme-put-coercion");
</script>
<p> (We assume that there are <tt>put-coercion</tt> and <tt>get-coercion</tt> procedures available for manipulating this table.) Generally some of the slots in the table will be empty, because it is not generally possible to coerce an arbitrary data object of each type into all other types. For example, there is no way to coerce an arbitrary complex number to an ordinary number, so there will be no general <tt>complex->scheme-number</tt> procedure included in the table.
<p> Once the coercion table has been set up, we can handle coercion in a uniform manner by modifying the <tt>apply-generic</tt> procedure of section 2-4-3. When asked to apply an operation, we first check whether the operation is defined for the arguments' types, just as before. If so, we dispatch to the procedure found in the operation-and-type table. Otherwise, we try coercion. For simplicity, we consider only the case where there are two arguments.<a name="footnote_link_2-50" class="footnote_link" href="#footnote_2-50">50</a> We check the coercion table to see if objects of the first type can be coerced to the second type. If so, we coerce the first argument and try the operation again. If objects of the first type cannot in general be coerced to the second type, we try the coercion the other way around to see if there is a way to coerce the second argument to the type of the first argument. Finally, if there is no known way to coerce either type to the other type, we give up. Here is the procedure:
<div id="scheme-define-apply-generic">
(define (apply-generic op . args)
(let ((type-tags (map type-tag args)))
(let ((proc (get op type-tags)))
(if proc
(apply proc (map contents args))
(if (= (length args) 2)
(let ((type1 (car type-tags))
(type2 (cadr type-tags))
(a1 (car args))
(a2 (cadr args)))
(let ((t1->t2 (get-coercion type1 type2))
(t2->t1 (get-coercion type2 type1)))
(cond (t1->t2
(apply-generic op (t1->t2 a1) a2))
(t2->t1
(apply-generic op a1 (t2->t1 a2)))
(else
(error "No method for these types"
(list op type-tags))))))
(error "No method for these types"
(list op type-tags)))))))
</div>
<script>
prompt("scheme-define-apply-generic");
</script>
<p> This coercion scheme has many advantages over the method of defining explicit cross-type operations, as outlined above. Although we still need to write coercion procedures to relate the types (possibly n^2 procedures for a system with n types), we need to write only one procedure for each pair of types rather than a different procedure for each collection of types and each generic operation.<a name="footnote_link_2-51" class="footnote_link" href="#footnote_2-51">51</a> What we are counting on here is the fact that the appropriate transformation between types depends only on the types themselves, not on the operation to be applied.
<p> On the other hand, there may be applications for which our coercion scheme is not general enough. Even when neither of the objects to be combined can be converted to the type of the other it may still be possible to perform the operation by converting both objects to a third type. In order to deal with such complexity and still preserve modularity in our programs, it is usually necessary to build systems that take advantage of still further structure in the relations among types, as we discuss next.
<h4> Hierarchies of types </h4>
<p> The coercion scheme presented above relied on the existence of natural relations between pairs of types. Often there is more ``global'' structure in how the different types relate to each other. For instance, suppose we are building a generic arithmetic system to handle integers, rational numbers, real numbers, and complex numbers. In such a system, it is quite natural to regard an integer as a special kind of rational number, which is in turn a special kind of real number, which is in turn a special kind of complex number. What we actually have is a so-called <tt>hierarchy of types</tt> , in which, for example, integers are a <tt>subtype</tt> of rational numbers (i.e., any operation that can be applied to a rational number can automatically be applied to an integer). Conversely, we say that rational numbers form a <tt>supertype</tt> of integers. The particular hierarchy we have here is of a very simple kind, in which each type has at most one supertype and at most one subtype. Such a structure, called a <tt>tower</tt> , is illustrated in Figure 2-25.
<div class="figure">
<img src="http://mitpress.mit.edu/sicp/full-text/book/ch2-Z-G-66.gif">
<p> <b>Figure 2.25:</b> A tower of types.
</div>
<p> If we have a tower structure, then we can greatly simplify the problem of adding a new type to the hierarchy, for we need only specify how the new type is embedded in the next supertype above it and how it is the supertype of the type below it. For example, if we want to add an integer to a complex number, we need not explicitly define a special coercion procedure <tt>integer->complex</tt>. Instead, we define how an integer can be transformed into a rational number, how a rational number is transformed into a real number, and how a real number is transformed into a complex number. We then allow the system to transform the integer into a complex number through these steps and then add the two complex numbers.
<p> We can redesign our <tt>apply-generic</tt> procedure in the following way: For each type, we need to supply a <tt>raise</tt> procedure, which ``raises'' objects of that type one level in the tower. Then when the system is required to operate on objects of different types it can successively raise the lower types until all the objects are at the same level in the tower. (Exercise 2-83 and Exercise 2-84 concern the details of implementing such a strategy.)
<p> Another advantage of a tower is that we can easily implement the notion that every type ``inherits'' all operations defined on a supertype. For instance, if we do not supply a special procedure for finding the real part of an integer, we should nevertheless expect that <tt>real-part</tt> will be defined for integers by virtue of the fact that integers are a subtype of complex numbers. In a tower, we can arrange for this to happen in a uniform way by modifying <tt>apply-generic</tt>. If the required operation is not directly defined for the type of the object given, we raise the object to its supertype and try again. We thus crawl up the tower, transforming our argument as we go, until we either find a level at which the desired operation can be performed or hit the top (in which case we give up).
<p> Yet another advantage of a tower over a more general hierarchy is that it gives us a simple way to ``lower'' a data object to the simplest representation. For example, if we add 2 + 3i to 4 - 3i, it would be nice to obtain the answer as the integer 6 rather than as the complex number 6 + 0i. Exercise 2-85 discusses a way to implement such a lowering operation. (The trick is that we need a general way to distinguish those objects that can be lowered, such as 6 + 0i, from those that cannot, such as 6 + 2i.)
<div class="figure">
<img src="http://mitpress.mit.edu/sicp/full-text/book/ch2-Z-G-67.gif">
<p> <b>Figure 2.26:</b> Relations among types of geometric figures.
</div>
<h4> Inadequacies of hierarchies </h4>
<p> If the data types in our system can be naturally arranged in a tower, this greatly simplifies the problems of dealing with generic operations on different types, as we have seen. Unfortunately, this is usually not the case. Figure 2-26 illustrates a more complex arrangement of mixed types, this one showing relations among different types of geometric figures. We see that, in general, a type may have more than one subtype. Triangles and quadrilaterals, for instance, are both subtypes of polygons. In addition, a type may have more than one supertype. For example, an isosceles right triangle may be regarded either as an isosceles triangle or as a right triangle. This multiple-supertypes issue is particularly thorny, since it means that there is no unique way to ``raise'' a type in the hierarchy. Finding the ``correct'' supertype in which to apply an operation to an object may involve considerable searching through the entire type network on the part of a procedure such as <tt>apply-generic</tt>. Since there generally are multiple subtypes for a type, there is a similar problem in coercing a value ``down'' the type hierarchy. Dealing with large numbers of interrelated types while still preserving modularity in the design of large systems is very difficult, and is an area of much current research.<a name="footnote_link_2-52" class="footnote_link" href="#footnote_2-52">52</a>
<div class="exercise">
<p> <b>Exercise 2.81:</b> Louis Reasoner has noticed that <tt>apply-generic</tt> may try to coerce the arguments to each other's type even if they already have the same type. Therefore, he reasons, we need to put procedures in the coercion table to <tt>coerce</tt> arguments of each type to their own type. For example, in addition to the <tt>scheme-number->complex</tt> coercion shown above, he would do:
<div id="scheme-define-scheme-number-to-scheme-number">
(define (scheme-number->scheme-number n) n)
(define (complex->complex z) z)
(put-coercion 'scheme-number 'scheme-number
scheme-number->scheme-number)
(put-coercion 'complex 'complex complex->complex)
</div>
<script>
prompt("scheme-define-scheme-number-to-scheme-number");
</script>
<ul>
<li>
<p> With Louis's coercion procedures installed, what happens if <tt>apply-generic</tt> is called with two arguments of type <tt>scheme-number</tt> or two arguments of type <tt>complex</tt> for an operation that is not found in the table for those types? For example, assume that we've defined a generic exponentiation operation:
<div id="scheme-define-exp">
(define (exp x y) (apply-generic 'exp x y))
</div>
<script>
prompt("scheme-define-exp");
</script>
<p> and have put a procedure for exponentiation in the Scheme-number package but not in any other package:
<div id="scheme-put-exp">
;; following added to Scheme-number package
(put 'exp '(scheme-number scheme-number)
(lambda (x y) (tag (expt x y)))) ; using primitive <tt>expt</tt>
</div>
<script>
prompt("scheme-put-exp");
</script>
<p> What happens if we call <tt>exp</tt> with two complex numbers as arguments?
</li>
<li>
<p> Is Louis correct that something had to be done about coercion with arguments of the same type, or does <tt>apply-generic</tt> work correctly as is? </li> <li> Modify <tt>apply-generic</tt> so that it doesn't try coercion if the two arguments have the same type.
</li>
</ul>
</div>
<p>
<div class="exercise">
<p> <b>Exercise 2.82:</b> Show how to generalize
<tt>apply-generic</tt> to handle coercion in the general case of multiple arguments. One strategy is to attempt to coerce all the arguments to the type of the first argument, then to the type of the second argument, and so on. Give an example of a situation where this strategy (and likewise the two-argument version given above) is not sufficiently general. (Hint: Consider the case where there are some suitable mixed-type operations present in the table that will not be tried.)
</div>
<p>
<div class="exercise">
<p> <b>Exercise 2.83:</b> Suppose you are designing a generic arithmetic system for dealing with the tower of types shown in Figure 2-25: integer, rational, real, complex. For each type (except complex), design a procedure that raises objects of that type one level in the tower. Show how to install a generic <tt>raise</tt> operation that will work for each type (except complex).
</div>
<p>
<div class="exercise">
<p> <b>Exercise 2.84:</b> Using the <tt>raise</tt> operation of Exercise 2-83, modify the <tt>apply-generic</tt> procedure so that it coerces its arguments to have the same type by the method of successive raising, as discussed in this section. You will need to devise a way to test which of two types is higher in the tower. Do this in a manner that is "compatible" with the rest of the system and will not lead to problems in adding new levels to the tower.
</div>
<p>
<div class="exercise">
<p> <b>Exercise 2.85:</b> This section mentioned a method for "simplifying" a data object by lowering it in the tower of types as far as possible. Design a procedure <tt>drop</tt> that accomplishes this for the tower described in Exercise 2-83. The key is to decide, in some general way, whether an object can be lowered. For example, the complex number $1.5 + 0i$ can be lowered as far as <tt>real</tt>, the complex number $1 + 0i$ can be lowered as far as <tt>integer</tt>, and the complex number $2 + 3i$ cannot be lowered at all. Here is a plan for determining whether an object can be lowered: Begin by defining a generic operation <tt>project</tt> that "pushes" an object down in the tower. For example, projecting a complex number would involve throwing away the imaginary part. Then a number can be dropped if, when we <tt>project</tt> it and <tt>raise</tt> the result back to the type we started with, we end up with something equal to what we started with. Show how to implement this idea in detail, by writing a <tt>drop</tt> procedure that drops an object as far as possible. You will need to design the various projection operations<a name="footnote_link_2-53" class="footnote_link" href="#footnote_2-53">53</a> and install <tt>project</tt> as a generic operation in the system. You will also need to make use of a generic equality predicate, such as described in Exercise 2-79. Finally, use <tt>drop</tt> to rewrite <tt>apply-generic</tt> from Exercise 2-84 so that it "simplifies" its answers.
</div>
<p>
<div class="exercise">
<p> <b>Exercise 2.86:</b> Suppose we want to handle complex numbers whose real parts, imaginary parts, magnitudes, and angles can be either ordinary numbers, rational numbers, or other numbers we might wish to add to the system. Describe and implement the changes to the system needed to accommodate this. You will have to define operations such as <tt>sine</tt> and <tt>cosine</tt> that are generic over ordinary numbers and rational numbers.
</div>
<h3> Example: Symbolic Algebra </h3>
<p> The manipulation of symbolic algebraic expressions is a complex process that illustrates many of the hardest problems that occur in the design of large-scale systems. An algebraic expression, in general, can be viewed as a hierarchical structure, a tree of operators applied to operands. We can construct algebraic expressions by starting with a set of primitive objects, such as constants and variables, and combining these by means of algebraic operators, such as addition and multiplication. As in other languages, we form abstractions that enable us to refer to compound objects in simple terms. Typical abstractions in symbolic algebra are ideas such as linear combination, polynomial, rational function, or trigonometric function. We can regard these as compound ``types,'' which are often useful for directing the processing of expressions. For example, we could describe the expression
$$
x^2 \sin (y^2 + 1) + r \cos 2y + \cos(y^3 - 2y^2)
$$
<p> as a polynomial in x with coefficients that are trigonometric functions of polynomials in y whose coefficients are integers.
<p> We will not attempt to develop a complete algebraic-manipulation system here. Such systems are exceedingly complex programs, embodying deep algebraic knowledge and elegant algorithms. What we will do is look at a simple but important part of algebraic manipulation: the arithmetic of polynomials. We will illustrate the kinds of decisions the designer of such a system faces, and how to apply the ideas of abstract data and generic operations to help organize this effort.
<h4> Arithmetic on polynomials </h4>
<p> Our first task in designing a system for performing arithmetic on polynomials is to decide just what a polynomial is. Polynomials are normally defined relative to certain variables (the <tt>indeterminates</tt> of the polynomial). For simplicity, we will restrict ourselves to polynomials having just one indeterminate (<tt>univariate polynomials</tt>).<a name="footnote_link_2-54" class="footnote_link" href="#footnote_2-54">54</a> below.} We will define a polynomial to be a sum of terms, each of which is either a coefficient, a power of the indeterminate, or a product of a coefficient and a power of the indeterminate. A coefficient is defined as an algebraic expression that is not dependent upon the indeterminate of the polynomial. For example,
$$
5x^2 + 3x + 7
$$
<p> is a simple polynomial in x, and
$$
(y^2 + 1)r^3 + (2y)x + 1
$$
<p> is a polynomial in x whose coefficients are polynomials in y.
<p> Already we are skirting some thorny issues. Is the first of these polynomials the same as the polynomial $5y^2 + 3y + 7$, or not? A reasonable answer might be "yes, if we are considering a polynomial purely as a mathematical function, but no, if we are considering a polynomial to be a syntactic form." The second polynomial is algebraically equivalent to a polynomial in $y$ whose coefficients are polynomials in x. Should our system recognize this, or not? Furthermore, there are other ways to represent a polynomial – for example, as a product of factors, or (for a univariate polynomial) as the set of roots, or as a listing of the values of the polynomial at a specified set of points.<a name="footnote_link_2-55" class="footnote_link" href="#footnote_2-55">55</a> We can finesse these questions by deciding that in our algebraic-manipulation system a "polynomial" will be a particular syntactic form, not its underlying mathematical meaning.
<p> Now we must consider how to go about doing arithmetic on polynomials. In this simple system, we will consider only addition and multiplication. Moreover, we will insist that two polynomials to be combined must have the same indeterminate.
<p> We will approach the design of our system by following the familiar discipline of data abstraction. We will represent polynomials using a data structure called a <tt>poly</tt> , which consists of a variable and a collection of terms. We assume that we have selectors <tt>variable</tt> and <tt>term-list</tt> that extract those parts from a poly and a constructor <tt>make-poly</tt> that assembles a poly from a given variable and a term list. A variable will be just a symbol, so we can use the <tt>same-variable?</tt> procedure of section 2-3-2 to compare variables. The following procedures define addition and multiplication of polys:
<div id="scheme-define-add-mul-poly">
(define (add-poly p1 p2)
(if (same-variable? (variable p1) (variable p2))
(make-poly (variable p1)
(add-terms (term-list p1)
(term-list p2)))
(error "Polys not in same var -- ADD-POLY"
(list p1 p2))))
(define (mul-poly p1 p2)
(if (same-variable? (variable p1) (variable p2))
(make-poly (variable p1)
(mul-terms (term-list p1)
(term-list p2)))
(error "Polys not in same var -- MUL-POLY"
(list p1 p2))))
</div>
<script>
prompt("scheme-define-add-mul-poly");
</script>
<p> To incorporate polynomials into our generic arithmetic system, we need to supply them with type tags. We'll use the tag <tt>polynomial</tt>, and install appropriate operations on tagged polynomials in the operation table. We'll embed all our code in an installation procedure for the polynomial package, similar to the ones in section 2-5-1:
<div id="scheme-define-install-polynomial-package">
(define (install-polynomial-package)
;; internal procedures
;; representation of poly
(define (make-poly variable term-list)
(cons variable term-list))
(define (variable p) (car p))
(define (term-list p) (cdr p))
;procedures same-variable? and variable? from section 2.3.2
;; representation of terms and term lists
;procedures adjoin-term ... coeff from text below
;; continued on next page
(define (add-poly p1 p2) ...)
;procedures used by add-poly
(define (mul-poly p1 p2) ...)
;procedures used by mul-poly
;; interface to rest of the system
(define (tag p) (attach-tag 'polynomial p))
(put 'add '(polynomial polynomial)
(lambda (p1 p2) (tag (add-poly p1 p2))))
(put 'mul '(polynomial polynomial)
(lambda (p1 p2) (tag (mul-poly p1 p2))))
(put 'make 'polynomial
(lambda (var terms) (tag (make-poly var terms))))
'done)
</div>
<script>
prompt("scheme-define-install-polynomial-package");
</script>
<p> Polynomial addition is performed termwise. Terms of the same order (i.e., with the same power of the indeterminate) must be combined. This is done by forming a new term of the same order whose coefficient is the sum of the coefficients of the addends. Terms in one addend for which there are no terms of the same order in the other addend are simply accumulated into the sum polynomial being constructed.
<p> In order to manipulate term lists, we will assume that we have a constructor <tt>the-empty-termlist</tt> that returns an empty term list and a constructor <tt>adjoin-term</tt> that adjoins a new term to a term list. We will also assume that we have a predicate <tt>empty-termlist?</tt> that tells if a given term list is empty, a selector <tt>first-term</tt> that extracts the highest-order term from a term list, and a selector <tt>rest-terms</tt> that returns all but the highest-order term. To manipulate terms, we will suppose that we have a constructor <tt>make-term</tt> that constructs a term with given order and coefficient, and selectors <tt>order</tt> and <tt>coeff</tt> that return, respectively, the order and the coefficient of the term. These operations allow us to consider both terms and term lists as data abstractions, whose concrete representations we can worry about separately.
<p> Here is the procedure that constructs the term list for the sum of two polynomials:<a name="footnote_link_2-56" class="footnote_link" href="#footnote_2-56">56</a>
<div id="scheme-define-add-terms">
(define (add-terms L1 L2)
(cond ((empty-termlist? L1) L2)
((empty-termlist? L2) L1)
(else
(let ((t1 (first-term L1)) (t2 (first-term L2)))
(cond ((> (order t1) (order t2))
(adjoin-term
t1 (add-terms (rest-terms L1) L2)))
((< (order t1) (order t2))
(adjoin-term
t2 (add-terms L1 (rest-terms L2))))
(else
(adjoin-term
(make-term (order t1)
(add (coeff t1) (coeff t2)))
(add-terms (rest-terms L1)
(rest-terms L2)))))))))
</div>
<script>
prompt("scheme-define-add-terms");
</script>
<p> The most important point to note here is that we used the generic addition procedure <tt>add</tt> to add together the coefficients of the terms being combined. This has powerful consequences, as we will see below.
<p> In order to multiply two term lists, we multiply each term of the first list by all the terms of the other list, repeatedly using <tt>mul-term-by-all-terms</tt>, which multiplies a given term by all terms in a given term list. The resulting term lists (one for each term of the first list) are accumulated into a sum. Multiplying two terms forms a term whose order is the sum of the orders of the factors and whose coefficient is the product of the coefficients of the factors:
<div id="scheme-define-mul-terms">
(define (mul-terms L1 L2)
(if (empty-termlist? L1)
(the-empty-termlist)
(add-terms (mul-term-by-all-terms (first-term L1) L2)
(mul-terms (rest-terms L1) L2))))
(define (mul-term-by-all-terms t1 L)
(if (empty-termlist? L)
(the-empty-termlist)
(let ((t2 (first-term L)))
(adjoin-term
(make-term (+ (order t1) (order t2))
(mul (coeff t1) (coeff t2)))
(mul-term-by-all-terms t1 (rest-terms L))))))
</div>
<script>
prompt("scheme-define-mul-terms");
</script>
<p> This is really all there is to polynomial addition and multiplication. Notice that, since we operate on terms using the generic procedures <tt>add</tt> and <tt>mul</tt>, our polynomial package is automatically able to handle any type of coefficient that is known about by the generic arithmetic package. If we include a coercion mechanism such as one of those discussed in section 2-5-2, then we also are automatically able to handle operations on polynomials of different coefficient types, such as
$$
\left[3x^2 + (2 + 3i)x + 7\right] \cdot \left[ x^4 + \frac{2}{3} x^2 + (5 + 3i) \right]
$$
<p> Because we installed the polynomial addition and multiplication procedures <tt>add-poly</tt> and <tt>mul-poly</tt> in the generic arithmetic system as the <tt>add</tt> and <tt>mul</tt> operations for type <tt>polynomial</tt>, our system is also automatically able to handle polynomial operations such as
$$
\left[(y + 1)x^2 + (y^2 + 1)x + (y - 1)\right] \cdot \left[(y - 2)x + (y^3 + 7)\right]
$$
<p> The reason is that when the system tries to combine coefficients, it will dispatch through <tt>add</tt> and <tt>mul</tt>. Since the coefficients are themselves polynomials (in y), these will be combined using <tt>add-poly</tt> and <tt>mul-poly</tt>. The result is a kind of ``data-directed recursion'' in which, for example, a call to <tt>mul-poly</tt> will result in recursive calls to <tt>mul-poly</tt> in order to multiply the coefficients. If the coefficients of the coefficients were themselves polynomials (as might be used to represent polynomials in three variables), the data direction would ensure that the system would follow through another level of recursive calls, and so on through as many levels as the structure of the data dictates.<a name="footnote_link_2-57" class="footnote_link" href="#footnote_2-57">57</a>
<h4> Representing term lists </h4>
<p> Finally, we must confront the job of implementing a good representation for term lists. A term list is, in effect, a set of coefficients keyed by the order of the term. Hence, any of the methods for representing sets, as discussed in section 2-3-3, can be applied to this task. On the other hand, our procedures <tt>add-terms</tt> and <tt>mul-terms</tt> always access term lists sequentially from highest to lowest order. Thus, we will use some kind of ordered list representation.
<p> How should we structure the list that represents a term list? One consideration is the ``density'' of the polynomials we intend to manipulate. A polynomial is said to be <tt>dense</tt> if it has nonzero coefficients in terms of most orders. If it has many zero terms it is said to be <tt>sparse</tt> . For example,
$$
A : x^5 + 2x^4 + 3x^2 - 2x - 5
$$
<p> is a dense polynomial, whereas
$$
B : x^{100} + 2x^2 + 1
$$
<p> is sparse.
<p> The term lists of dense polynomials are most efficiently represented as lists of the coefficients. For example, A above would be nicely represented as <tt>(1 2 0 3 -2 -5)</tt>. The order of a term in this representation is the length of the sublist beginning with that term's coefficient, decremented by 1.<a name="footnote_link_2-58" class="footnote_link" href="#footnote_2-58">58</a> This would be a terrible representation for a sparse polynomial such as B: There would be a giant list of zeros punctuated by a few lonely nonzero terms. A more reasonable representation of the term list of a sparse polynomial is as a list of the nonzero terms, where each term is a list containing the order of the term and the coefficient for that order. In such a scheme, polynomial B is efficiently represented as <tt>((100 1) (2 2) (0 1))</tt>. As most polynomial manipulations are performed on sparse polynomials, we will use this method. We will assume that term lists are represented as lists of terms, arranged from highest-order to lowest-order term. Once we have made this decision, implementing the selectors and constructors for terms and term lists is straightforward:<a name="footnote_link_2-59" class="footnote_link" href="#footnote_2-59">59</a>
<div id="scheme-define-adjoin-term">
(define (adjoin-term term term-list)
(if (=zero? (coeff term))
term-list
(cons term term-list)))
(define (the-empty-termlist) '())
(define (first-term term-list) (car term-list))
(define (rest-terms term-list) (cdr term-list))
(define (empty-termlist? term-list) (null? term-list))
(define (make-term order coeff) (list order coeff))
(define (order term) (car term))
(define (coeff term) (cadr term))
</div>
<script>
prompt("scheme-define-adjoin-term");
</script>
<p> where <tt>=zero?</tt> is as defined in Exercise 2-80. (See also Exercise 2-87 below.)
<p> Users of the polynomial package will create (tagged) polynomials by means of the procedure:
<div id="scheme-define-make-polynomial">
(define (make-polynomial var terms)
((get 'make 'polynomial) var terms))
</div>
<script>
prompt("scheme-define-make-polynomial");
</script>
<div class="exercise">
<p> <b>Exercise 2.87:</b> Install <tt>=zero?</tt> for polynomials in the generic arithmetic package. This will allow <tt>adjoin-term</tt> to work for polynomials with coefficients that are themselves polynomials.
</div>
<div class="exercise">
<p> <b>Exercise 2.88:</b> Extend the polynomial system to include subtraction of polynomials. (Hint: You may find it helpful to define a generic negation operation.)
</div>
<div class="exercise">
<p> <b>Exercise 2.89:</b> Define procedures that implement the term-list representation described above as appropriate for dense polynomials.
</div>
<div class="exercise">
<p> <b>Exercise 2.90:</b> Suppose we want to have a polynomial system that is efficient for both sparse and dense polynomials. One way to do this is to allow both kinds of term-list representations in our system. The situation is analogous to the complex-number example of section 2-4, where we allowed both rectangular and polar representations. To do this we must distinguish different types of term lists and make the operations on term lists generic. Redesign the polynomial system to implement this generalization. This is a major effort, not a local change.
</div>
<div class="exercise">
<p> <b>Exercise 2.91:</b> A univariate polynomial can be divided by another one to produce a polynomial quotient and a polynomial remainder. For example,
<pre>
x^5 - 1
------- = x^3 + x, remainder x - 1
x^2 - 1
</pre>
<p> Division can be performed via long division. That is, divide the highest-order term of the dividend by the highest-order term of the divisor. The result is the first term of the quotient. Next, multiply the result by the divisor, subtract that from the dividend, and produce the rest of the answer by recursively dividing the difference by the divisor. Stop when the order of the divisor exceeds the order of the dividend and declare the dividend to be the remainder. Also, if the dividend ever becomes zero, return zero as both quotient and remainder.
<p> We can design a <tt>div-poly</tt> procedure on the model of <tt>add-poly</tt> and <tt>mul-poly</tt>. The procedure checks to see if the two polys have the same variable. If so, <tt>div-poly</tt> strips off the variable and passes the problem to <tt>div-terms</tt>, which performs the division operation on term lists. <tt>Div-poly</tt> finally reattaches the variable to the result supplied by <tt>div-terms</tt>. It is convenient to design <tt>div-terms</tt> to compute both the quotient and the remainder of a division. <tt>Div-terms</tt> can take two term lists as arguments and return a list of the quotient term list and the remainder term list.
<p> Complete the following definition of <tt>div-terms</tt> by filling in the missing expressions. Use this to implement <tt>div-poly</tt>, which takes two polys as arguments and returns a list of the quotient and remainder polys.
<div id="scheme-define-div-terms">
(define (div-terms L1 L2)
(if (empty-termlist? L1)
(list (the-empty-termlist) (the-empty-termlist))
(let ((t1 (first-term L1))
(t2 (first-term L2)))
(if (> (order t2) (order t1))
(list (the-empty-termlist) L1)
(let ((new-c (div (coeff t1) (coeff t2)))
(new-o (- (order t1) (order t2))))
(let ((rest-of-result
;compute rest of result recursively>
))
;form complete result>
))))))
</div>
<script>
prompt("scheme-define-div-terms");
</script>
</div>
<h4> Hierarchies of types in symbolic algebra </h4>
<p> Our polynomial system illustrates how objects of one type (polynomials) may in fact be complex objects that have objects of many different types as parts. This poses no real difficulty in defining generic operations. We need only install appropriate generic operations for performing the necessary manipulations of the parts of the compound types. In fact, we saw that polynomials form a kind of ``recursive data abstraction,'' in that parts of a polynomial may themselves be polynomials. Our generic operations and our data-directed programming style can handle this complication without much trouble.
<p> On the other hand, polynomial algebra is a system for which the data types cannot be naturally arranged in a tower. For instance, it is possible to have polynomials in x whose coefficients are polynomials in y. It is also possible to have polynomials in y whose coefficients are polynomials in x. Neither of these types is ``above'' the other in any natural way, yet it is often necessary to add together elements from each set. There are several ways to do this. One possibility is to convert one polynomial to the type of the other by expanding and rearranging terms so that both polynomials have the same principal variable. One can impose a towerlike structure on this by ordering the variables and thus always converting any polynomial to a ``canonical form'' with the highest-priority variable dominant and the lower-priority variables buried in the coefficients. This strategy works fairly well, except that the conversion may expand a polynomial unnecessarily, making it hard to read and perhaps less efficient to work with. The tower strategy is certainly not natural for this domain or for any domain where the user can invent new types dynamically using old types in various combining forms, such as trigonometric functions, power series, and integrals.
<p> It should not be surprising that controlling coercion is a serious problem in the design of large-scale algebraic-manipulation systems. Much of the complexity of such systems is concerned with relationships among diverse types. Indeed, it is fair to say that we do not yet completely understand coercion. In fact, we do not yet completely understand the concept of a data type. Nevertheless, what we know provides us with powerful structuring and modularity principles to support the design of large systems.
<div class="exercise">
<p> <b>Exercise 2.92:</b> By imposing an ordering on variables, extend the polynomial package so that addition and multiplication of polynomials works for polynomials in different variables. (This is not easy!)
</div>
<h4> Extended exercise: Rational functions </h4>
<p> We can extend our generic arithmetic system to include <tt>rational functions</tt> . These are ``fractions'' whose numerator and denominator are polynomials, such as
$$
\frac{x+1}{x^3 - 1}
$$
<p> The system should be able to add, subtract, multiply, and divide rational functions, and to perform such computations as
$$
\frac{x+1}{x^3 - 1} + \frac{x}{x^2 - 1} = \frac{x^3 + 2x^2 + 3x + 1}{x^4 + x^3 - x - 1}
$$
<p> (Here the sum has been simplified by removing common factors. Ordinary ``cross multiplication'' would have produced a fourth-degree polynomial over a fifth-degree polynomial.)
<p> If we modify our rational-arithmetic package so that it uses generic operations, then it will do what we want, except for the problem of reducing fractions to lowest terms.
<div class="exercise">
<p> <b>Exercise 2.93:</b> Modify the rational-arithmetic package to use generic operations, but change <tt>make-rat</tt> so that it does not attempt to reduce fractions to lowest terms. Test your system by calling <tt>make-rational</tt> on two polynomials to produce a rational function
<div id="scheme-test-rational-functions">
(define p1 (make-polynomial 'x '((2 1)(0 1))))
(define p2 (make-polynomial 'x '((3 1)(0 1))))
(define rf (make-rational p2 p1))
</div>
<script>
prompt("scheme-test-rational-functions");
</script>
<p> Now add <tt>rf</tt> to itself, using <tt>add</tt>. You will observe that this addition procedure does not reduce fractions to lowest terms.
<p> We can reduce polynomial fractions to lowest terms using the same idea we used with integers: modifying <tt>make-rat</tt> to divide both the numerator and the denominator by their greatest common divisor. The notion of ``greatest common divisor'' makes sense for polynomials. In fact, we can compute the GCD of two polynomials using essentially the same Euclid's Algorithm that works for integers.<a name="footnote_link_2-60" class="footnote_link" href="#footnote_2-60">60</a> The integer version is
<div id="scheme-define-gcd">
(define (gcd a b)
(if (= b 0)
a
(gcd b (remainder a b))))
</div>
<script>
prompt("scheme-define-gcd");
</script>
<p> Using this, we could make the obvious modification to define a GCD operation that works on term lists:
<div id="scheme-define-gcd-terms">
(define (gcd-terms a b)
(if (empty-termlist? b)
a
(gcd-terms b (remainder-terms a b))))
</div>
<script>
prompt("scheme-define-gcd-terms");
</script>
<p> where <tt>remainder-terms</tt> picks out the remainder component of the list returned by the term-list division operation <tt>div-terms</tt> that was implemented in Exercise 2-91.
</div>
<p>
<div class="exercise">
<p> <b>Exercise 2.94:</b> Using <tt>div-terms</tt>, implement the procedure <tt>remainder-terms</tt> and use this to define <tt>gcd-terms</tt> as above. Now write a procedure <tt>gcd-poly</tt> that computes the polynomial GCD of two polys. (The procedure should signal an error if the two polys are not in the same variable.) Install in the system a generic operation <tt>greatest-common-divisor</tt> that reduces to <tt>gcd-poly</tt> for polynomials and to ordinary <tt>gcd</tt> for ordinary numbers. As a test, try
<div id="scheme-test-polynomial-gcd">
(define p1 (make-polynomial 'x '((4 1) (3 -1) (2 -2) (1 2))))
(define p2 (make-polynomial 'x '((3 1) (1 -1))))
(greatest-common-divisor p1 p2)
</div>
<script>
prompt("scheme-test-polynomial-gcd");
</script>
<p> and check your result by hand.
</div>
<p>
<div class="exercise">
<p> <b>Exercise 2.95:</b> Define P_1, P_2, and P_3 to be the polynomials
$$
P_1 : x^2 - 2x + 1 \\
P_2 : 11x^2 + 7 \\
P_3 : 13x + 5
$$
<p> Now define $Q_1$ to be the product of $P_1$ and $P_2$ and $Q_2$ to be the product of $P_1$ and $P_3$, and use <tt>greatest-common-divisor</tt> (Exercise 2-94) to compute the GCD of $Q_1$ and $Q_2$. Note that the answer is not the same as $P_1$. This example introduces noninteger operations into the computation, causing difficulties with the GCD algorithm.<a name="footnote_link_2-61" class="footnote_link" href="#footnote_2-61">61</a> To understand what is happening, try tracing <tt>gcd-terms</tt> while computing the GCD or try performing the division by hand.
<p> We can solve the problem exhibited in Exercise 2-95 if we use the following modification of the GCD algorithm (which really works only in the case of polynomials with integer coefficients). Before performing any polynomial division in the GCD computation, we multiply the dividend by an integer constant factor, chosen to guarantee that no fractions will arise during the division process. Our answer will thus differ from the actual GCD by an integer constant factor, but this does not matter in the case of reducing rational functions to lowest terms; the GCD will be used to divide both the numerator and denominator, so the integer constant factor will cancel out.
<p> More precisely, if $P$ and $Q$ are polynomials, let $O_1$ be the order of $P$ (i.e., the order of the largest term of $P$) and let $O_2$ be the order of $Q$. Let $c$ be the leading coefficient of $Q$. Then it can be shown that, if we multiply $P$ by the <tt>integerizing factor</tt> $ c^{1 + O_1 - O_2}$, the resulting polynomial can be divided by $Q$ by using the <tt>div-terms</tt> algorithm without introducing any fractions. The operation of multiplying the dividend by this constant and then dividing is sometimes called the <tt>pseudodivision</tt> of $P$ by $Q$. The remainder of the division is called the <tt>pseudoremainder</tt> . </div>
<div class="exercise">
<b>Exercise 2.96:</b>
<ul>
<li>
<p> Implement the procedure <tt>pseudoremainder-terms</tt>, which is just like <tt>remainder-terms</tt> except that it multiplies the dividend by the integerizing factor described above before calling <tt>div-terms</tt>. Modify <tt>gcd-terms</tt> to use <tt>pseudoremainder-terms</tt>, and verify that <tt>greatest-common-divisor</tt> now produces an answer with integer coefficients on the example in Exercise 2-95.
</li>
<li>
<p> The GCD now has integer coefficients, but they are larger than those of P_1. Modify <tt>gcd-terms</tt> so that it removes common factors from the coefficients of the answer by dividing all the coefficients by their (integer) greatest common divisor.
</li>
</ul>
<p> Thus, here is how to reduce a rational function to lowest terms:
<ul>
<li>
Compute the GCD of the numerator and denominator, using the version of <tt>gcd-terms</tt> from Exercise 2-96.
</li>
<li>
<p> When you obtain the GCD, multiply both numerator and denominator by the same integerizing factor before dividing through by the GCD, so that division by the GCD will not introduce any noninteger coefficients. As the factor you can use the leading coefficient of the GCD raised to the power 1 + O_1 - O_2, where O_2 is the order of the GCD and O_1 is the maximum of the orders of the numerator and denominator. This will ensure that dividing the numerator and denominator by the GCD will not introduce any fractions.
</li>
<li>
<p> The result of this operation will be a numerator and denominator with integer coefficients. The coefficients will normally be very large because of all of the integerizing factors, so the last step is to remove the redundant factors by computing the (integer) greatest common divisor of all the coefficients of the numerator and the denominator and dividing through by this factor.
<li>
</ul>
</div>
<div class="exercise">
<b>Exercise 2.97:</b>
<ul>
<li>
<p> Implement this algorithm as a procedure <tt>reduce-terms</tt> that takes two term lists <tt>n</tt> and <tt>d</tt> as arguments and returns a list <tt>nn</tt>, <tt>dd</tt>, which are <tt>n</tt> and <tt>d</tt> reduced to lowest terms via the algorithm given above. Also write a procedure <tt>reduce-poly</tt>, analogous to <tt>add-poly</tt>, that checks to see if the two polys have the same variable. If so, <tt>reduce-poly</tt> strips off the variable and passes the problem to <tt>reduce-terms</tt>, then reattaches the variable to the two term lists supplied by <tt>reduce-terms</tt>.
</li>
<li>
<p> Define a procedure analogous to <tt>reduce-terms</tt> that does what the original <tt>make-rat</tt> did for integers:
<div id="scheme-define-reduce-integers">
(define (reduce-integers n d)
(let ((g (gcd n d)))
(list (/ n g) (/ d g))))
</div>
<script>
prompt("scheme-define-reduce-integers");
</script>
<p> and define <tt>reduce</tt> as a generic operation that calls <tt>apply-generic</tt> to dispatch to either <tt>reduce-poly</tt> (for <tt>polynomial</tt> arguments) or <tt>reduce-integers</tt> (for <tt>scheme-number</tt> arguments). You can now easily make the rational-arithmetic package reduce fractions to lowest terms by having <tt>make-rat</tt> call <tt>reduce</tt> before combining the given numerator and denominator to form a rational number. The system now handles rational expressions in either integers or polynomials. To test your program, try the example at the beginning of this extended exercise:
<div id="scheme-test-make-rational">
(define p1 (make-polynomial 'x '((1 1)(0 1))))
(define p2 (make-polynomial 'x '((3 1)(0 -1))))
(define p3 (make-polynomial 'x '((1 1))))
(define p4 (make-polynomial 'x '((2 1)(0 -1))))
(define rf1 (make-rational p1 p2))
(define rf2 (make-rational p3 p4))
(add rf1 rf2)
</div>
<script>
prompt("scheme-test-make-rational");
</script>
<p> See if you get the correct answer, correctly reduced to lowest terms.
<p> The GCD computation is at the heart of any system that does operations on rational functions. The algorithm used above, although mathematically straightforward, is extremely slow. The slowness is due partly to the large number of division operations and partly to the enormous size of the intermediate coefficients generated by the pseudodivisions. One of the active areas in the development of algebraic-manipulation systems is the design of better algorithms for computing polynomial GCDs.<a name="footnote_link_2-62" class="footnote_link" href="#footnote_2-62">62</a>
</li>
</ul>
</div>
<br>
<br>
<hr>
<div id="footnotes">
<h3 id='Notes'> Notes </h3>
<div id="footnote_2-49" class="footnote">
<p> <a href="#footnote_link_2-49" class="footnote_backlink">49</a> We also have to supply an almost identical procedure to handle the types <tt>(scheme-number complex)</tt>.
</div>
<div id="footnote_2-50" class="footnote">
<p> <a href="#footnote_link_2-50" class="footnote_backlink">50</a> See exercise 2.82 for generalizations.
</div>
<div id="footnote_2-51" class="footnote">
<p> <a href="#footnote_link_2-51" class="footnote_backlink">51</a> If we are clever, we can usually get by with fewer than n2 coercion procedures. For instance, if we know how to convert from type 1 to type 2 and from type 2 to type 3, then we can use this knowledge to convert from type 1 to type 3. This can greatly decrease the number of coercion procedures we need to supply explicitly when we add a new type to the system. If we are willing to build the required amount of sophistication into our system, we can have it search the ``graph'' of relations among types and automatically generate those coercion procedures that can be inferred from the ones that are supplied explicitly.
</div>
<div id="footnote_2-52" class="footnote">
<p> <a href="#footnote_link_2-52" class="footnote_backlink">52</a> This statement, which also appears in the first edition of this book, is just as true now as it was when we wrote it twelve years ago. Developing a useful, general framework for expressing the relations among different types of entities (what philosophers call ``ontology'') seems intractably difficult. The main difference between the confusion that existed ten years ago and the confusion that exists now is that now a variety of inadequate ontological theories have been embodied in a plethora of correspondingly inadequate programming languages. For example, much of the complexity of object-oriented programming languages -- and the subtle and confusing differences among contemporary object-oriented languages -- centers on the treatment of generic operations on interrelated types. Our own discussion of computational objects in chapter 3 avoids these issues entirely. Readers familiar with object-oriented programming will notice that we have much to say in chapter 3 about local state, but we do not even mention ``classes'' or ``inheritance.'' In fact, we suspect that these problems cannot be adequately addressed in terms of computer-language design alone, without also drawing on work in knowledge representation and automated reasoning.
</div>
<div id="footnote_2-53" class="footnote">
<p> <a href="#footnote_link_2-53" class="footnote_backlink">53</a> A real number can be projected to an integer using the round primitive, which returns the closest integer to its argument.
</div>
<div id="footnote_2-54" class="footnote">
<p> <a href="#footnote_link_2-54" class="footnote_backlink">54</a> On the other hand, we will allow polynomials whose coefficients are themselves polynomials in other variables. This will give us essentially the same representational power as a full multivariate system, although it does lead to coercion problems, as discussed below.
</div>
<div id="footnote_2-55" class="footnote">
<p> <a href="#footnote_link_2-55" class="footnote_backlink">55</a> For univariate polynomials, giving the value of a polynomial at a given set of points can be a particularly good representation. This makes polynomial arithmetic extremely simple. To obtain, for example, the sum of two polynomials represented in this way, we need only add the values of the polynomials at corresponding points. To transform back to a more familiar representation, we can use the Lagrange interpolation formula, which shows how to recover the coefficients of a polynomial of degree n given the values of the polynomial at $n + 1$ points.
</div>
<div id="footnote_2-56" class="footnote">
<p> <a href="#footnote_link_2-56" class="footnote_backlink">56</a> This operation is very much like the ordered union-set operation we developed in exercise 2.62. In fact, if we think of the terms of the polynomial as a set ordered according to the power of the indeterminate, then the program that produces the term list for a sum is almost identical to union-set.
</div>
<div id="footnote_2-57" class="footnote">
<p> <a href="#footnote_link_2-57" class="footnote_backlink">57</a> To make this work completely smoothly, we should also add to our generic arithmetic system the ability to coerce a ``number'' to a polynomial by regarding it as a polynomial of degree zero whose coefficient is the number. This is necessary if we are going to perform operations such as
$$
[x^2 + (y+1)x + 5] + [x^2 + 2x + 1]
$$
<p> which requires adding the coefficient y + 1 to the coefficient 2.