# Turing Machines

(チューリング機械)

## Language Theory and Compilers

http://www.sw.it.aoyama.ac.jp/2018/Compiler/lecture12.html

### Martin J. Dürst

© 2005-18 Martin J. Dürst 青山学院大学

# Today's Schedule

• Summary of last lecture
• Example solution for `bison` homework
• Turing Machines
• How a Turing Machine works
• Universal Turing Machine
• Computability is everywhere

# Summary of Previous Lecture

• Error processing in a parser has many requirements and difficulties
• In `bison`, errors can be caught with the `error` token
• The output of a parser includes the symbol table and an abstract parse tree as intermediate representations
• Semantic analysis is mostly concerned with type checking and type inference

# Homework: A Calculator for Dates and Time Periods

Order is important when making `YYSTYPE` a `struct` that can represent dates, numbers of days, and integers:

1. `typedef` ...
2. `#define YYSTYPE` ...
3. `#include "calc.tab.h"`

# Remaining Schedule

• July 6: Turing machines
• July 13: Code generation
• July 20: Optimization
• July 24 (Friday lectures on Tuesday): Executing environment: virtual machines, garbage collection,...
• July 27, 11:10-12:35: Term final exam

# Final Exam

• Past problems and example solutions (85'): 2017, 2016, 2015, 2014, 2013 (60'), 2012, 2011 (45'), 2010, 2009, 2008, 2007, 2006, 2005
• How to view example solutions (recent exams):
• Use buttons in upper left corner (blue area)
• Use 's' and 'p' keys
• How to view example solutions (older exams):
• Use Opera 12.17 (Windows/Mac/Linux)
• Select solutions style (e.g. View → Style → solutions)
• For Google Chrome, install Alt CSS
• Solutions are only examples; other solutions may be possible; sometimes, solutions are missing
• Best way to use:
• Simulate actual exam: Solve a full set of problems in 85'
• Check solutions
• Find weak areas and review content
• Repeat

# Formal Language Hierarchy

(Chomsky hierarchy)

 Grammar Type Language (family) Automaton phrase structure grammar (psg) 0 phrase structure language Turing machine context-sensitive grammar (csg) 1 context-sensitive language linear bounded automaton context-free grammar (cfg) 2 context-free language pushdown automaton regular grammar (rg) 3 regular language finite state automaton

# Automata Commonalities

• (finite set of) states
• (finite set of) transitions between states
• Start state
• Accepting state(s)
• Deterministic or nondeterministic

# Automata Differences

• Finite state automaton: No memory
• Pushdown automaton: Memory stack
• (Linear bounded automaton: Finite-length tape)
• Turing machine: Infinite length tape
(can be simulated with two stacks)

# How a Turing Machine Works

• 'Infinite' tape with symbols
• Special 'blank' symbol (␣ or _),
used outside actual work area
• Start at first non-blank symbol from the right
(or some other convenient position)
• Reads a tape symbol from present position
• Decides on symbol to write and next state
• Writes symbol at present position
• Moves to the right (R) or to the left (L)
• Changes to new state
• Start state and accept state(s)

# Turing Machine Example: State Transition Table

 Current state Current tape symbol New tape symbol Movement direction Shortcut Next state →1 0 1 R 0/1;R 2 →1 1 0 L 1/0;L 1 →1 _ 1 R _/1;R 2 2 0 0 R 0/0;R 2 2 1 1 R 1/1;R 2 2 _ _ L _/_;L 3*

# Explanations for Example

• Function: Adding 1 to a binary number
• Tape symbols: 0, 1 (+blank)
• Three states:
2. Move back to right
3. Accept

# Turing Machine Definition

6-tuple:

• Finite, non-empty set of states Q
• Finite, non-empty set of tape symbols Σ
• Transition function
• Blank symbol (∈Σ)
• Initial state (∈Q)
• Set of final states (⊂Q)

# Techniques and Tricks for Programming

• Interleave data fields and control fields
• Use special symbols as markers
• Use special states to move across tape to different locations
• Build up functionality from primitives (e.g. plus 1 → addition → multiplication → exponentiation)

# Extensions

• Nondeterminism
• Parallel tapes
• 2-dimensional tape
• Subroutines

It can be shown that all these extensions can be simulated on a plain Turing machine

# Universal Turing Machine

• It is possible to design a Turing machine that can simulate any Turing machine (even itself)
• Encode states (e.g. as binary or unary numbers)
• Encode tape symbols (e.g. as binary or unary numbers)
• Create different sections on tape for:
• Data (encoded tape symbols)
• State transition table (program)
• Internal state
• Main problems:
• Construction is tedious
• Execution is very slow

# Computability is Everywhere

It turns out that there are many other mechanisms that can simulate an (universal) Turing machine:

• Lambda calculus (everything is a function)
• Partial recursive functions
• SKI Combinator Calculus
• ι (iota) Calculus
• (Cyclic) tag systems
• Conway's game of life
• Wolfram's Rule 110 cellular automaton
• Wolfram's 2,3 Turing machine
(Turing machine with only 2 states and three symbols)

All these mechanisms can simulate each other and have the same power.

All these mechanisms are very simple, but simulation is very slow.

# Church-Turing Thesis

• Anything (any function on natural numbers) that can be calculated can be calculated by a Turing machine
• Anything that cannot be calculated by a Turing machine cannot be calculated

It is unclear whether this applies to Physics in general.

# Turing Completeness

• A mechanism (or programming language,...) is called Turing complete if it can be shown to have computing power equivalent to a Turing machine
• Future programming languages will not be substantially more powerful that current ones,
but they may be more convenient and/or faster

# Other Contributions

• Von Neumann style architecture: Current computer architecture closely follows Turing machine
(main difference: Random Acccess Memory)
• Entscheidungsproblem: There are some Mathematical facts that cannot be proven
• Computable numbers: There are real numbers that cannot be computed
• Halting problem: There is no general way to decide whether a program will terminate (halt) or not

# Bibliography

• The Annotated Turing, Charles Petzold, Wiley, 2008
• Understanding Computation, Tom Stuart, O'Reilly, 2013 (also available in Japanese)
• A New Kind of Science, Stephen Wolfram, Wolfram Media, 2002
• To Mock a Mockingbird, Raymond M. Smullyan, Oxford University Press, 2000

# Homework

Deadline: July 16, 2017 (Monday), 19:00

Where to submit: Box in front of room O-529 (building O, 5th floor)

Format: A4 single page (using both sides is okay; NO cover page, staple in top left corner if more than one page is necessary), easily readable handwriting (NO printouts), name (kanji and kana) and student number at the top right

For the Turing machine given by the following state transition table:

 Current state Current tape symbol New tape symbol Movement direction Next state →1 0 1 L 1 →1 1 0 R 2 →1 _ _ L 3* 2 0 0 R 2 2 1 1 R 2 2 _ _ L 3*
1. Draw the state transition diagram for this machine
2. Show in detail how this machine processes the input ..._1101000_...
3. Guess and explain what kind of calculation this machine does if the tape contains only a single contiguous sequence of '0'es and '1'es (surrounded by blanks) and where the leftmost non-blank symbol is a '1'

(this Turing machine always starts on the rightmost non-blank symbol)

# Glossary

commonalities

nondeterminism

universal turing machine

mechanism

lambda calculus
ラムダ計算
partial recursive function

unary numbers

Entscheidungsproblem
ヒルベルトの決定問題
Turing complete(ness)
チューリング完全 (性)
halting problem