>catsequence-maps-for-modelling-state-machines.md

Sequence Maps for Modelling State Machines

date

Notes on Victor Yodaiken, ‘State Machines for Large Scale Computer Software and Systems’, Formal Aspects of Computing, Vol. 36, No. 2, June 2024

The problem of scale

State machines are fundamental to the design of computer systems; distributed systems, operating systems and real-time controllers are all, in the end, state machines. The difficulty is that the traditional representations (state diagrams, transition tables, definitions by tuples) scale badly. Even a modest increase in a system’s complexity produces a combinatorial explosion in the state space, and the tables and diagrams become too large to reason about.

Drawing a diagram for a system with thousands of states, or enumerating every transition in a table, quickly becomes impractical; and yet an exhaustive representation of this kind is exactly what real systems require. Yodaiken’s paper proposes a different instrument, sequence maps, which rest on primitive recursive functions and make state machines a practical model for complex software.

Sequence maps

A sequence map is a function from a finite sequence of events to the output of the system in the state which that sequence reaches. Rather than enumerating states, we define the behaviour recursively:

f(ε)=cf(ε) = c
(output in the initial state, where ε is the empty sequence)

f(wa)=g(f(w),a)f(wa) = g(f(w), a)
(output after event a following sequence w)

The approach stays firmly within classical deterministic state machine theory while being far more practical for complex systems. What gets written down is the transition, as a function of the previous state and the current event, rather than an enormous state table; the exploration of the state space can therefore be deferred, and the space computed or generated only when it is needed.

A saturating counter

Suppose we want to count consecutive occurrences of the same event, saturating at some maximum, a problem that turns up in signal processing, control systems and network monitoring. The specification is to count how many consecutive 1s have occurred, saturating at k (so that once k consecutive 1s have been seen, the counter stays at k until a 0 appears), and to reset to 0 whenever a 0 occurs. As a sequence map:

SatCounter(ε) = 0
SatCounter(w0) = 0                          (any 0 resets the count)
SatCounter(w1) = min(k, SatCounter(w) + 1)  (increment, but cap at k)

The empty sequence ε represents the initial state, with a count of 0; any sequence ending in 0 produces a count of 0; and any sequence ending in 1 increments the previous count without ever exceeding k. Traced with k=3:

Sequence      | Output
--------------|--------
ε             | 0
1             | 1
11            | 2
111           | 3   (saturated)
1111          | 3   (stays saturated)
11110         | 0   (reset)
111101        | 1   (counting again)

The specification is complete and unambiguous in three lines. The equivalent state diagram has k+1 states and 2(k+1) transitions, and a transition table would have to carry the same information.

Composition

Sequence maps compose. Outputs can be transformed: if Counter(w)\rm{Counter}(w) counts up from 0, then k−Counter(w)\rm{k - Counter}(w) counts down from kk. Components can be connected, with connection maps specifying how they interact; in a shift register, for instance, each cell takes its input from the previous cell’s output. And parallel systems, in which several components change state simultaneously, can be described entirely through function composition.

The paper proves that sequence primitive recursive (s.p.r.) maps are equivalent to Moore-type state machines and their products. Anything expressible as a traditional state machine can therefore be expressed as a sequence map, only more concisely and at any scale.

Composition is formalised with a connection map CC that routes outputs to inputs:

FC(w,a)=G(F(w),  C(F(w),a))F_C(w, a) = G\big(F(w),\; C(F(w), a)\big)

Here FF is a component (itself a sequence map), GG is the environment, and CC wires the signals. Because each piece is primitive recursive on sequences, the composed system remains s.p.r.

Paxos

Yodaiken demonstrates the method’s practical value on Paxos, a distributed consensus algorithm notorious for being difficult to understand and to implement correctly. With sequence maps he defines a packet network as a collection of agents joined by connection maps, specifies the four Paxos message types and the rules for their transmission, and proves the safety property (that at most one value can win consensus) with a clear mathematical proof. The specification is more detailed than Lamport’s original ‘Paxos Made Simple’ and catches subtle issues the original left implicit, which is precisely the kind of detail one wants settled before writing any code.

Implementation

As with many convenient representations, the direct translation is not necessarily the best implementation. In Rust:

// Define the possible events that can trigger state changes
#[derive(Clone)]
enum Event {
    A,
    B,
    C,
    // Add more events as needed
}

// Define the system's state (for simplicity, just use an integer, but you could have a more complex struct)
struct State {
    value: i32,  // Example of state data
}

// Define the function that computes the state after a sequence of events
fn process_sequence(w: Vec<Event>) -> State {
    // Base case: Empty sequence returns the initial state
    if w.is_empty() {
        return State { value: 0 };  // Initial state (can be any value or struct)
    }

    // Recursive case: f(wa) = g(f(w), a), so split off the last event
    let last_event = w[w.len() - 1].clone();
    let preceding_events = w[..w.len() - 1].to_vec();

    // Process the last event with the result of the recursive call
    let previous_state = process_sequence(preceding_events);
    transition(&previous_state, last_event)
}

// Define the transition function for processing an event
fn transition(state: &State, event: Event) -> State {
    match event {
        Event::A => State { value: state.value + 1 },  // Example logic for event A
        Event::B => State { value: state.value - 1 },  // Example logic for event B
        Event::C => State { value: state.value * 2 },  // Example logic for event C
    }
}

fn main() {
    // Example sequence of events
    let events = vec![Event::A, Event::B, Event::A, Event::C];

    // Compute the resulting state after the sequence of events
    let final_state = process_sequence(events);

    // Print the resulting state
    println!("Final state value: {}", final_state.value);
}

The recursive version mirrors the mathematical definition, but it has a serious performance flaw. The line let preceding_events = w[..w.len() - 1].to_vec() allocates a new vector on every recursive call, which makes the function O(n²) in both time and memory. For a sequence of length n there are n allocations, of size n-1 on the first call, n-2 on the second, and so on down to 0 on the nth, so that the total allocated is 1 + 2 + … + (n-1) = O(n²).

In practice it would be written imperatively:

fn process_sequence(w: &[Event]) -> State {
    let mut state = State { value: 0 };  // Initial state

    for event in w {
        state = transition(&state, event.clone());
    }

    state
}

This runs in O(n)O(n) time with O(1)O(1) additional memory, and it is how a sequence map would actually be implemented in production code. The recursive version is better read as the specification, since it maps directly onto the mathematical definition. Primitive recursion supplies the clarity needed to reason about correctness; the loop supplies the performance needed to run at scale.