414 lines
No EOL
14 KiB
Scheme
414 lines
No EOL
14 KiB
Scheme
#lang sicp
|
|
|
|
; Building on top of exercise 2.80
|
|
|
|
;
|
|
; a) First part is the program where apply-generic is generalized
|
|
; b) The second part of the exercise is the example of a situation where the
|
|
; approach is not general enough. This will be outlined in the bottom of the
|
|
; file.
|
|
;
|
|
|
|
;;;
|
|
;;;
|
|
;;; Setup for the exercise
|
|
;;;
|
|
;;;
|
|
|
|
(define (square x) (* x x))
|
|
|
|
; Dispatch table: list of rows, structure below:
|
|
; one row: (op-tag . (... one cell) (...) (...))
|
|
; one cell: ((list of type tags) . procedure)
|
|
; essentially, the whole table is an alist of alists
|
|
; (list of type tags) is a list of length n for n-ary op
|
|
; a unary op can be called with a symbol instead of a list of symbols for a type
|
|
; tag, but this will be converted to a length-1 list while searching the table
|
|
(define *dispatch-table* '())
|
|
(define *coercion-table* '())
|
|
|
|
;;;
|
|
;;; Dispatch table
|
|
;;;
|
|
|
|
; finds member in alist based on tag, which is the car of the row
|
|
(define (search-in-list tag list)
|
|
(if (null? list)
|
|
nil
|
|
(let* ((current-member (car list))
|
|
(member-tag (car current-member)))
|
|
(if (equal? member-tag tag)
|
|
current-member ; we don't strip the key when returning
|
|
(search-in-list tag (cdr list))))))
|
|
|
|
(define (get op-tag init-type-tags)
|
|
(let* ((type-tags (if (symbol? init-type-tags)
|
|
(list init-type-tags)
|
|
init-type-tags))
|
|
; wrap type-tags in a list if it's supplied as a symbol
|
|
(row-1 (search-in-list op-tag *dispatch-table*))
|
|
(row (if (null? row-1) nil (cdr row-1))) ; we strip the name of the row
|
|
(procedure-1 (search-in-list type-tags row))
|
|
(procedure (if (null? procedure-1) nil (cdr procedure-1))))
|
|
(if (null? procedure)
|
|
#f
|
|
procedure)))
|
|
|
|
|
|
(define (add-pair! alist tag value)
|
|
(set! alist (cons (cons tag value)
|
|
alist)))
|
|
|
|
(define (put op-tag init-type-tags procedure)
|
|
(let* ((type-tags (if (symbol? init-type-tags)
|
|
(list init-type-tags)
|
|
init-type-tags))
|
|
(search-row (search-in-list op-tag *dispatch-table*))
|
|
; here we don't strip the key from search-in-list
|
|
(row (if (null? search-row)
|
|
(begin (set! *dispatch-table* (cons (cons op-tag nil)
|
|
*dispatch-table*))
|
|
(car *dispatch-table*))
|
|
search-row))
|
|
(search-cell (search-in-list type-tags (cdr row))))
|
|
(if (null? search-cell)
|
|
(set-cdr! row (cons (cons type-tags procedure)
|
|
(cdr row)))
|
|
; attaching new pair to row
|
|
(set-cdr! search-cell procedure))))
|
|
|
|
;;;
|
|
;;; Auxillary functions for type tags
|
|
;;;
|
|
|
|
; The actual exercise 2.78
|
|
(define (attach-tag type-tag contents)
|
|
(if (eq? type-tag 'scheme-number)
|
|
contents
|
|
(cons type-tag contents)))
|
|
(define (type-tag datum)
|
|
(cond ((pair? datum) (car datum))
|
|
((number? datum) 'scheme-number)
|
|
(error "Bad tagged datum: TYPE-TAG" datum)))
|
|
(define (contents datum)
|
|
(cond ((pair? datum) (cdr datum))
|
|
((number? datum) datum)
|
|
(error "Bad tagged datum: CONTENTS" datum)))
|
|
|
|
;;;
|
|
;;; Complex packages
|
|
;;;
|
|
|
|
(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)
|
|
|
|
(install-rectangular-package)
|
|
(install-polar-package)
|
|
|
|
;;;
|
|
;;; Apply generic and complex function definitions (modified for exercise 2.82)
|
|
;;;
|
|
|
|
; Check if all members in a list are not #f
|
|
(define (all list)
|
|
(cond ((null? list) #t)
|
|
((car list) (all (cdr list)))
|
|
(else #f)))
|
|
|
|
(define (try-coercion t1 t2)
|
|
(if (eq? t1 t2)
|
|
(lambda (x) x)
|
|
(get-coercion t1 t2)))
|
|
|
|
; Generate list of lists, where every i-th member is a list of conversions of
|
|
; all other types into the i-th type
|
|
(define (generate-coercion-attempts type-tags)
|
|
(map (lambda (to)
|
|
; list of coercions
|
|
(map (lambda (from)
|
|
(try-coercion from to))
|
|
type-tags))
|
|
type-tags))
|
|
|
|
(define (filter p list)
|
|
(cond ((null? list) nil)
|
|
((p (car list)) (cons (car list)
|
|
(filter p (cdr list))))
|
|
(else (filter p (cdr list)))))
|
|
|
|
(define (get-op-for-any-coercion-attempt op args coercion-attempts)
|
|
(if (null? coercion-attempts)
|
|
(error "No method for these types"
|
|
(list op (map type-tag args)))
|
|
(let* ((current-attempt (car coercion-attempts))
|
|
(new-args (map (lambda (arg coercion)
|
|
(coercion arg))
|
|
args current-attempt))
|
|
(new-type-tags (map type-tag new-args))
|
|
(new-contents (map contents new-args))
|
|
(proc (get op new-type-tags)))
|
|
(if proc
|
|
(apply proc new-contents)
|
|
(get-op-for-any-coercion-attempt op args
|
|
(cdr coercion-attempts))))))
|
|
|
|
(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))
|
|
(let* ((coercion-attempts (generate-coercion-attempts type-tags))
|
|
(filtered-attempts (filter all coercion-attempts)))
|
|
; only try those coercion lists where only procedures are
|
|
; returned, without a single #f
|
|
(get-op-for-any-coercion-attempt op args filtered-attempts))))))
|
|
|
|
(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))
|
|
|
|
(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))
|
|
|
|
|
|
;;;
|
|
;;; Generic function definition
|
|
;;;
|
|
|
|
(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))
|
|
|
|
;;;
|
|
;;; 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)))
|
|
; part of exercise 2.79
|
|
(put 'equ? '(scheme-number scheme-number) =)
|
|
; part of exercise 2.80
|
|
(put '=zero? '(scheme-number) (lambda (x) (zero? x)))
|
|
'done)
|
|
(install-scheme-number-package)
|
|
|
|
(define (make-scheme-number n)
|
|
((get 'make 'scheme-number) n))
|
|
|
|
;;;
|
|
;;; Rational number package
|
|
;;;
|
|
|
|
(define (install-rational-package)
|
|
;; internal procedures
|
|
(define (numer x) (car x))
|
|
(define (denom x) (cdr x))
|
|
; Modified to make exercise 2.79 easier to implement
|
|
(define (make-rat n d)
|
|
(let ((g (gcd n d))
|
|
(sign-flip (if (< d 0) -1 1)))
|
|
(cons (/ n g sign-flip) (/ d g sign-flip))))
|
|
(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))))
|
|
; part of exercise 2.79
|
|
(put 'equ? '(rational rational)
|
|
(lambda (x y) (and (= (numer x) (numer y))
|
|
(= (denom x) (denom y)))))
|
|
; part of exercise 2.80
|
|
(put '=zero? '(rational)
|
|
(lambda (x) (= (numer x) 0)))
|
|
'done)
|
|
(install-rational-package)
|
|
(define (make-rational n d)
|
|
((get 'make 'rational) n d))
|
|
|
|
;;;
|
|
;;; Complex number 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))))
|
|
|
|
; Code added as fix in the exercise 2.77
|
|
(put 'real-part '(complex) real-part)
|
|
(put 'imag-part '(complex) imag-part)
|
|
(put 'magnitude '(complex) magnitude)
|
|
(put 'angle '(complex) angle)
|
|
; Code added as fix in the exercise
|
|
|
|
; part of exercise 2.79
|
|
(put 'equ? '(complex complex)
|
|
(lambda (z1 z2) (and (= (real-part z1) (real-part z2))
|
|
(= (imag-part z1) (imag-part z2)))))
|
|
; part of exercise 2.80
|
|
(put '=zero? '(complex)
|
|
(lambda (z) (= (magnitude z) 0)))
|
|
'done)
|
|
(install-complex-package)
|
|
(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))
|
|
|
|
|
|
|
|
;;;
|
|
;;; Type coercion functions (added in exercise 2.82)
|
|
;;;
|
|
|
|
(define (get-coercion t1 t2)
|
|
(let* ((row-1 (search-in-list t1 *coercion-table*))
|
|
(row (if (null? row-1) nil (cdr row-1))) ; we strip the name of the row
|
|
(procedure-1 (search-in-list t2 row))
|
|
(procedure (if (null? procedure-1) nil (cdr procedure-1))))
|
|
(if (null? procedure)
|
|
#f
|
|
procedure)))
|
|
|
|
(define (put-coercion t1 t2 procedure)
|
|
(let* ((search-row (search-in-list t1 *coercion-table*))
|
|
; here we don't strip the key from search-in-list
|
|
(row (if (null? search-row)
|
|
(begin (set! *coercion-table* (cons (cons t1 nil)
|
|
*coercion-table*))
|
|
(car *coercion-table*))
|
|
search-row))
|
|
(search-cell (search-in-list t2 (cdr row))))
|
|
(if (null? search-cell)
|
|
(set-cdr! row (cons (cons t2 procedure)
|
|
(cdr row)))
|
|
; attaching new pair to row
|
|
(set-cdr! search-cell procedure))))
|
|
|
|
(put-coercion 'scheme-number 'rational ; assuming integer
|
|
(lambda (x) (make-rational x 1)))
|
|
(put-coercion 'scheme-number 'complex
|
|
(lambda (x) (make-complex-from-real-imag x 0)))
|
|
(put-coercion 'rational 'complex ; assuming integer
|
|
(lambda (x)
|
|
(make-complex-from-real-imag (exact->inexact (/ (cadr x)
|
|
(caddr x)))
|
|
0)))
|
|
; I'm not going to be very careful and modular about this code, because this
|
|
; exercise is based on a leaky idea of the type hierarchy anyways
|
|
; (scheme-numbers can already be real, which is a superset of the rationals,
|
|
; this approach will have to be fixed in the following exercises anyways
|
|
|
|
; As for the flaws of the approach given in apply-generic, there are plenty of
|
|
; examples as to how the approach could fail. For example imagine the op:
|
|
; (exp 'complex 'rational)
|
|
; now, imagine we apply it to a 'complex and a 'scheme-number
|
|
; if we had a more general strategy the scheme-number would be converted to a
|
|
; 'rational, and the operation would succeed, but the current version will only
|
|
; try (exp 'complex 'complex), and will then fail. |