sicp-ex-3.31



<< Previous exercise (3.30) | Index | Next exercise (3.32) >>


For our event-driven system, it needs an change in bit of a wire to trigger an action (invert, or, add, etc). However, when all the wires are initialized to 0, no actions will be triggered. Therefore we need to trigger those actions manually during the initialization stage.

sam

we use add-action in our function element definitions. If we don't initialize; the time when this function element is inserted in the system will not be recorded on the agenda. Then it is possible, when agenda is simulated via propagate, the function element will not be simulated at all.


joe w

In the specific example, calling propagate after changing the first signal to 1 without running all the actions won't result in the inverter bit having already been flipped and the and gate will fail.



Rather Iffy

At any moment, not necessarily moment 0, when an action procedure is added to a wire that wire and the wires it is connected to by a function box can hold signal values that are not in the correct relationship as defined by that function box. Only when the signal value on the wire changes will the correct relationship between inputs and the output after a delay be enforced. To ensure correctness from the earlier moment of adding the function box it is necessary that the action procedure is run immediately.



thomas

I think the explanations given above are a little hazy. So here's my take: When we build a function (eg define an and-gate) we call add-action! to the input wires. This procedure adds the appropriate procedure (add-action-procedure) to the input-wires, which will be called when the wire signal changes. When that happens it will add commands to the appropriate time-slot in the agenda, that will then change signals and possibly build new-functions. However this will not happen when we run propagate (without an initialization), since the propagate-procedure will only call the procedures stored in the agenda, but the commands will only get stored in the agenda when the procedures are run in the first place. So in order to store them in the agenda we'll have to run the procedure when it gets added to the wire. When we then run propagate, the first (initlaizer) commands can be executed, which change signals and possibly build new-functions-that add procedures to new wires and store new commands in the agenda until we're finished.