Theory of Computation
Programming Language L, Primitive
Instructions & Macros, Partial Functions,
Program States & Snapsho...
Outline
●
●
●
●

Review of Programming Language L
L Macros
Partial Functions
Program States & Snapshots
Review of Programming Language L
Choice of Formalism
●

●

●

We will use the programming language L developed in
Chapter 2 of Computability, Complexity, a...
L’s Tokens
Input variables : X 1 , X 2 , X 3 ,...
Local variables : Z 1 , Z 2 , Z 3 ,...
Output variable : Y
Labels : A1 ,...
L’s Basic Instructions (Primitives)
1. V  V  1 (increment)
2. V  V  1 (decrement)
3. V  V (no - opp)
4. IF V  0 GOTO...
Instruction V ← V + 1
●

These instructions are primitives:





●

X1  X1 + 1
Z10  Z10 + 1
YY+1
X102  X102 + 1

T...
L’s Labeled Primitives
1. LV  V  1 (increment)
2. LV  V  1 (decrement)
3. LV  V (no - opp)

4. L IF V  0 GOT...
Instruction V  V - 1
●

These instructions are primitives:





●

X1  X1 - 1
Z10  Z10 - 1
YY-1
X102  X102 - 1

T...
Instruction V  V
●

These instructions are primitives:





●

X1  X1
Z10  Z10
X120  X120
YY

These instructions ...
L’s Labeled Primitives
1. LV  V  1 (increment)
2. LV  V  1 (decrement)
3. LV  V (no - opp)

4. L IF V  0 GOT...
Labeled Primitives: Examples
[A1]
[B1]
[C10]
[E1]
[D101]

X1  X1 + 1
X23  X23 – 1
Z12  Z12 + 1
YY
IF X1 != 0 GOTO E1
L Macros
The Macro Plan
●

●

Once we have a set of primitive instructions, we can start
building a repertoire of higher level comp...
Example

1 if x  0
f ( x)  
x otherwise

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...
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 ca...
Program 2
[A]

[B]

IF X != 0 GOTO B
Z←Z+1
IF Z != 0 GOTO E
X←X–1
Y←Y+1
Z←Z+1
IF Z != 0 GOTO A
Comments on Program 2
●

You may have noticed this code block
Z←Z+1
I Z != 0 GOTO A
F

●

This is an unconditional branch ...
Macro Expansion
●

Consider the following block of L code for an
unconditional branch:
Z←Z+1

IF Z != 0 GOTO <LABEL>
●

Fr...
Macro Expansion
●

Consider the following block of L code for an
unconditional branch:
Z← Z+1

IF Z != 0 GOTO <LABEL>
●

F...
More Comments on Program 2
●
●
●

●

Program 2 does copy the value of X into Y
But, what happens to the original value of ...
Program 3
[A]
[B]

[C]
[D]

IF X != 0 GOTO B
GOTO C
X←X–1
Y←Y+1
Z←Z+1
GOTO A
IF Z != 0 GOTO D
GOTO E
Z←Z–1
X←X+1
GOTO C
Comments on Program 3
●

●
●
●

●

The [ A ] to [ B ] back to [ A ] loop continues as long as X
!= 0
In this loop, the val...
More Comments on Program 3
●

●

●

●

Program 3 can be used as the foundation of another
macro: V ← V'
This macro puts th...
More Comments on Program 3
●

●

●

There is a subtle assumption behind the code for
Program 3: it works correctly if the ...
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
[A]

V←V–1...
Safer Version of V ← V' Macro
[A]
[B]

[C]
[D]

V←0
IF V' != 0 GOTO B
GOTO C
V' ← V' – 1
V ←V+1
Z←Z+1
GOTO A
IF Z != 0 GOT...
Comments on the Safer Version of V ← V'
●

●

●

When the macro is expanded into the main program, the variable
Z must be ...
Program 4

f  x1 , x2   x1  x2
Program 4

[A]
[B]

Y ← X1
Z ← X2
IF Z != 0 GOTO B
GOTO E
Z←Z–1
Y←Y+1
GOTO A
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 ...
Program 5

f  x1 , x2   x1  x2
Program 5
[A]
[B]

Z2 ← X2
IF Z2 != 0 GOTO B
GOTO E
Z2 ← Z2 – 1
Z1 ← X1 + Y
Y ← Z1
GOTO A
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...
Program 6

f  x1 , x2   x1  x2
Program 6

[C]
[A]
[B]

Y ← X1
Z ← X2
IF Z != 0 GOTO A
GOTO E
IF Y != 0 GOTO B
GOTO A
Y←Y-1
Z←Z-1
GOTO C
Comments on Program 6
●
●
●

●

●

Program works so long as the value of X1 >= X2
Question: What happens if X1 < X2?
Answe...
Comments on Program 6
●
●
●

●

●

Program works so long as the value of X1 >= X2
Question: What happens if X1 < X2?
Answe...
Partial Functions
Function Computed by Program 6

 x1  x2 if x1  x2
f  x1 , x2   
if x1  x2

