Chapter 4 - Turing Machine

Updated 4 Oct 2026

"Dive into the origin of assembler language - This is how you control the computer"

Table of Contents

  1. Turing Machines (TM)
  2. Combining TMs
  3. Recursive languages
  4. Recursive Functions
  5. Recursively Enumerable Languages
  6. Extensions of Turing Machines

1. Turing Machines (TM)

Historical Context: The Colossus Computer

Real-life Connection: The Colossus was one of the first electronic digital programmable computers. Built during WWII at Bletchley Park, it was used for codebreaking. The image in the lecture shows operators working with this massive machine that used vacuum tubes and had magnetic tape for memory storage.

Components of a Turing Machine

A Turing Machine consists of:

  1. CPU (Finite Control)
    • State register that memorizes the current stage of the machine
    • Contains the program (instruction set) of the machine
    • Has states: q0,q1,q2,q3,hq_0, q_1, q_2, q_3, h (where hh is the halting state)
  2. Memory (Tape)
    • But back then we didn't have RAM!
    • We have magnetic tape instead
    • Infinite tape divided into cells
    • Each cell can contain a symbol from the alphabet
  3. Read/Write Head
    • Can move in both directions (left or right)
    • Can read the current symbol
    • Can write a new symbol


Analogy: Think of a Turing Machine like an old cassette tape player. The tape moves left and right, there's a read/write head that can detect and change what's on the tape, and there's a control unit that decides what to do based on what it reads.

Formal Definition

มาเริ่มกันละว่า How can we define those components mathematically

Definition: A Turing machine is a 5-tuple M=(K,Σ,δ,s,H)\boxed{M = (K, \Sigma, \delta, s, H)} where:

  • KK is a finite set of states
  • Σ\Sigma is an alphabet containing:
    • The blank symbol ⊔\sqcup (represents empty cells)
    • The left end symbol ▹\triangleright (marks the start of the tape)
    • But NOT containing the symbols →\rightarrow and ←\leftarrow (these are for movement)
  • s∈Ks \in K is the initial state (starting point)
  • H⊆KH \subseteq K is the set of halting states (when machine stops)
    • It gets out of an infinite loop when it reaches a halting state
  • δ\delta is the transition function (the program/instruction set):
    δ:(K−H)×Σ→K×(Σ∪{←,→})\boxed{\delta: (K - H) \times \Sigma \rightarrow K \times (\Sigma \cup \{\leftarrow, \rightarrow\})}
    With these constraints:
    • For all q∈K−Hq \in K - H, there exists pp such that δ(q,▹)=(p,→)\delta(q, \triangleright) = (p, \rightarrow)
      • Meaning: It never writes ▹\triangleright onto the memory. The tape start is always ▹\triangleright, and when you see it, you can only move right.
      • Write down อะไรก็ได้ แต่จะ Write ▹\triangleright หรอ ตลก55555
    • For all q∈K−Hq \in K - H and a∈Σ−{▹}a \in \Sigma - \{\triangleright\}, if δ(q,a)=(p,b)\delta(q, a) = (p, b) then b≠▹b \neq \triangleright
      • Meaning: Only one tape start exists, and it's always on the left

Exam Tip: Remember the mnemonic:

  • K = Kollection of states
  • Σ\Sigma = Symbols (alphabet)
  • δ\delta = Decisions (transitions)
  • s = start
  • H = Halt

Understanding the Transition Function

The transition function δ\delta takes:

  • Input: (current state, input symbol)
  • Output: (next state, action)

Where action can be:

  • A symbol to write (replacing current cell content)
  • ←\leftarrow: move pointer to the left (LHS)
  • →\rightarrow: move pointer to the right (RHS)

Important constraints:

  • Start of tape marker (▹\triangleright) is never written by the machine (นี่แหละ ๆ)
  • When seeing ▹\triangleright, the head must move right

Example 4.1.1: Simple Turing Machine

Consider the Turing machine M=(K,Σ,δ,s,{h})M = (K, \Sigma, \delta, s, \{h\}) where:

  • K={q0,q1,h}K = \{q_0, q_1, h\}
  • Σ={a,⊔,▹}\Sigma = \{a, \sqcup, \triangleright\}
  • s=q0s = q_0 (initial state)
  • hh is the halting state

Transition Function δ\delta (given by table):

State qqSymbol σ\sigmaδ(q,σ)\delta(q, \sigma)Meaning
q0q_0aa(q1,⊔)(q_1, \sqcup)Read aa, write blank, go to q1q_1
q0q_0⊔\sqcup(h,⊔)(h, \sqcup)Read blank, stay blank, halt
q0q_0▹\triangleright(q0,→)(q_0, \rightarrow)See start marker, move right
q1q_1aa(q0,a)(q_0, a)Read aa, keep aa, go to q0q_0
q1q_1⊔\sqcup(q0,→)(q_0, \rightarrow)Read blank, move right, go to q0q_0
q1q_1▹\triangleright(q1,→)(q_1, \rightarrow)See start marker, move right

Transition Diagram:

What this machine does:

  • Starts at q0q_0
  • Replaces every aa with blank (⊔\sqcup)
  • Effectively erases all aa's from the tape

Example Execution:

ใน Lecture PDF มันไม่มี Blank หลัง start แต่คุ้น ๆ หลัง ๆ อาจารย์บอกให้มี

Starting configuration: (q0,▹⊔aaaa)(q_0, \triangleright \sqcup aaaa)

(q0,▹⊔‾aaaa)⊢M(q0,▹⊔a‾aaa)[▹/→]⊢M(q1,▹⊔⊔‾aaa)[a/⊔]⊢M(q0,▹⊔⊔a‾aa)[⊔/→]⊢M(q1,▹⊔⊔⊔‾aa)[a/⊔]⊢M(q0,▹⊔⊔⊔a‾a)[⊔/→]⊢M(q1,▹⊔⊔⊔⊔‾a)[a/⊔]⊢M(q0,▹⊔⊔⊔⊔a‾)[⊔/→]⊢M(q1,▹⊔⊔⊔⊔⊔‾)[a/⊔]⊢M(q0,▹⊔⊔⊔⊔⊔⊔‾)[⊔/→]⊢M(h,▹⊔⊔⊔⊔⊔⊔‾)[⊔/⊔] HALT\begin{align*} (q_0, \triangleright \underline{\sqcup} aaaa) &\vdash_M (q_0, \triangleright \sqcup \underline{a} aaa) && [\triangleright/\rightarrow] \\ &\vdash_M (q_1, \triangleright \sqcup \underline{\sqcup} aaa) && [a/\sqcup] \\ &\vdash_M (q_0, \triangleright \sqcup \sqcup \underline{a} aa) && [\sqcup/\rightarrow] \\ &\vdash_M (q_1, \triangleright \sqcup \sqcup \underline{\sqcup} aa) && [a/\sqcup] \\ &\vdash_M (q_0, \triangleright \sqcup \sqcup \sqcup \underline{a} a) && [\sqcup/\rightarrow] \\ &\vdash_M (q_1, \triangleright \sqcup \sqcup \sqcup \underline{\sqcup} a) && [a/\sqcup] \\ &\vdash_M (q_0, \triangleright \sqcup \sqcup \sqcup \sqcup \underline{a}) && [\sqcup/\rightarrow] \\ &\vdash_M (q_1, \triangleright \sqcup \sqcup \sqcup \sqcup \underline{\sqcup}) && [a/\sqcup] \\ &\vdash_M (q_0, \triangleright \sqcup \sqcup \sqcup \sqcup \sqcup \underline{\sqcup}) && [\sqcup/\rightarrow] \\ &\vdash_M (h, \triangleright \sqcup \sqcup \sqcup \sqcup \sqcup \underline{\sqcup}) && [\sqcup/\sqcup] \text{ HALT} \end{align*}

