52
Theory of Computation Step Counter Theorem & Program Simulation Vladimir Kulyukin www.vkedco.blogspot.com

Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Embed Size (px)

Citation preview

Page 1: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Theory of Computation

Step Counter Theorem & Program Simulation

Vladimir Kulyukin

www.vkedco.blogspot.com

Page 2: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Outline

● Review of Universality Theorem● Program Simulation● Step-Counter Theorem as Theoretical Basis of Program

Simulation

Page 3: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Review of Universality Theorem

Page 4: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Review: Universal Program

C

UlZlK

Ul

SP

Ul

Ul

pP

ZrU

KZLtK

K

pS

XZ

iZLti

Ur

K

Xn

ii

n

i

GOTO

2min

M GOTO 2)( IF

N GOTO | IF

A GOTO 1)( IF

N GOTO 0)( IF

E GOTO 01 IF C

1

1

)(

1)(

12

1

1

GOTO

1

GOTO

/

SYE

C

KKN

PSSA

N

PSSM

Part 01 Part 02

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

Page 5: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Review: Theorem 3.1, Ch. 04

.computablepartially is ,,...,function the

,0each For :Theorem)ity (Universal 3.1 Theorem

1)( yxxΦ

n

nn

Page 6: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Back to The Big Picture

GÖDEL Coder

GÖDEL Decoder

L Program Number Universal Program

Number

???

GÖDEL Coder = CompilerGÖDEL Decoder = Reverse CompilerUniversal Program = Operating System/VM/Interpreter

Universality Theorem

Page 7: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Enumeration of Partially Computable Functions

. variables of functions computablepartially all enumerates

,...3,,...,,2,,...,,1,,...,,0,,...,

sequence the,0each For

1)(

1)(

1)(

1)(

n

xxΦxxΦxxΦxxΦ

n

nn

nn

nn

nn

Page 8: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Remark on Notation

yxΦxΦxΦ

yxxΦxxΦ

yy

nn

nn

y

,,

:omittedoften is 1t Superscrip

,,...,,...,

)1(

1)(

1)(

Page 9: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Program Simulation

Page 10: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

On To Program Simulation

● Suppose a universal program runs a program on some inputs● We can expect two mutually exclusive outcomes:

The program terminates (halts) after some finite number of steps and produces an output (a number);

The program does not terminate at all (e.g., it has an infinite loop)● In many situations, we would like to know if a program terminates

within a given number of steps without having to run the program● Enter Program Simulation

Page 11: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

The New Big Picture

GÖDEL Coder

GÖDEL Decoder

L Program Number Universal Program

Number

???

GÖDEL Coder = CompilerGÖDEL Decoder = Reverse CompilerUniversal Program = Operating System/VM/Interpreter

Universality Theorem

Simulator

Step-Counter Theorem

Page 12: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Execution vs. Simulation

● Once a universal program (e.g., an operating system/interpreter) starts executing the program, it cannot stop

● If a program has an infinite loop, because there is a bug or because it is working on a problem with no solution, there is no way of stopping the program

● Simulation enables us to run a program for a specific number of steps: many IDEs have code steppers that allow the programmer to step through the program one instruction at a time

Page 13: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Where Else Can Program Simulation Be Used?

● Many AI problems can be formally represented in terms of states, operators, goal tests, and path costs

● A state is a formal representation of the world in which a problem solver operates

● An operator is an action that the problem solver can carry out in the world.

● A goal test is a predicate that returns true when the problem solver’s goal is satisfied in a given state of the world and false otherwise

● A path cost is the cost of applying a sequence of operators that transforms the start state to a goal state

Page 14: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Simulation Through Successive Snapshots

● Program snapshots can be used to simulate computations

● Suppose we have an L program P and a given snapshot

● We have defined the successor snapshot function● Basic idea: Use the successor function to compute a

sequence of snapshots that starts at a given snapshot and check if the last snapshot in that sequence is terminal

Page 15: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Review: Snapshots

.0or 1 if terminalis

program. theof state a is

executed; be n toinstructio theofnumber theis

program; somein nsinstructio ofnumber theis

where,11 ),,( as defined is Snapshot

inis

i

Ln

niis

Page 16: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step-Counter Theorem as Theoretical Basis of Program Simulation

Page 17: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

STP Predicate: Two Definitions

otherwise. 0

;.,...,

inputs with beginning ,1length of

program ofn computatio a is thereif 1

,,,...,

otherwise. 0

steps;fewer or in

,..., inputson halts program if 1

,,,...,

11

)(

1

1)(

nn

n

n

nn

xx

t

y

tyxxSTP

t

xxy

tyxxSTP

Page 18: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Theorem 3.2 (Ch. 4): STP Theorem

recursive. primitive is

,,,..., predicate the,0For 1)( tyxxSTPn n

n

Page 19: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Proof Outline● Step 1: Encoding a program state● Step 2: Encoding the initial snapshot● Step 3: Extracting an instruction components● Step 4: Defining conditions of instruction simulation:

4a) When to simulate skipping instruction; 4b) When to simulate an increment; 4c) When to simulate a decrement; 4d) When to simulate a branch