Partial Functions
1. (a, b)  f and (a, c)  f  b  c
2. A partial function on S is a function whose domain is a
subset o...
Partial Functions
1. (a, b)  f and (a, c)  f  b  c
2. A partial function on S is a function whose domain is a
subset o...
Partial Functions
1. A partial function on S whose domain is S is called total
2.  (the empty set) is a partial function ...
Program States & Snapshots
Instructions and Programs
• An instruction is either a labeled or
unlabeled statement
• A program is a finite sequence of
...
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...
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! ...
A Program State: Example
• The program on the previous slide (in this context, it does not
really matter what it does in t...
A Program is a Finite Sequence of Primitive L Instructions
1. Primitive L Instruction
2. Primitive L Instruction
3. Primit...
Program Snapshots
• A program P is assumed to have n ≥ 0 instructions
• A program snapshot is a pair (i ,σ) where i is the...
Initial Snapshot
Let P be a program in L and let
r1 ,..., rm be m given numbers. Let 
consist of two sets of equations :
...
Terminal Snapshot

A snapshot (i,  ) is terminal if i  n  1
and  is a legal program state
Reading Suggestions
●

Ch. 2, Computability, Complexity, and Languages by
Davis, Weyuker, Sigal
Upcoming SlideShare
Loading in …5
×

Theory of Computation: Programming Language L, Primitive Instructions & Macros, Program States, Snapshots, & Computations

576 views

Published on

Programming Language L, Primitive Instructions & Macros, Program States, Snapshots, & Computations

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

  • Be the first to like this

No Downloads
Views
Total views
576
On SlideShare
0
From Embeds
0
Number of Embeds
177
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Theory of Computation: Programming Language L, Primitive Instructions & Macros, Program States, Snapshots, & Computations

  1. 1. Theory of Computation Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots Vladimir Kulyukin
  2. 2. Outline ● ● ● ● Review of Programming Language L L Macros Partial Functions Program States & Snapshots
  3. 3. Review of Programming Language L
  4. 4. Choice of Formalism ● ● ● We will use the programming language L developed in Chapter 2 of Computability, Complexity, and Languages by Davis, Weyuker, and Sigal While L is a theoretical construct, it can be thought of as a higher level assembly language Since L is a programming language, it is more appealing to the programmatically inclined than more formal constructs such as λ-calculus or Turing machine
  5. 5. L’s Tokens Input variables : X 1 , X 2 , X 3 ,... Local variables : Z 1 , Z 2 , Z 3 ,... Output variable : Y Labels : A1 , B1 , C1 , D1 , E1 , A2 ,... If the subscript is omitted, it is assumed to be 1. For example, X is the same as X 1 Z is the same as Z 1 A is the same as A1
  6. 6. L’s Basic Instructions (Primitives) 1. V  V  1 (increment) 2. V  V  1 (decrement) 3. V  V (no - opp) 4. IF V  0 GOTO L (cond. branch) NOTE : In instructions 1, 2, 3 the variables on the left - hand side and the right - hand side are the same
  7. 7. Instruction V ← V + 1 ● These instructions are primitives:     ● X1  X1 + 1 Z10  Z10 + 1 YY+1 X102  X102 + 1 These instructions are NOT primitives:    X1  X10 + 1 Z10  X1 + 1 Y  X102 + 1
  8. 8. L’s Labeled Primitives 1. LV  V  1 (increment) 2. LV  V  1 (decrement) 3. LV  V (no - opp) 4. L IF V  0 GOTO L (cond. branch) NOTE : At the beginning of the line the label is in square brackets. However, in conditional dispatches the square brackets are dropped after GOTO.
  9. 9. Instruction V  V - 1 ● These instructions are primitives:     ● X1  X1 - 1 Z10  Z10 - 1 YY-1 X102  X102 - 1 These instructions are NOT primitives:    X1  X10 - 1 Z10  X1 - 1 Y  X102 - 1
  10. 10. Instruction V  V ● These instructions are primitives:     ● X1  X1 Z10  Z10 X120  X120 YY These instructions are NOT primitives:    X1  Y X120  Z10 Z10  X1
  11. 11. L’s Labeled Primitives 1. LV  V  1 (increment) 2. LV  V  1 (decrement) 3. LV  V (no - opp) 4. L IF V  0 GOTO L (cond. branch) NOTE : At the beginning of the line the label is in square brackets. However, in conditional dispatches the square brackets are dropped after GOTO.
  12. 12. Labeled Primitives: Examples [A1] [B1] [C10] [E1] [D101] X1  X1 + 1 X23  X23 – 1 Z12  Z12 + 1 YY IF X1 != 0 GOTO E1
  13. 13. L Macros
  14. 14. 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 - V2
  15. 15. Example 1 if x  0 f ( x)   x otherwise 
  16. 16. 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 A
  17. 17. 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 = 0 We make the modification in Program 2
  18. 18. Program 2 [A] [B] IF X != 0 GOTO B Z←Z+1 IF Z != 0 GOTO E X←X–1 Y←Y+1 Z←Z+1 IF Z != 0 GOTO A
  19. 19. Comments on Program 2 ● You may have noticed this code block Z←Z+1 I Z != 0 GOTO A F ● This is an unconditional branch because control always goes to the instruction labeled with [A]
  20. 20. Macro Expansion ● Consider the following block of L code for an unconditional branch: Z←Z+1 IF Z != 0 GOTO <LABEL> ● From now on, we will replace it with GOTO <LABEL> ● This is a case of macro expansion: we will assume that the L compiler/interpreter will always expand GOTO <LABEL> into the above block of two primitive instructions
  21. 21. Macro Expansion ● Consider the following block of L code for an unconditional branch: Z← Z+1 IF Z != 0 GOTO <LABEL> ● From now on, we will replace it with GOTO <LABEL> ● This is a case of macro expansion: we will assume that the L compiler/interpreter will always expand GOTO <LABEL> into the above block of two primitive instructions
  22. 22. 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 even when its value is above zero when computation starts Let us remedy this by writing another program in L that takes advantage of the new GOTO <LABEL> macro
  23. 23. Program 3 [A] [B] [C] [D] IF X != 0 GOTO B GOTO C X←X–1 Y←Y+1 Z←Z+1 GOTO A IF Z != 0 GOTO D GOTO E Z←Z–1 X←X+1 GOTO C
  24. 24. 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 terminates 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 E
  25. 25. 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 value
  26. 26. 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 ← 0
  27. 27. 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 [A] V←V–1 IF V != 0 GOTO <A> ● Caveat: the value of <A> must be chosen to be different from all the other labels in the program into which V ← 0 is expanded
  28. 28. Safer Version of V ← V' Macro [A] [B] [C] [D] V←0 IF V' != 0 GOTO B GOTO C V' ← V' – 1 V ←V+1 Z←Z+1 GOTO A IF Z != 0 GOTO D GOTO E Z ←Z–1 V' ← V' + 1 GOTO C
  29. 29. 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 A must be replaced in such a way that the next instruction after the macro expansion is done if there is one is labeled [ A ]
  30. 30. Program 4 f  x1 , x2   x1  x2
  31. 31. Program 4 [A] [B] Y ← X1 Z ← X2 IF Z != 0 GOTO B GOTO E Z←Z–1 Y←Y+1 GOTO A
  32. 32. 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 By the time the value of Z reaches 0, Y has been 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 macro
  33. 33. Program 5 f  x1 , x2   x1  x2
  34. 34. Program 5 [A] [B] Z2 ← X2 IF Z2 != 0 GOTO B GOTO E Z2 ← Z2 – 1 Z1 ← X1 + Y Y ← Z1 GOTO A
  35. 35. 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 has been incremented by the value of X1 X2 times and the program terminates with the value of Y equal to X1 * X2 Program 5 gives us the V ← V1 * V2 macro
  36. 36. Program 6 f  x1 , x2   x1  x2
  37. 37. Program 6 [C] [A] [B] Y ← X1 Z ← X2 IF Z != 0 GOTO A GOTO E IF Y != 0 GOTO B GOTO A Y←Y-1 Z←Z-1 GOTO C
  38. 38. 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?
  39. 39. 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?
  40. 40. Partial Functions
  41. 41. Function Computed by Program 6  x1  x2 if x1  x2 f  x1 , x2    if x1  x2 
  42. 42. Partial Functions 1. (a, b)  f and (a, c)  f  b  c 2. A partial function on S is a function whose domain is a subset of S 3. g (n)  n , g : N  N . Then g is a partial function on N , because the domain of g is the set of perfect squares, not all natural numbers 4. If f is a partial function on S and a is in the domain of f , then f (a )  5. If f is a partial function on S and a is not in the domain of f , then f (a ) 
  43. 43. Partial Functions 1. (a, b)  f and (a, c)  f  b  c 2. A partial function on S is a function whose domain is a subset of S 3. g (n)  n , g : N  N . Then g is a partial function on N , because the domain of g is the set of perfect squares, not all natural numbers 4. If f is a partial function on S and a is in the domain of f , then f (a )  5. If f is a partial function on S and a is not in the domain of f , then f (a ) 
  44. 44. Partial Functions 1. A partial function on S whose domain is S is called total 2.  (the empty set) is a partial function on any set that is nowhere defined
  45. 45. Program States & Snapshots
  46. 46. 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
  47. 47. 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 (this is very useful for passing meta information) • Corollary 2: It is irrelevant whether a state can or cannot be achieved by P
  48. 48. 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 E1
  49. 49. A Program State: Example • The program on the previous slide (in this context, it 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 X1
  50. 50. 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 Instruction
  51. 51. Program Snapshots • A program P is assumed to have n ≥ 0 instructions • A program snapshot 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]
  52. 52. 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.
  53. 53. Terminal Snapshot A snapshot (i,  ) is terminal if i  n  1 and  is a legal program state
  54. 54. Reading Suggestions ● Ch. 2, Computability, Complexity, and Languages by Davis, Weyuker, Sigal

×