CS20a: Models (Nov 5, 2002) • Turing Machines – Church’s thesis: TM are a general model of computation – But (almost) everything is undecidable • Today: alternative models – Primitive recursive functions – Total recursive functions – Partial recursive functions T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 1 I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Primitive recursion Functions N tuples → N tuples The following functions are primitive recursive: • s(x) = x + 1 (the successor function) • z(x) = 0 (the zero function) • π m i (x 1 , . . . , x m ) = x i (projection) • For any f : N m → N m , and g 1 , . . . , g m : N k → N , the composition f (g 1 (x), . . . , g m (x)) is primitive recursive T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 2 I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Primitive recursion (conventional form) Given h : N n − 1 → N m , and g : N n + m → N m , define f : N n → N m as follows: • f ( 0 , x) = h(x) • f (s(y), x) = g(y, x, f(y, x)) T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 3 I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Arithmetic Addition + x + y ≡ x + 1 + · · · + 1 � �� � y times x + 0 x = x + s(y) s(x + y) = Multiplication ∗ xy x + · · · + x = � �� � y times x 0 0 = x s(y) xy + x = T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 4 I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Exponentiation Exponentiation x y = x · x · · · · · x � �� � y times x 0 = 1 x y · x x s(y) = Hyperexponentation x . . . x x x ↑ y ≡ � �� � y times x ↑ 0 1 = x x ↑ y x ↑ s(y) = T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 5 I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Hyper-exponentation Hyper-hyper-exponentiation x ↑↑ y x ↑ x ↑ · · · ↑ x ≡ � �� � y times x ↑↑ 0 1 = x ↑↑ s(y) x ↑ (x ↑↑ y) = T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 6 I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Ackerman’s function A( 0 , x, y) s(y) A( 0 , x, y) s(y) = = A( 1 , x, y) x + y A( 1 , x, 0 ) x = = A( 2 , x, y) = xy A( 2 , x, 0 ) = 0 A(n, x, 0 ) 1 (n ≥ 3 ) = A(s(n), x, s(y)) = A(n, x, A(s(n), x, y)) Theorem For any fixed n , A(n, x, y) is primitive recur- sive. T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 7 I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Subtraction predecessor p( 0 ) = 0 p(s(y)) y = subtraction � . x − y if y ≤ x x − y 0 otherwise Algorithm: iterate predecessor on x , y times T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 8 I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Equality equality . . | x − y | = (x − y) + (y − x) � 0 if x ≠ y eq (x, y) = 1 if x = y . 1 − | x − y | = conditionals � z if x = y cond (x, y, z, w) = w if x ≠ y . = eq (x, y) · z + ( 1 − eq (x, y)) · w T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 9 I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
For-programs • Variables x, y, z over N • Simple assignments: – x ← y – x ← s(y) – x ← 0 • Induction: if p, q are for-programs , so are: – p ; q (sequential composition) – if x = y then p else q (conditional) – for y do p done (for-loop) T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 10 I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
For-loop: for y do p done • Execute p , y times • Assignments to y in p do not affect the number of iterations • for i = 1 to y do p done is equiv to i ← 0; for y do p ; i ← s(i) done T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 11 I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Interpretation of for-programs • Some variables variables are designated as input • Some are designated as output • The program defines a function Ninput � Noutput T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 12 I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
For programs and primitive recursion Theorem The for-programs define exactly the primitive- recursive functions. Encoding p.r. functions with for-loops. By induction on the size of the p.r. function. f( 0 , x) h(x) = f(s(y), x) = g(y, x, f (y, x)) T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 13 I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Encoding p.r. functions as for-loops f ( 0 , x) = h(x) f (s(y), x) = g(y, x, f (y, x)) Assume by induction that we have for-programs for g, h . Then the translation is: f (y, x) = z ← h(x) ; ( z are new variables) i ← 0; (assume y does not appear in h, g ) for y do z ← g(i, x, z) ; i ← s(i) done T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 14 I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Encoding for-loops as p.r. functions for y do . . . body Hard part: find a function f (y, x) = . . . done • By induction, we assume that there is a function g : N m → N m that describes the body • Define: f (s(i), x) = g(f(i, x)) T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 15 I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Partial-recursive functions T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 16 I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Properties of p.r. programs • The programs always terminate • However, does not include all recursive computations – Can code extremely “large” functions, but some functions are not definable (Ackerman’s) – We’ll argue later that all tractable computations are primitive recursive T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 17 I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Recommend
More recommend