● Step 5: Simulating the successor snapshot● Step 6: Figuring out when to terminate● Step 7: Simulating the n-th snapshot● Step 8: Putting it all together

Page 20: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 1: Encoding a Program State

.,...,0,,0

where,, as encoded is ,

21 xxs

sii

Page 21: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 2: Initial Snapshot

n

i

Xin

n ipxx1

21)( ,1,...,INIT

Page 22: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 3: Extracting Instruction Components

label. GOTO theis This // 21,GTLBL 4.

n type.instructio is This// 1,INSTR 3.

variable.sn'instructio is This //11,VAR 2.

label. sn'instructio is This // 1,LABEL 1.

:have Then we .,,1 So, code.

source its ofnumber theis 1 Thus, number. program a be Let

i

i

i

i

i

yrlyi

yrlyi

yrryi

ylyi

cbay

yy

Page 23: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Proof So Far• Step 1: Encoding a program state• Step 2: Encoding the initial snapshot• Step 3: Extracting an instruction components• Step 4: Defining conditions of instruction simulation:

– 4a) When to simulate skipping instruction;– 4b) When to simulate an increment;– 4c) When to simulate a decrement;– 4d) When to simulate a branch

• Step 5: Simulating the successor snapshot• Step 6: Figuring out when to terminate• Step 7: Simulating the n-th snapshot• Step 8: Putting it all together

Page 24: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 4a: When to Skip

• Let y be a program number. Then y+1 is the source code number. Let x = <i,s> be the current snapshot

• Question: When should we skip instruction i?• Answer hint: There are 3 conditions

Page 25: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 4a: When to Skip

Answer:1. When instruction i is of type V ← V and i is less than

or equal to the length of the program2. When instruction i is of type V ← V – 1 and V = 03. When instruction i is of type IF V != 0 GOTO L & V = 0

Page 26: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 4a: Condition 1

1 & 0,INSTR :1Condition Formalized

program theoflength the

and type theof is n Instructio :1Condition

number. program a is snapshot, a be ,Let

yLtxlyxl

i

VVi

ysix

Page 27: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 4a: Condition 2

s

V

xrpyxl

V VVi

ysix

yxlVAR

state thedivide

not does prime ingcorrespond theifonly and if 0 :Recall

| & 2,INSTR :2Condition Formalized

0 and 1 type theof is n Instructio :2Condition

number. program a is snapshot, a be ,Let

,

Page 28: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 4a: Condition 3

xrpyxl

V

LVi

ysix

yxlVAR | & 2,INSTR :3Condition Formalized

0 and

GOTO 0! IF type theof isn Instructio :3Condition

number. program a is snapshot, a be ,Let

,

Page 29: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 4a: Conditions 2 & 3

xrpyxl

xrpyxl

V

LVi

xrpyxl

V V - V i

ysix

yxlVAR

yxlVAR

