# Symbolic differentiation program with dispatching

Section 2.3.2 described a program that performs symbolic differentiation:

``````(define (deriv exp var)
(cond ((number? exp) 0)
((variable? exp) (if (same-variable? exp var) 1 0))
((sum? exp)
(deriv (augend exp) var)))
((product? exp)
(make-sum
(make-product (multiplier exp)
(deriv (multiplicand exp) var))
(make-product (deriv (multiplier exp) var)
(multiplicand exp))))
<more rules can be added here>
(else (error "unknown expression type -- DERIV" exp))))
``````

We can regard this program as performing a dispatch on the type of the expression to be differentiated. In this situation the ''type tag'' of the datum is the algebraic operator symbol (such as `+` ) and the operation being performed is `deriv` . We can transform this program into data-directed style by rewriting the basic derivative procedure as

``````(define (deriv exp var)
(cond ((number? exp) 0)
((variable? exp) (if (same-variable? exp var) 1 0))
(else ((get 'deriv (operator exp)) (operands exp)
var))))
(define (operator exp) (car exp))
(define (operands exp) (cdr exp))
``````

a. Explain what was done above. Why can't we assimilate the predicates `number?` and `same-variable?` into the data-directed dispatch?

b. Write the procedures for derivatives of sums and products, and the auxiliary code required to install them in the table used by the program above.

c. Choose any additional differentiation rule that you like, such as the one for exponents (exercise 2.56 ), and install it in this data-directed system.

d. In this simple algebraic manipulator the type of an expression is the algebraic operator that binds it together. Suppose, however, we indexed the procedures in the opposite way, so that the dispatch line in `deriv` looked like

``````((get (operator exp) 'deriv) (operands exp) var)
``````

What corresponding changes to the derivative system are required?

##### Authentication required

``````(define (make-table)
(let ((local-table (mlist '*table*)))
(define (lookup key-1 key-2)
(let ((subtable (massoc key-1 (mcdr local-table))))
(if subtable
(let ((record (massoc key-2 (mcdr subtable))))
(if record
(mcdr record)
#f))
#f)))
(define (insert! key-1 key-2 value)
(let ((subtable (massoc key-1 (mcdr local-table))))
(if subtable
(let ((record (massoc key-2 (mcdr subtable))))
(if record
(set-mcdr! record value)
(set-mcdr! subtable
(mcons (mcons key-2 value)
(mcdr subtable)))))
(set-mcdr! local-table
(mcons (mlist key-1
(mcons key-2 value))
(mcdr local-table)))))
'ok)
(define (dispatch m)
(cond ((eq? m 'lookup-proc) lookup)
((eq? m 'insert-proc!) insert!)
(else (error "Unknown operation -- TABLE" m))))
dispatch))

(define operation-table (make-table))
(define get (operation-table 'lookup-proc))
(define put (operation-table 'insert-proc!))

(define (same-variable? v1 v2)
(and (variable? v1) (variable? v2) (eq? v1 v2)))

(define (variable? x) (symbol? x))

(define (deriv exp var)
(cond ((number? exp) 0)
((variable? exp) (if (same-variable? exp var) 1 0))
(else ((get 'deriv (operator exp)) (operands exp)
var))))
(define (operator exp) (car exp))
(define (operands exp) (cdr exp))

(install-sum-package)
(install-product-package)

(check-equal? (deriv '(+ x 3) 'x) 1)
(check-equal? (deriv '(+ 3 x) 'x) 1)
(check-equal? (deriv '(* 5 x) 'x) 5)
(check-equal? (deriv '(* x 3) 'x) 3)``````