C742 of 57
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

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
  1. State 0. On 1 it goes to 1 ("1" matched); on 0 it stays at 0.
  2. State 1. On 0 it goes to 2 ("10"); on 1 it stays at 1 (the new 1 is itself a prefix).
  3. State 2. On 1 it goes to 3 ("101", output 1); on 0 back to 0 ("100" ends in no prefix).
  4. State 3. On 0 it goes to 2 ("1010" ends in "10"); on 1 to 1.
  5. 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
  1. Step through the bits. After each bit, the state and the output are:
Bit0101101
State0123123
Output0001001
  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
  1. Compare states 2 and 4. Both output 0, and both go to 0 on 0 and to 3 on 1.
  2. Minimize. Partition refinement never splits them, so the minimized machine has 4 states, the same as the original.
  3. Confirm. The equivalence checker confirms that the five-state and four-state machines produce identical outputs for every input.
  4. 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?

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.