1.
CS 5000: Theory of Computation Lecture 07 Vladimir Kulyukin Department of Computer Science Utah State University
2.
Outline● Review● Partially Computable and Computable Functions● Function Macros and their Expansion● On to Computability
3.
Review: What does P compute?• Suppose we have some program P• Suppose that we have some inputs for P• If we start running or simulating P on the inputs, we have two cases: 1. There is a computation, i.e. a sequence of snapshots s1 , s2 , … , sk with sk being a terminal snapshot 2. There is no computation, i.e., the sequence of snapshots is infinite
4.
Review: Partial Functions1. (a, b) ∈ f and (a, c) ∈ f ⇒ b = c2. A partial function on S is a function whose domain is asubset of S3. g (n) = n , g : N → N . Then g is a partial function on N ,because the domain of g is the set of perfect squares, not allnatural numbers4. If f is a partial function on S and a is in the domain of f ,then f (a) ↓5. If f is a partial function on S and a is not in the domain off , then f (a) ↑
5.
Review: Partial Functions1. A partial function on S whose domain is S is called total2. ∅ (the empty set) is a partial function on any set that is nowheredefined
7.
Example 1 1 if n = 0 nf ( n) = ∏1 i if n > 0 i=
8.
Example 1: C Program fact.clong fact(long n){ if ( n == 0 ) return 1; else return n * fact(n-1);}
9.
Example 1: Comments on fact.c● We can say that f(n) is computed by the C program in fact.c● We can also say that the C program in fact.c computes f(n)● The two statements are semantically equivalent: – programs compute functions – functions are computed by programs
10.
Partially Computable FunctionsA partial function g is said to be partially computable if itis computed by some L program P.
11.
Definition: Partially Computable Functionsg is a partially computable function iff :1. ( ∃ P )( ∀ r1 , r2 ,..., rm ) g ( r1 , r2 ,..., rm ) = ψ (m) P ( r1 , r2 ,..., rm ),where P is an L program2. g ( r1 , r2 ,..., rm ) ↓ iff ψ (m) P ( r1 , r2 ,..., rm ) ↓
12.
Example 2 x1 − x2 if x1 ≥ x2g= ↑ if x1 < x2
13.
Example 2 Y ← X1 Z ← X2[A] IF Z != 0 GOTO B GOTO E[B] IF Y != 0 GOTO C GOTO B[C] Y ← Y – 1 Z←Z–1 GOTO A
14.
Example 2● g is a partially computable function because there exists a L program, defined in Example 2, that computes it● For any two natural numbers x and y, – g(x, y) is the same as the value of Y and the program terminates; – g(x, y) is undefined and the program does not terminate, in which case the value of Y is undefined as well
15.
Definition: Computable Functions● g(r1 , r2 , …, rm) is total if it is defined for all r1 , r2 , …, rm in its domain● g is computable if it is both partially computable and total● Example: g(x, y) = x + y● Remember the following equivalents: – partially computable = partially recursive – computable = recursive.
16.
Partially Computable vs. Computable Functions● f(x) = x● f(x, y) = x + y● f(x, y) = x * y● f(x, y) = x – y● All are partially computable on natural numbers● Only the first three are computable on natural numbers
17.
Function Macros● Let f(x) be a partially computable function● Let P be a program that computes f● What assumptions we can make about P? – P has the following variables: Y, X1, …. Xn, Z1, …, Zk – P has the following labels: A1, …, Ak – Can we assume this? Yes, because we can rename the labels to be only A labels – For every dispatch instruction IF Vi != 0 GOTO Aj, Aj exists. No loss of generality here, because we can do a simple syntactic check to verify this condition and refuse to compile a program if it does not meet this condition
19.
Function MacrosWhat do we need to do to be able to use macros of the form? W ← f (V1 ,..., Vn )
20.
Context-Sensitive Macro Expansion L Instruction 1 L Instruction 2 L Instruction 3 … W <= f(V1, …, Vn) … L Instruction r
21.
Variable and Label Replacements● We can always find a natural number m such that it is greater than all other subscripts used in P (variable subscripts and label subscripts)● Once m is found, we replace P’s Y references to Zm● All input variable references are replaced according to the following mapping: – X1 Zm+1, X2 Zm+2, …, Xn Zm+n● All local variable references are replaced as follows: – Z1 Zm+n+1, Z2 Zm+n+2, …, Zk Zm+n+k● The unique end label E1 is replaced with Em● All other labels are replaced according to the following mapping: – A1 Am+1, A2 Am+2, … , Am+j
22.
Variable and Label ReplacementsLet Qm denote P s source code with all the replacementscarried out. Formally, we can say thatQm = P ( Z m , Z m + 1 ,..., Z m + n , Z m + n + 1 ,..., Z m + n + k , Em , Am + 1 ,..., Am + j ), where1. Z m replaces Y;2. Z m + 1 ,..., Z m + n , Z m + n + 1 replace X 1 ,..., X n ;3. Z m + n + 1 ,..., Z m + n + k replace Z1 ,..., Z k ;4. Em replaces E1 ;5. Am + 1 ,..., Am + j replace A1 ,..., A j
23.
Function Macro Expansion• After the replacements are done, we are ready to expand the entire macro• There are two steps to this procedure: 1. Expand the macro into the macros that we already know how to expand; 2. Expand all the macros into the L primitive instructions
24.
Macro Expansion: Step 1Thus, the macro W ← f (V1 ,..., Vn ) is expanded into :Zm ← 0Z m + 1 ← V1...Z m + n ← VnZ m+ n+ 1 ← 0...Z m+ n+ k ← 0Qm[ Em ] W ← Z m
25.
Macro Expansion: Step 2● After the completion of step 1, we can further expand each macro into L primitive instructions● We have to deal with two types of macros: Zero Assignment and Variable Assignment● We will skip the technicalities here, because we already know from the previous lectures how to do that
26.
Dispatching on Predicates• Once we know how to handle W ← f(V1, …, Vn), we can make our conditional dispatches more powerful by dispatching on predicates (functions that return 0 or 1)• Let P(V1, …, Vn) be a predicate• Then IF P(V1, …, Vn) GOTO L expands into: 1. Zk ← P(V1, …, Vn) 2. IF Zk != 0 GOTO L
27.
The Point of Functional Macros● The use of macros makes it easier for us to reason about computational properties of functions as mathematical objects● If challenged by a mathematician on some function, we can write short, understandable programs to illustrate specific computational properties of that function● If challenged by an computer scientist, we can expand the short programs into much longer programs written in the L primitive instructions● Any computer scientist will readily acknowledge the realistic assumptions behind the primitive instructions of L
29.
Big Picture● Let us take stock of what we have done● We have defined a programming language L● We have offered a reasonable feasibility argument that the primitive instructions of L can be executed on a chip● We have found a way to combine primitive instructions into macros and expand those macros back into primitive instructions● We have found a way to expand function macros into primitive extractions● We have shown how function macros allow us to use predicates in IF statements
30.
Where To Now?● A scientific theory starts with primitives and proceeds to combining those primitives into increasingly complex structures● We will proceed in the same fashion: we will combine computable functions in such a way that the output of one becomes the input of another● Such function combination is called composition● This is the foundation of functional programming: programs return values consumed by other programs that, in turn, return new values consumed by still other programs, etc.
31.
Definition: CompositionLet f be a function of k variables.Let g1 ,..., g k be functions of n variables.Leth( x1 ,..., xn ) = f ( g1 ( x1 ,..., xn ) ,..., g k ( x1 ,..., xn ) ) ,Then h is obtained from f and g1 ,..., g k bycomposition.
32.
Reading Suggestions● Ch. 2, Computability, Complexity, and Languages by Davis, Weyuker, Sigal
33.
FeedbackErrors, comments to vladimir.kulyukin@gmail.com
Views
Actions
Embeds 0
Report content