Turing machines for function computation

Computing Functions
with
Turing Machines
Costas Busch - LSU
1
A function
Domain:
has:
f (w)
Result Region:
D
f (w)
w D
Costas Busch - LSU
S
f ( w)  S
2
A function may have many parameters:
Example:
Addition function
f ( x, y )  x  y
Costas Busch - LSU
3
Integer Domain
Decimal:
5
Binary:
101
Unary:
11111
We prefer unary representation:
easier to manipulate with Turing machines
Costas Busch - LSU
4
Definition:
f
A function
is computable if
there is a Turing Machine M such that:
Initial configuration

w
Final configuration


qf
q0
initial state
For all
f (w) 
accept state
w D
Domain
Costas Busch - LSU
5
In other words:
f
A function
is computable if
there is a Turing Machine M such that:

q0 w  q f f ( w)
Initial
Configuration
For all
Final
Configuration
w D
Domain
Costas Busch - LSU
6
Example
The function
f ( x, y )  x  y is computable
x, y
are integers
Turing Machine:
Input string:
x0 y
unary
Output string:
xy0
unary
Costas Busch - LSU
7
x
Start
 1 1

y
1 0 1  1 
q0
initial state
The 0 is the delimiter that
separates the two numbers
Costas Busch - LSU
8
y
x
Start
 1 1
1 0 1  1 

q0 initial state
x y
Finish
 1 1

1 1 0 
q f final state
Costas Busch - LSU
9
The 0 here helps when we use
the result for other operations
x y
Finish
 1 1

1 1 0 
q f final state
Costas Busch - LSU
10
Turing machine for function
1  1, R
f ( x, y )  x  y
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Costas Busch - LSU
q4
11
Execution Example:
x  11 (=2)
y  11
Time 0
y
x
 1 1 0 1 1 
(=2)
q0
Final Result
x y
 1 1 1 1 0 
Costas Busch - LSU
q4
12
Time 0
 1 1 0 1 1 
q0
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Costas Busch - LSU
q4
13
Time 1
 1 1 0 1 1 
q0
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Costas Busch - LSU
q4
14
Time 2
 1 1 0 1 1 
q0
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Costas Busch - LSU
q4
15
Time 3
 1 1 1 1 1 
q1
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Costas Busch - LSU
q4
16
Time 4
 1 1 1 1 1 
q1
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Costas Busch - LSU
q4
17
Time 5
 1 1 1 1 1 
q1
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Costas Busch - LSU
q4
18
Time 6
 1 1 1 1 1 
q2
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Costas Busch - LSU
q4
19
Time 7
 1 1 1 1 0 
q3
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Costas Busch - LSU
q4
20
Time 8
 1 1 1 1 0 
q3
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Costas Busch - LSU
q4
21
Time 9
 1 1 1 1 0 
q3
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Costas Busch - LSU
q4
22
Time 10
 1 1 1 1 0 
q3
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Costas Busch - LSU
q4
23
Time 11
 1 1 1 1 0 
q3
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Costas Busch - LSU
q4
24
Time 12
 1 1 1 1 0 
q4
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
HALT & accept
Costas Busch - LSU
q4
25
Another Example
The function
f ( x)  2 x
x
is computable
is integer
Turing Machine:
Input string:
Output string:
x
unary
xx
unary
Costas Busch - LSU
26
x
Start
 1 1

1 
q0 initial state
2x
Finish
 1 1

1 1 1 
q f accept state
Costas Busch - LSU
27
Turing Machine Pseudocode for
f ( x)  2 x
• Replace every 1 with $
• Repeat:
• Find rightmost $, replace it with 1
• Go to right end, insert 1
Until no more $ remain
Costas Busch - LSU
28
Turing Machine for
1 $, R
f ( x)  2 x
1  1, L
1  1, R
q0   , L q1 $  1, R
  , R
q3
q2
  1, L
Costas Busch - LSU
29
Start
Example
 1 1 
Finish
 1 1 1 1 
q0
q3
1 $, R
1  1, L
1  1, R
q0   , L q1 $  1, R
  , R
q3
q2
  1, L
Costas Busch - LSU
30
Another Example
The function
f ( x, y ) 
is computable
Input:
Output:
1
if
x y
0
if
x y
x0 y
1
or
0
Costas Busch - LSU
31
Turing Machine Pseudocode:
• Repeat
Match a 1 from
Until all of
x with a 1 from y
x or y is matched
• If a 1 from x is not matched
erase tape, write 1
else
erase tape, write 0
Costas Busch - LSU
( x  y)
( x  y)
32
Combining Turing Machines
Costas Busch - LSU
33
Block Diagram
input
Turing
Machine
Costas Busch - LSU
output
34
Example:
x  y if x  y
f ( x, y) 
0
x, y
x, y
Comparator
if x  y
Adder
x y
Eraser
0
x y
x y
Costas Busch - LSU
35