1.
Theory of Computation
Review of Program States & Snapshots,
Successor Snapshots, Computation,
Partially Computable & Computable Functions
Vladimir Kulyukin
www.vkedco.blogspot.com
2.
Outline
●
●
●
●
Review of Program States & Snapshots
Successor Snapshots
Computation
Partially Computable & Computable Functions
4.
Instructions & Programs
• An instruction is either a labeled or
unlabeled statement
• A program is a finite sequence of
instructions
• A program with no instructions is called the
empty program
5.
Definition: A State of Program P
• A state of a program P is a set of equations of the form V = m,
where V is a variable and m is a number
• The set must have exactly one equation for every variable that
occurs in P
• Corollary 1: The state may have equations for variables that do
not occur in P (this is very useful for passing meta information)
• Corollary 2: It is irrelevant whether a state can or cannot be
achieved by P
6.
Example
[ A1] IF X 1! 0 GOTO B1
Y Y 1
Z1 Z1 1
IF Z1! 0 GOTO E1
[ B1] X 1 X 1 1
Y Y 1
Z1 Z1 1
IF Z1! 0 GOTO C1
[C1] IF X 1! 0 GOTO B1
Z1 Z1 1
IF Z1! 0 GOTO E1
7.
Program State Example
• The program on the previous slide (in this context, it does not
really matter what it does in this context of program states)
contains variables X1, Z1, and Y
• { X1 = 2, Z1 = 3, Y = 5 } is a legal state
• { X1 = 2, X2 = 4, Z1 = 3, Y = 5 } is also a legal state,
although it contains a variable that does not occur in the program;
however, it does satisfy the definition
• { X1 = 2, Y = 5 } is not a state, because it does not define the
value of Z1
• { X1 = 2, Z1 = 3, Y = 5, X1 = 5 } is not a state, because it
has two equations for X1
8.
Formal Definition of Program
A Program is a Finite Sequence of Primitive L Instructions
1. Primitive L Instruction
2. Primitive L Instruction
3. Primitive L Instruction
4. Primitive L Instruction
5. Primitive L Instruction
…
n. Primitive L Instruction
9.
Program Snapshots
• A program P is assumed to have n ≥ 0 instructions
• A program snapshot is a pair (i ,σ) where i is the number
of the instruction to be executed and σ is a state of P
• The values of i are in [1, n+1]
10.
Initial Snapshot
Let P be a program in L and let
r1 ,..., rm be m given numbers. Let
consist of two sets of equations :
1. X 1 r1 ,..., X m rm , Y 0;
2. V 0, for any other V in P.
Then
(1, ) is the initial snapshot.
11.
Terminal Snapshot
A snapshot (i, ) is terminal if i n 1
and is a legal program state
13.
Successor Snapshots
●
●
●
Suppose there is a program P whose current state is
captured in a snapshot S
If we want to answer the question of what will happen
next in P, we have to be able to compute the next
instantaneous description, i.e., the successor snapshot
of the current snapshot
If we want to proceed in a deterministic fashion, there
must be one and only one successor snapshot
14.
Key Successor Snapshot Assumption
●
●
Let us assume that every program P, for which we are
interested in computing successor snapshots (i.e.,
emulating), is compiled down to four types of primitive
instructions, i.e., all of the macros have been expanded
appropriately
Recall that the four primitive instructions are:
1) V ← V + 1;
2) V ← V – 1;
3) V ← V;
4) IF V != 0 GOTO L
15.
Successor Snapshots
●
The computation of the successor shot depends
on two things:
the current snapshot
the
●
instruction that is about to be executed
We will define the successor snapshot function
with four cases (four primitive instruction types)
16.
Successor Snapshot
successor((i, )) ( j , ), where
i, is the current snapshot;
( j , ) is the successor snapshot
17.
Successor Snapshot: Case 1: Increment
The i instruction of P is V V 1
and contains the equation V m.
Then j i 1 and is obtained from
by replacing the equation V m
by V m 1.
th
18.
Successor Snapshot: Case 2: Decrement
The i instruction of P is V V 1
th
and contains the equation V m.
Then j i 1 and is obtained from
by replacing the equation V m
by V m 1 if m 0; if m 0, .
19.
Successor Snapshot: Case 3: NO-OP
The i instruction of P is V V .
th
Then and j i 1.
20.
Successor Snapshot: Case 4: Conditional Dispatch
th
The i instruction of P is
IF V 0 GOTO L
1. contains the equation V 0. Then j i 1.
2. contains the equation V m, where m 0.
Then, if there is an instruction of P labeled L,
j is the least number such that the jth instruction
of P is labeled L. Otherwise, j n 1.
21.
Example
1. [A]
2.
3.
4. [B]
5.
6.
7.
IF X != 0 GOTO B
Z←Z+1
IF Z != 0 GOTO E
X←X–1
Y←Y+1
Z←Z+1
IF Z != 0 GOTO A
Suppose the current snapshot is (i, σ), where σ = {X = 4, Y = 0, Z = 0}.
If i = 1, the successor snapshot is (4, σ).
If i = 2, the successor snapshot is (3,σ'), where
σ' = {X = 4, Y = 0, Z = 1}.
23.
Formal Definition of Computation
A computation of a program P is
a sequence of snapshots
s1 , s2 ,..., sk of P such that
si 1 is the successor of si ,
i 1,2,..., k 1 and sk is terminal.
24.
Remarks on Computation
●
●
●
Given a program P and a sequence of snapshots S, it is
possible to determine whether S constitutes a
computation of P on a given input
Computation is finite by definition
The finiteness requirement may draw some
disagreement, because one can think of meaningful
programs that deliberately go into infinite loops (e.g.,
robot control loops of the type sense – act – sense)
25.
What does P compute?
•
•
•
Suppose there is a program P
Suppose that we give some inputs for P
If we start running or simulating P on the inputs, there
are two cases:
1. There is a computation, i.e., a sequence of snapshots s1 , s2 ,
… , sk with sk being a terminal snapshot
2. There is no computation, i.e., the sequence of snapshots is
infinite
26.
What does P compute?
P is said to compute the value of Y at terminal snapshots.
(m)
Ψ P (r1,r2 ,...,rm ) denotes the value of Y at the terminal
snapshot sk and the initial snapshot s1 contains
the equations X 1 r1, X 2 r2 , ... X m rm .
27.
Example 1: What does P compute?
[A1]
[B1]
IF X1 != 0 GOTO B1
GOTO E1
X1 ← X1 – 1
Y ← Y+1
GOTO A1
28.
Example 1: What does P compute?
( x) x
(1)
P
29.
Example 2: What does P compute?
[A1]
[B1]
Y ← X1
Z1 ← X2
IF Z1 != 0 GOTO B1
GOTO E1
Z1 ← Z1 – 1
Y ← Y+1
GOTO A1
30.
Example 2: What Does P Compute?
Ψ P x1 , x2 x1 x2
2
31.
Number of Program Inputs
Let P be a program of n variables.
What is
(m)
P
(r1 ,..., rm ) if m n ?
Case 1 : m n.
Case 2 : m n.
32.
Case 1: m < n
●
●
●
If the remaining inputs are not specified in the input, all of
them default to 0
Example: If a program computes a function of 5
variables and only X1, X2, and X3 are specified in the
initial snapshot, the values of X4 and X5 are assumed
to be 0
In practical terms, X4 = 0 and X5 = 0 are added to the
initial snapshot
33.
Case 2: m > n
●
●
If more inputs are specified than necessary, the
extra inputs are ignored
Example: If a program computes a function of 2
variables but X1, X2, X3, and X4 have
equations in the initial snapshot, the values for X3
and X4 are ignored
34.
Example 3
Suppose P is a program that assigns
r1 to Y . In other words,
(1)
P
( x) x.
Then the following statements are true :
1 .
(1)
P
2 .
( 2)
P
( r1 , r2 ) r1
3 .
( 3)
P
(r1 , r2 , r3 ) r1
( r1 ) r1
35.
Example 4
Suppose that P is a program such that
(
P2 ) ( x, y ) x y.
Then the following statements are true :
(1)
P
(r1 ) r1 0.
(
P3) (r1 , r2 , r3 ) r1 r2 .
37.
Example 1
1 if n 0
n
f ( n)
i if n 0
i 1
38.
Example 1: C Program fact.c
long fact(long n)
{
if ( n == 0 )
return 1;
else
return n * fact(n-1);
}
39.
Example 1: Comments on fact.c
●
●
●
We can say that f(n) is computed by the C program in
fact.c
We can also say that the C program in fact.c computes
f(n)
The two statements are semantically equivalent:
programs compute functions
functions are computed by programs
40.
Definition: Partially Computable Functions
A partial function g is said to be partially computable if it
is computed by some L program P.
41.
Definition: Partially Computable Functions
g is a partially computable function iff :
1. P r1 , r2 ,..., rm g r1 , r2 ,..., rm
(m)
P
where P is an L program
2. g r1 , r2 ,..., rm iff
(m)
P
r1 , r2 ,..., rm
r1 , r2 ,..., rm ,
42.
Example 2
x1 x2 if x1 x2
g x1 , x2
if x1 x2
43.
Example 2
Y ← X1
Z ← X2
[A] IF Z != 0 GOTO B
GOTO E
[B] IF Y != 0 GOTO C
GOTO B
[C] Y ← Y – 1
Z←Z–1
GOTO A
44.
Example 2
●
●
g is a partially computable function because there exists
a L program, defined in Example 2, that computes it
For any two natural numbers x and y,
g(x, y) is the same as the value of Y and the program terminates;
g(x, y) is undefined and the program does not terminate,
in which case the value of Y is undefined as well
45.
Definition: Computable Functions
●
●
●
●
g(r1 , r2 , …, rm) is total if it is defined for all r1 , r2 , …, rm in
its domain
g is computable if it is both partially computable and total
Example: g(x, y) = x + y
Remember the following equivalents:
partially computable = partially recursive
computable = recursive.
46.
Partially Computable vs. Computable Functions
●
●
●
●
●
●
f(x) = x
f(x, y) = x + y
f(x, y) = x * y
f(x, y) = x – y
All are partially computable on natural numbers
Only the first three are computable on natural numbers
Be the first to comment