yxlVAR

| & 2,INSTR :Combined 3&2 Conditions

| & 2,INSTR

.0 and

GOTO 0! IF type theof isn Instructio :3Condition

| & 2,INSTR

.0 and 1 type theof is n Instructio :2Condition

number. program a is snapshot, a be ,Let

,

,

,

Page 30: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 4a: Summary

as" defined" means

| & 2,INSTR

1 & 0,INSTR

,SKIP

n?instructionext theskip weshouldWhen

number. program a is snapshot, a be ,Let

,

xrpyxl

yLtxlyxl

yx

ysix

yxlVAR

Page 31: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Proof So Far• Step 1: Encoding a program state• Step 2: Encoding the initial snapshot• Step 3: Extracting an instruction components• Step 4: Defining conditions of instruction simulation:

– 4a) When to simulate skipping instruction;– 4b) When to simulate an increment;– 4c) When to simulate a decrement;– 4d) When to simulate a branch.

● Step 5: Simulating the successor snapshot● Step 6: Figuring out when to terminate● Step 7: Simulating the n-th snapshot● Step 8: Putting it all together

Page 32: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 4b: When to Simulate an Increment

predicate. a is ,INCR that Note

.1,INSTR,INCR

.1

typeof is n instructiowhen increment an simulate We

state. program a is snapshot, a be ,Let

yx

yxlyx

V V

i

ysix

Page 33: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 4c: When to Simulate a Decrement

predicate. a is ,DECR that Note

;|& 2,INSTR,DECR

0

and 1 typeof is n instructiowhen decrement We

state. program a is snapshot, a be ,Let

,

yx

xrpyxlyx

V

V - V i

ysix

yxlVAR

Page 34: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 4d: When to Simulate a Branch

yxlyii

xrpyxl

yx

L V

L V i

ysix

yLt

yxlVAR

,GTLBL,LABEL

& |& 2,INSTR

,BRANCH

exists. label and 0

and GOTO 0! IF typeof is n instruction branch whe We

state. program a is snapshot, a be ,Let

1

,

Page 35: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Proof So Far

• Step 1: Encoding a program state• Step 2: Encoding the initial snapshot• Step 3: Extracting an instruction components• Step 4: Defining conditions of instruction simulation:

– 4a) When to simulate skipping instruction;– 4b) When to simulate an increment;– 4c) When to simulate a decrement;– 4d) When to simulate a branch.

● Step 5: Simulating the successor snapshot● Step 6: Figuring out when to terminate● Step 7: Simulating the n-th snapshot● Step 8: Putting it all together

Page 36: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 5: Successor Snapshot

.,SKIP if ,1,SUCC

s,other wordIn .,1,1

issnapshot next then theskipped, be tois n instructio If

number. program a is snapshot,current thebe ,Let

yxxrxlyx

xrxlsi

i

ysix

Page 37: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 5: Successor Snapshot

.,INCR if ,1,SUCC

,INCR if s,other wordIn .,1 is

snapshotnext theincrement,an simulate tohave weIf

number. program a is snapshot, theis ,Let

,

,

yxpxrxlyx

yxpxrxl

ysix

yxlVAR

yxlVAR

Page 38: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 5: Successor Snapshot

.,DECR if ,1,SUCC

s,other wordIn .,1

issnapshot next thedecrement, a simulate toneed weIf

number. program a is snapshot, thebe ,Let

,

,

yxp

xrxlyx

p

xrxl

ysix

yxlVAR

yxlVAR

Page 39: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 5: Successor Snapshot

.,BRANCH if

,GTLBL,LABELmin,SUCC

s,other wordIn .,GTLBL,LABELmin

issnapshot next thebranch, tohave weIf

number. program a is snapshot, thebe ,Let

1

1

yx

xryxlyiyx

xryxlyi

ysix

yLti

yLti

Page 40: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 5: Successor Snapshot

.simulation thestop webranch,or decrement,

increment, skip,not do weif s,other wordIn

;,11,SUCC

