Download presentation
Presentation is loading. Please wait.
Published byAllyson Cooper Modified over 5 years ago
1
Theory of Computation Lecture 5: Programs and Computable Functions III
Sample 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: X X-1 IF X0 GOTO A2 [A1] Z Z+1 IF Z0 GOTO A1 [A2] X X-1 Y Y+1 IF X 0 GOTO A2 Y Y-1 February 14, 2019 Theory of Computation Lecture 5: Programs and Computable Functions III
2
Theory of Computation Lecture 5: Programs and Computable Functions III
Sample Questions Question 2: a) Write a program S that computes S(1)(r) = r 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) February 14, 2019 Theory of Computation Lecture 5: Programs and Computable Functions III
3
Theory of Computation Lecture 5: Programs and Computable Functions III
Sample Questions 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 Z3 Z3-1 IF Z30 GOTO A4 [A3] Z4 Z4-1 IF Z40 GOTO A3 [A4] Z3 Z3-1 Z2 Z2+1 Z2 Z2-1 Y Z2 Z5 0 // exp. of Y p(Y) with m = 5 Z6 Y Z7 0 Z6 Z6-1 IF Z60 GOTO A7 [A6] Z7 Z7+1 IF Z70 GOTO A6 [A7] Z6 Z6-1 Z5 Z5+1 Z5 Z5-1 Y Z5 February 14, 2019 Theory of Computation Lecture 5: Programs and Computable Functions III
4
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 that TRUE = 1, FALSE = 0. According to this convention, predicates are just total functions whose values are always either 0 or 1. February 14, 2019 Theory of Computation Lecture 5: Programs and Computable Functions III
5
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. February 14, 2019 Theory of Computation Lecture 5: Programs and Computable Functions III
6
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 February 14, 2019 Theory of Computation Lecture 5: Programs and Computable Functions III
7
(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. February 14, 2019 Theory of Computation Lecture 5: Programs and Computable Functions III
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.