Insight: This is essentially a "clear all a's" program - like find and replace in a text editor, but at the machine level!

Example 4.1.2: Even Simpler Machine

Consider M=(K,Σ,δ,s,H)M = (K, \Sigma, \delta, s, H) where:

  • K={q0,h}K = \{q_0, h\}
  • Σ={a,⊔,▹}\Sigma = \{a, \sqcup, \triangleright\}
  • s=q0s = q_0
  • H={h}H = \{h\}

Transition Table:

State qqSymbol σ\sigmaδ(q,σ)\delta(q, \sigma)
q0q_0aa(q0,←)(q_0, \leftarrow)
q0q_0⊔\sqcup(h,⊔)(h, \sqcup)
q0q_0▹\triangleright(q0,→)(q_0, \rightarrow)

Transition Diagram:

What this machine does:

  • Moves left when seeing aa
  • Halts when seeing blank
  • This machine scans left until it finds a blank

Example 4.1.3: Recognizing Even-Length Strings

A Turing machine for accepting L={ω∈{a,b}∗∣∣ω∣ is even}L = \{\omega \in \{a,b\}^* \mid |\omega| \text{ is even}\}

  • K={s,q0,q1,y,n}K = \{s, q_0, q_1, y, n\} where H={y,n}H = \{y, n\}
    • yy = accept state
    • nn = reject state
  • Σ={a,b,⊔,▹}\Sigma = \{a, b, \sqcup, \triangleright\}
  • Transition function δ\delta:
qqσ\sigmaδ(q,σ)\delta(q, \sigma)
ss⊔\sqcup(q0,→)(q_0, \rightarrow)
q0q_0⊔\sqcup(y,⊔)(y, \sqcup)← even count ends in q0q_0
q0q_0aa(q1,→)(q_1, \rightarrow)
q0q_0bb(q1,→)(q_1, \rightarrow)
q1q_1⊔\sqcup(n,⊔)(n, \sqcup)← odd count ends in q1q_1
q1q_1aa(q0,→)(q_0, \rightarrow)
q1q_1bb(q0,→)(q_0, \rightarrow)

Transition Diagram:

         a,b→
        ┌────┐
        ↓    │
  ▷─○──→○────○
    s   q₀   q₁
   ⊔/→  │    │
        ↓⊔/⊔ ↓⊔/⊔
        ◎y   ◎n

Accepting computation for aaaa:

Initial config: (s, ▷⊔aa) ≠ (s, ▷⊔⊔ω)
                              ↑
                           cursor on input string

(s, ▷⊔aa) ⊢ₘ (q₀, ⊔aa) ⊢ₘ 
(q₁, ⊔aa) ⊢ₘ (q₀, ⊔aa⊔) ⊢ₘ 
(y, ⊔aa⊔)  STOP/HALT

Rejecting computation for aa:

(s, ▷⊔a) ⊢ₘ (q₀, ⊔a) ⊢ₘ
(q₁, ⊔a⊔) ⊢ₘ (n, ⊔a⊔)  STOP/HALT

Exam Strategy: For simple state machine problems, always alternate between two states to count parity (even/odd). State q0q_0 = even, state q1q_1 = odd.

Real-life Connection: This is essentially TM ≡ Modern Computer. The Turing Machine is a theoretical model that captures what computers can do!


Configurations

Definition: Configuration

มันก็คือการเขียน การอ่าน ของ Turing machine นั่นแหละมั้ง

A configuration of a Turing machine M=(K,Σ,δ,s,H)M = (K, \Sigma, \delta, s, H) is a member of:

K×▹Σ∗×(Σ∗(Σ−{⊔})∪{e})\boxed{K \times \triangleright \Sigma^* \times (\Sigma^* (\Sigma - \{\sqcup\}) \cup \{e\})}

Breaking this down:

  • Current state: q∈Kq \in K
  • Before the header: ▹ω1\triangleright \omega_1 (everything to the left including start marker)
  • After the header: ω2\omega_2 (everything to the right)
    • If it consists of only ⊔\sqcup's or empty string, we can ignore it

Notation: Configuration is written as (q,ω1a‾ω2)(q, \omega_1 \underline{a} \omega_2) where:

  • qq = current state
  • ▹ω1\triangleright \omega_1 = tape content before head
  • a‾\underline{a} = symbol under the head (current cell)
  • ω2\omega_2 = tape content after head

Syntactic Sugar:

  • (q1,▹⊔abac⊔)=(q1,▹⊔ab‾ac)(q_1, \triangleright \sqcup abac\sqcup) = (q_1, \triangleright \sqcup a\underline{b}ac) (CHECK)
  • The underline shows where the head is positioned

Computation Steps (Yields Relation)

Definition: Let (q1,ω1a1u1)(q_1, \omega_1 a_1 u_1) and (q2,ω2a2u2)(q_2, \omega_2 a_2 u_2) be configurations of MM. Then:
(q1,ω1a1u1)⊢M(q2,ω2a2u2)\boxed{(q_1, \omega_1 a_1 u_1) \vdash_M (q_2, \omega_2 a_2 u_2)}
if and only if for some b∈Σ∪{←,→}b \in \Sigma \cup \{\leftarrow, \rightarrow\}, δ(q1,a1)=(q2,b)\delta(q_1, a_1) = (q_2, b) and one of the following cases holds:

Case 1: Write a symbol (b∈Σb \in \Sigma)

  • b∈Σ,ω1=ω2,u1=u2b \in \Sigma, \omega_1 = \omega_2, u_1 = u_2 and a2=ba_2 = b
  • Meaning: Replace the cell's content with symbol bb, stay in place

Example:
(q2,▹⊔ab‾ac⊔)⊢M(q2,▹⊔a⊔‾ac⊔)if δ(q2,b)=(q2,⊔)(q_2, \triangleright \sqcup a\underline{b}ac\sqcup) \vdash_M (q_2, \triangleright \sqcup a\underline{\sqcup}ac\sqcup) \quad \text{if } \delta(q_2, b) = (q_2, \sqcup)

Case 2a: Move left (b=←b = \leftarrow, normal case)

  • b=←,ω1=ω2a2b = \leftarrow, \omega_1 = \omega_2 a_2, and either:
    • u2=a1u1u_2 = a_1 u_1, if a1≠⊔a_1 \neq \sqcup or u1≠eu_1 \neq e, or
    • u2=eu_2 = e, if a1=⊔a_1 = \sqcup and u1=eu_1 = e

Example:

(q1,▹⊔ab‾c⊔)⊢←(q2,▹⊔a‾bc⊔)(q_1, \triangleright \sqcup a\underline{b}c\sqcup) \vdash_\leftarrow (q_2, \triangleright \sqcup \underline{a}bc\sqcup)

Case 2b: Move left (edge case with blanks)

  • u2=eu_2 = e, if a1=⊔a_1 = \sqcup and u1=eu_1 = e
  • Empty strings - we can ignore them

Example:

(q1,▹⊔‾a⊔)⊢←(q2,▹⊔‾a⊔)(q_1, \triangleright \underline{\sqcup}a\sqcup) \vdash_\leftarrow (q_2, \triangleright \underline{\sqcup}a\sqcup)

