
State Machines - fogus
http://blog.markwshead.com/869/state-machines-computer-science/
======
RodgerTheGreat
State machines are a powerful approach for programming, but often expressing
them in structured languages can be inelegant- state machines and error
handling are the primary applications of "goto" in C. They aren't an
impassible obstacle, though, if you're working with a language that can define
new syntax easily.

I am reminded of a particularly interesting DSL[1] for Forth that makes state
machines look like their transition tables:

    
    
      4 WIDE FSM: <Fixed.Pt#>
      \ input:  |  other?  |  num?   |  minus?  |   dp?     |
      \ state:  ---------------------------------------------
         ( 0 )     DROP >0    EMIT >1   EMIT >1     EMIT >2
         ( 1 )     DROP >1    EMIT >1   DROP >1     EMIT >2
         ( 2 )     DROP >2    EMIT >2   DROP >2     DROP >2 ;
    

[1][http://galileo.phys.virginia.edu/classes/551.jvn.fall01/fsm....](http://galileo.phys.virginia.edu/classes/551.jvn.fall01/fsm.html)

~~~
nova
I like the approach of "Practical UML Statecharts in C/C++", at least the
simple one. More or less, keep the state on a function pointer, each state is
represented by a function which gets incoming messages or events and returns
the next state. I used it for a RS485 protocol running on a microcontroller
quite successfully.

~~~
pierrebouchet
There is also the HSM (hierarchical state machine) extension of this concept
(by the same author, not sure it's in the same book) where a given state
either handles an event or returns a pointer to a "superstate" that handles
it. This kind of "behavioral inheritance" allows for a much more compact code.

Compact and efficient state machines are immensely useful when working with
small microcontrollers that don't even run an OS.

~~~
nickmain
The other nice thing about HSMs are concurrent sub-states. They allow clean
modeling of the orthogonal aspects of the state.

------
ahelwer
One absolutely phenomenal implementation of a state machine is in Intel's
Thread Building Blocks library. They (somewhat[1]) recently added a feature
called flow graphs, which is an abstraction over tasks (themselves an
abstraction over threads). I've yet to find a better control structure for
concurrent scheduling problems.

TBB isn't NUMA-aware though, which limits its use in HPC.

[1] [http://software.intel.com/en-us/blogs/2011/09/08/the-
intel-t...](http://software.intel.com/en-us/blogs/2011/09/08/the-intel-
threading-building-blocks-flow-graph-is-now-fully-supported/)

~~~
ralph
What do you prefer about TBB over CSP-style channels, as in #golang?

------
charliesome
He neglected to mention PDAs (push-down automaton) - which is basically the
same concept as a DFA or NFA but with a stack.

State transitions can manipulate the stack, and the top of a stack can be used
to pick which state to move to.

PDAs aren't as powerful as Turing machines, but they are able to parse any
context free grammar, so they are able to recognize languages that contain any
number of 'a' characters followed by the same number of 'b' characters.

~~~
alecbenzer
> PDAs... are able to parse any context free grammar

To be complete, _non-deterministic_ PDAs can recognize any context-free
language. Afaik you can't use a normal, deterministic parser to recognize
things like the language of all palindromes (which is context-free).

~~~
charliesome
Ah, thanks for the correction!

------
nano_o
It may be worthwhile realizing that most programming or specification
languages describe State Machines [1], more or less explicitly (sorry if this
is obvious). Moreover State Machines can be described using ordinary
mathematics instead of custom programming languages or logics. Hence State
Machines could be used as a common denominator to describe all kinds of
computation, similarly to what equations written using ordinary mathematics
are to physics. According to Leslie Lamport, from whom the ideas in this
comment are borrowed, State Machines are also a powerful teaching tool that
prevents language from getting in the way of concepts [1].

State Machines described in ordinary mathematics have other advantages. For
example, substitution distributes over operators in mathematics (but not in
most programming languages). This is very handy for deriving an implementation
from a specification. Another example is that composition can be treated
uniformly by using a single state space for all State Machines.

If you are interested in the applications and advantages of describing
computation by State Machines you may like reading the works of Leslie Lamport
[2] or texts about the ASM method [3].

[1] [https://research.microsoft.com/en-
us/um/people/lamport/pubs/...](https://research.microsoft.com/en-
us/um/people/lamport/pubs/state-machine.pdf)

[2] <https://research.microsoft.com/en-us/um/people/lamport/>

[3] <https://en.wikipedia.org/wiki/Abstract_state_machines>

------
vibrunazo
Udacity's course on programming languages (CS262) explains State Machines,
among many other things. Their teaching format is genius. Highly recommended.

[http://www.udacity.com/overview/Course/cs262/CourseRev/apr20...](http://www.udacity.com/overview/Course/cs262/CourseRev/apr2012)

~~~
bhrgunatha
Coursera are currently running a whole course on Automata [1] and some similar
material is covered in their Compilers course [2]. I'm not sure if you can
still enroll or if they'll be repeated in the future.

[1] <https://class.coursera.org/automata/>

[2] <https://class.coursera.org/compilers>

------
patrickg
I do have a degree in computer science so I know about state machines. Yet I
don't use them at all in my code. Am I missing a great opportunity? I know it
is close to impossible to answer that question without knowing the problem
space, but perhaps there is a nice article about solving (common) problems
with state machines?

~~~
ajuc
It's possible you use state machines and don't know it :) Have you ever used
loop with if inside, that dispatches based on value of some variable? That's
state machine implementation in structured language.

I've encountered such code many times in our codebase.

Or maybe you used some business process engine (like jbpm) - that's also state
machine.

I've even made jbpm-like engine in javascript for my html5 game - I use it to
write quests in my game, and I plan to refactor dialog trees to also use it.

It's graph with nodes and transitions, nodes specify actions game should do,
transitions specify conditions player has to do to move to next node.

Here's code if anybody's interested: <https://github.com/ajuc/pefjs>

And here's graphical editor for graphs:
<https://github.com/ajuc/jsDotForPefjs>

------
userdeveloper
State machines just seem natural to me. I actually sketched out a state
machine before I even knew what a state machine was. The other concept I
really like is lookup tables. Reminds me of truth tables. Are state machines
really just a form of lookup table? I guess it's just how my mind works
because these tables seem natural to me.

This is great thread. I have only used lex/flex to make my state machines. I'd
like to try something new eventually.

~~~
endgame
Zed Shaw's article about building state machines with ragel might interest
you: <http://www.zedshaw.com/essays/ragel_state_charts.html>

~~~
userdeveloper
That write-up contains a little too much hype for my tastes. And as soon as I
saw the words Java and C++ I lost 95% of any curiousity I have about ragel.

------
k4st
For those interested in symbolically manipulating [1] state machines (NFAs),
pushdown automata (PDAs), and context-free grammars (CFGs), take a look at
<http://www.grailplus.org>.

[1] <http://www.ioreader.com/2011/03/15/pattern-matching-in-grail>

------
sudonim
This is great timing and a really digestible summary. There's a talk on
StateManager in Ember.js tonight [1] and I was embarrassed to know nothing
about state machines. Glad to be able to attend a little more knowledgeable

[1] <http://www.meetup.com/Sproutcore-NYC/events/59870422/>

------
signa11
_nothing_ teaches state machines like networking protocols, just look at the
server & client fsm's for tftp (rfc-1350).

------
husted
I use a lot of state machines in my embedded projects. Perhaps it's because I
was a developer on a state chart to C converter (www.visualstate.com) some
time ago and that have colored me. I find it strange that it isn't more widely
used. Perhaps it's a lack of tools for checking deadlocks etc?

------
baseonmars
1

