SICP Exercise 3.31 accept-action-procedure! implementation

Exercise 3.31.   The internal procedure accept-action-procedure! defined in make-wire specifies that when a new action procedure is added to a wire, the procedure is immediately run. Explain why this initialization is necessary. In particular, trace through the half-adder example in the paragraphs above and say how the system's response would differ if we had defined accept-action-procedure! as

(define (accept-action-procedure! proc)
  (set! action-procedures (cons proc action-procedures)))


SOLUTION

The code is here.

EXPLANATION

This program contains the complete implementation of the agenda operations and the after-delay proc as explained in the SICP text. The test results below show the behavior of the simulator in both cases:

1. When action procedures are run immediately after they are added to wires
2. When they are not run immediately after they are added to wires

In the case where action procedures are immediately run, we always ensure that when any primitive gate is created or when a complex combination of gates are wired together, all the intermediate wires and all the external outputs are in the correct state right from the beginning. After this, any change in the inputs will trigger a series of activations that will ensure that the final external outputs contain the correct signals.

When action procedures are not immediately run, the circuit may start with incorrect states in some wires. In this example of a half-adder, signal on wire E remains 0 when in-fact, it should be a 1 since the input to the inverter is 0 (signal on C). This wrong state of the inverter output at the start causes incorrect outputs once we start changing the half-adder inputs A and B.

When A is changed to 1, sum does not change to 1 (when we expect it to).
And when B is changed to 1, carry changes to 1 and sum remains at 0. However, this correctness is accidental.


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