### Chapter 2, Building Abstractions with Data

### Section - 2.5 - Systems with Generic Operations

#### Exercise 2.84

It is an interesting exercise. I implemented it for raising operation by assuming single rooted hierarchy - or tree shaped hierarchy - so tower shape will automatically work.

Points to note:

- It is easy now to add a new type in the existing system.
- As of now there is following tower implemented in my arithmetic system:
*rational -> scheme-number -> complex*. Note my scheme-number is a real number. I have not implemented the package for only integers. Since the idea is same so I skipped the integer package :). Thus the lowest in my hierarchy is rational and not integer. - When a new type is added in arithmatic package, the developer of the package needs to tell two things - parent by calling
`put-parent <child> <parent>`

and how to raise to parent by calling`put-rais <child> <raise-proc>`

. - Thus the new type need not to worry about any other type - just mention the parent and how to raise into the parent!
- The current system works if there is no defined operation for the current types. For Eg: Here
`exp`

is only defined for â€˜scheme-number, but not for rational. When rational-numbers are supplied for`exp`

then the procedure first checks - if this operation is present in rational package. If not it tries to*upcast*to next type which in this case is â€˜scheme-number. Now after upcasting to scheme-number, the exp procedure is found as it is defined there. Thus it is called for the result. - Suppose even after upcasting to all the super-types - the operation is not found - then it reports error.
- To check if this works with tree - create tree as:
*scheme-number -> complex*,*rational -> complex*. And to test lets remove`sub`

procedure from the`rational`

package. Then this call`(sub (make-number 5) (make-rational 1 5))`

should work by*upcasting*both arguments to complex and then calling`sub`

for the complex package. Note in this case`(exp (make-rational 1 2) (make-number -1))`

wont work because in the new-hierarchy rational number cannot be upcasted to scheme-number where*exp*is defined. - Note that the procedures used inside
`apply-generic`

should not be based on`apply-generic`

for eg:`raise`

can not be called using`apply-generic`

because it creates a circular dependency: raise -> apply-generic -> raise â€¦(I happen to make this mistake and later removed raise from apply-generic and moved it to separate table - raise-table). - I used two new tables - type-table and cast-table - which can be combined also but for simplicity i kept them separate.

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