Case 3a: Move right (b=→b = \rightarrow, normal case)

  • b=→,ω2=ω1a1b = \rightarrow, \omega_2 = \omega_1 a_1, and either:
    • u1=a2u2u_1 = a_2 u_2

Example:

(q1,▹⊔ab‾c⊔)⊢→(q2,▹⊔abc‾⊔)(q_1, \triangleright \sqcup a\underline{b}c\sqcup) \vdash_\rightarrow (q_2, \triangleright \sqcup ab\underline{c}\sqcup)

Case 3b: Move right (edge case)

  • u1=u2=eu_1 = u_2 = e and a2=⊔a_2 = \sqcup
(q1,▹⊔abc⊔‾)⊢→(q2,▹⊔abc⊔⊔‾)(q_1, \triangleright \sqcup abc\underline{\sqcup}) \vdash_\rightarrow (q_2, \triangleright \sqcup abc\sqcup\underline{\sqcup})

Intuition: Think of configurations as "snapshots" of the machine at any point in time. The ⊢M\vdash_M symbol means "yields" or "steps to" - it shows how one snapshot transitions to the next.

Halted Configuration and Computation

แล้วหยุด กูจะหยุดมึงเอง!

Definition: Halted Configuration

A halted configuration is of the form:
(h,ω1a‾ω2) with h∈H\boxed{(h, \omega_1 \underline{a} \omega_2) \text{ with } h \in H}

  • The machine returns a result when it reaches a halted configuration
  • Result is whatever is on the tape at that moment

Definition: Computation

A computation is a sequence of configurations C1,C2,…,CnC_1, C_2, \ldots, C_n such that:
C1⊢MC2⊢M⋯⊢MCnC_1 \vdash_M C_2 \vdash_M \cdots \vdash_M C_n
We say that:

  • The computation has length nn
  • C1C_1 yields CnC_n

Definition: Halting

