-
Notifications
You must be signed in to change notification settings - Fork 0
/
025_systems_with_generic_operations.scm
312 lines (275 loc) · 11.1 KB
/
025_systems_with_generic_operations.scm
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
;; Helpers
(define (say input) (display input) (newline))
;; Get and put operations
;; Global list of available operations
(define op-table '())
;; Operation ((op type) proc), (op type) is key, proc is value
;; Operation constructor and selectors
(define (make-op op type proc)
(cons (cons op type) proc))
(define (op-key op) (car op))
(define (op-proc op) (cdr op))
;;(say "operation test cases")
;;(say (make-op 'op-1 'type-1 (lambda (x) x)))
;;(define test-op (make-op 'op-1 'type-1 (lambda (x) x)))
;;(say (op-key test-op))
;;(say (op-proc test-op))
;;(say (make-op 'op-1 '(type-1 type-1) (lambda (x) x)))
;;(define test-op (make-op 'op-1 '(type-1 type-1) (lambda (x) x)))
;;(say (op-key test-op))
;;(say (op-proc test-op))
;; If operations list is empty - create with given entry
;; If operations list contains entry - don't add duplicate
;; If operations list doesn't contain entry - add it
(define (put-opt op type proc)
(define (put-iter op-table-slice)
(cond ((null? op-table-slice)
(set! op-table (cons (make-op op type proc)
op-table)))
((not (equal? (op-key (car op-table-slice))
(cons op type)))
(put-iter (cdr op-table-slice)))))
(put-iter op-table))
;;(say "put test cases")
;;(put-opt 'op-1 '(type-1 type-1) (lambda (x) x))
;;(say op-table)
;;(put-opt 'op-2 '(type-2 type-2) (lambda (x) x))
;;(say op-table)
;;(put-opt 'op-1 '(type-1 type-1) (lambda (x) x))
;;(say op-table)
(define (get-opt op type)
(define (get-iter op-table-slice)
(cond ((null? op-table-slice) #f)
((equal? (op-key (car op-table-slice))
(cons op type))
(op-proc (car op-table-slice)))
(else (get-iter (cdr op-table-slice)))))
(get-iter op-table))
;;(say "get test cases")
;;(put-opt 'op-1 '(type-1 type-1) (lambda (x) x))
;;(put-opt 'op-2 '(type-2 type-2) (lambda (x) x))
;;(put-opt 'op-3 '(type-3 type-3) (lambda (x) x))
;;(say (get-opt 'op-1 '(type-1 type-1)))
;;(say (get-opt 'op-3 '(type-3 type-3)))
;;(say (get-opt 'op-2 '(type-3 type-3)))
;; Procedures for manipulating tagged data:
(define (set-tag tag content)
(cons tag content))
;; Get tag from dataset
(define (get-tag datum)
(if (pair? datum)
(car datum)
(say "Error! Bad tagged datum.")))
;; Get data from dataset
(define (get-content datum)
(if (pair? datum)
(cdr datum)
(say "Error! Bad tagged datum.")))
;; 2.5 Systems with generic operations
;; Here we will learn how to define operations that are generic over different
;; operations and kinds of arguments. We will now use data-direct techniques
;; to construct a package of arithmetic operations that incorporates all the
;; arithmetic packages we have already constructed. The system will have following
;; abstraction barriers (laysers):
;; 1.
;; From the perspective of someone using numbers, there is a single procedure
;; 'add', that operates on whatever numbers are supplied. 'Add' is a part of
;; a generic interface that allows the separate:
;; - ordinary-arthmetic
;; - rational-arthmetic
;; - complex arthmetic
;; packages to be accessed uniformly by programs that use numbers.
;; 2.
;; Any individual arithmetic package may be accessed through generic procedures
;; (such as 'add-complex' or 'add-rat'), that combine packages designed for
;; different representations (such as rectangular and polar for complex numbers).
;; 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.
;; 2.5.1 Generic arithmetic operations
;; The dask of designing generic arithmetic operations is analogous to designing
;; the generic complex number operations. We would like to have a generic addition
;; procedure 'add', that acts like:
;; - the ordinary primitive addition + on ordinary numbers
;; - 'add-rat' on rational numbers
;; - 'add-complex' on (guess what) 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:
(define (apply-generic op . args)
(let ((tags (map get-tag args)))
(let ((proc (get-opt op tags)))
(if proc
(apply proc (map get-content args))
(say "Error! No method for given types.")))))
;; The generic arithmetic procedures are as follows:
(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))
;; We begin by installing a package for ordinary numbers. We will tag these with
;; the symbol 'scheme-number'. Since the operations take two arguments, they are
;; installed in the table keyed by list (scheme-number scheme-number):
(define (install-scheme-number-arithmetic-package)
(define (tag x) (set-tag 'scheme-number x))
(put-opt 'add '(scheme-number scheme-number)
(lambda (x y) (tag (+ x y))))
(put-opt 'sub '(scheme-number scheme-number)
(lambda (x y) (tag (- x y))))
(put-opt 'mul '(scheme-number scheme-number)
(lambda (x y) (tag (* x y))))
(put-opt 'div '(scheme-number scheme-number)
(lambda (x y) (tag (/ x y))))
(put-opt 'make 'scheme-number
(lambda (x) (tag x)))
#t)
(define (make-scheme-numer n)
((get-opt 'make 'scheme-number) n))
;; Rational arithmetic package:
(define (install-rational-arithmetic-package)
(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))))
(define (tag x) (set-tag 'rational x))
(put-opt 'add '(rational rational)
(lambda (x y) (tag (add-rat x y))))
(put-opt 'sub '(rational rational)
(lambda (x y) (tag (sub-rat x y))))
(put-opt 'mul '(rational rational)
(lambda (x y) (tag (mul-rat x y))))
(put-opt 'div '(rational rational)
(lambda (x y) (tag (div-rat x y))))
(put-opt 'make 'rational
(lambda (n d) (tag (make-rat n d))))
#t)
(define (make-rational n d)
((get-opt 'make 'rational) n d))
;; Handling complex numbers:
;; Rectangular package
(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) (set-tag 'rectangular x))
(put-opt 'real-part '(rectangular) real-part)
(put-opt 'imag-part '(rectangular) imag-part)
(put-opt 'magnitude '(rectangular) magnitude)
(put-opt 'angle '(rectangular) angle)
(put-opt 'make-from-real-imag 'rectangular
(lambda (x y) (tag (make-from-real-imag x y))))
(put-opt 'make-from-mag-ang 'rectangular
(lambda (r a) (tag (make-from-mag-ang r a))))
#t)
;; Polar package
(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) (set-tag 'polar x))
(put-opt 'real-part '(polar) real-part)
(put-opt 'imag-part '(polar) imag-part)
(put-opt 'magnitude '(polar) magnitude)
(put-opt 'angle '(polar) angle)
(put-opt 'make-from-real-imag 'polar
(lambda (x y) (tag (make-from-real-imag x y))))
(put-opt 'make-from-mag-ang 'polar
(lambda (r a) (tag (make-from-mag-ang r a))))
#t)
(define (install-complex-arithmetic-package)
;; Imported from rectangular and polar packages
(define (make-from-real-imag x y)
((get-opt 'make-from-real-imag 'rectangular) x y))
(define (make-from-mag-ang r a)
((get-opt 'make-from-mag-ang 'polar) a r))
;; Internal stuff
(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
(define (tag z) (set-tag 'complex z))
(put-opt 'add '(complex complex)
(lambda (z1 z2) (tag (add-complex z1 z2))))
(put-opt 'sub '(complex complex)
(lambda (z1 z2) (tag (sub-complex z1 z2))))
(put-opt 'mul '(complex complex)
(lambda (z1 z2) (tag (mul-complex z1 z2))))
(put-opt 'div '(complex complex)
(lambda (z1 z2) (tag (div-complex z1 z2))))
(put-opt 'make-from-real-imag 'complex
(lambda (x y) (tag (make-from-real-imag x y))))
(put-opt 'make-from-mag-ang 'complex
(lambda (r a) (tag (make-from-mag-ang r a))))
#t)
(define (make-complex-from-real-imag x y)
((get-opt 'make-from-real-imag 'complex) x y))
(define (make-complex-from-mag-ang r a)
((get-opt 'make-from-mag-ang 'complex) r a))
;; Here we have a two-level tag system. The outer tag 'complex is used to direct
;; the number to the complex package. Once within the complex package, the next
;; tag ('rectangular or 'polar) is used to direct the number to the correct
;; package.
;; Install ALL the packages
(install-scheme-number-arithmetic-package)
(install-rational-arithmetic-package)
(install-rectangular-package)
(install-polar-package)
(install-complex-arithmetic-package)
;; Test cases
;;
;;(define scheme-number-t1 (make-scheme-numer 5))
;;(define scheme-number-t2 (make-scheme-numer 10))
;;(say (get-content (add scheme-number-t1 scheme-number-t2)))
;;(say (get-content (sub scheme-number-t1 scheme-number-t2)))
;;(say (get-content (mul scheme-number-t1 scheme-number-t2)))
;;(say (get-content (div scheme-number-t1 scheme-number-t2)))
;;
;;(define rational-number-t1 (make-rational 5 7))
;;(define rational-number-t2 (make-rational 7 4))
;;(say (get-content (add rational-number-t1 rational-number-t2)))
;;(say (get-content (sub rational-number-t1 rational-number-t2)))
;;(say (get-content (mul rational-number-t1 rational-number-t2)))
;;(say (get-content (div rational-number-t1 rational-number-t2)))