CS 5000: Theory of Computation                         Lecture 05                       Vladimir Kulyukin                D...
Outline    ●        Programming Language L    ●        Primitive Instructions and Macros    ●        Program States and Sn...
The Macro Plan   ●       Once we have a set of primitive instructions, we can start       building a repertoire of higher ...
Example                       1 if x = 0             f ( x) =                        x otherwisewww.youtube.com/vkedco ...
Implementing f(x) in L: Program 1                    [ A1 ] X 1 ← X 1 − 1                         Y← Y+1                  ...
Comments on Program 1   ●       Program 1 copies the value of X into Y for X > 0   ●        If X = 0, then Y = 1 at termin...
Program 2             [A]         IF X != 0 GOTO B                         Z←Z+1                         IF Z != 0 GOTO E ...
Comments on Program 2   ●       Question: What function does Program 2 compute?   ●       Answer: f(x) = x, because when i...
Macro Expansion   ●       Consider the following block of L code for an       unconditional branch:            Z←Z+1      ...
More Comments on Program 2   ●       Program 2 does copy the value of X into Y   ●       But, what happens to the original...
Program 3                 [A]     IF X != 0 GOTO B                         GOTO C                 [B]     X←X–1           ...
Comments on Program 3   ●       The [A] to [B] back to [A] loop continues as long as X !=       0   ●       In this loop, ...
More Comments on Program 3   ●       Program 3 can be used as the foundation of another       macro: V ← V   ●       This ...
More Comments on Program 3   ●       There is a subtle assumption behind the code for       Program 3: it works correctly ...
Macro V ← 0   ●       When we want to zero out the value of a variable, we       need to loop on V ← V – 1 so long as V !=...
A Safer Version of the V ← V Macro                           V←0                 [A]       IF V != 0 GOTO B               ...
Comments on the Safer Version of V ← V   ●       When the macro is expanded into the main program, the       variable Z mu...
Program 4               f ( x1 , x2 ) = x1 + x2www.youtube.com/vkedco      www.vkedco.blogspot.com
Program 4                         Y ← X1                         Z ← X2                  [A]    IF Z != 0 GOTO B          ...
Comments on Program 4   ●       We place the value of X1 into Y and the value of X2 into       Z   ●       Then we go into...
Program 5              f ( x1 , x2 ) = x1 ⋅ x2www.youtube.com/vkedco       www.vkedco.blogspot.com
Program 5                         Z2 ← X2                 [A]     IF Z2 != 0 GOTO B                         GOTO E        ...
Comments on Program 5   ●       We place the value of X2 into Z2   ●       Then we go into the [A] to [B] back to [A] loop...
Program 6                f ( x1 , x2 ) = x1 − x2www.youtube.com/vkedco       www.vkedco.blogspot.com
Program 6                         Y ← X1                         Z ← X2                  [C]    IF Z != 0 GOTO A          ...
Comments on Program 6   ●       Program works so long as the value of X1 >= X2   ●       Question: What happens if X1 < X2...
Function Computed by Program 6                             x1 − x2 if x1 ≥ x2            f ( x1 , x2 ) =                ...
Instructions and Programs   • An instruction is either a labeled or unlabeled statement.   • A program is a finite sequenc...
Definition: A State of a Program P   • A state of a program P is a set of equations of the form V = m,     where V is a va...
Example                         [ A1] IF X 1! = 0 GOTO B1                               Y ← Y+1                           ...
A Program State: Example   • The program on the previous slide (does not really matter what it     does in this context of...
A Program is a Finite Sequence of Primitive L                        Instructions                   1. Primitive L Instruc...
Program Snapshots   • A program P is assumed to have n ≥ 0 instructions   • A program snapshot, snapshot for short, is a p...
Initial Snapshot              Let P be a program in L and let              r1 ,..., rm be m given numbers. Let σ          ...
Terminal Snapshot   A snapshot (i, σ ) is terminal if i = n + 1www.youtube.com/vkedco      www.vkedco.blogspot.com
Reading Suggestions    ●        Ch. 2, Computability, Complexity, and Languages by        Davis, Weyuker, Sigalwww.youtube...
Feedback       Errors, comments to vladimir dot kulyukin at gmail dot       comwww.youtube.com/vkedco               www.vk...
Upcoming SlideShare
Loading in …5
×

Theory of Computation: Lecture 05: Programming Language L, Instructions, Macros, Program States & Snapshots

785 views

Published on

1) Programming language L
2) Primitive Instructions and Macros
3) Program States and Snapshots

