80 likes | 239 Views
Homework Questions. Question 1: Write a program P that computes P (1) (r) = r-2 using no macros. Notice that we only consider natural numbers! This means that, for example, the expression 1-2 is undefined. Any program computing such an expression must never halt . Sample solution:
E N D
Homework Questions • Question 1: • Write a program P that computes P(1)(r) = r-2 using no macros. • Notice that we only consider natural numbers! This means that, for example, the expression 1-2 is undefined. Any program computing such an expression must never halt. • Sample solution: • IF X0 GOTO B • [A] Z Z+1 • IF Z0 GOTO A • [B] X X-1 • IF X0 GOTO D [C] Z Z+1 IF Z0 GOTO C [D] X X-1 Y Y+1 IF X0 GOTO D Y Y-1 Theory of Computation Lecture 6: Primitive Recursive Functions II
Homework Questions • Question 2: • a) Write a program S that computes S(1)(r) = r-4. Use a macro based on program P of the form W p(V) (which has the effect W V-2). • Sample solution: • Y p(X) • Y p(Y) Theory of Computation Lecture 6: Primitive Recursive Functions II
Homework Questions Z7 0 // exp. of Y p(Y) with m = 7 Z8 Y Z9 0 IF Z80 GOTO A9 [A8] Z9 Z9+1 IF Z90 GOTO A8 [A9] Z8 Z8-1 IF Z80 GOTO A11 [A10] Z9 Z9+1 IF Z90 GOTO A10 [A11] Z8 Z8-1 Z7 Z7+1 IF Z80 GOTO A11 Z7 Z7-1 Y Z7 • b) Now comes the best part: Expand all macros in S using the method we discussed in the lecture. • Z2 0 // expansion of Y p(X) with m = 2 • Z3 X • Z4 0 • IF Z30 GOTO A4 • [A3] Z4 Z4+1 • IF Z40 GOTO A3 • [A4] Z3 Z3-1 • IF Z30 GOTO A6 • [A5] Z4 Z4+1 • IF Z40 GOTO A5 • [A6] Z3 Z3-1 • Z2 Z2+1 • IF Z30 GOTO A6 • Z2 Z2-1 • Y Z2 Theory of Computation Lecture 6: Primitive Recursive Functions II
Some Primitive Recursive Functions • So we have learned that every primitive recursive function is computable. • We will now look at some examples of primitive recursive functions. • In order to prove that a function is primitive recursive, we have to show how that function can be derived from the initial functions by using composition and recursion. Theory of Computation Lecture 6: Primitive Recursive Functions II
Some Primitive Recursive Functions • Remember? The recursive definition of a function looks like this: • h(x1, …, xn, 0) = f(x1, …, xn) • h(x1, …, xn, t + 1) = g(t, h(x1, …, xn, t), x1, …, xn) . • If f is a function of k variables, g1, …, gk are functions of n variables, and • h(x1, …, xn) = f(g1(x1, …, xn), …, gk(x1, …, xn)). • Then h is said to be obtained from f and g1, …, gkby composition. Theory of Computation Lecture 6: Primitive Recursive Functions II
Some Primitive Recursive Functions • We defined the following initial functions: • s(x) = x + 1 • n(x) = 0 • uin(x1, …, xn) = xi , 1 ≤ i ≤ n. Theory of Computation Lecture 6: Primitive Recursive Functions II
Some Primitive Recursive Functions • Example 1: f(x, y) = x + y • We can transform this into a recursive definition: • f(x, 0) = x • f(x, y + 1) = f(x, y) + 1 • This can be rewritten as: • f(x, 0) = u11(x) • f(x, y + 1) = g(y, f(x, y), x) • where g(x1, x2, x3) = s(u23(x1, x2, x3)). • Obviously, u11(x), u23(x1, x2, x3), and s(x) are primitive recursive functions – they are initial functions. • g(x1, x2, x3) is obtained by composition of primitive recursive functions, so it is primitive recursive itself. • Therefore, f(x, y) = x + y is primitive recursive. Theory of Computation Lecture 6: Primitive Recursive Functions II
Some Primitive Recursive Functions • Example 2: h(x, y) = x y • We can obtain the following recursive definition: • h(x, 0) = 0 • h(x, y + 1) = h(x, y) + x • This can be rewritten as: • h(x, 0) = n(x) • h(x, y + 1) = g(y, h(x, y), x) • where • f(x1, x2) = x1 + x2 and • g(x1, x2, x3) = f(u23(x1, x2, x3), u33(x1, x2, x3)). • Obviously, these are all primitive recursive functions. • Therefore, h(x, y) = x y is primitive recursive. Theory of Computation Lecture 6: Primitive Recursive Functions II