Lecture 3: Count Programs, While Programs and Recursively Defined Functions 虞台文 大同大學資工所 智慧型多媒體研究室.

Slides:



Advertisements
Similar presentations
Turing Machines Memory = an infinitely long tape Persistent storage A read/write tape head that can move around the tape Initially, the tape contains only.
Advertisements

1 Turing Machines and Equivalent Models Section 13.2 The Church-Turing Thesis.
CS 345: Chapter 9 Algorithmic Universality and Its Robustness
Variants of Turing machines
1 Savitch and Immerman- Szelepcsènyi Theorems. 2 Space Compression  For every k-tape S(n) space bounded offline (with a separate read-only input tape)
Lecture 24 MAS 714 Hartmut Klauck
The Recursion Theorem Sipser – pages Self replication Living things are machines Living things can self-reproduce Machines cannot self reproduce.
Bayesian Networks Bucket Elimination Algorithm 主講人:虞台文 大同大學資工所 智慧型多媒體研究室.
Foundations of (Theoretical) Computer Science Chapter 3 Lecture Notes (Section 3.2: Variants of Turing Machines) David Martin With.
Optimization Problems 虞台文 大同大學資工所 智慧型多媒體研究室. Content Introduction Definitions Local and Global Optima Convex Sets and Functions Convex Programming Problems.
主講人:虞台文 大同大學資工所 智慧型多媒體研究室
1 Introduction to Computability Theory Lecture11: Variants of Turing Machines Prof. Amos Israeli.
Fall 2004COMP 3351 Turing Machines. Fall 2004COMP 3352 The Language Hierarchy Regular Languages Context-Free Languages ? ?
December 8, 2009Theory of Computation Lecture 22: Turing Machines IV 1 Turing Machines Theorem 1.1: Any partial function that can be computed by a Post-
Computability and Complexity 3-1 Turing Machine Computability and Complexity Andrei Bulatov.
1 Turing Machines. 2 The Language Hierarchy Regular Languages Context-Free Languages ? ?
Prof. Busch - LSU1 Turing Machines. Prof. Busch - LSU2 The Language Hierarchy Regular Languages Context-Free Languages ? ?
Chapter 4: A Universal Program 1. Coding programs Example : For our programs P we have variables that are arranged in a certain order: Y 1 X 1 Z 1 X 2.
Lecture 18 Various TMs. Allow the head not move Theorem. If the head is allowed to stay at the cell in each move, then every function computed by the.
Great Theoretical Ideas in Computer Science.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
Turing Machines A more powerful computation model than a PDA ?
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
Computability Turing machines for functions. Recursive functions. Homework: Modify class example for addition to produce contiguous 1s. Construct TM for.
CSCI 2670 Introduction to Theory of Computing September 28, 2005.
Introduction to CS Theory Lecture 15 –Turing Machines Piotr Faliszewski
1 Turing Machines There are languages that are not context-free. What can we say about the most powerful automata and the limits of computation?. Alan.
::ICS 804:: Theory of Computation - Ibrahim Otieno SCI/ICT Building Rm. G15.
The Simplex Algorithm 虞台文 大同大學資工所 智慧型多媒體研究室. Content Basic Feasible Solutions The Geometry of Linear Programs Moving From Bfs to Bfs Organization of a.
December 1, 2009Theory of Computation Lecture 21: Turing Machines II 1 Simulation of L n in T We will now construct a Post-Turing program Q that simulates.
Lecture 1: A Formal Model of Computation 虞台文 大同大學資工所 智慧型多媒體研究室.
1 Section 13.2 The Church-Turing Thesis The Church-Turing Thesis: Anything that is intuitively computable can be be computed by a Turing machine. It is.
CSCI 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Variants.
The Church-Turing Thesis Chapter 3 Giorgi Japaridze Theory of Computability.
The Simplex Algorithm 虞台文 大同大學資工所 智慧型多媒體研究室. Content Basic Feasible Solutions The Geometry of Linear Programs Moving From Bfs to Bfs Organization of a.
TM Design Macro Language D and SD MA/CSSE 474 Theory of Computation.
Lecture 5: Finite Automata 虞台文 大同大學資工所 智慧型多媒體研究室.
Hopfield Neural Networks for Optimization 虞台文 大同大學資工所 智慧型多媒體研究室.
Lecture 2: Limiting Models of Instruction Obeying Machine 虞台文 大同大學資工所 智慧型多媒體研究室.
Overview of the theory of computation Episode 3 0 Turing machines The traditional concepts of computability, decidability and recursive enumerability.
Lecture 24UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 24.
Chapter 5 Finite Automata Finite State Automata n Capable of recognizing numerous symbol patterns, the class of regular languages n Suitable for.
1 Introduction to Turing Machines
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 12 Mälardalen University 2007.
1 Turing Machines. 2 The Language Hierarchy Regular Languages Context-Free Languages ? ?
EM Algorithm 主講人:虞台文 大同大學資工所 智慧型多媒體研究室. Contents Introduction Example  Missing Data Example  Mixed Attributes Example  Mixture Main Body Mixture Model.
Lecture 6: Context-Free Languages
CS 3240 – Chapter 9.  Turing Machines  From Alan Turing, 1936  Turns out to be the LAST machine  The only machine you'll ever need  The Church-Turing.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY * Read chapter 4 of the book for next time * Lecture9x.ppt.
Universal Turing Machine
Chapters 11 and 12 Decision Problems and Undecidability.
CSE202: Introduction to Formal Languages and Automata Theory
Busch Complexity Lectures: Turing Machines
Theory of Computation Lecture 22: Turing Machines III
Turing Machines 2nd 2017 Lecture 9.
CSE 105 theory of computation
Turing Machines Acceptors; Enumerators
Chapter 9 TURING MACHINES.
COSC 3340: Introduction to Theory of Computation
Theory of Computation Lecture 21: Turing Machines II
CSE 105 theory of computation
Instructor: Aaron Roth
Hopfield Neural Networks for Optimization
Lecture 5: Turning Machine
Variants of Turing machines
Lecture 4: Unsolvable Problems
Theory of Computation Lecture 23: Turing Machines III
Lecture 2: Limiting Models of Instruction Obeying Machine
Lecture 6: Computational Complexity
CSE 105 theory of computation
Presentation transcript:

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 11 START HALT 22 START 11 HALT 22 Linear Concatenation Linear Concatenation

