SICP Exercise 3.34 Louis Reasoner squarer

Exercise 3.34.  Louis Reasoner wants to build a squarer, a constraint device with two terminals such that the value of connector b on the second terminal will always be the square of the value a on the first terminal. He proposes the following simple device made from a multiplier:

(define (squarer a b)
  (multiplier a a b))


There is a serious flaw in this idea. Explain.

EXPLANATION

The code is here.

See tests in the code link above. After creating the squarer, when we set the value of 'a', 'b' is set correctly to be the square of a. However, the reverse does not work. When b is set, a does not change. So the non-directionality of computation that we expect from constraint-based systems is not there.

The reason for this can be seen by looking at the logic of the procedure "process-new-value" in the multiplier. This procedure expects two of the multiplier's three pieces of data (m1, m2 and p) to be valid before it computes and sets the third value. In the squarer defined above, since m1 and m2 are the same connector, they are both without values when we set p externally. So the process-new-value procedure is unable to set the value of the multiplier's input connectors. (All the conditional checks in process-new-value fail.)

Comments

Popular posts from this blog

SICP Exercise 2.56 differentiation rule

SICP Exercise 1.28 (Miller-Rabin Test)

SICP Exercise 4.18 a alternative strategy for interpreting internal definitions