Finite State Machine a tcl example Initiated by [Theo Verelst] There are more pages about the principles and practices of [Finite State Machines], which is probably good idea, this one is supposed to be a tcl example, simple to begin with, in the line of thinking that an example is probably best to make clear what a theory can be about. Lets set an [array] which covers the domain of the input and current_state of a tiny [Finite State Machine], and spans the range of possible state transitions or next states. Because it is not so easy to straight ahead deal with multi dimensional arrays in a way where the lookup is without iterative functions, I've simply used a comma in the index, as probably some languages would use as syntactic construction. In Tcl, I think this is just like using any other legal character, so what follows are simly one dimensional array indices with human reader friendly notation. array set ns {nop,nop nop nop,inc 1 1,inc 2 1,nop 1 2,inc inf 2,nop inf inf,inc inf inf,nop inf} set state nop We see that we have two inputs to the next_state function, one is a number, or no_operation (sort of like an unactive state) or inf_inite, which is much sooner than need be, for obvious reason. The other is like a basic command: nop (no operation), or inc_crement, which works always except in infinity. The domains of the two variables are chosen independently, which makes it easier to make in principle any combination of command and previous state legal. The next state is of course obtained by: set state $ns($state,$input) Where input can be set as 'nop' 'inc', as example: set state nop set state $ns($state,nop) nop set state $ns($state,inc) 1 set state $ns($state,nop) 1 set state $ns($state,inc) 2 set state $ns($state,nop) 2 set state $ns($state,inc) inf set state $ns($state,nop) inf set state $ns($state,inc) inf Because we enumerate all possible state,input combinations a larger set of state transitions become easily unmanageable, though of course it would be easy to define them for a larger set of natural numbers in this way, even automatically. As from common enough mathematical notation, we'd have #states x #inputs total state transition possibilities, unless we limit the domains of the two variables in combinations somehow, for instance we could limit the domain for the 'nop' input to the initial state nop and the final state inf, and easily extend the domain of the state variable to {nop,1,2,inf} to {nop,1,2,...,n,inf}, where n is an element of the set of natural numbers.