# state equations in practice

When people think of mathematical models of state for programs and other computer systems, it’s natural and conventional to consider state as a map from symbolic names of state variable to values. This is an error for a number of reasons including, most critically, the lack of any compositional structure. Let’s step back and think about what a discrete state system looks like.

Notation warning: I’m using _ for subscript x_p or f_r or one_(two).

- We have a set E of discrete “events” called the
*event alphabet,*The event alphabet defines all the possible events and inputs that can cause state to change. - A finite sequence of events over E can be thought of as a system history, describing what series of events drove the system from its initial state (which is the point at which no events at all had happened.) You could complain at this point that deterministic, single thread of execution (the sequence) models can’t describe non-determinism, parallelism, concurrency, and abstract properties, but you’d be wrong – keep reading.
- Now we are going to take an odd step – I don’t want to start with a state machine or other object that represents the whole system. I want to start with state variables that express some system property and how it changes in response to events. For example, a state variable
*Executing*might tell us the process identifier of the currently running (executing) process or it could be set valued in a multi-processor environment. And*priority_**p*might be the priority of the process with identifier,*p.*As the system advances state, the values of the state variable change but often we want to have properties that are true in any state. For example, (*Executing=p and**q in Waiting and priority_p*>*priority_q*implies*TimeSinceSwitch*< t) would be a good property for some operating system that needs to schedule by priority but also let lower priority processes advance. - State variables are dependent variables that depend on the event sequence and some map that extracts information from the event sequence. The event sequence is the free variable and the state variable is a dependent variable that embodies some aspect of system state. Everything that determines current state is in the event sequence but state is not a completed thing, not an discrete object, it is the cumulative information of multiple state variables depending on that event sequence. The same event sequence may correspond to completely different collections of state variables for different systems.
- We might have many state variables to specify how an operating system works or even how a simple gate works. Let’s think of all of them at the same level of abstraction as depending on the same event sequence (finite, of course).
- The key to this approach is to be able to compactly define state variables and abstract properties of state variables even though the event alphabets and maps on sequences of events will often be enormously complicated.
- Say a variable
*y*is a state variable for an event alphabet*E*if*y=f(*for some function**σ**)*f*where**σ**is a free variable in the set of finite sequences over E which includes the empty sequence*Nil*. The map*f*is a*solution*for*y*that extracts values from the sequence as state advances (as events are appended to the event sequence). Essentially*y*depends on two parameters: the sequence and the map. These parameters are often implicit. - By the definition of state variable, if
*y*is a state variable, then*y=f(*for some**σ**)*f*so*y(z) = f(z)*by the usual convention. - If
*y*is a state variable then*y(Nil)*is the value of*y*in the initial state since*y(Nil)*=*f(Nil).* - The convention here is that
**σ**is always a free variable over E* (the set of finite sequences over alphabet E). - If
*z*is a sequence and e is an event or a variable over events, write Append(z,e) or just*ze*for the sequence obtained by appending*e*to*z*on the right. So*y(*is the value of**σ**e)*y*in the “next state” – the state after*e –*since*y(***σ**e) = f(**σ**e). - If
*y = y(*then the event “e” leaves the value of**σ**e)*y*unchanged. - If
*y(*then the event “e” increases the value of**σ**e) = y +2*y*by 2. The equation*y(***σ***e)=y+2*can be rewritten as*f(***σ***e)=f(*if we know**σ) +**2*f.* - A primitive recursive definition on the sequence will completely define the solution
*f*if the recursive map is defined. So [*y(Nil)= k*and*y(*defines**σ**e) = h(e, y)]*f*if*h*and*k*are defined. Note*y(Nil) = f(Nil)=k*. And*y(*Suppose sequence**σ**e)= h(e,y) = h(e,y(**σ**)) = h(e,f(**σ**)).*w=<a,b,c>*, then*y(w) = f(<a,b,c>) = h(c,f(<a,b>)) = h(c,h(b,f(<a>)) = h(c,h(b,h(a,k)))).*In many cases we don’t want to or can’t specify*f*completely –*f*is a solution to the constraints on*y*and there may be many or infinitely many or no solutions. - For example, suppose C is a state variable and
*C(Nil)=0 and C(*. Then we have defined C to be the length of the event sequence.**σ**e) = 1+C - Define
*L(*so**σ**e) = e*L*is the most recent event. Note we do not need to define*L(Nil)*. - Define [
*SinceReset(Nil) = 0*and*SinceReset(*if e=RESET and**σ**e) = ( 0*1+ SinceReset*otherwise). - Suppose we have a state variable
*Clock*that somehow determines time passed since initial state from the event sequence. Then (*Clock(*) is the duration of event**σ**e)- Clock*e*in this state (and may be different for different values of**σ**). Define*waiting*_p(Nil)=0 and*waiting*_p(**σ**e)=0 if*Executing(*and**σ**e) =p*waiting*_p(**σ**e)=*waiting*_p+*(Clock(*otherwise. So**σ**e)- Clock)*waiting_*p is the time that process*p*has been waiting to become the executing process. We might want a property (*waiting*_p > k only if*priority*_p <*priority*_p where*q = Executing)*. - Ok, now we can consider component composition – which is just function composition. Consider the state variable L defined above and suppose we have a new alphabet B consisting of
*Left_x*and*Right_x*for x in some set X of data values. The goal is to construct a shift register from*n*instances of*L.*Define L*i =*L(*ui*) where*ui*is also a state variable that has values that are sequences of elements of X. Usually we want components to state in the initial state so*ui(Nil)**= Nil.*This means L*i*(Nil)= L(*Nil*) which is something we didn’t define. Now define*ui(*where “e” is an element of the event alphabet of the composite system. Remember I’m using juxtaposition to mean “append on the right” so, for example (**σ**e)*u_iL_(i-1)*) means “append the current value of*L_(i-1) to the current value of “**u_i(***σ**e)=*u_i x if i=1 and e=Right_x*or*if i=n and e=Left_x**u_i(*if i>1 and**σ**e)= u_i L_(i-1)

*e=Left_x* *u_i(***σ**e)= u_i L_(i+1) if i<n and e=Right_x

- The definition above causes all
*n*components to change state in parallel when an event is appended to the event sequence of the composite system – but it’s just composition:*L_i = L(u_i)= L(u_i(*and**σ**))*L_i(*where e’ is calculated as above.**σ**e)= L(u_i(**σ**e) ) =L(u_i(**σ)**e’) = e’ - If a device D that is constructed from components
*C_1, … C_n*that are interconnected. D is a function ofbut the components don’t “see” the same events. For each component, we need a state variable**σ***u_i*that is sequence valued and a function of. Usually these are defined recursively:**σ***u_i(Nil)= Nil*and*u_i(*or something like that. That is, each event**σ**e)= concatenate(u_i,h_i(e,C_1(u_1)… C_n(u_n )))*e*for*D*causes event or events*h_i(e,C_1(u_1 )… C_n(u_n ))*to be appended to the sequence of events*u_i.*