1 / 8

Homework Questions

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:

carminda
Download Presentation

Homework Questions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 X0 GOTO B • [A] Z  Z+1 • IF Z0 GOTO A • [B] X  X-1 • IF X0 GOTO D [C] Z  Z+1 IF Z0 GOTO C [D] X  X-1 Y  Y+1 IF X0 GOTO D Y  Y-1 Theory of Computation Lecture 6: Primitive Recursive Functions II

  2. 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

  3. Homework Questions Z7  0 // exp. of Y  p(Y) with m = 7 Z8  Y Z9  0 IF Z80 GOTO A9 [A8] Z9  Z9+1 IF Z90 GOTO A8 [A9] Z8  Z8-1 IF Z80 GOTO A11 [A10] Z9  Z9+1 IF Z90 GOTO A10 [A11] Z8  Z8-1 Z7  Z7+1 IF Z80 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 Z30 GOTO A4 • [A3] Z4  Z4+1 • IF Z40 GOTO A3 • [A4] Z3  Z3-1 • IF Z30 GOTO A6 • [A5] Z4  Z4+1 • IF Z40 GOTO A5 • [A6] Z3  Z3-1 • Z2  Z2+1 • IF Z30 GOTO A6 • Z2  Z2-1 • Y  Z2 Theory of Computation Lecture 6: Primitive Recursive Functions II

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

More Related