Published in: Science, Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
785
On SlideShare
0
From Embeds
0
Number of Embeds
249
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Theory of Computation: Lecture 05: Programming Language L, Instructions, Macros, Program States & Snapshots

  1. 1. CS 5000: Theory of Computation Lecture 05 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
  2. 2. Outline ● Programming Language L ● Primitive Instructions and Macros ● Program States and Snapshotswww.youtube.com/vkedco www.vkedco.blogspot.com
  3. 3. The Macro Plan ● Once we have a set of primitive instructions, we can start building a repertoire of higher level computing patterns that we will call macros ● The plan is to develop the following macro one by one: – GOTO L – V ← V – V←0 – V ← V1 + V2 – V ← V1 * V2 – V ← V1 - V2www.youtube.com/vkedco www.vkedco.blogspot.com
  4. 4. Example  1 if x = 0 f ( x) =   x otherwisewww.youtube.com/vkedco www.vkedco.blogspot.com
  5. 5. Implementing f(x) in L: Program 1 [ A1 ] X 1 ← X 1 − 1 Y← Y+1 IF X 1 ≠ 0 GOTO A1 Or, if we do not want to use subscripts : [ A] X ← X − 1 Y← Y+1 IF X ≠ 0 GOTO Awww.youtube.com/vkedco www.vkedco.blogspot.com
  6. 6. Comments on Program 1 ● Program 1 copies the value of X into Y for X > 0 ● If X = 0, then Y = 1 at termination ● We can modify Program 1 slightly to make sure that the value of Y = 0 when X = 0www.youtube.com/vkedco www.vkedco.blogspot.com
  7. 7. Program 2 [A] IF X != 0 GOTO B Z←Z+1 IF Z != 0 GOTO E [B] X←X–1 Y←Y+1 Z←Z+1 IF Z != 0 GOTO Awww.youtube.com/vkedco www.vkedco.blogspot.com
  8. 8. Comments on Program 2 ● Question: What function does Program 2 compute? ● Answer: f(x) = x, because when it terminates, the value of Y is equal to the value of X ● You may have noticed this code block Z←Z+1 IF Z != 0 GOTO A ● This is an unconditional branch because control always goes to the instruction labeled with [A]www.youtube.com/vkedco www.vkedco.blogspot.com
  9. 9. Macro Expansion ● Consider the following block of L code for an unconditional branch: Z←Z+1 IF Z != 0 GOTO L ● From now on, we will replace it with GOTO L ● This is a case of macro expansion: we will assume that the L compiler/interpreter will always expand GOTO L into the above block of two primitive instructionswww.youtube.com/vkedco www.vkedco.blogspot.com
  10. 10. More Comments on Program 2 ● Program 2 does copy the value of X into Y ● But, what happens to the original value of X? ● The value of X is lost, because X = 0 when the program terminates: X is zeroed out ● Let us remedy this by writing another program in L that takes advantage of the new GOTO L macrowww.youtube.com/vkedco www.vkedco.blogspot.com
  11. 11. Program 3 [A] IF X != 0 GOTO B GOTO C [B] X←X–1 Y←Y+1 Z←Z+1 GOTO A [C] IF Z != 0 GOTO D GOTO E [D] Z←Z–1 X←X+1 GOTO Cwww.youtube.com/vkedco www.vkedco.blogspot.com
  12. 12. Comments on Program 3 ● The [A] to [B] back to [A] loop continues as long as X != 0 ● In this loop, the value of X is copied into Y and Z ● The loop is terminated when X = 0 and control goes to [C] ● In the [C] to [D] back to [C] loop, the value of Z is put back into X ● The loop terminates when Z = 0 and the program terminates by executing GOTO Ewww.youtube.com/vkedco www.vkedco.blogspot.com
  13. 13. More Comments on Program 3 ● Program 3 can be used as the foundation of another macro: V ← V ● This macro puts the value of variable V into the variable V ● In the macro expansion, V can be treated as X and V as Y ● At the end of the assignment, V has the value of V and V is not zeroed out but remains equal to its original valuewww.youtube.com/vkedco www.vkedco.blogspot.com
  14. 14. More Comments on Program 3 ● There is a subtle assumption behind the code for Program 3: it works correctly if the values of Y and Z are both equal to 0 before the program starts running ● If we expand this macro into a larger program, this assumption may not necessarily hold ● To remedy this situation, we have to be able to set the value of a variable to 0, i.e., we need the macro V ← 0www.youtube.com/vkedco www.vkedco.blogspot.com
  15. 15. Macro V ← 0 ● When we want to zero out the value of a variable, we need to loop on V ← V – 1 so long as V != 0 [L] V←V–1 IF V != 0 GOTO L ● Caveat: the value of L must be chosen to be different from all the labels in the program into which V ← 0 is expandedwww.youtube.com/vkedco www.vkedco.blogspot.com
  16. 16. A Safer Version of the V ← V Macro V←0 [A] IF V != 0 GOTO B GOTO C [B] V ← V – 1 V ←V+1 Z←Z+1 GOTO A [C] IF Z != 0 GOTO D GOTO E [D] Z ←Z–1 V ← V + 1 GOTO Cwww.youtube.com/vkedco www.vkedco.blogspot.com
  17. 17. Comments on the Safer Version of V ← V ● When the macro is expanded into the main program, the variable Z must be replaced by a local variable (another Z with an appropriate subscript) that does not occur anywhere in the main program expanded ● The same must be done with the A, B, C, and D labels: this can be done by keeping track of the subscripts for each label that have been used ● The label L must be replaced in such a way that the next instruction after the macro expansion is done if there is one is labeled [L]www.youtube.com/vkedco www.vkedco.blogspot.com
  18. 18. Program 4 f ( x1 , x2 ) = x1 + x2www.youtube.com/vkedco www.vkedco.blogspot.com
  19. 19. Program 4 Y ← X1 Z ← X2 [A] IF Z != 0 GOTO B GOTO E [B] Z←Z–1 Y←Y+1 GOTO Awww.youtube.com/vkedco www.vkedco.blogspot.com
  20. 20. Comments on Program 4 ● We place the value of X1 into Y and the value of X2 into Z ● Then we go into the [A] to [B] back to [A] loop in which the value of Z is decremented by 1 while the value of Y is incremented by 1 ● When the value of Z reaches 0, Y was incremented by 1 X2 times and the program terminates with the value of Y equal to X1 + X2 ● Program 4 gives us the V ← V1 + V2 macrowww.youtube.com/vkedco www.vkedco.blogspot.com
  21. 21. Program 5 f ( x1 , x2 ) = x1 ⋅ x2www.youtube.com/vkedco www.vkedco.blogspot.com
  22. 22. Program 5 Z2 ← X2 [A] IF Z2 != 0 GOTO B GOTO E [B] Z2 ← Z2 – 1 Z1 ← X1 + Y Y ← Z1 GOTO Awww.youtube.com/vkedco www.vkedco.blogspot.com
  23. 23. Comments on Program 5 ● We place the value of X2 into Z2 ● Then we go into the [A] to [B] back to [A] loop in which the value of Z2 is decremented by 1 while the value of Y is incremented by X1 each time Z2 is decremented by 1 ● When the value of Z2 reaches 0, Y was incremented by X1 X2 times and the program terminates with the value of Y equal to X1 * X2 ● Program 5 gives us the V ← V1 * V2 macrowww.youtube.com/vkedco www.vkedco.blogspot.com
  24. 24. Program 6 f ( x1 , x2 ) = x1 − x2www.youtube.com/vkedco www.vkedco.blogspot.com
  25. 25. Program 6 Y ← X1 Z ← X2 [C] IF Z != 0 GOTO A GOTO E [A] IF Y != 0 GOTO B GOTO A [B] Y←Y-1 Z←Z-1 GOTO Cwww.youtube.com/vkedco www.vkedco.blogspot.com
  26. 26. Comments on Program 6 ● Program works so long as the value of X1 >= X2 ● Question: What happens if X1 < X2? ● Answer: Y reaches 0 before Z, at which point Program 6 goes into an infinite [A] loop ● Program 6 is an example of a program that implements a partial function: a function that is not defined on some inputs ● So, which function does Program 6 compute?www.youtube.com/vkedco www.vkedco.blogspot.com
  27. 27. Function Computed by Program 6  x1 − x2 if x1 ≥ x2 f ( x1 , x2 ) =  ↑ if x1 < x2www.youtube.com/vkedco www.vkedco.blogspot.com
  28. 28. Instructions and Programs • An instruction is either a labeled or unlabeled statement. • A program is a finite sequence of instructions. • A program with no instructions is called the empty program.www.youtube.com/vkedco www.vkedco.blogspot.com
  29. 29. Definition: A State of a Program P • A state of a program P is a set of equations of the form V = m, where V is a variable and m is a number • The set must have exactly one equation for every variable that occurs in P • Corollary 1: The state may have equations for variables that do not occur in P • Corollary 2: It is irrelevant whether a state can or cannot be achieved by Pwww.youtube.com/vkedco www.vkedco.blogspot.com
  30. 30. Example [ A1] IF X 1! = 0 GOTO B1 Y ← Y+1 Z1 ← Z1 + 1 IF Z1! = 0 GOTO E1 [ B1] X 1 ← X 1 − 1 Y ← Y+1 Z1 ← Z1 + 1 IF Z1! = 0 GOTO C1 [C1] IF X 1! = 0 GOTO B1 Z1 ← Z1 + 1 IF Z1! = 0 GOTO E1www.youtube.com/vkedco www.vkedco.blogspot.com
  31. 31. A Program State: Example • The program on the previous slide (does not really matter what it does in this context of program states) contains variables X1, Z1, and Y • { X1 = 2, Z1 = 3, Y = 5 } is a legal state • { X1 = 2, X2 = 4, Z1 = 3, Y = 5 } is also a legal state, although it contains a variable that does not occur in the program; however, it does satisfy the definition • { X1 = 2, Y = 5 } is not a state, because it does not define the value of Z1 • { X1 = 2, Z1 = 3, Y = 5, X1 = 5 } is not a state, because it has two equations for X1www.youtube.com/vkedco www.vkedco.blogspot.com
  32. 32. A Program is a Finite Sequence of Primitive L Instructions 1. Primitive L Instruction 2. Primitive L Instruction 3. Primitive L Instruction 4. Primitive L Instruction 5. Primitive L Instruction … n. Primitive L Instructionwww.youtube.com/vkedco www.vkedco.blogspot.com
  33. 33. Program Snapshots • A program P is assumed to have n ≥ 0 instructions • A program snapshot, snapshot for short, is a pair (i,σ) where i is the number of the instruction to be executed and σ is a state of P • The values of i are in [1, n+1]www.youtube.com/vkedco www.vkedco.blogspot.com
  34. 34. Initial Snapshot Let P be a program in L and let r1 ,..., rm be m given numbers. Let σ consist of two sets of equations : 1. X 1 = r1 ,..., X m = rm , Y = 0; 2. V = 0, for any other V in P. Then (1, σ ) is the initial snapshot.www.youtube.com/vkedco www.vkedco.blogspot.com
  35. 35. Terminal Snapshot A snapshot (i, σ ) is terminal if i = n + 1www.youtube.com/vkedco www.vkedco.blogspot.com
  36. 36. Reading Suggestions ● Ch. 2, Computability, Complexity, and Languages by Davis, Weyuker, Sigalwww.youtube.com/vkedco www.vkedco.blogspot.com
  37. 37. Feedback Errors, comments to vladimir dot kulyukin at gmail dot comwww.youtube.com/vkedco www.vkedco.blogspot.com

×