#lang sicp
(#%require (only racket/base error))
(#%require (only racket/base make-hash))
(#%require (only racket/base hash-set!))
(#%require (only racket/base hash-ref))
(define (apply-generic op . args)
(let ((type-tags (map type-tag args)))
(let ((proc (get op type-tags)))
(if (not (null? proc))
(apply proc (map contents args))
(let ((has-same-type
(accumulate
(lambda (a b) (and a b))
#t
(map
(lambda(x) (equal? (car type-tags) x))
(cdr type-tags)
)
)
))
(let ((highest-type (if has-same-type
(get-parent (car type-tags))
(find-highest-type type-tags)
)
))
(if (null? highest-type)
(error "Could not find a suitable ancestor" op args)
(let ((raised-args (map-until
'()
(lambda(arg)
(raise-to-type highest-type arg)
)
args
)
))
(if (not (null? raised-args))
(apply apply-generic op raised-args)
(error "Error. No suitable raise method found to raise all types into " highest-type " for raising types " type-tags)
)
)
)
)
)
)
)
)
)
(define *type-table* (make-hash))
(define (put-parent type parent)
(let ((already-set (hash-ref *type-table* (list 'parent type) '())))
(if (null? already-set)
(hash-set! *type-table* (list 'parent type) parent)
(error "Parent already defined for :" type " parent currently *present* is " already-set " the passed parent " parent)
)
)
)
(define (get-parent type)
(hash-ref *type-table* (list 'parent type) '())
)
(define *cast-table* (make-hash))
(define (put-raise type proc)
(let ((already-set (hash-ref *cast-table* (list 'raise type) '())))
(if (null? already-set)
(hash-set! *cast-table* (list 'raise type) proc)
(error "Raise is already defined for :" type)
)
)
)
(define (get-raise type)
(hash-ref *cast-table* (list 'raise type) '())
)
(define (get-ancestors type)
(if (null? type)
'()
(cons type (get-ancestors (get-parent type)))
)
)
(define (contains item item-list)
(fold-left
(lambda(result new)
(or result (equal? item new))
)
#f
item-list
)
)
(define (find-highest-type type-tags)
(let ((ancestors-of-each-type (map get-ancestors type-tags)))
(let ((smallest-ancestor-set
(accumulate (lambda(new remaining)
(if (< (size new) (size remaining))
new
remaining
)
)
(car ancestors-of-each-type)
ancestors-of-each-type
)
))
(define (find-type-present-in-each-ancestors type-list)
(if (null? type-list)
'()
(let ((found (accumulate
(lambda (a b) (and a b))
#t
(map
(lambda(ancestors) (contains (car type-list) ancestors))
ancestors-of-each-type)
)
))
(if found
(car type-list)
(find-type-present-in-each-ancestors (cdr type-list))
)
)
)
)
(find-type-present-in-each-ancestors smallest-ancestor-set)
)
)
)
(define (raise-to-type type arg)
(cond ((null? arg) '())
((equal? type (type-tag arg)) arg)
(else (let ((proc (get-raise (type-tag arg))))
(if (null? proc)
'()
(raise-to-type type (proc arg))
)
)
)
)
)
(define (map-until until-val proc args)
(if (null? args)
'()
(let ((arg (car args)))
(let ((marg (proc arg)))
(if (equal? marg until-val)
'()
(cons marg (map-until until-val proc (cdr args)))
)
)
)
)
)
(define (fold-left op initial sequence)
(define (iter result rest)
(if (null? rest)
result
(iter (op result (car rest))
(cdr rest))))
(iter initial sequence))
(define (raise n)
((get-raise (type-tag n)) n)
)
(define (square x) (* x x))
(define *op-table* (make-hash))
(define (put op type proc)
(hash-set! *op-table* (list op type) proc)
)
(define (get op type)
(hash-ref *op-table* (list op type) '())
)
(define (attach-tag type-tag contents)
(if (number? contents)
contents
(cons type-tag contents)
)
)
(define (type-tag datum)
(cond
((number? datum) 'scheme-number)
((pair? datum) (car datum))
(error "Bad tagged datum -- TYPE-TAG" datum)
)
)
(define (contents datum)
(cond ((number? datum) datum)
((pair? datum) (cdr datum))
(error "Bad tagged datum -- CONTENTS" datum)
)
)
(define (filter predicate sequence)
(cond ((null? sequence) nil)
((predicate (car sequence))
(cons (car sequence)
(filter predicate (cdr sequence))))
(else (filter predicate (cdr sequence)))))
(define (size list)
(if (null? list) 0 (+ 1 (size (cdr list)))))
(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))
)
)
(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)))
;equ?
(put 'equ? '(scheme-number scheme-number) =)
;; following added to Scheme-number package
(put 'exp '(scheme-number scheme-number)
(lambda (x y) (tag (expt x y)))) ; using primitive expt
; raise operation
(put-raise 'scheme-number (lambda(x) (make-complex-from-real-imag (contents x) 0)))
; set parent
(put-parent 'scheme-number 'complex)
'done
)
(define (make-number n)
((get 'make 'scheme-number) n)
)
(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))))
;; equ?
(define (equ? x y)
(and (= (numer x) (numer y)) (= (denom x) (denom 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))))
; comment this to check the example suggested above for tree structure support
(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))))
(put 'equ? '(rational rational) equ?)
; raise operation
(put-raise 'rational (lambda(r) (make-number (/ (numer (contents r)) (denom (contents r))))))
; set parent
(put-parent 'rational 'scheme-number)
; uncomment this and comment above raise to make rational as child of complex instead of number
; (put-raise 'rational (lambda(r) (make-complex-from-real-imag (/ (numer (contents r)) (denom (contents r))) 0)))
; uncomment this and comment above raise to make rational as child of complex instead of number
; (put-parent 'rational 'complex)
'done
)
(define (make-rational n d)
((get 'make 'rational) n d)
)
(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 (+ (apply-generic 'real-part z1) (apply-generic 'real-part z2))
(+ (apply-generic 'imag-part z1) (apply-generic 'imag-part z2))))
(define (sub-complex z1 z2)
(make-from-real-imag (- (apply-generic 'real-part z1) (apply-generic 'real-part z2))
(- (apply-generic 'imag-part z1) (apply-generic 'imag-part z2))))
(define (mul-complex z1 z2)
(make-from-mag-ang (* (apply-generic 'magnitude z1) (apply-generic 'magnitude z2))
(+ (apply-generic 'angle z1) (apply-generic 'angle z2))))
(define (div-complex z1 z2)
(make-from-mag-ang (/ (apply-generic 'magnitude z1) (apply-generic 'magnitude z2))
(- (apply-generic 'angle z1) (apply-generic 'angle z2))))
(define (equ? z1 z2)
(and
(= (apply-generic 'real-part z1) (apply-generic 'real-part z2))
(= (apply-generic 'imag-part z1) (apply-generic 'imag-part 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))))
(put 'equ? '(complex complex) equ?)
; raise operation is not defined for complex
; parent not defined for complex
'done
)
(define (install-rectangular-package)
;; internal procedures
(define (real-part z) (car z))
(define (imag-part z) (cdr z))
(define (make-from-real-imag x y) (cons x y))
(define (magnitude z)
(sqrt (+ (square (real-part z))
(square (imag-part z)))))
(define (angle z)
(atan (imag-part z) (real-part z)))
(define (make-from-mag-ang r a)
(cons (* r (cos a)) (* r (sin a))))
;; interface to the rest of the system
(define (tag x) (attach-tag 'rectangular x))
(put 'real-part '(rectangular) real-part)
(put 'imag-part '(rectangular) imag-part)
(put 'magnitude '(rectangular) magnitude)
(put 'angle '(rectangular) angle)
(put 'make-from-real-imag 'rectangular
(lambda (x y) (tag (make-from-real-imag x y))))
(put 'make-from-mag-ang 'rectangular
(lambda (r a) (tag (make-from-mag-ang r a))))
'done)
(define (install-polar-package)
;; internal procedures
(define (magnitude z) (car z))
(define (angle z) (cdr z))
(define (make-from-mag-ang r a) (cons r a))
(define (real-part z)
(* (magnitude z) (cos (angle z))))
(define (imag-part z)
(* (magnitude z) (sin (angle z))))
(define (make-from-real-imag x y)
(cons (sqrt (+ (square x) (square y)))
(atan y x)))
;; interface to the rest of the system
(define (tag x) (attach-tag 'polar x))
(put 'real-part '(polar) real-part)
(put 'imag-part '(polar) imag-part)
(put 'magnitude '(polar) magnitude)
(put 'angle '(polar) angle)
(put 'make-from-real-imag 'polar
(lambda (x y) (tag (make-from-real-imag x y))))
(put 'make-from-mag-ang 'polar
(lambda (r a) (tag (make-from-mag-ang r a))))
'done)
(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))
(define (equ? x y)
(apply-generic 'equ? x y)
)
(define (exp x y) (apply-generic 'exp x y))
(define (add x y) (apply-generic 'add x y))
(define (sub x y) (apply-generic 'sub x y))
(install-rectangular-package)
(install-polar-package)
(install-scheme-number-package)
(install-rational-package)
(install-complex-package)

Output/Examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

> (display (add (make-number 50000) (make-complex-from-real-imag 2 3)))
(complex rectangular 50002 . 3)
> (display (add (make-rational 1 5) (make-complex-from-real-imag 2.0 3)))
(complex rectangular 2.2 . 3)
> (exp (make-number 2) (make-number 3))
8
> (exp (make-rational 1 2) (make-number -1))
2
> (exp (make-complex-from-real-imag 1 2) (make-number 3))
. . Could not find a suitable ancestor exp ((complex rectangular 1 . 2) (complex rectangular 3 . 0))
> (exp (make-rational 25 100) (make-rational 1 2))
1/2
> (exp (make-rational 25.0 100.0) (make-rational 1.0 2.0))
0.5
>