
State Machine in Real Life - ColinWright
https://www.solipsys.co.uk/new/StateMachineInRealLife.html
======
jgwil2
I feel I've read a number of pieces on programming that talk about the evils
of if statements (on which I more or less agree) and advocating their
replacement by state machines. However I've never seen a super simple
implementation of this that seems to obviously fulfill most branching use
cases. The closest is the state/strategy pattern, which is great but if
patterns (as it's sometimes said) reflect deficiencies in a language, what
would a language with first-class state machines look like?

~~~
monocasa
State machines are pretty simple in rust. Pseudo code to get the gist:

    
    
      state = match (state, event) {
        (State1, Event1) => {
           State2
        }
        (State1, Event2) => {
           ...
        }
        ...
      };

~~~
jgwil2
Does this end up replacing a large portion of if/else or switch statements?

~~~
heavenlyblue
This is basically a switch statement :)

~~~
jgwil2
If it evaluates top to bottom in an order-dependent fashion then I suppose so.
If it is order independent then it's more like what I want from a state
machine.

~~~
monocasa
What exactly are you looking for wrt to "order independent". Like, this will
compile down to a computed jump table if that's what you're getting at.

~~~
jgwil2
Insert new cases or change order without affecting behavior and statically
check that all cases are covered. Unlike a switch statement or if else block
where each condition is checked in order so adding more cases gets difficult.

~~~
monocasa
All Rust match statements as statically checked that all cases are covered by
the compiler; you can't really turn that of even with unsafe blocks.

How are you expecting that inserting or adding statements would change
behavior?

------
wallflower
While there are less of these now (possibly because of the feature described
here), if you find a store with an automatic door that swings out, standing on
the pad that is outside to block the door from moving is a good way to annoy
your friends who are behind you and teach them about simple state machines.

~~~
jgwil2
Another common one (at least once upon a time) is the coin-operated turnstile,
which is basically (ab)* where a is inserting a coin and b is rotating.

------
stevefan1999
Eat, work, sleep, repeat is already a state machine

------
loopz
State Machines, also called _runtime programs_.

I think when people call it "State Machines", it's for when they know the
slightest breeze will trip it up. It's so seductive, because your initial use
case gets resolved fairly quickly..

Ironically, the places they _have_ worked wonders, is actually game engines
from early 90's, where they have specific design, purposes, and limitations.

~~~
heavenlyblue
State machines don’t need to be runtime programs.

They can be expressed exactly using a static type system - that’s kind of
exact the proposition of them.

~~~
ves
The idris book opens with a section on dependently-typed state machines and
it’s magnificent.

~~~
jgwil2
What book is this?

~~~
rowanG077
Type driven development with idris

