Skip to content

Latest commit

 

History

History
87 lines (62 loc) · 2.48 KB

modular-state-machines.md

File metadata and controls

87 lines (62 loc) · 2.48 KB
status
Still in research (please don't share)

Modular state machines

Motivation

  • One big state machine can become difficult to understand

  • How can we break a state machine into parts such that when combined they form the whole?

Products of states

  • The Cartesian product of states can be used when we need to keep track of two states running in parallel.

  • There are different two different ways we can advance two state machines that run in parallel, either by stepping one of them and leaving the other alone or stepping both of them in lockstep.

  • Angelic product, allows of stepping one of the state machines:

type SM s i o = i -> s -> (s, o)

angelic : SM s i o -> SM t j p -> SM (s, t) (i + j) (o + p)
angelic f g ij (s, t) case ij of
  Left i  -> let (s', o) = f i s in ((s', t), Left o)
  Right j -> let (t', p) = g j t in ((s, t'), Right p)

video : SM {playing, stopped} {stop, play} ()
audio : SM {mutued, unmuted} {mute, unmute} ()

player = angelic video audio
  • Tensor product, allows us to step both state machines in lockstep:
tensor : SM s i o -> SM t j p -> SM (s, t) (i, j) (o, p)
tensor f g (i, j) (s, t) =
  let
    (o, s') = f i s
    (p, t') = g j t
  in
    ((s', t'), (o, p))

See also:

The state pattern

Hierarchical states

Stack of states / pushdown automaton

See also