Theory of Computation Course note based on Computability, Complexity, - - PowerPoint PPT Presentation

theory of computation
SMART_READER_LITE
LIVE PREVIEW

Theory of Computation Course note based on Computability, Complexity, - - PowerPoint PPT Presentation

A Universal Program (4) Theory of Computation Course note based on Computability, Complexity, and Languages: Fundamentals of Theoretical Computer Science , 2nd edition, authored by Martin Davis, Ron Sigal, and Elaine J. Weyuker. course note


slide-1
SLIDE 1

A Universal Program (4)

Theory of Computation

Course note based on Computability, Complexity, and Languages: Fundamentals of Theoretical Computer Science, 2nd edition, authored by Martin Davis, Ron Sigal, and Elaine J. Weyuker.

course note prepared by Tyng–Ruey Chuang

Institute of Information Science, Academia Sinica Department of Information Management, National Taiwan University

Week 5, Spring 2008

1 / 22

slide-2
SLIDE 2

A Universal Program (4)

About This Course Note

◮ It is prepared for the course Theory of Computation taught at

the National Taiwan University in Spring 2008.

◮ It follows very closely the book Computability, Complexity,

and Languages: Fundamentals of Theoretical Computer Science, 2nd edition, by Martin Davis, Ron Sigal, and Elaine

  • J. Weyuker. Morgan Kaufmann Publishers. ISBN:

0-12-206382-1.

◮ It is available from Tyng-Ruey Chuang’s web site:

http://www.iis.sinica.edu.tw/~trc/ and released under a Creative Commons “Attribution-ShareAlike 2.5 Taiwan” license: http://creativecommons.org/licenses/by-sa/2.5/tw/

2 / 22

slide-3
SLIDE 3

A Universal Program (4) Universality (4.3)

Compute with Numbers of Programs

◮ Programs taking programs as input: Compilers, interpreters,

evaluators, Web browsers, . . . .

3 / 22

slide-4
SLIDE 4

A Universal Program (4) Universality (4.3)

Compute with Numbers of Programs

◮ Programs taking programs as input: Compilers, interpreters,

evaluators, Web browsers, . . . .

◮ Can we write a program in language S to accept the number

  • f another program P, as well as the input x to P, then

compute Ψ(1)

P (x) as output?

3 / 22

slide-5
SLIDE 5

A Universal Program (4) Universality (4.3)

Compute with Numbers of Programs

◮ Programs taking programs as input: Compilers, interpreters,

evaluators, Web browsers, . . . .

◮ Can we write a program in language S to accept the number

  • f another program P, as well as the input x to P, then

compute Ψ(1)

P (x) as output? ◮ Yes, we can! The program above is called an universal

program.

3 / 22

slide-6
SLIDE 6

A Universal Program (4) Universality (4.3)

Universality

For each n > 0, we define Φ(n)(x1, . . . , xn, y) = Ψ(n)

P (x1, . . . , xn),

where #(P) = y.

4 / 22

slide-7
SLIDE 7

A Universal Program (4) Universality (4.3)

Universality

For each n > 0, we define Φ(n)(x1, . . . , xn, y) = Ψ(n)

P (x1, . . . , xn),

where #(P) = y. Theorem 3.1. For each n > 0, the function Φ(n)(x1, . . . , xn, y) is partially computable.

  • 4 / 22
slide-8
SLIDE 8

A Universal Program (4) Universality (4.3)

Universality

For each n > 0, we define Φ(n)(x1, . . . , xn, y) = Ψ(n)

P (x1, . . . , xn),

where #(P) = y. Theorem 3.1. For each n > 0, the function Φ(n)(x1, . . . , xn, y) is partially computable.

  • We shall prove this theorem by showing how to construct, for each

n > 0, a program Un which computes Φ(n). That is, Ψ(n+1)

Un

(x1, . . . , xn, xn+1) = Φ(n)(x1, . . . , xn, xn+1). The programs Un are called universal.

4 / 22

slide-9
SLIDE 9

A Universal Program (4) Universality (4.3)

“Computer Organization” of Un

◮ Program Un accepts n + 1 input variables of which Xn+1 is a

number of a program P, and X1, . . . , Xn are provided to P as input variables.

◮ All variables used by P are arranged in the following order

