Download presentation
Presentation is loading. Please wait.
Published byCrystal Baldwin Modified over 8 years ago
2
Lecture 3: Count Programs, While Programs and Recursively Defined Functions 虞台文 大同大學資工所 智慧型多媒體研究室
3
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
4
Lecture 3: Count Programs, While Programs and Recursively Defined Functions Program Construction 大同大學資工所 智慧型多媒體研究室
5
Writing Programs Program Building Blocks Methods for Construction
6
Basic Programs STARTHALTSTARTHALT F The primitives
7
Methods of Program Construction 1. Linear Concatenation 2. Condition Branching 3. Count Loop 4. While Loop STARTHALT STARTHALT F Basic Programs
8
Linear Concatenation STARTHALT STARTHALT F Basic Programs START HALT 11 START HALT 22 START 11 HALT 22 Linear Concatenation Linear Concatenation
9
Condition Branching STARTHALT STARTHALT F Basic Programs START HALT 11 START HALT 22 Condition Branching Condition Branching START P HALT 11 true 22 false
10
Count Loop STARTHALT STARTHALT F Basic Programs START HALT Count Loop Count Loop START y>0? HALT yy1yy1 true false without using register y
11
While Loop STARTHALT STARTHALT F Basic Programs START HALT While Loop While Loop START P HALT true false
12
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
13
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?
14
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:
15
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:
16
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:
17
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
18
While Programs Three Classes of Programs Count Programs Conditional Programs Problem: Count Programs = While Programs? Count Programs While Programs? While Programs Count Programs?
19
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.
20
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?
21
Lecture 3: Count Programs, While Programs and Recursively Defined Functions While Structure as a Normal Form for Register Programs 大同大學資工所 智慧型多媒體研究室
22
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.
23
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.
24
L1: IF P THEN GOTO L2 ELSE GOTO L3 L2: IF P THEN GOTO L4 ELSE GOTO L5............... 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.
25
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
26
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 L5............... L1: IF P THEN GOTO L4 ELSE GOTO L3 L2: IF P THEN GOTO L4 ELSE GOTO L5............... m m Show that Suppose
27
Theorem 3 Every loop-free program can be transformed into an equivalent conditional program, i.e., loop-free program conditional program
28
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
29
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.
30
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
31
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
32
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
33
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
34
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
35
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
36
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.
37
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.
38
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.
39
Exercise START P1 F1 F2 P3 truefalse truefalse P2 HALT true false HALT Transform the program into an equivalent while program.
40
Corollary Every register function is an associated function of a while program. Register FunctionsWhile Programs
41
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
42
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
43
Lecture 3: Count Programs, While Programs and Recursively Defined Functions The While Recursive Functions 大同大學資工所 智慧型多媒體研究室
44
The Methods of Program Construction 1.Linear Concatenation 2.Conditional Branching 3.While-Loop 1212 if P then 1 else 2 while P do
45
Linear Concatenation ( = 1 2 ) START 11 HALT 22 ::
46
Linear Concatenation ( = 1 2 ) START 11 HALT 22 :: Defined as function composition:
47
Condition Branching :: START P HALT 11 true 22 false
48
:: START P HALT 11 true 22 false Condition Branching Defined by part:
49
While Loop :: START P HALT 11 true false Defined by while recursion:
50
While Loop :: START P HALT 11 true false Defined by while recursion:
51
Writing Functions Basic Functions Methods to Construction Functions
52
Basic Functions Successor Predecessor Projector e.g.,
53
Forms of Function Definition SuccessorPredecessorProjector 1. Generalize Composition 2. Conditional Definition 3. While Recursion m k >=<>=<
54
While Recursive Functions SuccessorPredecessorProjector
55
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
56
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
57
Example Iswhile recursive? Analysis SuccessorPredecessorProjector Which functional form(s) will be used? Which basic functions will be used?
58
Example Iswhile recursive? while recursion = ? basic function composition
59
Example Iswhile recursive? This shows that is while recursive. For convenience, we allow using the above syntax.
60
Example Iswhile recursive? This shows that is while recursive.
61
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; }
62
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); }
63
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
64
Example composition while recursion composition condition
65
Example Is while recursive? This shows that is while recursive.
66
Theorem The register functions are precisely the while recursive functions, i.e., Register Functions While Recursive Functions
67
Exercises Show that the following functions are while recursive
68
Lecture 3: Count Programs, While Programs and Recursively Defined Functions Primitive Recursive Functions & Partial Recursive Functions 大同大學資工所 智慧型多媒體研究室
69
Basic Functions Successor Predecessor Projector e.g.,
70
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
71
Definition 1.Basic Functions 2.Generalized Composition 3.Primitive Recursion 4. -Operator Primitive Recursive Functions Partial Recursive Functions
72
Example Analysis Primitive Recursive? Partial Recursive? Which forms are applicable?
73
Example Primitive Recursive? Partial Recursive? is primitive recursive. Analysis
74
Example Primitive Recursive? Partial Recursive? is primitive recursive. Analysis
75
Example Primitive Recursive? Partial Recursive? is primitive recursive. Analysis
76
Example Primitive Recursive? Partial Recursive? Analysis … To continue until reaching the lowest level. is partial recursive.
77
Question Primitive Recursive? Partial Recursive? Analysis Is div primitive recursive?
78
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
79
Theorem 5 Primitive Recursive Function Count Program Pf) 1.Basic Functions 2.Generalized Composition 3.Primitive Recursion Primitive Recursive Functions Count Programs
80
Theorem 5 Primitive Recursive Function Count Program Pf) 1.Basic Functions 2.Generalized Composition 3.Primitive Recursion Count Program Count Program
81
Theorem 5 Primitive Recursive Function Count Program Pf) 1.Basic Functions 2.Generalized Composition 3.Primitive Recursion Count Program Count Program trivial
82
Theorem 5 Count Program Generalized Composition START HALT
83
Theorem 5 Primitive Recursion Count Program START HALT y>0? true false
84
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
85
Theorem 5 Partial Recursive Function While Program Pf) 1.Basic Functions 2.Generalized Composition 3. -Operator Partial Recursive Functions While Programs
86
Theorem 5 Partial Recursive Function While Program Pf) 1.Basic Functions 2.Generalized Composition 3. -Operator While Program
87
Theorem 5 Partial Recursive Function While Program Pf) 1.Basic Functions 2.Generalized Composition 3. -Operator While Program As primitive recursive functions.
88
Theorem 5 While Program -Operator START r h >0? true HALT false
89
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
90
While Programs Discussion Partial Recursive Functions Count Programs Primitive Recursive Functions
91
Exercises Show that the following functions are primitive recursive:
92
Exercises Show that the following functions are partial recursive:
93
Lecture 3: Count Programs, While Programs and Recursively Defined Functions The Turing Machine 大同大學資工所 智慧型多媒體研究室
94
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;
95
The TM( ) Memory Set: Instruction Set: : Containing at least two symbols; : Completely blank tape ;
96
The TM( ) Operations F PRINT s MOVE LEFT MOVE RIGHT ERASE
97
The TM( ) Predicates P s? LEFT END? RIGHT END?
98
Accessing TM( ) Encoder Decoder By that, for any program , is a partial function over *.
99
Example
100
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
101
Simple Turing Machines Let {0, 1}. Then, TM( ), abbreviated as TM, is called the simple Turing machine.
102
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
103
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
104
Exercise Stepwise simulation of TM using a register machine.
105
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.
106
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.