Theory of Computation
Universal Programs
Vladimir KulyukinDepartment of Computer Science
Utah State University
www.vkedco.blogspot.com
A movement is accomplished in six stagesAnd the seventh brings return
Syd Barrett, Chapter 24, The Piper at the Gates of Dawn
Outline● Introduction● Developing Universal Programs in Six Stages
Universality Theorem
Stage 01: Source Code Extraction, Encoding Program State, Initializing In-struction Counter
Stage 02: Establishing Termination Condition
Stage 03: Decoding Next Instruction
Stage 04: Determining Next Instruction's Type
Stage 05: Execution of Conditional Dispatch
Stage 06: Execution of No-Op, Subtraction, Addition
Return
Introduction
Review: Variable & Label Orderings
● The variables are ordered as follows:Y X1 Z1 X2 Z2 X3 Z3 …
● The labels are ordered as follows:A1 B1 C1 D1 E1 A2 B2 C2 D2 E2 A3 …
● #(Y)=1, #(X1)=2, #(X3)=6, #(Z2)=5● #(B1)=2, #(A2)=6, #(C2)=8
Why Universal Programs are Called Universal
Every natural number in the universe is a program awaiting its computer. The program runs if and when its computer appears. Universal programs are one example of such computers. They are called universal, because they turn every natural number into a running program.
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
Developing Universal Programsin Six Stages
Theorem 3.1 (Ch. 4)
.computablepartially is
,,...,function the,0each For 1)( yxxΦn n
n
What does this theorem really say? y is a natural number; y is a compiled program; x
1, …, x
n are inputs to program
compiled as y; the function Φ takes y, runs it on the values of x
1, …, x
n and returns the result iff there is a computation,
which is why Φ is partially computable.
Proof 3.1
11)( ,,...,
:function following thecomputes
that construct can we,0each for
how, show will we3.1, Theorem prove To
nnn
n
XXXY
U n
Stage 01
Source Code Extraction, Encoding Program State, Initializing Instruction Counter
Program State & Instruction Counter
executed. be n toinstructio sprogram' theofnumber thebe Let
state. theencodest number tha-G thebeLet
.63731,0,2,00,1,0,2,0 :number-G
.0,1,0,2,0 :State
example,For odd.
are variablesinternal theof positions theand 1 is Yposition The
even. arenumber -G in the ablesinput vari theof positions The
number.-G a as encoded is program theof state The
2
2211
K
S
ZXZXY
Program State
. of value theencodes then odd, is and 1 if
; of value theencodes then even, is and 1 if
; of value theencodes always
: thatso variableprogram a
of value theencodes Each .,...,, state program the
let example,For number.-G a is state program that theis idea The
2/1
2/
1
21
ii
ii
im
Zvii
Xvii
Yv
vvvvS
Variable Values Encoded in Program State
on. so and , of value theis
; of value theis
; of value theis
; of value theis
; of value theis
; of value theis
; of value theis
Then . S Suppose
32/177
32/66
22/155
22/44
12/133
12/22
1
1
ZZv
XXv
ZZv
XXv
ZZv
XXv
Yv
,..., vv m
Un Construction: Stage 01
1. Extract the source code2. Initialize the state3. Initialize the instruction counter
Un Construction: Stage01
counter.n instructio theInitialize 3. // 1
well.asit rename could But we either. variable,//
legal anot is counter.n instructio theis //
state. program theInitialize 2. //
to.had weifsubscript eappropriat somewith into//
it rename could We.meaningful more isit But variable.//
legal anot ly technicalis S program. theof state initial theis S //
code. source eExtract th 1. // 1
number. program a is and ablesinput vari theare//
,..., where,,,..., computes program This//
12
1
1
111
K
L
KK
pS
Z
L
XZ
X
XXXXXY
n
i
Xi
n
n
nnnn
i
Stage 02
Establishing Termination Condition
Un Construction: Stage 02
• The next thing that Un must do is check for the termination condition
• So, when should Un stop running program y?• The termination condition occurs when the instruction
counter is I+1 or 0, where I=Lt(Z) is the number of instructions in the source code compiled into natural number Z
• The function Lt(Z) computes the length of natural number Z• The instruction counter is 0 when the program is dispatched to
a label that does not exist
Review: Access Function for Gödel Numbers
.3;4;2
then,3,4,2 if example,For recursive. primitive is
,1 ,function access The .,...,Let
321
1
xxx
x
niaxaax iin
Compiled Program's Source Code as G-Number
.number -Gin primeth - theofpower thealso
isIt n.instructioth - theofnumber theis 2)
;in nsinstructio
primitive ofnumber thei.e., length, s' is )1
: thatknow Then we . program- some
of source compiled theis number,-G a is which , If
ZK
KZ
L
PZLt
PL
Z
K
Un Construction: Stage 02
label. EXIT usual theis //
subscript.//
eappropriatan with Za usingby renamed//
becan but variable, legal anot is that Note//
label. CONTINUE theas of thought becan //
GOTO 01 IF
E
L C
C
EKZLtKC
Un So Far
Terminate 4. // E GOTO 1 0 IF C
counter.n instructio theInitialize 3. // 1
state. program theInitialize 2. //
code. source eExtract th 1. // 1
12
1
Z LtK K
K
pS
XZn
i
Xi
n
i
Stage 03
Decoding Next Instruction
Un Construction: Stage 03
● The next thing that Un must do is decode the next instruction to execute (decode simply means to extract the source code)
● The source code contains only primitive L instructions: all macros have been expanded properly and variables renamed
● Recall that an instruction is defined by three components: ● the label ● the instruction type● the variable
Un Construction: Stage 03
.1#
n;instructio of type theis
;#
where,,,Z n,compilatio of definitionby Then,
power); (primenumber sn'instructiocurrent theis
Vc
b
La
cba
Z
K
K
Un Construction: Stage 03
ordering.
variablein the 1number variableis s,other wordIn
.1#
;1#
;,
:follows asn instructio in the
used variable theand n typeinstructio eextract th nowcan We
UrV
UrV
VUrc
cbZrU K
Un Construction: Part 03
. of value theis i.e., where
, |such that prime a is ? isWhat
: of value thedetermine
nowcan .We...,..., that Recall
1
11
111
VvSv
Spppp
V
ppvvS
Ur
vUrUr
vm
vm
m
Example 01
.63|3 because ,2 s,other wordIn
.2 then , of i.e. , of value theis If
.3 is want weprime The . Thus,
...)., ,( ordering variablein the ofnumber theis
which 2, 1 and 1#1
Then .1,, that also Suppose
.63731,0,2,0 that Suppose
21
211
211
111
2
X
SSvXVv
ppXV
ZXYX
UrVcUr
bcbU
S
Ur
Ur
Example 02
.| then ,0 if that Observe
.63|5but ,63|5 because ,0 So,
.0.Then of i.e. , of value thebe Let
.5 prime ingcorrespond The . Thus,
....) , , ,( ordering leour variabin ofnumber the
is which 3, 1 So .1#2Then
.2,Let .63731,0,2,0Let
11
01
311
311
111
2
SpSv
Z
SSvZVv
ppZV
ZXYZ
UrVcUr
bUS
UrUr
Ur
Ur
Example 03
.1 So, .1
.7
. so ,...) , , , ,( ordering variable
ourin of # theis which 4, 1 So
.1#3Then .3,Let
.63731,0,2,0 that Assume
241
41
2211
2
2
XSS
pp
XVXZXY
XUr
VcUrbU
S
Ur
Ur
Un Construction: Stage 03
1)(
th prime. 1)( theis //
ninstructioth - theofpart right the;, //
ninstructioth - ;,, //
Ur
K
K
pP
UrP
ZrU
KcbU
KcbaZI
Un So Far
variablesn'instructioth -K of valuehe // t
ispower whoseingcorrespond prime theis P //
n instructioth -K theis //
Conditionn Terminatio // GOTO 1 0 IF [C]
countern instructio theisK // 1
state sprogram' theis S //
code source theis // Z1
1)(
12
1
Ur
KK
n
i
Xi
n
pP
ZZrU
EZLtK K
K
pS
XZ
i
Stage 04
Determining Next Instruction's Type
Un Construction: Part 04
● The next thing that Un must do is to determine the type of the next instruction to be executed
● The following labels are used:● N – stands for no-op (do nothing)● A – stands for addition● M – stands for subtraction (minus)● Conditional dispatch needs to go to a specific label
Un Construction: Stage 04
anything. do tohavenot do We. typeof is executed//
be n toinstructio the,0)( if s,other wordIn //
GOTO 0)( IF
. ; ;,,//
VV
Ul
NUl
bUlb,cZrUcbaZI KK
Un Construction: Stage 04
label.addition //
theodispatch t We.1 typeof is executed//
be n toinstructio the,1)( if s,other wordIn //
GOTO 1)( IF
.;,;,,//
VV
Ul
AUl
bUlcbZrUcbaZI KK
Un Construction: Stage 04
• Un now has to check if the instruction to execute is subtraction
• However, if we know that the value of the variable is 0, we do not need to subtract 1 from it, because it will remain 0 anyway
• So, before we dispatch to subtraction, we check if the value of the variable is 0; if it is, we do nothing
Program State & Variable Values
0.an greater th are powers their because 63, of factors
theare 7 and 3Only .0,2,0,17363 example,For
. offactor anot is ,0 if because trueis This
.0 ifonly and if |Then
number.-G a is that Recall state. program a be Let
2
SpS
SSp
SS
ii
ii
Un Construction: Stage 04
NSP
SP
GOTO | IF
nothing. do We0. isn instructio in the//
variable theof value the,| If//
Un Construction: Stage 04
MUl
Ul
GOTO 2)( IF
label.n subtractio theodispatch t // We
n.subtractio is n typeinstructio the,2)( If//
Un So Far
MINUS. togo ,1 If // M GOTO 2)( IF
NOTHING. togo ,0 If // N GOTO | IF
ADD. togo ,1 IfA // GOTO 1)( IF
OP.-NO togo , If // N GOTO 0)( IF
e.th variabl-1 toingcorrespond prime Get the //
n.instructioth -K of ,Get //
inationCheck term // E GOTO 01 IF C
counter.n instructio Initialize // 1
state. program initial theEncode //
number. source Get the // 1
1)(
12
1
V-V Ul
V SP
VV Ul
VV Ul
cpP
cbZrU
KZLtK
K
pS
XZ
Ur
K
n
i
Xi
n
i
Stage 05
Execution of Conditional Dispatch
Compilation of Conditional Dispatch
.2#then
, GOTO 0 IF form theof isn instructioth - IF
Lb
LVK
.2# and GOTO 0 IF
form theof isn instructio
current then the,| and 2 IF
.,;,,
UlLLV
SPUl
cbZrUcbaZI KK
Un Construction: Stage 05
Un Construction: Stage 05
.22 ly,equivalent
or, ,2such that ,,,n instructio
earliest for thelook toneed weThus, .2#
So .2#2,
Ulba
bacbaZ
bL
LUlbcblUl
i
iiiii
Un Construction: Stage 05
0. becomecan wherecode in the placeonly theis This //
.0 label,such no is thereif that Note//
GOTO
2min
.,;,, //
)(
K
K
C
UlZlK
cbZrUcbaZI
iZLti
KK
Un So Far
C
UlZlK
Ul
SP
Ul
Ul
pP
ZrU
KZLtK
K
pS
XZ
iZLti
Ur
K
n
i
Xi
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
Stage 06
Execution of NO-OP, Subtraction, & Addition
Un Construction: Stage 06
● All there is left to do is to code what Un must do at each of the following labels:●[N] – do no-op●[M] – subtract●[A] – add
Un Construction: Stage 06
C.back Go // GOTO
counter.n instructioIncrement // 1
Nothing Doing//
C
KKN
Un Construction: Stage 06
.1,0,1,073213
63 isexecuted, isn subtractio e//after th
state, new Then the .3 Thus, sequence. leour variab//in
variablesecond theis 1 .111# //Suppose
.1,0,2,063 example,//For
nSubtractio //
2
1
P
S
pP
XXXZ
S
P
SSM
Un Construction: Stage 06
.7632,0,2,0'
state new .Then the Thus, sequence.our in
eth variabl-4 theis 2 .122# Suppose
. Suppose .1,0,2,063 suppose example,For
Addition //
4
PSS
pP
XXXZ
iKS
PSSA
i
Un Construction Completed
1
)(
1)(
12
1
GOTO
1
GOTO
/
GOTO
2min
M GOTO 2)( IF
N GOTO | IF
A GOTO 1)( IF
N GOTO 0)( IF
E GOTO 01 IF C
1
1
SYE
C
KKN
PSSA
N
PSSM
C
UlZlK
Ul
SP
Ul
Ul
pP
ZrU
KZLtK
K
pS
XZ
iZLti
Ur
K
Xn
ii
n
i
Return
What We Just Proved
.computablepartially is ,,...,function the
,0each For :Theorem)ity (Universal 3.1 Theorem
1)( yxxΦ
n
nn
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
A Practical Interpretation of Universality Theorem
● If you look at the proof of Theorem 3.1 (Ch. 4) as a programmer or a computer scientist, you notice that we have constructed an interpreter (a mini-OS) for L in L itself
● It is feasible to write operating systems that can interpret/execute any program in a specific assembly dialect
● It is also feasible to write ● Java interpreter/compiler in Java● Python interpreter/compiler in Python● Lisp interpreter/compiler in Lisp● C interpreter/compiler in C
Stream, Infinite & Universal
0,run can
,...,,...,,
1
121
nUU
UUUU
nn
nn
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 running the program● Enter Simulation which we will look at in the next lecture
References
● Ch. 4, Computability, Complexity, and Languages, 2nd Edition, by Davis, Weyuker, Sigal
● http://en.wikipedia.org/wiki/Kurt_Gödel