Overview 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 S T T I U T E O F N T A I E C I H N N Computation, Computers, and Programs Recursive functions R O O I 1 I F L L A O G http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 C Y 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 U T T S I T E O F N T A I E C I H N N Computation, Computers, and Programs Recursive functions R O O I 2 F I O L L A G http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 C Y 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 S T I O F I N T E A C I H N N Computation, Computers, and Programs Recursive functions O R O 3 F I L I L O A G http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Overview 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 S T T I U T E O F N T I A C E I H N N Computation, Computers, and Programs Recursive functions R O O I 4 I F L A L G O http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 C Y 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) = U T S T T I E O F N T I A E C I H N N Computation, Computers, and Programs Recursive functions R O O I 5 F I O L L A G http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 C Y 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 S T I O F I N T E A C I H N N Computation, Computers, and Programs Recursive functions R O O 6 F L I I L O A G http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Overview 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 S T I U T E O F N T I A C E I H N N Computation, Computers, and Programs Recursive functions R O O I 7 I F L L A G O http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 C Y 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 U T T S I T E O F N T A I E C I H N N Computation, Computers, and Programs Recursive functions R O O I 8 F I O L L A G http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 C Y 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 T S I O F I N T E A C I H N N Computation, Computers, and Programs Recursive functions O R O 9 F I L I L O A G http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Overview 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) S T T I U T E O F N T I A E C I H N N Computation, Computers, and Programs Recursive functions R O O I 10 I F L A L O G http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 C Y 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 U T S T I T E O F N T A I E C I H N N Computation, Computers, and Programs Recursive functions R O O I 11 F I O L L A G http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 C Y 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 S T I O F I N T E A C I H N N Computation, Computers, and Programs Recursive functions O R O 12 F I L I L O A G http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Overview 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 S T I U T E O F N T I A E C I H N N Computation, Computers, and Programs Recursive functions R O O I 13 I F L L A O G http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 C Y 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 U T S T I T E O F N T I A E C I H N N Computation, Computers, and Programs Recursive functions R O O I 14 F I O L L A G http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 C Y 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 T S I O F I N T E A C I H N N Computation, Computers, and Programs Recursive functions R O O 15 F I L I L O A G http://www.cs.caltech.edu/courses/cs20/a/ November 4, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Recommend
More recommend