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.
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.
(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)- Output
- —
- Value
- —
- Diagnostic
- —
The first program returns ((2 sense) (5 open) (5 close)). The second returns ((2 1) (4 4) (5 14)).
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.
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.