• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Theory of Computation (Fall 2013): Programming Language L, Primitive Instructions & Macros, Program States, Snapshots, & Computations
 

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

on

  • 481 views

 

Statistics

Views

Total Views
481
Views on SlideShare
331
Embed Views
150

Actions

Likes
0
Downloads
1
Comments
0

19 Embeds 150

http://vkedco.blogspot.com 83
http://www.vkedco.blogspot.com 19
http://www.blogger.com 13
http://vkedco.blogspot.it 7
http://vkedco.blogspot.fr 6
http://vkedco.blogspot.in 5
http://vkedco.blogspot.ie 3
http://vkedco.blogspot.com.ar 2
http://reader.aol.com 2
http://vkedco.blogspot.co.il 1
http://www.vkedco.blogspot.nl 1
http://vkedco.blogspot.nl 1
http://www.vkedco.blogspot.it 1
http://vkedco.blogspot.de 1
http://www.vkedco.blogspot.in 1
http://vkedco.blogspot.ca 1
http://vkedco.blogspot.tw 1
http://vkedco.blogspot.gr 1
http://vkedco.blogspot.com.br 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Theory of Computation (Fall 2013): Programming Language L, Primitive Instructions & Macros, Program States, Snapshots, & Computations Theory of Computation (Fall 2013): Programming Language L, Primitive Instructions & Macros, Program States, Snapshots, & Computations Presentation Transcript

    • Theory of Computation Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots Vladimir Kulyukin
    • 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, 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
    • 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
    • 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
    • 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
    • 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.
    • 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
    • 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
    • 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.
    • 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 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
    • 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 : [ A] X  X  1 Y  Y 1 IF X  0 GOTO A
    • 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
    • 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 because control always goes to the instruction labeled with [A]
    • 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
    • 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
    • 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
    • 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 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
    • 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
    • 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
    • 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
    • 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
    • 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 ]
    • 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 [ 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
    • 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 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
    • 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? 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?
    • 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?
    • 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 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 ) 
    • 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 ) 
    • 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
    • Program States & Snapshots
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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]
    • 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.
    • 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