MM is said to halt on input ω\omega iff (s,▹⊔ω)(s, \triangleright \sqcup \omega) yields some halted configuration.
(s,▹⊔ω)⊢M∗(h,▹ω′)\boxed{(s, \triangleright \sqcup \omega) \vdash_M^* (h, \triangleright \omega')}
Where:

  • (s,▹⊔ω)(s, \triangleright \sqcup \omega) = initial configuration
  • (h,▹ω′)(h, \triangleright \omega') = final configuration (may not need to be the same)
  • ω′\omega' = the result
  • ⊢M∗\vdash_M^* means "yields in zero or more steps"

Important Note: The mem ctn (memory content) needs not be the same before and after computation.

Exam Tip: When tracing a computation:

  1. Start with initial config: (s,▹⊔ω)(s, \triangleright \sqcup \omega)
  2. Apply transitions step by step
  3. Stop when you reach a halting state
  4. Check if the final configuration is what you expect

2. Combining TMs

Symbol Writing Machines

  • For each symbol a∈Σ∪{←,→}−{▹}a \in \Sigma \cup \{\leftarrow, \rightarrow\} - \{\triangleright\}, MaM_a is a Turing machine which writes the symbol aa into the scanned tape square.
  • Formal definition: Ma=({s,h},Σ,δ,s,{h})M_a = (\{s, h\}, \Sigma, \delta, s, \{h\}) where δ(s,b)=(h,a)\delta(s, b) = (h, a) for each b∈Σ−{▹}b \in \Sigma - \{\triangleright\}.

Mnemonics (Short Notations)

To keep things simple, we write:

  • aa for MaM_a → write 'aa' to the current cell
  • LL for M←M_\leftarrow → move pointer to LHS (Left)
  • RR for M→M_\rightarrow → move pointer to RHS (Right)

Special composite machines:

  • L⊔L_\sqcup: Finds the first blank square to the left of the currently scanned symbol
(p,▹⊔abc⊔‾⊔efg)⊢L⊔(p,▹⊔‾abc⊔⊔efg)(p, \triangleright \sqcup abc\underline{\sqcup}\sqcup efg) \vdash_{L_\sqcup} (p, \triangleright \underline{\sqcup} abc{\sqcup}\sqcup efg)
- Scan left until blank found
  • R⊔R_\sqcup: Finds the first blank square to the right of the currently scanned symbol
(p,▹⊔a‾bcd⊔efg⊔hij)⊢R⊔(p,▹⊔abcd⊔‾efg⊔hij)(p, \triangleright \sqcup \underline{a}bcd\sqcup efg\sqcup hij) \vdash_{R_\sqcup} (p, \triangleright \sqcup abcd\underline{\sqcup}efg\sqcup hij)
- Scan right until blank found

Atomic vs Composite Machines

TypeExamplesDescription
Atomicaa, LL, RRWrite symbol, move left, move right
CompositeL⊔L_\sqcup, R⊔R_\sqcup, CC, S→S_\rightarrowBuilt from atomic machines
  • If you have two Turing machines, you can connect them together!

Example 4.1.5: Sequential Composition

Machine: RRRR or R2R^2

Diagram:

What it does:

  1. Start: move right
  2. If cell is aa, bb, ⊔\sqcup, or ▹\triangleright: move right again

Shorthand notation:

  • Unlabeled arrow = works for all symbols in alphabet
  • Can omit arrow entirely by juxtaposing: R→RR \rightarrow R becomes just RRRR

Note: "Program" states are hidden - each node is a sequence of commands, each edge is a peek to the current cell.

Example trace:

▹⊔‾⊔⊔b⊢R▹⊔⊔‾⊔b(move right once)⊢R▹⊔⊔⊔‾b(move right again)\begin{align*} \triangleright \underline{\sqcup} \sqcup \sqcup b &\vdash_R \triangleright \sqcup \underline{\sqcup} \sqcup b && \text{(move right once)} \\ &\vdash_R \triangleright \sqcup \sqcup \underline{\sqcup} b && \text{(move right again)} \end{align*}

Example 4.1.6: Conditional Movement (R⊔R_\sqcup)

Machine: R⊔R_\sqcup - moves right until blank found

Diagram:

Alternative notation:

Where a≠⊔a \neq \sqcup is read as "any symbol aa other than ⊔\sqcup"

What it does:

  • Scan right
  • If current cell is NOT blank: keep moving right
  • If current cell IS blank: halt

Advantage of a≠⊔a \neq \sqcup notation: The variable aa can be used elsewhere in the diagram

Example 4.1.6 continuation: Copy and Move

▹→R⊔→a≠⊔L→a\triangleright \rightarrow R_\sqcup \xrightarrow{a \neq \sqcup} L \rightarrow a
  • ตรง R⊔R_\sqcup มี Self-loop ถ้าเป็น ⊔\sqcup
    What this does:
  1. Scan right until blank found (R⊔R_\sqcup)
  2. If found a non-blank symbol aa (store it as variable)
  3. Move left (LL)
  4. Write that symbol aa at the new position

Example 4.1.7: Find Marked or Unmarked Squares

Four useful machines for finding specific squares:

(a) R⊔R_\sqcup: Find first blank square to the right

         ⊔
     ┌────────┐
     ↓        │
  ▷→ R

(b) L⊔L_\sqcup: Find first blank square to the left

         ⊔
     ┌────────┐
     ↓        │
  ←─ L

(c) R⊔‾R_{\overline{\sqcup}}: Find first non-blank square to the right

         ⊔
     ┌────────┐
     ↓        │
  ▷→ R

(d) L⊔‾L_{\overline{\sqcup}}: Find first non-blank square to the left

         ⊔
     ┌────────┐
     ↓        │
  ←─ L

Summary Table:

MachineDescriptionScan DirectionLooking For
R⊔R_\sqcupR-blankRightFirst blank
L⊔L_\sqcupL-blankLeftFirst blank
R⊔‾R_{\overline{\sqcup}}R-nonblankRightFirst non-blank
L⊔‾L_{\overline{\sqcup}}L-nonblankLeftFirst non-blank

Example 4.1.8: The Copying Machine (CC) (CHECK!)

Purpose: Copy input string - transforms ⊔ω⊔‾\sqcup \omega \underline{\sqcup} into ⊔ω⊔ω⊔‾\sqcup \omega \sqcup \omega \underline{\sqcup}

Starting condition:

  • Input: String ω\omega containing only non-blank symbols (possibly empty)
  • Tape: ω\omega on an otherwise blank tape with one blank square to its left
  • Head: Positioned on the blank square to the left of ω\omega

Result: Eventually stops with ω⊔ω\omega \sqcup \omega on an otherwise blank tape

Diagram:

\usepackage{tikz, amsmath, amssymb}
\usetikzlibrary{arrows.meta, positioning}
 
\begin{document}
 
\begin{tikzpicture}[node distance=3cm, >=Stealth, thick]
 
% Define nodes
\node (Lblank) {$>L_{\sqcup}$};
\node (R) [right=of Lblank] {$R$};
\node (Rblank) [below=of R] {$R_{\sqcup}$};
\node (copy) [right=4cm of R] {$\sqcup R_{\sqcup}^2 a L_{\sqcup}^2 a$};
 
% Arrows
\draw[->] (Lblank) -- (R);
\draw[->] (R) -- node[above] {$a \neq \sqcup$} (copy);
\draw[->] (R) -- node[left] {$\sqcup$} (Rblank);
 
% Up-straight-down loop (Π-shape)
\draw[->] (copy.north) |- ++(0,1.8) -| (R.north);
 
\end{tikzpicture}
 
\end{document}

Full Trace

⊔ab⊔‾⊢L⊔ab‾⊔⊢⊔⊔a⊔‾⊔⊢R⊔∗⊔a⊔⊔‾⊢b⊔a⊔b‾⊢L⊔∗⊔a⊔‾b⊢b⊔abb⊢L????(Loop back)\begin{aligned} \sqcup ab\underline{\sqcup}&\vdash_L\sqcup a\underline{b}\sqcup \\ &\vdash_\sqcup \sqcup a \underline{\sqcup}\sqcup \\ &\vdash_{R_\sqcup^*} \sqcup a \sqcup\underline{\sqcup} \\ &\vdash_b \sqcup a \sqcup\underline{b} \\ &\vdash_{L_\sqcup^*}\sqcup a \underline{\sqcup}b \\ &\vdash_b \sqcup a bb \\ &\vdash _L ???? \quad \quad\text{(Loop back)} \end{aligned}

How it works (Copy works backwards):

Starting tape: ▹⊔abc\triangleright \sqcup abc

  1. L⊔L_\sqcup: Move left to find first blank
    • ▹⊔abc\triangleright \sqcup abc → head at first blank
  2. Read symbol (if a≠⊔a \neq \sqcup): Store current symbol
    • Found symbol cc, store it as variable aa
  3. Replace with blank: Write ⊔\sqcup to mark as "copied"
    • ▹⊔ab⊔\triangleright \sqcup ab\sqcup
  4. R⊔R_\sqcup: Move right until blank found
    • ▹⊔ab⊔\triangleright \sqcup ab\sqcup → head at rightmost blank
  5. Write symbol aa: Write the stored symbol
    • ▹⊔ab⊔c\triangleright \sqcup ab\sqcup c
  6. L⊔L_\sqcup: Move left to find blank (the marker)
    • Back to the marked position
  7. Check if done: If current symbol is ⊔\sqcup, we're at start → halt
    • Otherwise, continue from step 2 with next symbol

Complete trace for input abcabc: (Check ด้วย ทำไมมัน clear หมด)
ถ้าว่างก็ทำหน่อยว้อย

Final result: ▷⊔abc⊔cba (reversed copy)

Insight: This machine copies string but in reverse order! It's like "lives program" - cursor enter me exit.

Example 4.1.9: The Right-Shifting Machine (S→S_\rightarrow) (CHECK)

Purpose: Transforms ⊔ω⊔‾\sqcup \omega \underline{\sqcup} into ⊔⊔ω⊔‾\sqcup \sqcup \omega \underline{\sqcup} (shifts string one position right)

Diagram:

         a≠⊔
     ┌────────┐
     ↓        │
  ▷→ L  ────⊔──→ ⊔ → R_⊔ → a → L_{\overline{⊔}}
     ↑                              │
     └──────────────────────────────┘

How it works:

Starting tape: ▷⊔abc

  1. Move to rightmost symbol
  2. Remember it (call it aa)
  3. Replace with blank (shift it right)
  4. Move left to previous non-blank
  5. Remember that symbol
  6. Write the previous symbol (aa) there
  7. Repeat until all shifted

Example trace:

▷⊔abc  ⊢ [to end]      ▷⊔abc   
       ⊢ [a=c]         ▷⊔ab⊔   (store c, shift)
       ⊢ [R_⊔, write]  ▷⊔ab⊔c  (write at next blank)
       ⊢ [back]        ▷⊔ab⊔c
       ⊢ [a=b]         ▷⊔a⊔⊔c  (store b, shift)
       ⊢ [write]       ▷⊔a⊔bc  
       ⊢ [a=a]         ▷⊔⊔⊔bc  (store a, shift)
       ⊢ [write]       ▷⊔⊔abc  (DONE!)

Result: ▷⊔⊔abc (shifted right by one position)

Analogy: Like pushing text to the right by inserting a space at the beginning.

Example 4.1.10: Erasing Machine (CHECK)

Purpose: Erase all aa's from the tape (from Example 4.1.1)

Simple diagram:

\usepackage{tikz, amsmath, amssymb}
\usetikzlibrary{arrows.meta, positioning}
 
\begin{document}
 
\begin{tikzpicture}[node distance=2cm, >=Stealth, thick]
 
% Nodes
\node (Rstart) {$>R$};
\node (blank) [right=of Rstart] {$\sqcup$};
 
% Main arrow (R -> blank)
\draw[->] (Rstart) -- node[below] {$a$} (blank);
 
% Sharp up–across–down arrow (blank -> R)
\draw[->] (blank.north) |- ++(0,1) -| (Rstart.north);
 
\end{tikzpicture}
 
\end{document}

What it does:

  1. If current cell is aa: replace with blank, stay in state
  2. If current cell is blank: halt
  3. Otherwise: move right

This is the "clear the tape" operation - removes all occurrences of symbol aa.

▹⊔‾aac⊔⊔⊢M∗▹⊔⊔⊔c‾\triangleright\underline{\sqcup} aac \sqcup \sqcup \vdash_M^*\triangleright\sqcup\sqcup\sqcup\underline{c}

Rules for Combining Machines (Formal Definition)

Suppose we have three Turing machines:

  • M1=(K1,Σ,δ1,s1,H1)M_1 = (K_1, \Sigma, \delta_1, s_1, H_1)
  • M2=(K2,Σ,δ2,s2,H2)M_2 = (K_2, \Sigma, \delta_2, s_2, H_2)
  • M3=(K3,Σ,δ3,s3,H3)M_3 = (K_3, \Sigma, \delta_3, s_3, H_3)

Combined Machine Structure

  ▷ M₁ ──a──→ M₂
       │
       b
       ↓
      M₃

How it works:

  1. Let M1M_1 work until it halts
  2. Peek the current cell
  3. If the cell contains aa, let M2M_2 work
  4. If the cell contains bb, let M3M_3 work

Formal Construction

The combined machine M=(K,Σ,δ,s,H)M = (K, \Sigma, \delta, s, H) where:

  • K=K1∪K2∪K3K = K_1 \cup K_2 \cup K_3 (union of all states)
  • s=s1s = s_1 (start with first machine's start state)
  • H=H2∪H3H = H_2 \cup H_3 (halt when any component halts)

For each σ∈Σ\sigma \in \Sigma, q∈K−Hq \in K - H, δ(q,σ)\delta(q, \sigma) is defined as follows:

  1. If q∈K1−H1q \in K_1 - H_1 (non-halting state of M1M_1):
    • δ(q,σ)=δ1(q,σ)\delta(q, \sigma) = \delta_1(q, \sigma)
    • Use M1M_1's transitions
  2. If q∈K2−H2q \in K_2 - H_2 (non-halting state of M2M_2):
    • δ(q,σ)=δ2(q,σ)\delta(q, \sigma) = \delta_2(q, \sigma)
    • Use M2M_2's transitions
  3. If q∈K3−H3q \in K_3 - H_3 (non-halting state of M3M_3):
    • δ(q,σ)=δ3(q,σ)\delta(q, \sigma) = \delta_3(q, \sigma)
    • Use M3M_3's transitions
  4. If q∈H1q \in H_1 (halting state of M1M_1):
    • δ(q,σ)=(s2,σ)\delta(q, \sigma) = (s_2, \sigma) if σ=a\sigma = a (jump to M2M_2)
    • δ(q,σ)=(s3,σ)\delta(q, \sigma) = (s_3, \sigma) if σ=b\sigma = b (jump to M3M_3)
    • δ(q,σ)∈H\delta(q, \sigma) \in H otherwise (halt if no condition matches)

Programming Analogy: This is like an if-clause in programming:

def combined_machine(tape):
    M1.run(tape)  # Run M1 until it halts
    symbol = tape.read_current()
    if symbol == 'a':
        M2.run(tape)  # JMP to M2
    elif symbol == 'b':
        M3.run(tape)  # JMP to M3
    else:
        halt()  # Stop if no condition matches

Assembly Language Equivalent:

  • JMP (unconditional jump)
  • JZ (jump if zero)
  • JNZ (jump if not zero)
  • These are the "branching" or "flowchart" operations

Quick Reference: Composite Machine Notations

NotationDescription
LLMove left
RRMove right
L⊔L_\sqcupScan left for previous blank
R⊔R_\sqcupScan right for next blank
L⊔‾L_{\overline{\sqcup}}Scan left for previous non-blank
R⊔‾R_{\overline{\sqcup}}Scan right for next non-blank
S←S_\leftarrowShift the tape to left
S→S_\rightarrowShift the tape to right
CCCopy the tape's content

Diagram notation:

  • Initial Configuration: ▹⊔ω\triangleright \sqcup \omega
  • Condition/Branch: Decision points (if-then)
  • Self-loop: Repeating state


3. Recursive Languages

3.1 Definition: Accepting and Rejecting Configurations

Context: Recursive languages can be defined as a recursive function (recursive algorithm)

Addition as recursion example:
f(a,b) = f(a+1, b-1) \text{ if } b > 0$$$$f(a,0) = a

Definition: Given is a Turing machine M=(K,Σ,δ,s,H)M = (K, \Sigma, \delta, s, H) with H={y,n}\boxed{H = \{y, n\}}.

  • Any halting configuration whose state is y\color{green}y is said to be an accepting configuration (y,_)(y, \_)
  • A halting configuration whose state is n\color{red}n is said to be a rejecting configuration (n,_)(n, \_)

An initial configuration is of the form (s,▹⊔ω)(s, \triangleright \sqcup \omega)

For an input ω∈(Σ−{⊔,▹})∗\omega \in (\Sigma - \{\sqcup, \triangleright\})^*, we say that:

  • MM accepts ω\omega if (s,▹⊔ω)(s, \triangleright \sqcup \omega) yields an accepting configuration
    ⊢M∗(y,_)\vdash_M^* (y, \_)
  • MM rejects ω\omega if (s,▹⊔ω)(s, \triangleright \sqcup \omega) yields a rejecting configuration
    ⊢M∗(n,_)\vdash_M^* (n, \_)

Important Note:

  • ถ้าจาก Initial config ถ้าไปถึง y,ny,n ได้ก็คือ Accepted
  • แต่ถ้าไม่ถึงทั้งคู่ y,ny,n เช่นอาจจะติด Loop ก็คือจะไม่ได้คำตอบ
  • If MM gets stuck in an ∞\infty-loop, we won't get answer

Definition: Deciding a Language (Recursive Languages)

Definition: Let Σ0⊆Σ−{⊔,▹}\Sigma_0 \subseteq \Sigma - \{\sqcup, \triangleright\} be an alphabet, called input alphabet. We say that MM decides a language L⊆Σ0∗L \subseteq \Sigma_0^* if for any string ω∈Σ0∗\omega \in \Sigma_0^*:

\begin{align*} &\text{If } \omega \in L \text{ then } M \text{ accepts } \omega \\ &\text{If } \omega \notin L \text{ (i.e. } \omega \in \Sigma_0^* - L\text{) then } M \text{ rejects } \omega \end{align*} }$$ A language $L$ is called **recursive** if it is decided by some Turing machine. > **Key Concept - Deciding**: > - **$M$ has to HALT** on all inputs in $\Sigma_0^*$ > - $M$ can tell if it accepts/rejects an input string > - คือถ้าได้คำตอบไม่ว่าจะเป็น $y,n$ คือมัน decided > - **ถ้าติดใน loop ก็คือ undecided** **Important Note**: If TM $M$ decides a language $L \subseteq \Sigma_0^*$, then $M$ halts on any $\omega \in \Sigma_0^*$, but $M$ is **not required** to halt on $\omega \in \Sigma^* - \Sigma_0^*$ (see supplementary note for further discussion) > **Exam Insight**: > - Aj. Hung จะถามงี้ Show that $a^nb^n$ is a recursive language > - (Actual Question) แปลว่าให้ design a turing machine for $a^nb^n$ ### Example: $L_0 = \{a^n b^n \mid n \geq 0\}$ is Recursive >ถึงนี่! **Example**: Show that $L_0 = \{a^n b^n \mid n \geq 0\}$ is recursive. **Transition Diagram**: ![[Pasted image 20251105120100.png|center|500]] **Strategy**: 1. Start from the left 2. Find an $a$, replace it with $d$ (mark as processed) 3. Find a $b$, replace it with $d$ (mark as processed) 4. Return to left using $L_\sqcup$ 5. Repeat until all symbols are replaced with $d$ 6. If pattern doesn't match $a^*b^*$, reject ($n$) 7. If all replaced successfully, accept ($y$) **State Diagram Explanation**:

\begin{align*}
&\triangleright R \xrightarrow{a} dR \xrightarrow{b} dL_\sqcup \
&\text{where:} \
&\quad R: \text{Move right (self-loop on } d\text{)} \
&\quad dR: \text{Replace } a \text{ with } d\text{, then move right (self-loop on } a,d\text{)} \
&\quad dL_\sqcup: \text{Replace } b \text{ with } d\text{, then scan left to blank}
\end{align*}

**Rejection conditions**: - If see $\sqcup$ while in first state $R$ → accept (empty string or all matched) - If see $b$ or $c$ while looking for $a$ → reject - If see $c$ or $\sqcup$ while looking for $b$ → reject - If see $a$ or $\sqcup$ while looking for $c$ → reject **Sample run for $\omega = aabb$**: Initial config: $(s, \triangleright \underline{\sqcup} aabb)$ ผิด แก้ด้วย

\begin{align*}
&\checkmark \triangleright \sqcup \underline{a}abb \vdash_R && \text{[start, move to first symbol]} \
&\checkmark \triangleright \sqcup \underline{d}abb \vdash_d && \text{[replace } a \text{ with } d\text{]} \
&\checkmark \triangleright \sqcup d\underline{a}bb \vdash_R && \text{[move right]} \
&\checkmark \triangleright \sqcup da\underline{b}b \vdash_R && \text{[skip over remaining } a\text{]} \
&\checkmark \triangleright \sqcup da\underline{d}b \vdash_d && \text{[replace first } b \text{ with } d\text{]} \
&\checkmark \triangleright \sqcup \underline{d}adb \vdash_{L_\sqcup} && \text{[return to left blank]} \
&\checkmark \triangleright \sqcup d\underline{a}db \vdash_R && \text{[move right, find next } a\text{]} \
&\checkmark \triangleright \sqcup d\underline{d}db \vdash_R && \text{[skip } d\text{]} \
&\checkmark \triangleright \sqcup dd\underline{d}b \vdash_R && \text{[skip } d\text{]} \
&\checkmark \triangleright \sqcup ddd\underline{b} \vdash_R && \text{[find } b\text{]} \
&\checkmark \triangleright \sqcup ddd\underline{d} \vdash_d && \text{[replace } b \text{ with } d\text{]} \
&\checkmark \triangleright \underline{\sqcup}dddd \vdash_{L_\sqcup} && \text{[return to left]} \
&\checkmark \triangleright \sqcup \underline{d}ddd \vdash_R && \text{[try to find } a\text{]} \
&\checkmark \triangleright \sqcup dddd\underline{\sqcup} \vdash_{R_\sqcup}^* && \text{[all } d\text{, no more } a\text{]} \
&\checkmark \triangleright \sqcup dddd\underline{\sqcup} \vdash_y && \text{[ACCEPT!]} \
&\triangleright \sqcup dddd\sqcup
\end{align*}

**Detailed trace with annotations**: ![[Pasted image 20251105120200.png|center|600]] > **Exam Strategy**: > - ต้อง cancel ตัวที่ pair กัน (ในอัตราส่วนที่เท่ากัน) $(a:b) = (n:n)$ > - Don't stop until reaching the halting states > - เวลา trace ให้เขียน configuration แต่ละ step > - Mark processed symbols with $d$ to avoid reprocessing **Alternative trace format**: Starting: $\triangleright \sqcup aabb$ | Step | Configuration | Action | |------|---------------|--------| | 0 | $\triangleright \underline{\sqcup}aabb$ | Initial | | 1 | $\triangleright \sqcup \underline{a}abb$ | $R$: move right | | 2 | $\triangleright \sqcup \underline{d}abb$ | Replace $a$ with $d$ | | 3 | $\triangleright \sqcup d\underline{a}bb$ | $R$: move right | | 4 | $\triangleright \sqcup da\underline{b}b$ | $R$: move right | | 5 | $\triangleright \sqcup da\underline{d}b$ | Replace $b$ with $d$ | | 6 | $\triangleright \underline{\sqcup}dadb$ | $L_\sqcup$: back to start | | ... | ... | Continue matching | | n | $\triangleright \sqcup dddd\underline{\sqcup}$ | Accept: $y$ | > **Real-world Analogy**: This is like checking if opening and closing parentheses match in code - you need to ensure each opening bracket has a corresponding closing bracket. --- ### 3.4 Example: $L_1 = \{a^n b^n c^n \mid n \geq 0\}$ is Recursive - เคยมีวใน #FinalExam ปีนึง - มีโปรแกรมมารันให้หน่อย - Design Machine บวกเลขง่าย ๆ $n+3=n+2+1$ **Example**: Show that $L_1 = \{a^n b^n c^n \mid n \geq 0\}$ is recursive. This language has previously evaded FSA, PDA recognizers - but TM can decide it! **Transition Diagram**: ![[Pasted image 20251105120300.png|center|500]]

\begin{align*}
&\text{From Figure 4-11:} \
&\triangleright R \xrightarrow{a} dR \xrightarrow{b} dR \xrightarrow{c} dL_\sqcup \
&\text{Self-loops: } d \text{ on each state}
\end{align*}

**Strategy**: 1. Start from left, find first $a$ 2. Replace $a$ with $d$, continue right to find $b$ 3. Replace $b$ with $d$, continue right to find $c$ 4. Replace $c$ with $d$, return to left with $L_\sqcup$ 5. Repeat: each stage replaces one $a$, one $b$, and one $c$ 6. Accept if all replaced (reach blank while looking for $a$) 7. Reject if pattern broken or mismatch **Reject conditions**: - $\sqcup$ at first $R$ → accept (empty string) - $b,c$ while looking for $a$ → reject - $c, \sqcup$ while looking for $b$ → reject - $a, \sqcup$ while looking for $c$ → reject **Sample run for $\omega = aabbcc$**: Initial: $\triangleright \underline{\sqcup} aabbcc$

\begin{align*}
\triangleright \sqcup \underline{a}abbcc &\vdash_R \triangleright \sqcup \underline{d}abbcc && [R: \text{find } a] \
\triangleright \sqcup d\underline{a}bbcc &\vdash_R \triangleright \sqcup da\underline{b}bcc && [dR: \text{skip } a] \
\triangleright \sqcup da\underline{d}bcc &\vdash_d \triangleright \sqcup dad\underline{b}cc && [dR: \text{find } b] \
\triangleright \sqcup dadb\underline{c}c &\vdash_R \triangleright \sqcup dadb\underline{d}c && [dR: \text{find } c] \
\triangleright \underline{\sqcup}dadbd &\vdash_{L_\sqcup} && [dL_\sqcup: \text{back to start}] \
\triangleright \sqcup \underline{d}adbdc &\vdash_R && [R: \text{skip } d] \
\triangleright \sqcup d\underline{a}dbdc &\vdash_R && [R: \text{find next } a] \
\triangleright \sqcup d\underline{d}dbdc &\vdash_d && [\text{replace } a \text{ with } d] \
\triangleright \sqcup dd\underline{d}bdc &\vdash_R && [dR: \text{skip } d] \
\triangleright \sqcup ddd\underline{b}dc &\vdash_R && [dR: \text{find } b] \
\triangleright \sqcup ddd\underline{d}dc &\vdash_d && [\text{replace } b \text{ with } d] \
\triangleright \sqcup dddd\underline{d}c &\vdash_R && [dR: \text{skip } d] \
\triangleright \sqcup ddddd\underline{c} &\vdash_R && [dR: \text{find } c] \
\triangleright \sqcup ddddd\underline{d} &\vdash_d && [\text{replace } c \text{ with } d] \
\triangleright \underline{\sqcup}dddddd &\vdash_{L_\sqcup} && [dL_\sqcup: \text{back to start}] \
\triangleright \sqcup \underline{d}ddddd &\vdash_R && [R: \text{try find } a] \
\triangleright \sqcup dddddd\underline{\sqcup} &\vdash_{R_\sqcup}^* && [\text{all } d\text{, no } a \text{ left}] \
\triangleright \sqcup dddddd\underline{\sqcup} &\vdash_y && \text{YES - ACCEPT}
\end{align*}

**Detailed computation trace**: ![[Pasted image 20251105120400.png|center|700]] **Visual trace for $aabbcc$**: ``` Stage 1: aabbcc → dabbcc → dadbcc → dadbdc → back to start Stage 2: dadbdc → dddbdc → dddddc → dddddd → back to start Check: dddddd → all d, no more a → ACCEPT ``` > **Exam Question (This appeared in last year's final)**: > > Show that $a^n b^m c^n d^m$ is recursive > > **Hint ๆ อย่าอย่าโกนไป**: > - Replace $d$ with $z$ > - For each decision point, write the remaining symbols in the $n$-transition > - Similar strategy: match $a$ with $c$, and $b$ with $d$ **Another diagram representation**: ![[Pasted image 20251105120500.png|center|400]]

\begin{align*}
&R^z \xrightarrow{a} zR \xrightarrow{b} zR \xrightarrow{c} zR \xrightarrow{d} dL_\sqcup \
&\text{With self-loops on: } a,z; \quad b,z; \quad c,z
\end{align*}

> **Exam Strategy**: > - อันนี้ยากกว่า $a^nb^nc^n$ เพราะต้อง track 2 pairs > - $n$ = count of $(a,c)$ pair > - $m$ = count of $(b,d)$ pair > - Each iteration: cancel one $a$ with one $c$, and one $b$ with one $d$ --- ### 3.5 Example: Any Regular Language is Recursive **Theorem**: Any regular language is recursive. **Proof**: Let $M = (K, \Sigma, \delta, s, F)$ be a DFA. We can build a TM: $$M' = (K \cup \{y, n\}, \Sigma', \delta', s', \{y, n\})$$ that decides $L(M)$ as follows: - $\Sigma' = \Sigma \cup \{\triangleright, \sqcup\}$ - $\delta': K \times \Sigma' \rightarrow (K \cup \{y,n\}) \times (\Sigma' \cup \{\leftarrow, \rightarrow\})$ where: - $\delta'(s', \sqcup) = (s, \rightarrow)$ — skip initial blank - $\delta'(p, a) = (\delta(p, a), \rightarrow)$ for $p \in K, a \in \Sigma$ — **scan through + make state transition** - $\delta'(p, \sqcup) = (y, \sqcup)$ for $p \in F$ — accept if in final state - $\delta'(p, \sqcup) = (n, \sqcup)$ for $p \notin F$ — reject if not in final state **Diagram**: ![[Pasted image 20251105120600.png|center|300]]

\begin{align*}
&\text{DFA: } p \xrightarrow{a} q \
&\text{TM: } \delta'(p,a) = (\delta(p,a), \rightarrow) = (q, \rightarrow)
\end{align*}

**Key insight**: The TM simulates the DFA by: 1. Reading input left to right (move right after each symbol) 2. Making the same state transitions as the DFA 3. When reaching end (blank), checking if in accepting state 4. Going to $y$ if accept, $n$ if reject > **Important**: **IF you have FSA, you can convert it into TM** > > This proves that: **Regular Languages ⊂ Recursive Languages** --- # 4. Recursive Functions ### 4.1 Definition: Recursive Functions > **Modern computing**: We treat math functions as TM... tape manipulation > > Operations: $+, -, \times, \div, \sqrt{}, \cup, \ldots$ **Definition**: Let $M = (K, \Sigma, \delta, s, \{h\})$, $\Sigma_0 \subseteq (\Sigma - \{\sqcup, \triangleright\})$ and $\omega \in \Sigma_0^*$. If $$(s, \triangleright \sqcup \omega) \vdash_M^* (h, \triangleright \sqcup u)$$ then $u$ is called the **output** of $M$ on input $\omega$ and denoted by $M(\omega)$. $$\boxed{f(\omega_1, \omega_2, \ldots, \omega_k) \xrightarrow{\text{TM}} M(\omega_1, \omega_2, \ldots, \omega_k)}$$ We say that: 1. **$M$ computes function** $f: \Sigma_0^* \rightarrow \Sigma_0^*$ if for any $\omega \in \Sigma_0^*$, $M(\omega) = f(\omega)$ 2. **$M$ computes function** $f: \mathbb{N}^k \rightarrow \mathbb{N}$ $(k \geq 1)$ if for all binary numbers $\omega_1, \ldots, \omega_k$: $$M(\omega_1; \ldots; \omega_k) = f(\omega_1, \ldots, \omega_k)$$ where $f(\omega_1, \ldots, \omega_k)$ is also in **binary representation** **A function $f$ is recursive if there is a Turing machine computing $f$.** > **Key Point**: In TM, integers are **"convert to be binary string"** not decimal! --- ### 4.2 Example: Successor and Doubling Functions **Example**: Show that $\text{succ}(n) = n + 1$ and $d(n) = 2n$ are recursive functions. #### 4.2.1 Successor Function: $\text{succ}(n) = n + 1$ **Strategy**: Add 1 to a binary number - If last digit is $0$: change to $1$ - If last digit is $1$: change to $0$ and carry **Examples**: - $\text{succ}(101_2) = 110_2$ (5 → 6) - $\text{succ}(111_2) = 1000_2$ (7 → 8) **Transition Diagram** (Figure 4-12): ![[Pasted image 20251105120700.png|center|400]]

\begin{align*}
&\triangleright R_\sqcup \begin{cases}
0 \rightarrow 1 \
1 \rightarrow 0
\end{cases} \text{ (go to last digit)} \
&\text{then: } \begin{cases}
0 \rightarrow 1 \text{ and halt} \
1 \rightarrow 0 \text{ and } L \text{ (carry)} \
\sqcup \rightarrow 1S_\rightarrow L_\sqcup \text{ (overflow: add digit)}
\end{cases}
\end{align*}

**Revised diagram**: ![[Pasted image 20251105120701.png|center|400]] **Sample traces**: **Case 1**: $\text{succ}(100_2) = 101_2$

\begin{align*}
\triangleright \underline{\sqcup}100 &\vdash_{R_\sqcup} \triangleright \sqcup 100\underline{\sqcup} && [\text{move to last digit}] \
\triangleright \sqcup 100\underline{\sqcup} &\vdash_L \triangleright \sqcup 10\underline{0} && [\text{last digit is } 0] \
\triangleright \sqcup 10\underline{1} &\vdash && [\text{change } 0 \text{ to } 1, \text{ halt}]
\end{align*}

**Case 2**: $\text{succ}(101_2) = 110_2$

\begin{align*}
\triangleright \underline{\sqcup}101 &\vdash_{R_\sqcup} \triangleright \sqcup 101\underline{\sqcup} && [\text{go to end}] \
\triangleright \sqcup 101\underline{\sqcup} &\vdash_L \triangleright \sqcup 10\underline{1} && [\text{last digit}] \
\triangleright \sqcup 10\underline{0} &\vdash_L \triangleright \sqcup 1\underline{0}0 && [\text{change } 1 \text{ to } 0\text{, carry left}] \
\triangleright \sqcup 1\underline{1}0 &\vdash && [\text{change } 0 \text{ to } 1\text{, halt}]
\end{align*}

**Case 3**: $\text{succ}(111_2) = 1000_2$ (overflow case)

\begin{align*}
\triangleright \underline{\sqcup}111 &\vdash_{R_\sqcup} \triangleright \sqcup 111\underline{\sqcup} \
\triangleright \sqcup 111\underline{\sqcup} &\vdash_L \triangleright \sqcup 11\underline{1} && [\text{last digit is } 1] \
\triangleright \sqcup 11\underline{0} &\vdash_L \triangleright \sqcup 1\underline{1}0 && [\text{change to } 0\text{, carry}] \
\triangleright \sqcup 1\underline{0}0 &\vdash_L \triangleright \sqcup \underline{1}00 && [\text{change to } 0\text{, carry}] \
\triangleright \sqcup \underline{0}00 &\vdash_L \triangleright \underline{\sqcup}000 && [\text{change to } 0\text{, carry}] \
\triangleright \underline{\sqcup}1000 &\vdash_{1S_\rightarrow} && [\text{overflow: prepend } 1] \
\triangleright \sqcup \underline{1}000 &\vdash_{L_\sqcup} && [\text{shift right and add } 1]
\end{align*}

> **Exam tip**: > - อันนี้เพราะว่ามี 2 สิ่งที่สำคัญ: $R_\sqcup$, $1L_\sqcup$ > - Integer ที่ส่งเข้า TM ต้อง "convert to be binary string" **Detailed trace for 101**: ![[Pasted image 20251105120800.png|center|500]] --- #### 4.2.2 Doubling Function: $d(n) = 2n$ **Strategy**: In binary, multiplying by 2 = shift left = append $0$ to the right **Examples**: - $d(101_2) = 1010_2$ (5 × 2 = 10) - $d(100_2) = 1000_2$ (4 × 2 = 8) **Two approaches**: **Approach 1** (Direct): Use the shift-right machine $S_\rightarrow$ then append $0$ $$\triangleright R_\sqcup \rightarrow 0 \rightarrow L \rightarrow 1L_\sqcup$$ **Approach 2** (Compositional): Think of it as: $f(n) = 2n + 2$ Breaking it down: - $= 2n + 1 + 1$ (that may sound alright) $\rightarrow 2n + 5$? - $= 2(n+1) + 1$ (this is better!) $\rightarrow 2n + 4 + 1$? - $= 2(n+1)$ (best!) - $= 2(n+2) + 1$ **Therefore**: $\text{succ} \rightarrow \text{double}$ $$\boxed{\text{Add 2} \rightarrow \text{Double} \rightarrow \text{Succ}}$$ Combining machines: ![[Pasted image 20251105120900.png|center|400]] $$M_1 \xrightarrow{\text{output}} M_2 \xrightarrow{\text{output}} \text{result}$$ > **Real-world analogy**: Like a factory assembly line - each machine does one operation, output from one becomes input to the next. --- # Summary & Key Concepts ### Recursive Languages vs Recursively Enumerable | Property | Recursive | Recursively Enumerable | |----------|-----------|------------------------| | **Always halts?** | ✅ Yes | ❌ No (may loop forever) | | **Decides membership?** | ✅ Yes (always $y$ or $n$) | ❌ No (may not halt) | | **Practical?** | ✅ Yes | ⚠️ Limited | ### Important Theorems 1. **Every regular language is recursive** - FSA → TM conversion 2. **Context-free languages** (not all are recursive) - Some CFLs are recursive, some are not 3. **$\{a^nb^nc^n\}$ is recursive** (but not context-free) - Shows TM is more powerful than PDA ### Hierarchy of Languages

\begin{align*}
&\text{Regular} \subset \text{Context-Free} \subset \text{Recursive} \subset \text{Recursively Enumerable} \
&\text{(FSA)} \qquad \text{(PDA)} \qquad\qquad \text{(TM always halts)} \quad \text{(TM may not halt)}
\end{align*}

--- ## Exam Tips & Strategies ### For Recursive Language Proofs 1. **Design the TM systematically**: - Identify what needs to be matched/counted - Use markers ($d$, $z$, etc.) to track processed symbols - Always return to start position with $L_\sqcup$ 2. **Common patterns**: - **Matching**: $a^nb^n$ → cancel pairs - **Multiple matching**: $a^nb^nc^n$ → cancel triplets - **Complex matching**: $a^nb^mc^nd^m$ → two separate pair counts 3. **Trace format**: - Start with initial configuration - Show each step with clear action - Mark current position with underline - End with accept/reject state ### For Recursive Function Proofs 1. **Binary representation**: - All numbers are in binary - Think bit-wise operations 2. **Decompose complex functions**: - Break into simpler operations - Combine existing machines (succ, double, etc.) - Example: $2n+2 = 2(n+1) = \text{succ} \circ \text{double}$ 3. **Common building blocks**: - $\text{succ}(n) = n+1$ - $\text{pred}(n) = n-1$ (if $n>0$) - $\text{double}(n) = 2n$ - $\text{half}(n) = \lfloor n/2 \rfloor$ ### Time Management - **Simple language** ($a^nb^n$): ~10-15 minutes - **Complex language** ($a^nb^nc^n$): ~15-20 minutes - **Function design**: ~15 minutes - **Leave time for trace**: Always show at least one complete example --- ## Practice Problems 1. **Design TM for**: $L = \{a^nb^{2n} \mid n \geq 0\}$ - Hint: For each $a$, need to find two $b$'s 2. **Design TM for**: $L = \{ww^R \mid w \in \{a,b\}^*\}$ (palindromes) - Hint: Match first with last, second with second-last, etc. 3. **Show recursive**: $f(n) = n \bmod 2$ (parity check) - Hint: Check last bit of binary representation 4. **Show recursive**: $f(n,m) = n + m$ (addition) - Hint: Use repeated successor --- *End of Recursive Languages & Functions* > "Decidability is about whether we can always get an answer. Recursive languages guarantee we'll always know yes or no - no infinite waiting!" - Theory of Computation wisdom ## Key Takeaways ### Turing Machine = Modern Computer The Turing Machine model captures the essence of computation: - **CPU** → Finite control (states and transitions) - **RAM** → Infinite tape (memory) - **Program** → Transition function (instruction set) ### Building Complex from Simple Just like assembly language, we: 1. Start with **atomic operations** ($L$, $R$, write symbols) 2. Build **composite operations** ($L_\sqcup$, $R_\sqcup$, $C$, $S_\rightarrow$) 3. Combine machines using **conditionals** (branching on tape symbols) ### Important Concepts to Remember 1. **Configuration**: Snapshot of machine state + tape + head position 2. **Computation**: Sequence of configurations (step-by-step execution) 3. **Halting**: When machine reaches a halting state 4. **Combining**: Connect machines like flowchart nodes --- ## Exam Tips Summary 1. **For definitions**: Remember the 5-tuple $(K, \Sigma, \delta, s, H)$ 2. **For tracing computations**: - Start with $(s, \triangleright \sqcup \omega)$ - Apply $\delta$ step by step - Use the notation $(q_1, \text{tape}) \vdash_M (q_2, \text{tape})$ - Stop at halting state 3. **For designing TMs**: - Break problem into smaller tasks - Use standard building blocks ($L_\sqcup$, $R_\sqcup$, etc.) - Draw state diagram first - Then write formal transition table 4. **Common patterns**: - **Even/odd counting**: Alternate between two states - **Finding blanks**: Use $L_\sqcup$ or $R_\sqcup$ - **Copying**: Use markers and back-and-forth scanning - **Shifting**: Store symbol, erase, move, write 5. **Don't forget**: - $\triangleright$ is never written, only read - When seeing $\triangleright$, must move right - Blank squares at the end can be omitted in configuration notation - Each "state node" in a flowchart is actually a sequence of basic commands