Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial...

52
Theory of Computation Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots Vladimir Kulyukin

description

 

Transcript of Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial...

Page 1: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Theory of Computation

Programming Language L, Primitive Instructions & Macros, Partial Functions,

Program States & Snapshots

Vladimir Kulyukin

Page 2: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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

Page 3: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Review of Programming Language L

Page 4: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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

Page 5: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

L’s Tokens

1

1

1

211111

321

321

as same theis

as same theis

as same theis

example,For 1. be toassumed isit omitted, issubscript theIf

,...,,,,, :Labels

:iableOutput var

,...,, : variablesLocal

,...,, :ablesInput vari

AA

ZZ

XX

AEDCBA

Y

ZZZ

XXX

Page 6: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

L’s Basic Instructions (Primitives)

same theare side hand-right theand

side hand-left on the variables the3 2, 1, nsinstructioIn :NOTE

branch) (cond. GOTO 0 IF 4.

opp)-(no .3

)(decrement 1 .2

)(increment 1 .1

LV

VV

VV

VV

Page 7: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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

Page 8: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

L’s Labeled Primitives

GOTO.after

dropped are brackets square thedispatches lconditionain However,

brackets. squarein is label theline theof beginning At the :NOTE

branch) (cond. GOTO 0 IF L 4.

opp)-(no L .3

)(decrement 1 L .2

)(increment 1 L .1

LV

VV

VV

VV

Page 9: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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

Page 10: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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

Page 11: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

L’s Labeled Primitives

GOTO.after

dropped are brackets square thedispatches lconditionain However,

brackets. squarein is label theline theof beginning At the :NOTE

branch) (cond. GOTO 0 IF L 4.

opp)-(no L .3

)(decrement 1 L .2

)(increment 1 L .1

LV

VV

VV

VV

Page 12: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Labeled Primitives: Examples

[A1] X1 X1 + 1[B1] X23 X23 – 1[C10] Z12 Z12 + 1[E1] Y Y[D101] IF X1 != 0 GOTO E1

Page 13: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

L Macros

Page 14: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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

Page 15: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Example

otherwise

0 if 1)(

x

xxf

Page 16: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Implementing f(x) in L: Program 1

AX

YY

XXA

AX

YY

XXA

GOTO 0 IF

1

1 ][

:subscripts use onot want t do weif Or,

GOTO 0 IF

1

1 ][

11

111

Page 17: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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

Page 18: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Program 2

[A] IF X != 0 GOTO B Z ← Z + 1 IF Z != 0 GOTO E[B] X ← X – 1

Y ← Y + 1Z ← Z + 1IF Z != 0 GOTO A

Page 19: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Comments on Program 2

● You may have noticed this code blockZ ← Z + 1

IF Z != 0 GOTO A● This is an unconditional branch because control always goes

to the instruction labeled with [A]

Page 20: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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 withGOTO <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

Page 21: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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 withGOTO <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

Page 22: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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

Page 23: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Program 3[A] IF X != 0 GOTO B GOTO C[B] X ← X – 1

Y ← Y + 1Z ← Z + 1GOTO A

[C] IF Z != 0 GOTO DGOTO E

[D] Z ← Z – 1X ← X + 1GOTO C

Page 24: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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 X has its original value● The program terminates by executing GOTO E

Page 25: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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

Page 26: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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

Page 27: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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

Page 28: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Safer Version of V ← V' Macro

V ← 0[A] IF V' != 0 GOTO B GOTO C[B] V' ← V' – 1

V ← V + 1Z ← Z + 1GOTO A

[C] IF Z != 0 GOTO DGOTO E

[D] Z ← Z – 1V' ← V' + 1GOTO C

Page 29: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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]

Page 30: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Program 4

2121, xxxxf

Page 31: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Program 4

Y ← X1Z ← X2

[A] IF Z != 0 GOTO B GOTO E[B] Z ← Z – 1

Y ← Y + 1GOTO A

Page 32: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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

Page 33: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Program 5

2121, xxxxf

Page 34: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Program 5

Z2 ← X2[A] IF Z2 != 0 GOTO B GOTO E[B] Z2 ← Z2 – 1

Z1 ← X1 + YY ← Z1GOTO A

Page 35: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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

Page 36: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Program 6

2121, xxxxf

Page 37: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Program 6

Y ← X1Z ← X2

[C] IF Z != 0 GOTO A GOTO E[A] IF Y != 0 GOTO B

GOTO A[B] Y ← Y - 1

Z ← Z - 1GOTO C

Page 38: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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?

Page 39: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Partial Functions

Page 40: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Function Computed by Program 6

21

2121

21 if

if ,

xx

xxxxxxf

Page 41: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Partial Functions

)(then ,

ofdomain in thenot is and on function partial a is If 5.

)(then

, ofdomain in the is and on function partial a is If 4.

numbers natural

allnot squares,perfect ofset theis ofdomain thebecause

,on function partial a is Then .:,)( 3.

ofsubset

a isdomain hosefunction w a is on function partialA 2.

),( and ),( 1.

aff

aSf

af

faSf

g

NgNNgnng

S

S

cbfcafba

Page 42: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Partial Functions

defined

nowhere isset that any on function partial a is set)empty (the .2

totalcalled is isdomain whoseon function partialA 1.

SS

Page 43: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Program States & Snapshots

Page 44: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, 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

Page 45: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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

Page 46: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Example

1 GOTO 0! 1 IF

11 1

1 GOTO 0! 1 IF ]1[

1 GOTO 0! 1 IF

111

1

111 ]1[

1 GOTO 0! 1 IF

1Z1 Z1

1

1 GOTO 0! 1 IF ]1[

EZ

ZZ

BXC

CZ

ZZ

YY

XXB

EZ

YY

BXA

Page 47: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

A Program State: Example• The program on the previous slide (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

Page 48: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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

Page 49: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

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]

Page 50: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Initial Snapshot

snapshot. initial theis )(1,

.in other any for ,0 .2

;0,,..., 1.

:equations of sets twoofconsist

Let numbers.given be ,...,

let and in program a be Let

11

1

Then

PVV

YrXrX

mrr

LP

mm

m

Page 51: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Terminal Snapshot

state. program legal a is and

1 if terminalis ),(snapshot A

nii

Page 52: Theory of Computation (Fall 2014): Programming Language L, Primitive Instructions & Macros, Partial Functions, Program States & Snapshots

Reading Suggestions

● Ch. 2, Computability, Complexity, and Languages by Davis, Weyuker, Sigal