100 likes | 117 Views
Learn about partial and total functions in computation, macros, and computable predicates. Explore how programs compute functions and expand macros to enhance program efficiency and functionality. Understand concepts through examples presented in the lecture.
E N D
Computation • 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. Theory of Computation Lecture 5: Programs and Computable Functions III
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. Theory of Computation Lecture 5: Programs and Computable Functions III
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 . Theory of Computation Lecture 5: Programs and Computable Functions III
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. Theory of Computation Lecture 5: Programs and Computable Functions III
Macros 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 • 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: Theory of Computation Lecture 5: Programs and Computable Functions III
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. • 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. • This is necessary because the macro expansion may be part of a loop in the main program. Theory of Computation Lecture 5: Programs and Computable Functions III
Macros • 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: • Z X1 – X2 • Y Z + X3 • This program computes f(x1 ,x2 ,x3), where • f(x1 ,x2 ,x3) = (x1 - x2) + x3 , if x1 x2 • = , if x1 < x2 Theory of Computation Lecture 5: Programs and Computable Functions III
Macros • Now let us introduce macros of the form • IF P(V1, …,Vn) GOTO L , • where P(x1, …,xn) is a computable predicate. • This will be based on the convention thatTRUE = 1, FALSE = 0. • According to this convention, predicates are just total functions whose values are always either 0 or 1. Theory of Computation Lecture 5: Programs and Computable Functions III
Macros • Let P(x1, …,xn) be a computable predicate. • Then we expand the macro • IF P(V1, …,Vn) GOTO L • to • Z P(V1, …,Vn) • IF Z0 GOTO L • As usual, the variable Z has to be chosen to create no conflicts with the main program. Theory of Computation Lecture 5: Programs and Computable Functions III
Macros • Example: • How can we expand the macro • IF V=0 GOTO L ? • V = 0 corresponds to the following predicate P(x): • P(x) = TRUE , if x = 0 • = FALSE, otherwise • This can be computed by the following program: • IF X0 GOTO E • Y Y+1 Theory of Computation Lecture 5: Programs and Computable Functions III