Download presentation
Presentation is loading. Please wait.
1
Theory Of Computer Science
Lecture1:Programs and Computable Functions
2
Davis: Part1: Chap 2
3
The Programming Language L
We will explore computability theory using an extremely simple programming language called L. Let us take a look at this language. L uses variables holding numbers. Throughout the course, ‘number’ will refer to a nonnegative integer. The variables named input variables : X1, X2, X3, … output variables:Y local variables : Z1, Z2, Z3, … Labels: A1 B1 C1 D1 E1 A2 B2 C2 D2 E2 … … … … …
4
The Programming Language L
We do not have to write the subscript 1, so instead of X1 or Z1 we can write X or Z. For Labels the subscript 1 can be omitted.
5
A program of L consists of a list (a finite sequence) of instructions.
What do the instructions look like? There are only three types of instructions in L: increment decrement conditional branch
6
The Programming Language L
In the following list of instructions, the letter V stands for any variable in the program, and L stands for a label: V V+1 increase by 1 the value of the variable V V V-1 If the value of V is 0, leave it unchanged; otherwise decrease by 1 the value of the variable V. If V0 GOTO L If the value of V is nonzero, perform the instruction with label L next, otherwise proceed to the next instruction in the list.
7
The Programming Language L
These are the only three instructions in our language L. You will be surprised how powerful this extremely simple programming language is. We just need two more conventions: The output variable Y and the local variables Zi initially have the value 0. A program halts when it attempts to move to a nonexistent instruction (beyond the end of the list) or branch to a nonexistent label.
8
The Programming Language L
Note that we have an unlimited supply of variables and labels. Moreover, there is no upper limit on the value that a variable can contain. Therefore, the language L is not a practical language, but it is well-suited for the theoretical evaluation of algorithms.
9
Sample Programs Consider the following program:
What function does this program compute? f(x) = 1, if x = 0 = x, otherwise. What would we have to change if we wanted to compute the function f(x) = x? [A] X X-1 Y Y+1 IF X0 GOTO A
10
Sample Programs The following program computes : [A] IF X0 GOTO B
Z Z+1 IF Z0 GOTO E [B] X X-1 Y Y+1 IF X0 GOTO B f(x) = x
11
Sample Programs In the previous example, the lines Z Z+1
IF Z0 GOTO L were used to implement an instruction that we could call GOTO L First, we make sure that Z has a nonzero value, and then the conditional branch (actually, here it is an unconditional branch) is executed.
12
Sample Programs From now on we will use the macro GOTO L in our programs. We know that we could always replace GOTO L with its macro expansion to obtain a valid L program. Now remember the program computing the function f(x) = x. Although it computes its output correctly, it deletes (sets to zero) the original input. This is an undesirable behavior, so we should come up with an improved program.
13
Sample Programs [A] IF X0 GOTO B GOTO C [A] IF X0 GOTO B [B] X X-1
Z Z+1 IF Z0 GOTO E [B] X X-1 Y Y+1 IF X0 GOTO B [A] IF X0 GOTO B GOTO C [B] X X-1 Y Y+1 Z Z+1 GOTO A [C] IF Z0 GOTO D GOTO E [D] Z Z-1 X X+1
14
However, there is one problem:
Sample Programs The previous program justifies the introduction of a macro VV’ The execution of this macro will replace the contents of variable V by those of variable V’ without changing the contents of V’. However, there is one problem: The previous program could rely on the initial condition Y = 0, which is not guaranteed for any variable V.
15
Sample Programs To solve this problem, we introduce the macro V0
Its macro expansion is: [L] V V-1 IF V0 GOTO L Now we can write down the macro expansion of VV’: Of course, the label L has to be chosen to be different from any other label in the program.
16
Sample Programs V 0 [A] IF V’0 GOTO B GOTO C [B] V’ V’-1 V V+1
Z Z+1 GOTO A [C] IF Z0 GOTO D GOTO E [D] Z Z-1 V’ V’+1 VV’:
17
Sample Programs Another example: Y X1 Z X2 [B] IF Z0 GOTO A
GOTO E [A] Z Z-1 Y Y+1 GOTO B f(x1, x2) = x1 + x2
18
The Syntax of L A statement is one of the following:
Here, V may be any variable and L may be any label. Note that the statement V V leaves all values unchanged, so it is a “dummy” command. We will later see why it is useful to have V V in our set of statements. Y X1 V V+1 V V-1 V V IF V0 GOTO L
19
The Syntax of L An instruction is either
a statement (unlabeled instruction) or [L] followed by a statement (instruction labeled L) A program is a list (i.e., a finite sequence) of instructions. The length of this list is called the length of the program. We also include the empty program – the program of length 0 – in the set of all programs.
20
The Syntax of L While a program is being executed, its variables assume different numerical values. This motivates the concept of the state of a program: A state of a program P is a list of equations of the form V = m, where V is a variable and m is a number, including exactly one equation for each variable that occurs in P (and possibly equations for other variables).
21
The Syntax of L Consider the following program P :
Is the list X = 4, Y = 3, Z = 3 a state of P ? Yes. [A] IF X0 GOTO B Z Z+1 IF Z0 GOTO E [B] X X-1 Y Y+1 IF Z0 GOTO A How about X1 = 4, X2 = 5, Y = 4, Z = 4? Yes. And X = 3, Z = 3? No.
22
Yes, we also need to store which instruction is to be executed next
The Syntax of L Consider a machine that can execute programs of L. During program execution, this machine needs to store the state of a program, i.e., keep track of the values of variables. Is there anything else that needs to be stored? Yes, we also need to store which instruction is to be executed next
23
The Syntax of L Therefore, we define a snapshot or instantaneous description of a program P of length n to be a pair (i, ) where 1 i (n + 1), and is a state of P. The number i indicates the number of the instruction that is to be executed next. i = n + 1 corresponds to a “stop” instruction. A snapshot (i, ) of a program P of length n is called terminal if i = n + 1. If s = (i, ) is a snapshot of P and V is a variable of P , then the value of V at s just means the value of V at .
24
The Syntax of L If (i, ) is a nonterminal snapshot of P , we define the successor of (i, ) to be the snapshot (j, ) defined as follows: Case 1: The i-th 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 (the value of V at is m + 1).
25
The Syntax of L Case 2: If m = 0, then = . Case 3:
The i-th 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 if m 0. The i-th instruction of P is V V. Then j = i + 1 and = .
26
The Syntax of L Case 4: The i-th instruction of P is IF V0 GOTO L.
Then = and there are two subcases: Case 4a: contains the equation V = 0. Then j = i + 1. Case 4b: 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 j-th instruction of P is labeled L. Otherwise, j = n + 1.
27
The Syntax of L A computation of a program P is defined to be a sequence s1, s2, …, sk of snapshots of P such that si+1 is the successor of si for i = 1, 2, …, k – 1 and sk is terminal.
28
Computable Functions What does it exactly mean when we say that a program computes a function? We would like to find a precise definition for this. Let P be any program in the language L and let r1, …, rm be m given numbers. We form the state of P which consists of the equations X1 = r1, X2 = r2, …, Xm = rm, Y = 0 together with the equations V = 0 for all other variables V in P. We call this the initial state.
29
Computable Functions Moreover, we call the snapshot (1, ) the initial snapshot. When running the program, there are two possible outcomes: Case 1: There is a computation s1, s2, …, sk of P beginning with the initial snapshot. Then we write P(m)(r1, r2, …, rm) for the value of the variable Y at the terminal snapshot sk.
30
Computable Functions Case 2:
There is no such computation; i.e., there is an infinite sequence s1, s2, s3, …beginning with the initial snapshot. In this case, P(m)(r1, r2, …, rm) is undefined.
31
Sample Computation (1, {X = r, Y = 0, Z = 0}),
[A] IF X 0 GOTO B (1) Z Z+1 (2) IF Z 0 GOTO E (3) [B] X X-1 (4) Y Y+1 (5) Z Z+1 (6) IF Z 0 GOTO A (7) (1, {X = r, Y = 0, Z = 0}), (4, {X = r, Y = 0, Z = 0}), (5, {X = r - 1, Y = 0, Z = 0}), (6, {X = r - 1, Y = 1, Z = 0}), (7, {X = r - 1, Y = 1, Z = 1}), (1, {X = r - 1, Y = 1, Z = 1}), . (1, {X = 0, Y = r, Z = r}), (2, {X = 0, Y = r, Z = r}), (3, {X = 0, Y = r, Z = r + 1}), (8, {X = 0, Y = r, Z = r + 1}).
32
Computation For any program P and any positive integer m, the function P(m)(r1, r2, …, rm) is said to be computed by P. We allow any program to be run with any number of inputs. Consider the summation program from the previous lecture: P(2)(r1, r2) = r1 + r2 P(1)(r1) = r1 + 0 = r1 P(3)(r1, r2 , r3) = r1 + r2
33
Computation For any program P and any positive integer m, the function P(m)(r1, r2, …, rm) is said to be computed by P. In general, a partial function f on a set Sm is a function whose domain is a subset of Sm. If a partial function on Sm has the domain Sm, then it is called total.
34
Computation A given partial function g (of one or more variables) is said to be partially computable if it is computed by some program. This is the case if there is a program P such that g(r1, r2, …, rm) = P(m)(r1, r2, …, rm) for all r1, r2, …, rm. This means not only that both sides have the same value when they are defined, but also that when either side of the equation is undefined, the other one is as well. A function is said to be computable if it is both partially computable and total.
35
Macros So far we have used macros in an informal way. Let us now develop a more precise definition of them. Let f(x1, x2, …, xn) be a partially computable function, and P be a program that computes f. Let us assume that the variables in P are named Y, X1, …, Xn, Z1, …, Zk, the labels in P are named E, A1, …, Al, and for each instruction of P of the form IF V0 GOTO Ai there is in P an instruction labeled Ai .
36
Macros We can modify any program of the language L to comply with these assumptions. We write: P = P (Y, X1, …, Xn, Z1, …, Zk; E, A1, …, Al) In particular, we will use: Qm = P (Zm, Zm+1, …, Zm+n, Zm+n+1, …, Zm+n+k; Em, Am+1, …, Am+l) for a given value m.
37
Macros Now we want to use macros of the form: W f(V1, …, Vn)
in our programs, where W, V1, …, Vn can be any variables; W could be among V1, …, Vn. We expand the macro as follows: Zm 0 Zm+1 V1 Zm+2 V2 : Zm+n Vn Zm+n+1 0 Zm+n+2 0 Zm+n+k 0 Qm [Em] W Zm
38
Macros Whenever we expand a macro, the number m has to be chosen large enough so that none of the variables or labels in Qm occur in the main program. This is necessary because the macro expansion may be part of a loop in the main program. Note that in the expansion the output and local variables are set to zero, although at the start of the main program they are set to zero anyway.
39
Macros Example: This program computes f(x1 ,x2 ,x3), where
Obviously, if f(V1, …, Vn) is undefined (), the program Qm will never terminate. So if f is not total, and the macro W f(V1, …, Vn) is encountered when V1, …, Vn have values for which f is not defined, the main program will never terminate. Example: This program computes f(x1 ,x2 ,x3), where Z X1 – X2 Y Z + X3 f(x1 ,x2 ,x3) = (x1 - x2) + x3 , if x1 x2 = , if x1 < x2
40
Macros Now let us introduce macros of the form
where P(x1, …,xn) is a computable predicate. This will be based on the convention that TRUE = 1, FALSE = 0. According to this convention, predicates are just total functions whose values are always either 0 or 1. IF P(V1, …,Vn) GOTO L
41
Macros Let P(x1, …,xn) be a computable predicate.
Then we expand the macro As usual, the variable Z has to be chosen to create no conflicts with the main program. Z P(V1, …,Vn) IF Z0 GOTO L IF P(V1, …,Vn) GOTO L
42
(Partially)Computable Functions
By introducing macros, we have seen that it is possible to compute complex functions with our very simple programming language L . Notice that macros do not change the specification of the language, but they just simplify writing down programs. As you know, we can always replace macros with actual code. So what are the limitations of the language L ? In order to find out, we need to do some mathematics.
43
Composition Let us combine computable functions in such a way that the output of one becomes an input to another. For example, we could combine the functions f and g to obtain a new function h: h(x) = f(g(x)) General Definition Let f be a function of k variables and let g1, …, gk be functions of n variables. Let h(x1, …, xn) = f(g1(x1, …, xn), …, gk(x1, …, xn)). Then h is said to be obtained from f and g1, …, gk by composition.
44
Composition Theorem 1.1: If h is obtained from the (partially) computable functions f, g1, …, gk by composition, then h is (partially) computable. Proof: The following program obviously computes h: Z1 ← g1(X1, …, Xn) : Zk ← gk(X1, …, Xn) Y ← f(Z1, …, Zk) If f, g1, …, gk are not only partially computable but are also total, then so is h.
45
Composition Example: We know that f(x1, x2) = x1 + x2 is a computable function. We also know that g1(x) = x2 and g2(x) = 3x are computable functions. According to Theorem 1.1, the following function h(x) must then also be computable: h(x) = f(g1(x), g2(x)) = f(x2, 3x) = x2 + 3x
46
Recursion Let k be some fixed number and h(0) = k h(t + 1) = g(t, h(t)) , where g is some given total function of two variables. Then we say that h is obtained from g by primitive recursion, or simply recursion. Theorem 2.1: Let h be obtained as shown above, and let g be computable. Then h is also computable.
47
Recursion Proof: Obviously, the function f(x) = k is computable.
The program computing f(x) simply consists of k times the instruction Y ← Y+1. This gives us the macro Y ← k. Now we can write a program that computes h(x): This is your homework question 1!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.