Lecture 3: Count Programs, While Programs and Recursively Defined Functions 虞台文 大同大學資工所 智慧型多媒體研究室
Content Program Construction While Structure as a Normal Form for Register Programs The While Recursive Functions Primitive Recursive Functions & Partial Recursive Functions The Turing Machine
Lecture 3: Count Programs, While Programs and Recursively Defined Functions Program Construction 大同大學資工所 智慧型多媒體研究室
Writing Programs Program Building Blocks Methods for Construction
Basic Programs STARTHALTSTARTHALT F The primitives
Methods of Program Construction 1. Linear Concatenation 2. Condition Branching 3. Count Loop 4. While Loop STARTHALT STARTHALT F Basic Programs
Linear Concatenation STARTHALT STARTHALT F Basic Programs START HALT 11 START HALT 22 START 11 HALT 22 Linear Concatenation Linear Concatenation
Condition Branching STARTHALT STARTHALT F Basic Programs START HALT 11 START HALT 22 Condition Branching Condition Branching START P HALT 11 true 22 false
Count Loop STARTHALT STARTHALT F Basic Programs START HALT Count Loop Count Loop START y>0? HALT yy1yy1 true false without using register y
While Loop STARTHALT STARTHALT F Basic Programs START HALT While Loop While Loop START P HALT true false
Program Construction STARTHALT STARTHALT F Basic Programs: START 11 HALT 22 START P HALT 11 true 22 false START y>0? HALT yy1yy1 true false START P HALT true false Methods of Program Construction: Linear Concatenation Condition Branching Count LoopWhile Loop
Count Loop vs. While Loop STARTHALT STARTHALT F Basic Programs: START 11 HALT 22 START P HALT 11 true 22 false START y>0? HALT yy1yy1 true false START P HALT true false Methods of Program Construction: Linear Concatenation Condition Branching Count LoopWhile Loop What is their main distinction?
Conditional Programs STARTHALT STARTHALT F Basic Programs: START 11 HALT 22 START P HALT 11 true 22 false START y>0? HALT yy1yy1 true false START P HALT true false Methods of Program Construction: Linear Concatenation Condition Branching Count LoopWhile Loop Conditional Programs Count Programs While Programs Three classes of programs:
Count Programs STARTHALT STARTHALT F Basic Programs: START 11 HALT 22 START P HALT 11 true 22 false START y>0? HALT yy1yy1 true false START P HALT true false Methods of Program Construction: Linear Concatenation Condition Branching Count LoopWhile Loop Conditional Programs Count Programs While Programs Three classes of programs:
While Programs STARTHALT STARTHALT F Basic Programs: START 11 HALT 22 START P HALT 11 true 22 false START P HALT true false Methods of Program Construction: Linear Concatenation Condition Branching START y>0? HALT yy1yy1 true false Count LoopWhile Loop Conditional Programs Count Programs While Programs Three classes of programs:
Three Classes of Programs Conditional Programs – Basic Programs – Linear Concatenation + Condition Branching Count Programs – Basic Programs – Linear Concatenation + Condition Branching + Count Loop While Programs – Basic Programs – Linear Concatenation + Condition Branching + While Loop
While Programs Three Classes of Programs Count Programs Conditional Programs Problem: Count Programs = While Programs? Count Programs While Programs? While Programs Count Programs?
While Programs Total Functions More on Count Programs Count Programs Conditional Programs 1.Count Programs While Programs. 2.“Is a counter program?” algorithmically checkable. 3.Every count program implements a total function.
While Programs Total Functions More on Count Programs Count Programs Conditional Programs 1.Count Programs While Programs. 2.“Is a counter program?” algorithmically checkable. 3.Every count program implements a total function. Problem: Count Programs = Total Functions? Count Programs Total Functions? Total Functions Count Programs?
Lecture 3: Count Programs, While Programs and Recursively Defined Functions While Structure as a Normal Form for Register Programs 大同大學資工所 智慧型多媒體研究室
Program Equivalence Definition. Two programs and ’ are M -equivalent if and only if Definition. Two programs are equivalent if and only if they are M -equivalent for every machine M.
Theorem 1 Every program is equivalent to one with just one halt instruction. Pf) Case 1: without halt instruction – Add one halt instruction. Case 2: with multiple halt instructions – Condense to one by making their halt labels identical.
L1: IF P THEN GOTO L2 ELSE GOTO L3 L2: IF P THEN GOTO L4 ELSE GOTO L Theorem 2 Every program is equivalent to one in which no test instruction leads directly to a test instruction involving the same predicate name. P true false true P false L1 L2 L3 L4L5 Without such a code sequence in a program.
Theorem 2 Every program is equivalent to one in which no test instruction leads directly to a test instruction involving the same predicate name. P true false true P false L1 L2 L3 L4L5 P true false true P false L1 L2 L3 L4 L5 ’’ Show that
Theorem 2 Every program is equivalent to one in which no test instruction leads directly to a test instruction involving the same predicate name. P true false true P false L1 L2 L3 L4L5 P true false true P false L1 L2 L3 L4 L5 ’’ L1: IF P THEN GOTO L2 ELSE GOTO L3 L2: IF P THEN GOTO L4 ELSE GOTO L L1: IF P THEN GOTO L4 ELSE GOTO L3 L2: IF P THEN GOTO L4 ELSE GOTO L m m Show that Suppose
Theorem 3 Every loop-free program can be transformed into an equivalent conditional program, i.e., loop-free program conditional program
Theorem 3 loop-free program conditional program Pf) The empty program is a conditional program. n : #instructions (operations and test) in . 1. n = 0 2. n = k 3. n = k+1 Assumed true. To be shown true. (Induction) Two Cases START 11 END P 22 truefalse START F END
Theorem 3 loop-free program conditional program Pf) The empty program is a conditional program. n : #instructions (operations and test) in . 1. n = 0 2. n = k 3. n = k+1 Assumed true. To be shown true. (Induction) Two Cases START 11 END P 22 truefalse START F END The first Instruction is an operation. The first Instruction is a predicate.
Theorem 3 loop-free program conditional program Pf) The empty program is a conditional program. n : #instructions (operations and test) in . 1. n = 0 2. n = k 3. n = k+1 Assumed true. To be shown true. (Induction) Two Cases START END P 11 22 truefalse START F END k instructions
Theorem 3 loop-free program conditional program Pf) The empty program is a conditional program. n : #instructions (operations and test) in . 1. n = 0 2. n = k 3. n = k+1 Assumed true. To be shown true. (Induction) Two Cases START END P 11 22 truefalse START F END k instructions ’ ’ START F END
Theorem 3 loop-free program conditional program Pf) The empty program is a conditional program. n : #instructions (operations and test) in . 1. n = 0 2. n = k 3. n = k+1 Assumed true. To be shown true. (Induction) Two Cases START END P 11 22 truefalse START F END k instructions START END P
Example loop-free program conditional program START P1 F1 F2 F3 P2 F4 F5 HALT truefalse truefalse START P1 F1 F2 F3 P2 F4 F5 HALT truefalse truefalse F4 F5
Example loop-free program conditional program START P1 F1 F2 F3 P2 F4 F5 HALT truefalse truefalse START P1 F1 F2 F3 P2 F4 F5 HALT truefalse truefalse F4 F5
Theorem 4 By making at most one extra variable, every program can be transformed into a while program which computes the same function over the set of registers used by . program while program Many methods
Theorem 4 program while program Pf) Assume that has n loops. Select a cut-point in each loop, and modify the code, such as: IiIi cut-point IiIi y i i th loop The program now becomes loop free.
Theorem 4 IiIi y i IiIi cut-point i th loop program while program IiIi y i InIn Transform the loop-free program obtained above to a conditional program according to Theorem 3.
Theorem 4 IiIi y i IiIi cut-point i th loop program while program y n+1 START y>0? y=i?y=i? y=n?y=n? IiIi y i InIn y 0 true HALT false true Any halt instruction is replaced with this one.
Exercise START P1 F1 F2 P3 truefalse truefalse P2 HALT true false HALT Transform the program into an equivalent while program.
Corollary Every register function is an associated function of a while program. Register FunctionsWhile Programs
Discussions What can be computed? What can be computed by SR (R)? What can be computed by while programs? What can be computed by: 1.Linear Concatenation 2.Conditional Branching 3.While-Loop
Gotoless Programming 1212 if P then 1 else 2 while P do START 11 HALT 22 START P HALT 11 true 22 false START P HALT true false
Lecture 3: Count Programs, While Programs and Recursively Defined Functions The While Recursive Functions 大同大學資工所 智慧型多媒體研究室
The Methods of Program Construction 1.Linear Concatenation 2.Conditional Branching 3.While-Loop 1212 if P then 1 else 2 while P do
Linear Concatenation ( = 1 2 ) START 11 HALT 22 ::
Linear Concatenation ( = 1 2 ) START 11 HALT 22 :: Defined as function composition:
Condition Branching :: START P HALT 11 true 22 false
:: START P HALT 11 true 22 false Condition Branching Defined by part:
While Loop :: START P HALT 11 true false Defined by while recursion:
While Loop :: START P HALT 11 true false Defined by while recursion:
Writing Functions Basic Functions Methods to Construction Functions
Basic Functions Successor Predecessor Projector e.g.,
Forms of Function Definition SuccessorPredecessorProjector 1. Generalize Composition 2. Conditional Definition 3. While Recursion m k >=<>=<
While Recursive Functions SuccessorPredecessorProjector
While Recursive Functions The class of functions which includes all the basic functions ; and are closed under SuccessorPredecessorProjector 1. Generalize Composition 2. Conditional Definition 3. While Recursion
Example Iswhile recursive? It will be true if we can define this function by applying Generalize Composition Conditional Definition While Recursion onto Successor Predecessor Projector
Example Iswhile recursive? Analysis SuccessorPredecessorProjector Which functional form(s) will be used? Which basic functions will be used?
Example Iswhile recursive? while recursion = ? basic function composition
Example Iswhile recursive? This shows that is while recursive. For convenience, we allow using the above syntax.
Example Iswhile recursive? This shows that is while recursive.
Example Is while recursive? int div(int x, int y) { if( x < y) return 0; return div(x - y, y) + 1; } int div(int x, int y) { if( x < y) return 0; return div(x - y, y) + 1; }
Example Is while recursive? int div(int x, int y) { return div1(x, y, 0, x < y); } int div(int x, int y) { return div1(x, y, 0, x < y); } int div1(int x, int y, int q, int x_less_y) { if(x_less_y) return q; return div1(x - y, y, q + 1, x - y < y); } int div1(int x, int y, int q, int x_less_y) { if(x_less_y) return q; return div1(x - y, y, q + 1, x - y < y); }
Example Is while recursive? Formally, div must be defined as: xy zero ( x ) 0: x < y 1: x y 0 For convenience, it is now defined as: composition
Example composition while recursion composition condition
Example Is while recursive? This shows that is while recursive.
Theorem The register functions are precisely the while recursive functions, i.e., Register Functions While Recursive Functions
Exercises Show that the following functions are while recursive
Lecture 3: Count Programs, While Programs and Recursively Defined Functions Primitive Recursive Functions & Partial Recursive Functions 大同大學資工所 智慧型多媒體研究室
Basic Functions Successor Predecessor Projector e.g.,
Forms of Function Definition SuccessorPredecessorProjector 1. Generalize Composition 2. Primitive Recursion 3. -Operator m k >=<>=< Value of f (x 1,…,x k ) is the smallest z such that and
Definition 1.Basic Functions 2.Generalized Composition 3.Primitive Recursion 4. -Operator Primitive Recursive Functions Partial Recursive Functions
Example Analysis Primitive Recursive? Partial Recursive? Which forms are applicable?
Example Primitive Recursive? Partial Recursive? is primitive recursive. Analysis
Example Primitive Recursive? Partial Recursive? is primitive recursive. Analysis
Example Primitive Recursive? Partial Recursive? is primitive recursive. Analysis
Example Primitive Recursive? Partial Recursive? Analysis … To continue until reaching the lowest level. is partial recursive.
Question Primitive Recursive? Partial Recursive? Analysis Is div primitive recursive?
Theorem 5 1. Every primitive recursive function is an associated function of a count program. 2. Every partial recursive function is an associated function of a while program. Primitive Recursive Function Count Program Partial Recursive Function While Program
Theorem 5 Primitive Recursive Function Count Program Pf) 1.Basic Functions 2.Generalized Composition 3.Primitive Recursion Primitive Recursive Functions Count Programs
Theorem 5 Primitive Recursive Function Count Program Pf) 1.Basic Functions 2.Generalized Composition 3.Primitive Recursion Count Program Count Program
Theorem 5 Primitive Recursive Function Count Program Pf) 1.Basic Functions 2.Generalized Composition 3.Primitive Recursion Count Program Count Program trivial
Theorem 5 Count Program Generalized Composition START HALT
Theorem 5 Primitive Recursion Count Program START HALT y>0? true false
Theorem 5 1. Every primitive recursive function is an associated function of a count program. 2. Every partial recursive function is an associated function of a while program. Primitive Recursive Function Count Program Partial Recursive Function While Program
Theorem 5 Partial Recursive Function While Program Pf) 1.Basic Functions 2.Generalized Composition 3. -Operator Partial Recursive Functions While Programs
Theorem 5 Partial Recursive Function While Program Pf) 1.Basic Functions 2.Generalized Composition 3. -Operator While Program
Theorem 5 Partial Recursive Function While Program Pf) 1.Basic Functions 2.Generalized Composition 3. -Operator While Program As primitive recursive functions.
Theorem 5 While Program -Operator START r h >0? true HALT false
Theorem 6 1. Every associated function of a count program is primitive recursive. 2. Every associated function of a while program is partial recursive. Count Program Primitive Recursive Function While Program Partial Recursive Function
While Programs Discussion Partial Recursive Functions Count Programs Primitive Recursive Functions
Exercises Show that the following functions are primitive recursive:
Exercises Show that the following functions are partial recursive:
Lecture 3: Count Programs, While Programs and Recursively Defined Functions The Turing Machine 大同大學資工所 智慧型多媒體研究室
The Configuration of TM( ) Symbol String S1S1 S2S2 Si1Si1 SiSi S i+1 Sn1Sn1 SnSn …… Blank Tape Head Notations: : Empty String; : Alphabet; The set of all finite strings of symbol;
The TM( ) Memory Set: Instruction Set: : Containing at least two symbols; : Completely blank tape ;
The TM( ) Operations F PRINT s MOVE LEFT MOVE RIGHT ERASE
The TM( ) Predicates P s? LEFT END? RIGHT END?
Accessing TM( ) Encoder Decoder By that, for any program , is a partial function over *.
Example
START Empty Tape? 0? Print 1 left end? Move left 0? Print 0 Print 1 left end? Move left 0? Print 1 HALT true false
Simple Turing Machines Let {0, 1}. Then, TM( ), abbreviated as TM, is called the simple Turing machine.
Stepwise Simulation of SR2 by TM Step 1: Step 2: Step 3: Define encoding function g as: Simulate operations of SR2 on TM. trivial next page Simulate predicates of SR2 on TM. Exercise
Simulate Operations of SR2 on TM START Move Right 0? insert1 HALT true false START Move Right 0? delete1 HALT true false Move Right 0? true false
Exercise Stepwise simulation of TM using a register machine.
Lemmas & Theorem TM( ) simulates TM where | | 2. TM simulates TM( ) where | | 2. Theorem The machines TM( ), for varying alphabets constitute a family of equivalent machines.
Church Thesis: – Any computable function (algorithmic process) is a partial recursive function (can be computed on register machines). Turing's Thesis (weak form): – A Turing machine can compute anything that can be computed by a general-purpose digital computer. Turing's Thesis (strong form): – A Turing machine can compute anything that can be computed. Church-Turing Thesis: – Any computable function can be computed on register machines or Turing Machines. Church-Turing Thesis