(Lispex)sicp.io
3.8 · 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.

Lispex · SICP sourceScheme-compatible SICP syntax executed by the Lispex SICP profile.
(begin
  (define (schedule time action agenda)
    (let ((event (list time action)))
      (cond ((null? agenda) (list event))
            ((< time (car (car agenda))) (cons event agenda))
            (else
             (cons (car agenda)
                   (schedule time action (cdr agenda)))))))
  (define agenda
    (schedule 5 'close
      (schedule 2 'sense
        (schedule 5 'open '()))))
  agenda)
Lispex learning runtimeLispex SICP profile 1.0.0
Loading Lispex SICP runtime
Lispex · SICP source407 / 1,048,576 UTF-8 bytes
Examples
Result
Output
Value
Diagnostic
Visible execution0 / 0 trace events
    This browser result is not a Lispex Vouch record or authority.wasm —
    Expected observation

    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 before you read the hint.

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

    Show one hint

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