Condition Branching STARTHALT STARTHALT F Basic Programs START HALT 11 START HALT 22 Condition Branching Condition Branching START P HALT 11 true 22 false

Count Loop STARTHALT STARTHALT F Basic Programs START HALT  Count Loop Count Loop START y>0? HALT  yy1yy1 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 11 HALT 22 START P HALT 11 true 22 false START y>0? HALT  yy1yy1 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 11 HALT 22 START P HALT 11 true 22 false START y>0? HALT  yy1yy1 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 11 HALT 22 START P HALT 11 true 22 false START y>0? HALT  yy1yy1 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 11 HALT 22 START P HALT 11 true 22 false START y>0? HALT  yy1yy1 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 11 HALT 22 START P HALT 11 true 22 false START P HALT  true false Methods of Program Construction: Linear Concatenation Condition Branching START y>0? HALT  yy1yy1 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 11 END P 22 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 11 END P 22 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 11 22 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 11 22 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 11 22 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 1212 if P then  1 else  2 while P do  START 11 HALT 22 START P HALT 11 true 22 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 1212 if P then  1 else  2 while P do 

Linear Concatenation (  =  1  2 ) START 11 HALT 22 ::

Linear Concatenation (  =  1  2 ) START 11 HALT 22 ::  Defined as function composition:

Condition Branching :: START P HALT 11 true 22 false

:: START P HALT 11 true 22 false Condition Branching  Defined by part:

While Loop :: START P HALT 11 true false Defined by while recursion:

While Loop :: START P HALT 11 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 Si1Si1 SiSi S i+1 Sn1Sn1 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