1.07k likes | 1.27k Views
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
E N D
Lecture 3:Count Programs, While Programsand 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 Programsand Recursively Defined Functions Program Construction 大同大學資工所 智慧型多媒體研究室
Writing Programs • Program Building Blocks • Methods for Construction
START HALT START F HALT Basic Programs The primitives
START HALT START F HALT Basic Programs Methods of Program Construction • Linear Concatenation • Condition Branching • Count Loop • While Loop
START HALT START F HALT START START 2 1 HALT HALT Basic Programs Linear Concatenation START Linear Concatenation 1 2 HALT
START HALT START F HALT START START true false 2 1 HALT HALT Basic Programs Condition Branching START P Condition Branching 1 2 HALT
START HALT START F HALT START false true HALT Basic Programs Count Loop START y>0? Count Loop without using register y yy1 HALT
START HALT START F HALT START false true HALT Basic Programs While Loop START While Loop P HALT
START HALT START F HALT START 1 START START START true false false false y>0? P P 2 true true 1 2 yy1 HALT HALT HALT HALT Program Construction Basic Programs: Methods of Program Construction: Count Loop While Loop Linear Concatenation Condition Branching
START HALT START F HALT START 1 START START START true false false false y>0? P P 2 true true 1 2 yy1 HALT HALT HALT HALT What is their main distinction? Count Loop vs. While Loop Basic Programs: Methods of Program Construction: Count Loop While Loop Linear Concatenation Condition Branching
START HALT START F HALT Basic Programs: START Methods of Program Construction: 1 Count Loop While Loop START START START true false false false y>0? P P 2 true true 1 2 yy1 HALT HALT HALT Linear Concatenation HALT Condition Branching Three classes of programs: Conditional Programs • Conditional Programs • Count Programs • While Programs
START HALT START F HALT Basic Programs: START Methods of Program Construction: 1 Count Loop While Loop START START START true false false false y>0? P P 2 true true 1 2 yy1 HALT HALT HALT Linear Concatenation HALT Condition Branching Three classes of programs: Count Programs • Conditional Programs • Count Programs • While Programs
START HALT START F HALT START 1 Count Loop START START START true false false false y>0? P P 2 true true 1 2 yy1 HALT HALT HALT HALT Three classes of programs: While Programs • Conditional Programs • Count Programs • While Programs Basic Programs: Methods of Program Construction: While Loop Linear Concatenation Condition Branching
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 Count Programs Conditional Programs Problem: Count Programs = While Programs? Count Programs While Programs? Three Classes of Programs While Programs Count Programs?
While Programs Total Functions Conditional Programs More on Count Programs Count Programs • Count Programs While Programs. • “Is a counter program?” algorithmically checkable. • Every count program implements a total function.
While Programs Conditional Programs Problem: Count Programs = Total Functions? Count Programs Total Functions? More on Count Programs Total Functions Count Programs? Total Functions Count Programs • Count Programs While Programs. • “Is a counter program?” algorithmically checkable. • Every count program implements a total function.
Lecture 3:Count Programs, While Programsand 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 areM-equivalent for every machine M.
Theorem 1 Every program is equivalent to one with just onehalt instruction. • Case 1: without halt instruction • Add one halt instruction. • Case 2: with multiple halt instructions • Condense to one by making their halt labels identical. Pf)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . L1: IFPTHENGOTO L2 ELSE GOTO L3 L1 L2: IF PTHENGOTO L4 ELSE GOTO L5 true false P . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . L3 L2 false true P L5 L4 Theorem 2 Every program is equivalent to one in which no test instruction leads directly to a test instruction involving the same predicate name. Without such a code sequence in a program.
L1 L1 true false P true false P L3 L2 L3 L2 false true P false true P L5 L4 L5 L4 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. ’
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . L1: IFPTHENGOTO L2 ELSE GOTO L3 L1: IFPTHENGOTO L4 ELSE GOTO L3 L2: IF PTHENGOTO L4 ELSE GOTO L5 L2: IF PTHENGOTO L4 ELSE GOTO L5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . L1 L1 true false P true false P L3 L2 L3 L2 false true P false true P L5 L4 L5 L4 Suppose 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. ’ m m
Theorem 3 Every loop-free program can be transformed into an equivalent conditional program, i.e., conditional program loop-free program
START START true false P F 1 2 END END loop-free program Theorem 3 conditional program n: #instructions (operations and test) in . (Induction) Pf) • n = 0 • n = k • n = k+1 The empty program is a conditional program. Assumed true. To be shown true. Two Cases
loop-free program Theorem 3 conditional program n: #instructions (operations and test) in . (Induction) Pf) • n = 0 • n = k • n = k+1 The empty program is a conditional program. The first Instruction is a predicate. The first Instruction is an operation. Assumed true. To be shown true. Two Cases START START true false P F 1 2 END END
1 2 loop-free program Theorem 3 conditional program n: #instructions (operations and test) in . (Induction) Pf) • n = 0 • n = k • n = k+1 The empty program is a conditional program. Assumed true. To be shown true. Two Cases START START true false P F k instructions END END
START F ’ 1 2 END loop-free program Theorem 3 conditional program n: #instructions (operations and test) in . (Induction) Pf) • n = 0 • n = k • n = k+1 The empty program is a conditional program. Assumed true. To be shown true. Two Cases START START true false P F k instructions END END
START P 1 2 END loop-free program Theorem 3 conditional program n: #instructions (operations and test) in . (Induction) Pf) • n = 0 • n = k • n = k+1 The empty program is a conditional program. Assumed true. To be shown true. Two Cases START START true false P F k instructions END END
START START F1 F1 true false P1 true false P1 F2 F2 true false P2 true false P2 F4 F4 F4 F3 F5 F5 F3 F5 HALT HALT loop-free program Example conditional program
START START F1 F1 true false P1 true false P1 F2 F2 true false P2 true false P2 F4 F4 F4 F3 F5 F5 F3 F5 HALT HALT loop-free program Example conditional program
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
Ii Ii program Theorem 4 while program Assume that has n loops. Select a cut-point in each loop, and modify the code, such as: Pf) cut-point y i ith loop The program now becomes loop free.
cut-point Ii ith loop In y i y i Ii Ii Theorem 4 Transform the loop-free program obtained above to a conditional program according to Theorem 3. program while program
START y n+1 false y>0? HALT true y=i? true y=n? cut-point Ii ith loop In y i y i Ii Ii y 0 Theorem 4 true program false while program false Any halt instruction is replaced with this one.
START false true P1 false P2 F2 true F1 HALT false true P3 HALT Exercise Transform the program into an equivalent while program.
Corollary Every register function is an associated function of a while program. Register Functions While 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: • Linear Concatenation • Conditional Branching • While-Loop
START 1 START START true false false P P 2 true 1 2 HALT HALT HALT Gotoless Programming if P then 1else 2 12 while P do
Lecture 3:Count Programs, While Programsand Recursively Defined Functions The While Recursive Functions 大同大學資工所 智慧型多媒體研究室
The Methods of Program Construction • Linear Concatenation • Conditional Branching • While-Loop 12 if P then 1else 2 while P do
START 1 2 HALT Linear Concatenation (=12) :
START 1 2 HALT Linear Concatenation (=12) : Defined as function composition:
START false true P 1 2 HALT Condition Branching :
START false true P 1 2 HALT Condition Branching : Defined by part:
START false P true 1 HALT While Loop : Defined by while recursion:
START false P true 1 HALT While Loop : Defined by while recursion:
Writing Functions • Basic Functions • Methods to Construction Functions