.,11,11snapshot terminal the togo we

dispatch, aor decrement, a increment,an skip, a simulatenot do weIf

number. program a is snapshot, thebe ,Let

xryLtyx

xryLtsyLt

ysix

Page 41: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 5: Summary

otherwise. ,11

,BRANCH if

,GTLBL,LABELmin

,DECR if ,1

,INCR if ,1

,SKIP if ,1

,SUCC

1

,

,

xryLt

yx

xryxlyi

yxp

xrxl

yxpxrxl

yxxrxl

yx

yLti

yxlVAR

yxlVAR

Page 42: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Proof So Far● Step 1: Encoding a program state● Step 2: Encoding the initial snapshot● Step 3: Extracting an instruction components● Step 4: Defining conditions of instruction simulation:

● 4a) When to simulate skipping instruction;● 4b) When to simulate an increment;● 4c) When to simulate a decrement;● 4d) When to simulate a branch.

● Step 5: Simulating the successor snapshot● Step 6: Figuring out when to terminate● Step 7: Simulating the n-th snapshot● Step 8: Putting it all together

Page 43: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 6: Terminal Snapshot

.10,TERM

.1 or 0

s,other wordIn .1,1 when terminateWe

state. program a is ,snapshot thebe ,Let

yLtxlxlyx

yLti i

yLti

ysix

Page 44: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 7: Simulating N-th Snapshot

ytyxxxtyxxx

xxxyxxx

yxxx

ysix

nn

nn

nn

nn

n

,,,,...,,SNAPSUCC1,,,...,,SNAP

,...,,INIT0,,,...,,SNAP

. toinputs are ,...,,

number, program a is snapshot, thebe ,Let

2121

2121

21

Page 45: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 7: Examples

yyxxx

yyyxxx

yyxxxyxxx

yxxx

yyxxxyxxx

xxxyxxx

nn

nn

nn

nn

nn

nn

nn

nn

nn

,,,...,,INITSUCCSUCC

,,0,,,...,,SNAPSUCCSUCC

,1,,,...,,SNAPSUCC11,,,...,,SNAP

,,...,,INITSUCC

,0,,,...,,SNAPSUCC10,,,...,,SNAP

,...,,INIT0,,,...,,SNAP

21

21

2121

21

2121

2121

Page 46: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Step 8: Putting It All Together

ytyxx

tyxx

nn

nn

,,,,...,SNAPTERM

,,,...,STP

1

1

Page 47: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Proof Completed!• Step 1: Encoding a program state• Step 2: Encoding the initial snapshot• Step 3: Extracting an instruction components• Step 4: Defining conditions of instruction simulation:

– 4a) When to simulate skipping instruction;– 4b) When to simulate an increment;– 4c) When to simulate a decrement;– 4d) When to simulate a branch.

• Step 5: Simulating the successor snapshot• Step 6: Figuring out when to terminate• Step 7: Simulating the n-th snapshot• Step 8: Putting it all together

Page 48: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Remember What We Have Proved?

Page 49: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Theorem 3.2 (Ch. 4): STP Theorem

recursive. primitive is

,,,..., predicate the,0For 1)( tyxxSTPn n

n

Page 50: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Back to The New Big Picture

GÖDEL Coder

GÖDEL Decoder

L Program Number Universal Program

Number

???

GÖDEL Coder = CompilerGÖDEL Decoder = Reverse CompilerUniversal Program = Operating System/VM/Interpreter

Universality Theorem

Simulator

Step-Counter Theorem

Page 51: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Practical Implications

● We do not have to be tied to a specific universal program (operating system) to run our programs● Example: We do not need the L operating system to run L programs; for exam-

ple, we can implement an L simulator on Linux in C and run L programs● We can simulate one universal program (operating system) on another universal

program (operating system)● Example: VirtualBox (www.virtualbox.org) can be used simulate Linux on

Windows and Mac OS

Page 52: Theory of Computation (Fall 2014): Step-Counter Theorem & Program Simulation

Reading Suggestions

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