1 / 105

Lecture 3: Count Programs, While Programs and Recursively Defined Functions

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

Télécharger la présentation

Lecture 3: Count Programs, While Programs and Recursively Defined Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. Lecture 3:Count Programs, While Programsand Recursively Defined Functions Program Construction 大同大學資工所 智慧型多媒體研究室

  4. Writing Programs • Program Building Blocks • Methods for Construction

  5. START HALT START F HALT Basic Programs The primitives

  6. START HALT START F HALT Basic Programs Methods of Program Construction • Linear Concatenation • Condition Branching • Count Loop • While Loop

  7. START HALT START F HALT START START 2 1 HALT HALT Basic Programs Linear Concatenation START Linear Concatenation 1 2 HALT

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

  9. START HALT START F HALT START false true  HALT Basic Programs Count Loop START y>0? Count Loop without using register y  yy1 HALT

  10. START HALT START F HALT START false  true HALT Basic Programs While Loop START While Loop P  HALT

  11. START HALT START F HALT START 1 START START START true false false false y>0? P P 2 true true 1 2   yy1 HALT HALT HALT HALT Program Construction Basic Programs: Methods of Program Construction: Count Loop While Loop Linear Concatenation Condition Branching

  12. START HALT START F HALT START 1 START START START true false false false y>0? P P 2 true true 1 2   yy1 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

  13. 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   yy1 HALT HALT HALT Linear Concatenation HALT Condition Branching Three classes of programs: Conditional Programs • Conditional Programs • Count Programs • While Programs

  14. 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   yy1 HALT HALT HALT Linear Concatenation HALT Condition Branching Three classes of programs: Count Programs • Conditional Programs • Count Programs • While Programs

  15. 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   yy1 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

  16. 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

  17. While Programs Count Programs Conditional Programs Problem: Count Programs = While Programs? Count Programs  While Programs? Three Classes of Programs While Programs  Count Programs?

  18. 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.

  19. 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.

  20. Lecture 3:Count Programs, While Programsand Recursively Defined Functions While Structure as a Normal Form for Register Programs 大同大學資工所 智慧型多媒體研究室

  21. 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.

  22. 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)

  23. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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.

  24. 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.  ’

  25. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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

  26. Theorem 3 Every loop-free program can be transformed into an equivalent conditional program, i.e., conditional program loop-free program

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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.

  36. 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

  37. 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.

  38. START false true P1 false P2 F2 true F1 HALT false true P3 HALT Exercise Transform the program into an equivalent while program.

  39. Corollary Every register function is an associated function of a while program.  Register Functions While Programs

  40. 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

  41. START 1 START START true false false P P 2 true 1 2  HALT HALT HALT Gotoless Programming if P then 1else 2 12 while P do 

  42. Lecture 3:Count Programs, While Programsand Recursively Defined Functions The While Recursive Functions 大同大學資工所 智慧型多媒體研究室

  43. The Methods of Program Construction • Linear Concatenation • Conditional Branching • While-Loop 12 if P then 1else 2 while P do 

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

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

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

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

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

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

  50. Writing Functions • Basic Functions • Methods to Construction Functions

More Related