sicp.io
3.3.5 · Discrete-event simulation

An agenda makes simulated time explicit.

Timestamped actions can be inserted into an ordered agenda, then processed from the earliest simulated time even when they were scheduled in another order.

Guiding question

How can a simulation determine what happens next without following scheduling order?

  • Represent an event as explicit time and action data
  • Insert events in nondecreasing timestamp order
  • Preserve scheduling order between events at the same time
  • Accumulate state by processing the earliest event first

schedule walks an already ordered agenda until the new time belongs before the current event. Equal timestamps do not satisfy the strict less-than test, so a new event at that time is inserted after events already there. The returned list exposes both simulated time and tie order.

propagate removes the earliest event, applies its numeric change to signal, and records the resulting state beside that event time. The agenda models logical simulation time rather than waiting for a wall clock, and these examples do not model simultaneous-event physics beyond their explicit insertion rule.

SICP code407 of 1,048,576 UTF-8 bytes
Examples
Result
Output
Value
Diagnostic
Execution trace0 / 0 events
    Programs run in the browser with their result and execution trace.
    Expected result

    The first program returns ((2 sense) (5 open) (5 close)). The second returns ((2 1) (4 4) (5 14)).

    Trace focus

    Follow schedule comparisons until each event reaches its timestamp position. Then watch propagate remove times 2, 4, and 5 in order while signal becomes 1, 4, and 14. These traces describe this agenda and its explicit tie rule only.

    Try it yourself

    Change the program and compare the result.

    Schedule an event named reset at time 1 in the first program. Predict its position without changing the insertion rule.

    Show hint

    The new timestamp is earlier than every event already in the agenda.

    Complete this lesson

    0 of 21 lessons complete in this chapter0%