Encoding Turing Machines (Code Words)

Context: FIT2014_MOC · turns a Turing machine into a string over — the step that makes a machine into data and so makes the Universal Turing Machine possible Problem it solves: “a program is just another input” — encode any TM as a word, decode any word back into a table.

Quick Revision

  • 🎯 Trigger: asked to encode/decode a TM, or to reason about the set of all TMs as a language ➔ go via the transition table, one row per instruction, each row fields.
  • ⚠️ Key Constraint: is the set of well-shaped code words, not the set of TM codes — encoding no machine. Well-formed meaningful.

🧩 Fixed assumptions

  • Input alphabet · tape alphabet (plus the blank ) · Start State · Accept State .
  • Why fixed ➔ the encoding has no field for “which state is the start” or “how big is the alphabet”; those are conventions, so only the transitions need coding.

📝 Step 1 — the machine as a table

One row per transition, columns From · To · Read · Write · Move.

stateDiagram-v2
    direction LR
    [*] --> s1
    s1: 1 Start
    s2: 2 Accept
    s1 --> s3: a→R
    s1 --> s3: b→R
    s3 --> s4: b→R
    s4 --> s2: ∆→R
FromToReadWriteMove
13
13
34
42

Validity conditions to check

  • A row with in the From column ➔ otherwise the machine can never leave the Start State.
  • No row with in the From column ➔ the Accept State is a halting state; nothing leaves it.
  • No two rows sharing the same From number and the same Read letter ➔ this is exactly determinism; a duplicate pair makes the “machine” ill-defined.

📝 Step 2 — the code

FieldItemCode
State number (unary, -terminated)
Letter / / / / / /
Direction / /
  • One instruction — two variable-length unary fields, then exactly 5 fixed letters.
  • Whole machine ➔ concatenate the instruction codes with no separators and no spaces. The terminators alone make it parseable.

Worked encoding — the machine

FromToReadWriteMoveCode
13abaaabaabbb
33aaabaaabaaaab
34aaabaaaabababb
44aaaabaaaabababb
45aaaabaaaaabaaaaa
56aaaaabaaaaaababaab
66aaaaaabaaaaaabaaaab
67aaaaaabaaaaaaabbabaa
78aaaaaaabaaaaaaaabaabaa
89aaaaaaaabaaaaaaaaabaabaa
99aaaaaaaaabaaaaaaaaabaaaaa
99aaaaaaaaabaaaaaaaaabababa
91aaaaaaaaababbbbbb
12abaabbabab
  • Row 1 dissected, , read , write , ab|aaab|aa|bb|b.
  • The machine’s code word ➔ all fourteen rows run together as one long string with no breaks.

📝 Step 3 — the Code-Word Language

  • CWL is regular ➔ it is defined by a regular expression: forces a state number , each terminates a unary field, is read/write/move.
  • Every TM code lies in CWL — but not every word of CWL encodes a TM: the shape is right while the content may violate the validity conditions (no From- row, a From- row, two rows with the same From and Read).

Quantifier practice (the negation chain, cf. Quantifiers (Existential and Universal))

📝 Step 4 — decoding

While there are unread letters:

  1. Read and count the next clump of s, then the ➔ the unary From state number.
  2. Read and count the next clump of s, then the ➔ the unary To state number.
  3. Read the next two letters ➔ the letter to be Read.
  4. Read the next two letters ➔ the letter to be Written.
  5. Read the next letter ➔ the direction.

Worked decode — abaaabaaaababaaabababbaaabaaaabababbaaaabaabbabab

ChunkFromToReadWriteMove
ab aaab aa aa b13
ab aaab ab ab b13
aaab aaaab ab ab b34
aaaab aab ba ba b42

Result: exactly the machine of Step 1 — it accepts , i.e. any first letter, then , then end of input.

✍️ Practice

⚠️ Common Mistakes

  • 💡 CWL {codes of TMs} ➔ the containment is proper; saying “CWL is the set of Turing machine encodings” throws away the whole point of the quantifier slide.
  • 💡 The state code is , the letter code is 2 letters ➔ mixing the unary state alphabet with the fixed-width letter alphabet is the standard decoding derailment. Count s only until the next .
  • 💡 State is impossible requires at least one , so state numbers start at ; a bare never opens a field.
  • 💡 No separators between instructions ➔ the concatenated code word has no delimiter; only the field lengths make it unambiguous — which is why the -letter tail is fixed-width.
  • 💡 Duplicate (From, Read) breaks determinism ➔ a table can be well-shaped and still fail; check that condition explicitly before claiming a word encodes a machine.

🧠 Active Recall