Y , X1, Z1, X2, Z2, . . . and their state is coded by the G¨

  • del number

[y, x1, z1, x2, z2, . . .].

◮ Let variable S in program Un store the current state of

program P coded in the above manner.

◮ Let variable K in program Un store the number such that the

Kth instruction of program P is about to be executed.

◮ Let variable Z in program Un store the instruction sequence of

program P coded as a G¨

  • del number.

5 / 22

slide-10
SLIDE 10

A Universal Program (4) Universality (4.3)

Setting Up

As program Un computes Φ(n)(X1, . . . , Xn, Xn+1), we begin Un by setting up the initial environment for program (number) Xn+1 to execute: Z ← Xn+1 + 1 S ← n

i=1(p2i)Xi

K ← 1

6 / 22

slide-11
SLIDE 11

A Universal Program (4) Universality (4.3)

Setting Up

As program Un computes Φ(n)(X1, . . . , Xn, Xn+1), we begin Un by setting up the initial environment for program (number) Xn+1 to execute: Z ← Xn+1 + 1 S ← n

i=1(p2i)Xi

K ← 1

◮ If Xn+1 = #(P), where P consists of instructions I1, . . . , Im,

then Z gets the value [#(I1), . . . , #(Im)].

6 / 22

slide-12
SLIDE 12

A Universal Program (4) Universality (4.3)

Setting Up

As program Un computes Φ(n)(X1, . . . , Xn, Xn+1), we begin Un by setting up the initial environment for program (number) Xn+1 to execute: Z ← Xn+1 + 1 S ← n

i=1(p2i)Xi

K ← 1

◮ If Xn+1 = #(P), where P consists of instructions I1, . . . , Im,

then Z gets the value [#(I1), . . . , #(Im)].

◮ S is initialized as [0, X1, 0, X2, . . . , 0, Xn] which gives the first

n input variables their appropriate values and gives all other variables the value 0.

6 / 22

slide-13
SLIDE 13

A Universal Program (4) Universality (4.3)

Setting Up

As program Un computes Φ(n)(X1, . . . , Xn, Xn+1), we begin Un by setting up the initial environment for program (number) Xn+1 to execute: Z ← Xn+1 + 1 S ← n

i=1(p2i)Xi

K ← 1

◮ If Xn+1 = #(P), where P consists of instructions I1, . . . , Im,

then Z gets the value [#(I1), . . . , #(Im)].

◮ S is initialized as [0, X1, 0, X2, . . . , 0, Xn] which gives the first

n input variables their appropriate values and gives all other variables the value 0.

◮ K, the instruction counter, is given the initial value 1.

6 / 22

slide-14
SLIDE 14

A Universal Program (4) Universality (4.3)

Decoding Instruction

We first see if the execution of program P shall halt. If not, we fetch the Kth instruction and decode the instruction. [C] IF K = Lt(Z) + 1 ∨ K = 0 GOTO F U ← r((Z)k) P ← pr(U)+1

7 / 22

slide-15
SLIDE 15

A Universal Program (4) Universality (4.3)

Decoding Instruction

We first see if the execution of program P shall halt. If not, we fetch the Kth instruction and decode the instruction. [C] IF K = Lt(Z) + 1 ∨ K = 0 GOTO F U ← r((Z)k) P ← pr(U)+1

◮ If the computation has ended, GOTO F, where the proper

value will be output. (The case for K = 0 will be explained later.)

7 / 22

slide-16
SLIDE 16

A Universal Program (4) Universality (4.3)

Decoding Instruction

We first see if the execution of program P shall halt. If not, we fetch the Kth instruction and decode the instruction. [C] IF K = Lt(Z) + 1 ∨ K = 0 GOTO F U ← r((Z)k) P ← pr(U)+1

◮ If the computation has ended, GOTO F, where the proper

value will be output. (The case for K = 0 will be explained later.)

◮ (Z)k = a, b, c is the number of the Kth instruction. Thus

U = b, c is the code of the statement to be executed.

7 / 22

slide-17
SLIDE 17

A Universal Program (4) Universality (4.3)

Decoding Instruction

We first see if the execution of program P shall halt. If not, we fetch the Kth instruction and decode the instruction. [C] IF K = Lt(Z) + 1 ∨ K = 0 GOTO F U ← r((Z)k) P ← pr(U)+1

◮ If the computation has ended, GOTO F, where the proper

value will be output. (The case for K = 0 will be explained later.)

◮ (Z)k = a, b, c is the number of the Kth instruction. Thus

U = b, c is the code of the statement to be executed.

◮ The variable mentioned in the statement is the (r(U) + 1)th

in our list, and its current value is stored as the exponent to which P divides S.

7 / 22

slide-18
SLIDE 18

A Universal Program (4) Universality (4.3)

Instruction Execution

IF l(U) = 0 GOTO N IF l(U) = 1 GOTO A IF ∼ (P|S) GOTO N IF l(U) = 2 GOTO M

8 / 22

slide-19
SLIDE 19

A Universal Program (4) Universality (4.3)

Instruction Execution

IF l(U) = 0 GOTO N IF l(U) = 1 GOTO A IF ∼ (P|S) GOTO N IF l(U) = 2 GOTO M

◮ If l(U) = 0, the instruction is a dummy V ← V

and the computation does nothing. Hence, it goes to N (for Nothing).

8 / 22

slide-20
SLIDE 20

A Universal Program (4) Universality (4.3)

Instruction Execution

IF l(U) = 0 GOTO N IF l(U) = 1 GOTO A IF ∼ (P|S) GOTO N IF l(U) = 2 GOTO M

◮ If l(U) = 0, the instruction is a dummy V ← V

and the computation does nothing. Hence, it goes to N (for Nothing).

◮ If l(U) = 1, the instruction is V ← V + 1 . The computation

goes to A (for Add) to add 1 to the exponent on P in the prime power factorization of S.

8 / 22

slide-21
SLIDE 21

A Universal Program (4) Universality (4.3)

Instruction Execution

IF l(U) = 0 GOTO N IF l(U) = 1 GOTO A IF ∼ (P|S) GOTO N IF l(U) = 2 GOTO M

◮ If l(U) = 0, the instruction is a dummy V ← V

and the computation does nothing. Hence, it goes to N (for Nothing).

◮ If l(U) = 1, the instruction is V ← V + 1 . The computation

goes to A (for Add) to add 1 to the exponent on P in the prime power factorization of S.

◮ If l(U) = 0, 1, the instruction is either V ← V − 1 , or

IF V = 0 GOTO L . In both cases, if V = 0, the computation does nothing so goes to N. This happens when P is not a divisor of S.

8 / 22

slide-22
SLIDE 22

A Universal Program (4) Universality (4.3)

Instruction Execution

IF l(U) = 0 GOTO N IF l(U) = 1 GOTO A IF ∼ (P|S) GOTO N IF l(U) = 2 GOTO M

◮ If l(U) = 0, the instruction is a dummy V ← V

and the computation does nothing. Hence, it goes to N (for Nothing).

◮ If l(U) = 1, the instruction is V ← V + 1 . The computation

goes to A (for Add) to add 1 to the exponent on P in the prime power factorization of S.

◮ If l(U) = 0, 1, the instruction is either V ← V − 1 , or

IF V = 0 GOTO L . In both cases, if V = 0, the computation does nothing so goes to N. This happens when P is not a divisor of S.

◮ If P|S and l(U) = 2, the computation goes to M (for Minus).

8 / 22

slide-23
SLIDE 23

A Universal Program (4) Universality (4.3)

Branching

K ← mini≤Lt(Z)[l((Z)i) + 2 = l(U)] GOTO C

9 / 22

slide-24
SLIDE 24

A Universal Program (4) Universality (4.3)

Branching

K ← mini≤Lt(Z)[l((Z)i) + 2 = l(U)] GOTO C

◮ If l(U) > 2 and P|S, the current instruction is of the form

IF V = 0 GOTO L where V has a nonzero value and L is the label whose position in our label list is l(U) − 2.

9 / 22

slide-25
SLIDE 25

A Universal Program (4) Universality (4.3)

Branching

K ← mini≤Lt(Z)[l((Z)i) + 2 = l(U)] GOTO C

◮ If l(U) > 2 and P|S, the current instruction is of the form

IF V = 0 GOTO L where V has a nonzero value and L is the label whose position in our label list is l(U) − 2.

◮ The next instruction should be the first with this label.

9 / 22

slide-26
SLIDE 26

A Universal Program (4) Universality (4.3)

Branching

K ← mini≤Lt(Z)[l((Z)i) + 2 = l(U)] GOTO C

◮ If l(U) > 2 and P|S, the current instruction is of the form

IF V = 0 GOTO L where V has a nonzero value and L is the label whose position in our label list is l(U) − 2.

◮ The next instruction should be the first with this label. ◮ That is, K should get as its value the least i for which

l((Z)i) = l(U) − 2. If there is no instruction with the appropriate label, K gets the 0, which will lead to termination the next time through the main loop.

9 / 22

slide-27
SLIDE 27

A Universal Program (4) Universality (4.3)

Branching

K ← mini≤Lt(Z)[l((Z)i) + 2 = l(U)] GOTO C

◮ If l(U) > 2 and P|S, the current instruction is of the form

IF V = 0 GOTO L where V has a nonzero value and L is the label whose position in our label list is l(U) − 2.

◮ The next instruction should be the first with this label. ◮ That is, K should get as its value the least i for which

l((Z)i) = l(U) − 2. If there is no instruction with the appropriate label, K gets the 0, which will lead to termination the next time through the main loop.

◮ Once the instruction counter K is adjusted, the execution

enters the main loop by GOTO C .

9 / 22

slide-28
SLIDE 28

A Universal Program (4) Universality (4.3)

Subtraction and Addition

[M] S ← ⌊S/P⌋ GOTO N [A] S ← S · P [N] K ← K + 1 GOTO C

10 / 22

slide-29
SLIDE 29

A Universal Program (4) Universality (4.3)

Subtraction and Addition

[M] S ← ⌊S/P⌋ GOTO N [A] S ← S · P [N] K ← K + 1 GOTO C

◮ 1 is subtracted from the variable by dividing S by P.

10 / 22

slide-30
SLIDE 30

A Universal Program (4) Universality (4.3)

Subtraction and Addition

[M] S ← ⌊S/P⌋ GOTO N [A] S ← S · P [N] K ← K + 1 GOTO C

◮ 1 is subtracted from the variable by dividing S by P. ◮ 1 is added to the variable by multiplying S by P.

10 / 22

slide-31
SLIDE 31

A Universal Program (4) Universality (4.3)

Subtraction and Addition

[M] S ← ⌊S/P⌋ GOTO N [A] S ← S · P [N] K ← K + 1 GOTO C

◮ 1 is subtracted from the variable by dividing S by P. ◮ 1 is added to the variable by multiplying S by P. ◮ The instruction counter is increased by 1 and the computation

returns to the main loop to fetch the next instruction.

10 / 22

slide-32
SLIDE 32

A Universal Program (4) Universality (4.3)

Finalizing

[F] Y ← (S)1

11 / 22

slide-33
SLIDE 33

A Universal Program (4) Universality (4.3)

Finalizing

[F] Y ← (S)1

◮ One termination, the value of Y for the program being

simulated is stored at the exponent on p1 in S.

11 / 22

slide-34
SLIDE 34

A Universal Program (4) Universality (4.3)

Un, Finally

Z ← Xn+1 + 1 S ← n

i=1(p2i)Xi

K ← 1 [C] IF K = Lt(Z) + 1 ∨ K = 0 GOTO F U ← r((Z)k) P ← pr(U)+1 IF l(U) = 0 GOTO N IF l(U) = 1 GOTO A IF ∼ (P|S) GOTO N IF l(U) = 2 GOTO M K ← mini≤Lt(Z)[l((Z)i) + 2 = l(U)] GOTO C [M] S ← ⌊S/P⌋ GOTO N [A] S ← S · P [N] K ← K + 1 GOTO C [F] Y ← (S)1

12 / 22

slide-35
SLIDE 35

A Universal Program (4) Universality (4.3)

Notations

For each n > 0, the sequence Φ(n)(x1, . . . , xn, 0), Φ(n)(x1, . . . , xn, 1), . . . enumerates all partially computable functions of n variables. When we want to emphasize this aspect we write Φ(n)

y (x1, . . . , xn) = Φ(n)(x1, . . . , xn, y)

It is often convenient to omit the superscript when n = 1, writing Φy(x) = Φ(x, y) = Φ(1)(x, y).

13 / 22

slide-36
SLIDE 36

A Universal Program (4) Universality (4.3)

The Stepping Function STP

A simple modification of the program Un would enable us to prove that following predicate is computable: STP(x1, . . . , xn, y, t) ⇔ Program number y halts after t or fewer steps on inputs x1, . . . , xn ⇔ There is a computation of program y

  • f length ≤ t + 1, beginning with

inputs x1, . . . , xn We simply need to add a counter to determine when we have simulated t steps. However, we can prove a stronger result.

14 / 22

slide-37
SLIDE 37

A Universal Program (4) Universality (4.3)

Function STP is Primitive Recursive

Theorem 3.2. For each n > 0, the predicate STP(n)(x1, . . . , xn, y, t) is primitive recursive.

  • The idea is to provide numeric versions of the notations of

snapshot and successor of snapshot, and to show that the necessary functions are primitive recursive. We first define the following functions for extracting the components of the ith instruction of program number y: LABEL(i, y) = l((y + 1)i) VAR(i, y) = r(r((y + 1)i))) + 1 INSTR(i, y) = l(r((y + 1)i)) LABEL′(i, y) = l(r((y + 1)i)) ˙ −2

15 / 22

slide-38
SLIDE 38

A Universal Program (4) Universality (4.3)

Function STP is Primitive Recursive, Continued

Next we define some predicates that indicate, for program y and the snapshot represented by x = i, s, where i is the number of the instruction to be executed and s the current state (i.e., variable S in Un), what kind of action is to be performed next. SKIP(x, y) ⇔ [INSTR(l(x), y) = 0 & l(x) ≤ Lt(y + 1)] ∨ [INSTR(l(x), y) ≥ 2 & ∼ (pVAR(l(x),y)|r(x))] INCR(x, y) ⇔ INSTR(l(x), y) = 1 DECR(x, y) ⇔ INSTR(l(x), y) = 2 & pVAR(l(x),y)|r(x) BRANCH(x, y) ⇔ INSTR(l(x), y) > 2 & pVAR(l(x),y)|r(x) & (∃i)≤Lt(y+1)LABEL(i, y) = LABEL′(l(x), y)

16 / 22

slide-39
SLIDE 39

A Universal Program (4) Universality (4.3)

Function STP is Primitive Recursive, Continued

Now we can define SUCC(x, y), which for program number y, computes the successor to the snapshot represented by x.

SUCC(x, y) =            l(x) + 1, r(x) if SKIP(x, y) l(x) + 1, r(x) · pVAR(l(x),y) if INCR(x, y) l(x) + 1, r(x)/pVAR(l(x),y) if DECR(x, y) mini≤Lt(y+1)[LABEL(i, y) = LABEL′(l(x), y)], r(x) if BRANCH(x, y) Lt(y + 1) + 1, r(x)

  • therwise.

17 / 22

slide-40
SLIDE 40

A Universal Program (4) Universality (4.3)

Function STP is Primitive Recursive, Continued

We also need INIT(n)(x1, . . . , xn) = 1,

n

  • i=1

(p2i)xi which gives the representation of the initial snapshot for input x1, . . . , xn, and TERM(x, y) ⇔ l(x) > Lt(y + 1) which tests whether x represents a terminal snapshot for program y.

18 / 22

slide-41
SLIDE 41

A Universal Program (4) Universality (4.3)

Function STP is Primitive Recursive, Continued

Putting these together we can define a primitive recursive function that gives the numbers of the successive snapshots produced by a given program: SNAP(n)(x1, . . . , xn, y, 0) = INIT(n)(X1, . . . , xn) SNAP(n)(x1, . . . , xn, y, i + 1) = SUCC(SNAP(n)(x1, . . . , xn, y, i), y) Thus, STP(n)(x1, . . . , xn, y, t) ⇔ TERM(SNAP(n)(x1, . . . , xn, y, t), y) It is clear that STP(n)(x1, . . . , xn, y, t) is primitive recursive.

  • 19 / 22
slide-42
SLIDE 42

A Universal Program (4) Universality (4.3)

A Normal Form for Partial Computable Functions

Theorem 3.3. Let f (x1, . . . , xn) be a partially computable

  • function. Then there is a primitive recursive predicate

R(x1, . . . , xn, y) such that f (x1, . . . , xn) = l(min

z

R(x1, . . . , xn, z))

  • Proof. Let y0 be the number of a program that computes

f (x1, . . . , xn). Let R(x1, . . . , xn, z) be the following predicate STP(n)(x1, . . . , xn, y0, r(z)) & (r(SNAP(n)(x1, . . . , xn, y0, r(z))))1 = l(z)

20 / 22

slide-43
SLIDE 43

A Universal Program (4) Universality (4.3)

A Normal Form for Partial Computable Functions

Theorem 3.3. Let f (x1, . . . , xn) be a partially computable

  • function. Then there is a primitive recursive predicate

R(x1, . . . , xn, y) such that f (x1, . . . , xn) = l(min

z

R(x1, . . . , xn, z))

  • Proof. Let y0 be the number of a program that computes

f (x1, . . . , xn). Let R(x1, . . . , xn, z) be the following predicate STP(n)(x1, . . . , xn, y0, r(z)) & (r(SNAP(n)(x1, . . . , xn, y0, r(z))))1 = l(z) If the above predicate is defined, then for any such z, the computation by program y0 terminates in r(z) or few steps, and l(z) is the value held in the output variable Y .

20 / 22

slide-44
SLIDE 44

A Universal Program (4) Universality (4.3)

A Normal Form for Partial Computable Functions

Theorem 3.3. Let f (x1, . . . , xn) be a partially computable

  • function. Then there is a primitive recursive predicate

R(x1, . . . , xn, y) such that f (x1, . . . , xn) = l(min

z

R(x1, . . . , xn, z))

  • Proof. Let y0 be the number of a program that computes

f (x1, . . . , xn). Let R(x1, . . . , xn, z) be the following predicate STP(n)(x1, . . . , xn, y0, r(z)) & (r(SNAP(n)(x1, . . . , xn, y0, r(z))))1 = l(z) If the above predicate is defined, then for any such z, the computation by program y0 terminates in r(z) or few steps, and l(z) is the value held in the output variable Y . If on the other hand, the predicate is undefined, then STP(n)(x1, . . . , xn, y0, t) must be false for all t. That is, f (x1, . . . , xn) ↑.

  • 20 / 22
slide-45
SLIDE 45

A Universal Program (4) Universality (4.3)

A Characterization of Partially Computable Functions

Theorem 3.4. A function is partially computable if and only if it can be obtained from the initial functions by a finite number of applications of composition, recursion, and minimalization.

  • 21 / 22
slide-46
SLIDE 46

A Universal Program (4) Universality (4.3)

A Characterization of Partially Computable Functions

Theorem 3.4. A function is partially computable if and only if it can be obtained from the initial functions by a finite number of applications of composition, recursion, and minimalization.

  • Proof. (⇐) That every function which can be so obtained is

partially computable is a consequence of Theorems 1.1, 2.1, 2,2, 3.1, and 7.2 in Chapter 3. That is, there is a program in S for the function so obtained.

21 / 22

slide-47
SLIDE 47

A Universal Program (4) Universality (4.3)

A Characterization of Partially Computable Functions

Theorem 3.4. A function is partially computable if and only if it can be obtained from the initial functions by a finite number of applications of composition, recursion, and minimalization.

  • Proof. (⇐) That every function which can be so obtained is

partially computable is a consequence of Theorems 1.1, 2.1, 2,2, 3.1, and 7.2 in Chapter 3. That is, there is a program in S for the function so obtained. (⇒) Given a partially computable function — a program in language S — Theorem 3.3 in this Chapter show how to express this function in the form l(min

z

R(x1, . . . , xn, z)) where R is a primitive recursive predicate. Finally, R is used in a minimalization and then composed with the primitive recursive function l.

  • 21 / 22
slide-48
SLIDE 48

A Universal Program (4) Universality (4.3)

A Characterization of Computable Functions

When minz R(x1, . . . , xn, z) is a total function, we say that we are applying the operation of proper minimalization to R. Now, if l(min

z

R(x1, . . . , xn, z)) is total, then R(x1, . . . , xn, z) must be total too. Hence we have: Theorem 3.5. A function is computable if and only if it can be

  • btained from the initial functions by a finite number of

applications of composition, recursion, and proper minimalization.

  • 22 / 22