(load "util.scm") (display "\nexample - generic arithmetic operations\n") ; (define (display x) ()) ; (define (newline) ()) ; can be used to import stuff silently ; Put and get functions. We could have implemented this via a list of ; three-tuples, but I don't know how to create global variables yet so we just ; use this code from SO. Doesn't look too complicated. ; https://stackoverflow.com/questions/5499005/how-do-i-get-the-functions-put-and-get-in-sicp-scheme-exercise-2-78-and-on (define *op-table* (make-hash-table)) (define (put op type proc) (hash-table/put! *op-table* (list op type) proc)) (define (get op type) (hash-table/get *op-table* (list op type) #f)) (define *coercion-table* (make-hash-table)) (define (put-coercion type1 type2 proc) (hash-table/put! *coercion-table* (list type1 type2) proc)) (define (get-coercion type1 type2) (hash-table/get *coercion-table* (list type1 type2) #f)) ;; Helpers for generic arithmetic operations (define (attach-tag type-tag contents) (cond ((eq? type-tag 'scheme-number) contents) (else (cons type-tag contents)))) (define (type-tag datum) (cond ((number? datum) 'scheme-number) ((pair? datum) (car datum)) (else (error "Bad tagged datum -- TYPE-TAG" datum)))) (define (contents datum) (cond ((number? datum) datum) ((pair? datum) (cdr datum)) (else (error "Bad tagged datum -- CONTENTS" datum)))) (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)) (error "No method for these types -- APPLY-GENERIC" (list op type-tags)))))) (define (install-integer-package) (define (make-integer x) (if (integer? x) (tag x) (error "Not an integer -- MAKE-INTEGER " x))) (define (integer->rational x) (make-rational x 1)) (define (tag x) (attach-tag 'integer x)) (put 'add '(integer integer) (lambda (x y) (tag (+ x y)))) (put 'sub '(integer integer) (lambda (x y) (tag (- x y)))) (put 'mul '(integer integer) (lambda (x y) (tag (* x y)))) (put 'div '(integer integer) (lambda (x y) (tag (/ x y)))) (put 'equ? '(integer integer) (lambda (x y) (= x y))) (put 'exp '(integer integer) (lambda (x y) (tag (expt x y)))) ; using primitive expt (put '=zero? '(integer) (lambda (x) (= x 0))) (put 'make 'integer (lambda (x) (make-integer x))) (put 'raise '(integer) integer->rational) (display "[install-integer-package]\n") 'done) (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)))) (define (add3-rat x y z) (add-rat (add-rat x y) z)) (define (equ? x y) (= (* (numer x) (denom y)) (* (numer y) (denom x)))) (define (rational->real x) (make-scheme-number (/ (numer x) (denom x)))) ;; 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 'add3 '(rational rational rational) (lambda (x y z) (tag (add3-rat x y z)))) (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 'equ? '(rational rational) equ?) (put '=zero? '(rational) (lambda (x) (= (numer x) 0))) (put 'make 'rational (lambda (n d) (tag (make-rat n d)))) (put 'raise '(rational) rational->real) (display "[install-rational-package]\n") 'done) (define (install-scheme-number-package) (define (tag x) (attach-tag 'scheme-number x)) (define (real->complex x) (make-complex-from-real-imag x 0)) (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 'equ? '(scheme-number scheme-number) (lambda (x y) (= x y))) (put 'exp '(scheme-number scheme-number) (lambda (x y) (tag (expt x y)))) ; using primitive expt (put '=zero? '(scheme-number) (lambda (x) (= x 0))) (put 'make 'scheme-number (lambda (x) (tag x))) (put 'raise '(scheme-number) real->complex) (display "[install-scheme-number-package]\n") 'done) (define (install-rectangular-package) (define (real-part z) (car z)) (define (imag-part z) (cdr z)) (define (magnitude z) (sqrt (+ (square (real-part z)) (square (imag-part z))))) (define (angle z) (atan (imag-part z) (real-part z))) (define (tag z) (attach-tag 'rectangular z)) (define (make-from-real-imag x y) (tag (cons x y))) (define (make-from-mag-ang r a) (tag (cons (* r (cos a)) (* r (sin a))))) ; interface to the rest of the system (put 'real-part '(rectangular) real-part) (put 'imag-part '(rectangular) imag-part) (put 'magnitude '(rectangular) magnitude) (put 'angle '(rectangular) angle) (put '=zero? '(rectangular) (lambda (z) (= (real-part z) (imag-part z) 0))) (put 'make-from-mag-ang 'rectangular make-from-mag-ang) (put 'make-from-real-imag 'rectangular make-from-real-imag) (display "[install-rectangular-package]\n") 'done) (define (install-polar-package) (define (real-part z) (* (magnitude z) (cos (angle z)))) (define (imag-part z) (* (magnitude z) (sin (angle z)))) (define (magnitude z) (car z)) (define (angle z) (cdr z)) (define (tag z) (attach-tag 'polar z)) (define (make-from-real-imag x y) (tag (cons (sqrt (+ (square x) (square y))) (atan y x)))) (define (make-from-mag-ang r a) (tag (cons r a))) ; interface to rest of the system (put 'real-part '(polar) real-part) (put 'imag-part '(polar) imag-part) (put 'magnitude '(polar) magnitude) (put 'angle '(polar) angle) (put '=zero? '(polar) (lambda (z) (= (magnitude z) 0))) (put 'make-from-mag-ang 'polar make-from-mag-ang) (put 'make-from-real-imag 'polar make-from-real-imag) (display "[install-polar-package]\n") 'done) (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)) ;; getters (define (real-part z) (apply-generic 'real-part z)) (define (imag-part z) (apply-generic 'imag-part z)) (define (magnitude z) (apply-generic 'magnitude z)) (define (angle z) (apply-generic 'angle z)) ;; 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)))) (define (equ? z1 z2) (and (= (magnitude z1) (magnitude z2)) (= (angle z1) (angle z2)))) ;; interface to rest of the system (put 'real-part '(complex) real-part) (put 'imag-part '(complex) imag-part) (put 'magnitude '(complex) magnitude) (put 'angle '(complex) angle) (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?) (put '=zero? '(complex) =zero?) (display "[install-complex-package]\n") 'done) ;; constructors (define (make-integer n) ((get 'make 'integer) n)) (define (make-rational n d) ((get 'make 'rational) n d)) (define (make-scheme-number n) ((get 'make 'scheme-number) n)) (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 (real-part z) ((get 'real-part '(complex)) z)) (define (imag-part z) ((get 'imag-part '(complex)) z)) (define (magnitude z) ((get 'magnitude '(complex)) z)) (define (angle z) ((get 'angle '(complex)) z)) ;; generic operations (define (add x y) (apply-generic 'add x y)) (define (add3 x y z) (apply-generic 'add3 x y z)) (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)) (define (equ? x y) (apply-generic 'equ? x y)) (define (=zero? x) (apply-generic '=zero? x)) (define (exp x y) (apply-generic 'exp x y)) (define (raise x) (apply-generic 'raise x)) (install-integer-package) (install-rational-package) (install-scheme-number-package) (install-rectangular-package) (install-polar-package) (install-complex-package) (assert (add (make-scheme-number 10) (make-scheme-number 20)) (make-scheme-number 30)) (define p1 (make-complex-from-mag-ang 14.142135623730951 0.7853981633974483)) (define e1 (make-complex-from-real-imag 10 10)) (assert (add e1 e1) (make-complex-from-real-imag 20 20)) (newline) (display "ex-2.77 - see comments") (newline) ; real-part (and all other selectors are implemented via calls to apply ; generic. The first call to apply generic has the type 'magnitude '(complex). ; By adding the code from Alyssa that call gets dispatched a second time which ; results in a call to apply generic with 'magnitude '(rectangular). This calls ; the actual magnitude function from the rectangular package. (newline) (display "ex-2.78 - simplify scheme number") (newline) ; Solution at the beginning of this file. (assert (add 5 3) 8) (newline) (display "ex-2.79 - equ?") (newline) ; Extended each of the packages and defined generic procedure (assert (equ? (make-scheme-number 10) (make-scheme-number 10)) #t) (assert (equ? (make-rational 3 4) (make-rational 6 8)) #t) (assert (equ? (make-complex-from-mag-ang 3 4) (make-complex-from-real-imag 6 8)) #f) (assert (equ? p1 e1) #t) ; define above (newline) (display "ex-2.80 - =zero?") (newline) ; Extended each of the packages and defined generic procedure (assert (=zero? 0) #t) (assert (=zero? 1) #f) (assert (=zero? (make-rational 0 1)) #t) (assert (=zero? (make-rational 1 1)) #f) (assert (=zero? e1) #f) (assert (=zero? p1) #f) (newline) (display "ex-2.81 - Louis trying things") (newline) (define (scheme-number->complex n) (make-complex-from-real-imag (contents n) 0)) (put-coercion 'scheme-number 'complex scheme-number->complex) (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 ((eq? type1 type2) (error "No need to coerce identical types" (list op type-tags))) (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))))))) (display "[see comments]\n") (assert (exp 3 3) 27) (assert (add (make-scheme-number 3) (make-complex-from-real-imag 3 4)) (make-complex-from-real-imag 6 4)) ; a. This is an endless loop. Louis change is not necessary, because if we ; coerce the arguments into the same type we would have found the respective ; procedure already. ; (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) ; (exp (make-complex-from-real-imag 3 4) (make-complex-from-mag-ang 2 3)) ; b. apply-generic already handles arguments of the same type correctly. It ; will simply not find a coercion procedure and return. ; c. added check for identical types to apply-generic. The following now just ; causes an error and no endless loop. ; (exp (make-complex-from-real-imag 3 4) (make-complex-from-mag-ang 2 3)) (newline) (display "ex-2.82 - multi argument coercion") (newline) (define (scheme-number->rational n) (make-rational (contents n) 1)) (put-coercion 'scheme-number 'rational scheme-number->rational) (define (coerce-args target-type args) (define (coerce-arg arg) (let ((t1->t2 (get-coercion (type-tag arg) target-type))) (if (procedure? t1->t2) (t1->t2 arg) arg))) (map coerce-arg args)) (define (apply-generic op . args) (define (try-args args-list) (if (null? args-list) (error "No method for these types" (list op (map type-tag args))) (let ((proc (get op (map type-tag (car args-list)))) (args-contents (map contents (car args-list)))) (if (procedure? proc) (apply proc args-contents) (try-args (cdr args-list)))))) (define (coerce-to-arg arg) (coerce-args (type-tag arg) args)) (try-args (cons args (map coerce-to-arg args)))) (assert (add3 (make-rational 1 3) 2 (make-rational 3 9)) (make-rational 8 3)) ; This approach does not work if there exist procedures for mixed types or if ; the coerced type that would work is different from any of the existing ; arguments' types. (display (coerce-args 'rational (list (make-rational 1 3) 2 3))) (newline) (newline) (display "ex-2.83") (newline) ; Our scheme-number package supports real numbers so we use that as our ; real-number package without further changes. Additionally, we create an ; integer package that only accepts integers in the constructor. (assert (sub (make-integer 3) (make-integer 1)) (make-integer 2)) (define i (make-integer 3)) (display i) (newline) (display (raise i)) (newline) (display (raise (raise i))) (newline) (display (raise (raise (raise i)))) (newline) (newline) (display "ex-2.84") (newline) ; All we have to do is update coerce-args to do consecutive raises ; to reach the target type. (define (coerce-args target-type args) (define (coerce-arg arg) (if (eq? (type-tag arg) target-type) arg (let ((proc (get 'raise (list (type-tag arg))))) (if (procedure? proc) (proc (contents arg)) arg)))) (let ((coerced-args (map coerce-arg args))) (if (equal? args coerced-args) coerced-args ; no more raising possible (coerce-args target-type coerced-args)))) (assert (equ? (make-integer 3) (make-complex-from-real-imag 3 0)) #t) (assert (equ? (make-integer 3) (make-complex-from-real-imag 3 1)) #f) (assert (equ? (make-integer 3) (make-rational 3 1)) #t) (assert (add3 (make-rational 1 3) (make-integer 2) (make-rational 3 9)) (make-rational 8 3)) ;(display (coerce-args 'scheme-number (list (make-rational 1 3) 2 (make-rational 3 9)))) (newline) (display "ex-2.85 - drop it") (newline) (newline) (display "ex-2.86") (newline)