C7 · Lesson 42 · State machine lab
Running, building, minimizing and comparing state machines, in Python
Lessons 37 to 41 drew state machines as diagrams and tables. A table is data, and data is what programs handle well. In this lab you'll store a machine as two lists, a transition table and an output table, then write the tools a designer leans on: a simulator, a generator that builds any sequence detector from its pattern, a minimizer that merges states that can't be told apart, a reachability check that finds dead states, and an equivalence checker that proves two machines behave identically on every input.
Play
- The circuit is the 101 detector of lesson 39, running on a stream of bits. Its output rises one clock after each completed 101, overlaps included.
- The lab's
detector('101')builds the same machine from the pattern alone. - Step 5's equivalence checker can prove that the hand-built machine and the generated one agree on every possible input.
Predict
A sequence detector for a 6-bit pattern is built as a Moore machine with one state per matched prefix. How many states does it have?
Seven: one for each prefix length from 0 to 6, the last one with output 1. The trick is in the transitions: on a mismatch the machine doesn't fall back to the start but to the longest prefix of the pattern that's still a suffix of what it has seen, which is what lets it find overlapping matches.
Name it
Machines as tables
States are numbered 0 to n − 1, inputs are bits, trans[s][b] is the next state and out[s] the Moore output. A list of lists is a complete description.
Building a detector
State i means "the last i bits match the first i bits of the pattern". On input b, the next state is the length of the longest prefix of the pattern that's a suffix of the first i bits followed by b. Checking candidate lengths from the longest down finds it; the full-match state has output 1 and continues the same way, so overlapping matches are found.
Minimizing
Two states are equivalent if no input sequence can tell them apart by the outputs. Partition refinement finds them: start with the states grouped by output; split any group whose members go, on some input, to different groups; repeat until nothing splits. Each final group becomes one state.
Reachability and equivalence
A state that can't be reached from the start is dead logic: a search from the start state finds the live ones. Two machines are equivalent if, run side by side on every input sequence, they always produce the same output. A search over pairs of states (one from each machine) starting from the two start states finds any reachable pair with different outputs, or proves there is none.
Worked example
Build the 101 detector's table from its pattern.
Show the solution
- State 0. On 1 it goes to 1 ("1" matched); on 0 it stays at 0.
- State 1. On 0 it goes to 2 ("10"); on 1 it stays at 1 (the new 1 is itself a prefix).
- State 2. On 1 it goes to 3 ("101", output 1); on 0 back to 0 ("100" ends in no prefix).
- State 3. On 0 it goes to 2 ("1010" ends in "10"); on 1 to 1.
- Write the table. The table [[0, 1], [2, 1], [0, 3], [2, 1]] with outputs [0, 0, 0, 1] is exactly lesson 39's machine.
Answer the next-state table [[0, 1], [2, 1], [0, 3], [2, 1]] with outputs [0, 0, 0, 1].
Worked example
Run the detector on 0101101. Where does it flag a match?
Show the solution
- Step through the bits. After each bit, the state and the output are:
| Bit | 0 | 1 | 0 | 1 | 1 | 0 | 1 |
|---|---|---|---|---|---|---|---|
| State | 0 | 1 | 2 | 3 | 1 | 2 | 3 |
| Output | 0 | 0 | 0 | 1 | 0 | 0 | 1 |
- Find the matches. The stream holds 101 at bits 2 to 4 and again at bits 5 to 7, and the detector flags both, at the fourth and seventh bits.
Answer at the fourth and seventh bits.
Worked example
A designer adds a fifth state that behaves exactly like state 2, and points state 1's 0-transition at it. Does the minimizer notice?
Show the solution
- Compare states 2 and 4. Both output 0, and both go to 0 on 0 and to 3 on 1.
- Minimize. Partition refinement never splits them, so the minimized machine has 4 states, the same as the original.
- Confirm. The equivalence checker confirms that the five-state and four-state machines produce identical outputs for every input.
- Why it matters. Redundant states like this are common in hand designs and cost flip-flops and logic for nothing.
Answer yes: states 2 and 4 merge, and the minimized machine has the original 4 states.
Check
Step 1. Write run_moore(trans, out, start, bits) returning the list of outputs after each input bit.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 2. Write detector(pattern) returning (trans, out) for a Moore machine that detects the bit string pattern (such as '101'), overlaps allowed.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 3. Write minimize(trans, out) returning the number of states after merging equivalent ones, by partition refinement.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 4. Write reachable(trans, start) returning the set of states reachable from start.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 5. Write equivalent(m1, m2) where each machine is (trans, out, start), returning True if the two produce the same output sequence for every input sequence.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
How many states does the generated Moore detector have for a 5-bit pattern?
Explain why
How can a finite search prove that two state machines agree on infinitely many input sequences?
Whatever input sequence the two machines receive, at each moment the pair is in some combination of one state from each. There are only finitely many such pairs, so a search that starts from the two start states and follows every input from every pair it reaches will visit every pair that any input sequence could ever produce. If none of those pairs has different outputs, no input sequence can make the machines disagree, because any disagreement would have to happen in one of those reachable pairs. The infinite set of sequences collapses onto a finite set of reachable state pairs.
Practice
Keep practicing
2 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.