Upload
vladimir-kulyukin
View
520
Download
1
Embed Size (px)
Citation preview
Theory of Computation
Access Functions for Gödel NumbersLengths of Natural Numbers
Formal Foundations of Program Compilation & Interpretation/Execution:
Gödel Coder & Gödel Decoder
Vladimir Kulyukin
www.vkedco.blogspot.comwww.vkedco.blogspot.com
Outline● Review
– Pairing Functions
– Gödel Numbers
● Access Functions for Gödel Numbers● Lengths of Natural Numbers● Formal Foundations of Program Compilation &
Interpretation/Execution– Gödel Coder
– Gödel Decoder
Review
Review: Pairing Functions
( )
( )( )1221,
01122
1122,
+=+≥−+
−+=•
•
yyx
y
yyx
x
x
x
Review: Equation 8.1 (Ch. 3)
zyx
x, y
z
=,
:equation following the tofor solution
unique a is therenumber, natural a a is If
Review: Splitting Natural Numbers
( ) ( )( ) ( )( ) ( )
( ) ( ) ( ) ( )( ) ( ) ( ) ( ) .22,219;22,219
.55,010;05,010
:Examples
.,
.,
. , and :functions twodefines 8.1Equation
====
====
==
==∈
rrrl
rrll
yyxrzr
xyxlzl
Nzzrzl
Review: l(z) and r(z) are primitive recursive
( ) [ ][ ]( ) [ ][ ] ,min)(
,min)(
Thus, .,,, then , If
yxzxzr
yxzyzl
zyzxyxzNz
zzy
zzx
=∃=
=∃=
≤≤=∈
≤≤
≤≤
Review: Gödel Numbers
( )
[ ] ∏=
=n
ii
ain
n
ppaa
aa
i
11
1
prime.th -i theis where,,..., define We
numbers. of sequence a be ,...,Let
[ ]naa ,...,1is the Gödel number (G-number) of this sequence.
Review: Example
( )
[ ] .5321,3,2
is sequence thisofnumber -G The
.2,3,1 sequence following thehave weSuppose
23123
32
11 ⋅⋅=⋅⋅= ppp
Lemma
[ ]recursive
primitive is ,..., ,each For 1 naan
Proof
3. Ch. 6.2,Corollary
by recursive, primitive isfunction recursive primitive a of
product bounded thebecause recursive, primitive is
recursive. primitive is so recursive primitive is
recursive. primitive is
1∏
=
n
i
ai
ai
y
i
i
i
p
px
p
Note 1 on Theorem 8.2 (Ch. 3)
[ ] [ ] [ ]....11......
,..., because,0,0,,...,,...,
example,For number.-G its changing without sequence the
ofright the toszero' ofnumber any addcan wegeneral,In
02
01111
111
111++ ⋅⋅⋅⋅=⋅⋅⋅⋅=⋅⋅
==
nnan
aan
aan
a
nnn
pppppppp
aaaaaannn
Note 2 on Theorem 8.2 (Ch. 3)
...75325323221
because 1, be to
defined is () sequenceempty theofnumber Godel The
0000000000 =====
Note 2 on Theorem 8.2 (Ch. 3)
[ ] [ ]
[ ] [ ][ ] [ ] .755322,1,018322,1
because,2,1,02,1
example,For
.......
because ,,...,,0,...,
general,In number.-G its changingwithout
sequence theofleft the toszero' addcannot We
21021
12011
11
11
=⋅⋅=≠=⋅=
≠
⋅⋅⋅≠⋅⋅
≠
+nn a
naa
na
nn
ppppp
aaaa
Access Function for Gödel Numbers
Motivation
● Suppose we have a Gödel number n ● By definition, n is a sequence of numbers each of which
is the power of the corresponding prime● We can think of n as an array of numbers and, as with
arrays in other programming languages, we need access to their individual elements
● In our context, we would like to access those individual prime powers
Access Function for Gödel Numbers
[ ]( )
[ ]( )( )( ) .3
;4
;2
then,3,4,2 if example,For
.1 ,function
recursive primitive a define want to We.,...,Let
3
2
1
1
===
=
≤≤==
x
x
x
x
niax
aax
ii
n
Access Function for Gödel Numbers
( ) ( )[ ]
( )( ) . allfor ,00 .2
; allfor ,0 .1
thatNote
|min
0
1
i
xx
xpx
i
ti
xti
==
¬= +
≤
Example 01
[ ]
( ) ( )[ ]( )
( ) ( )[ ]( ) .2 so ,18|3 but 18;|3 ;18|3 ;18|3
and ;3 because ,218| min18
.1 so,18|2 but ;18|2 ;18|2
and 2; because ,118| min18
Then .322,118Let
3210
21
218
2
210
11
118
1
2122
11
=¬
==¬==¬
==¬=
⋅=⋅==
====
+
≤
===
+
≤
t
pp
t
pp
pp
tttt
t
t
ttt
t
t
Example 02
[ ]
( )( ) 3108
2108
Then .274323,2108Let
2
1
3222
11
==
⋅=⋅=⋅== pp
Access Function for G-Numbers: Implementation
// x is a natural number, compute (x)i
long GAccess(long x, long i) {
long p = computeIthPrime(i); // compute i-th prime pi
for t from 0 upto x {
if ( pt does not divide x )
return t – 1;
}
}
Access Function for G-Numbers: Implementation
● The previous slide assumes that we represent G-Numbers as integers
● This is a sensible choice, but only for small numbers● If we want to be generic, we can represent G-numbers as
sequences of prime powers, e.g. arrays, lists, etc. ● In this case, we can represent much larger values without
actually having to compute them● Access function becomes much easier, because we do not have
to compute primes or their powers
Lengths of Natural Numbers
Length of a Natural Number
● Since Gödel numbering and access functions give us techniques to split any natural number into its constituents
● Next question: how many constituents does a given natural number have? How long is the number?
● Example: 18=[1, 2]. So the length of 18 is 2
Length of a Natural Number: Definition
( ) ( ) ( ) ( )( )( )
( ) ( ) .010 ,definitionBy
number.-G ain power prime
zero-nonlast theofindex theis s,other wordIn
.0&0min
==
=∨≤∀≠= ≤≤
LtLt
i
xijjxxLt jxixi
Example of Lt()
( )[ ]
( )( ) ( ) ( )
( ) .320
.0...
.1
.1,0,253220
.?20
2054
3
102
=====
==⋅⋅==
=
Lt
xxx
x
x
Lt
Theorem 8.3 (Ch. 3)
[ ]( )
( ) ( )[ ] ( ) nxLtxxx
niaaa
x
n
iin
≤= ≤≤
=
>
if ,..., .2
otherwise 0
1 if ,..., .1
Then .1Let
1
1
Proof
( )[ ]( )
( ) ( ) ( )[ ] .,..., and 0 then ,
and 1 if Thus, .power prime zero-nonlast theis iff
because, divides angreater th prime nobut
, then ,0 and 1 if that,observe we2,part For
number.-G theof definition thefrom follows 1Part
1
1
xxxnnxLt
x a
n,...,aaLtx p
|xpnxLt x
n
n
nn
n
=>≤>
=>=>
Formal Foundations of
Program Compilation & Execution/Interpretation
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
The Software/Hardware Duality Principle
1) There is no point in developing a formalism unless it can be executed on an existing hardware device
2) There is no point in developing a hardware device unless there is (or there will be) a formalism for it
Formalism Hardware DeviceCo-Dependency
Gödel Coder & Decoder
● We will demonstrate that compilation (coding) and reverse compilation (decoding) are both primitive recursive
● In other words: Given an L program P, there is a primitive
recursive function that maps P to its number #(P)
Given #(P), there is a primitive recursive function that maps #(P) to its source code P
Gödel Coder
Gödel Coder
● Sub-task 1: Mapping variables and labels to numbers
● Sub-task 2: Mapping instructions to numbers
● Sub-task 3: Mapping programs to numbers
Sub-task 1: Mapping Variables & Labels
● We order the variables as follows: Y X1 Z1 X2 Z2 X3 Z3 …
● We order the labels as follows: A1 B1 C1 D1 E1 A2 B2 C2 D2 E2 A3 …
● Let #(V) be the position of a variable in the variable ordering
● Let #(L) be the position of a label in the label ordering ● Note that the chosen orderings are arbitrary in the
sense that other orderings are possible
Examples of #(V) & #(L)
● We order the variables as follows: Y X1 Z1 X2 Z2 X3 Z3 …
● We order the labels 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
Sub-task 2: Mapping Instructions to Numbers
● There are four primitive instruction types in L: addition, subtraction, self-assignment (no-op), conditional dispatch
● There are three pieces of information we have to known in order to map an instruction to a number:
label or no label variable used in the instruction instruction type
Sub-Task 2: Assigning Numbers to Instruction Types
( ) 2# n typeinstructio be willThis // 0
2 n typeinstructio be willThis // 1
1 n typeinstructio be willThis // 1
0 n typeinstructio be willThis //
+≠←
+←←
L GOTO LIF V
-VV
VV
VV
Sub-Task 2: Mapping Instructions to Numbers
( )
ninstructio in the used variable theofnumber theis .3
number n typeinstructio theis 2.
number label theis 1.
where,,,#Then n.instructio primitive a be Let
c
b
a
cbaILI =
Sub-Task 2: Mapping Instructions to Numbers
( )
( )( )
( )( ) .22
case in this that Note .2then ,0! is if 7.
;2 then ,1 is if 6.
;1 then ,1 is if 5.
;0 then , is if .4
;1 then ,in used is variablea if .3
; then , label some with labeled is if 2.
;0 then unlabeled, is if 1.
where,,,#Then n.instructio primitive a beLet
>+=+==
=←=+←
=←−=
==
=
L#b
L#b GOTO LIF VI
b-VVI
bVVI
bVV I
V#cIV
L#aLI
aI
cbaILI
Instruction Coding Example 01
Let I be X1 X1 + 1← . What is #(I)?
Instruction Coding Example 01
( )
( ) ( )
( ) ( )( )[ ]( )
[ ]( ) .101111113221
111112222
111,1221,1,0#
Thus, 4.
;21 because ,111 3.
addition; is because ,1 .2
unlabeled; is because ,0 .1
where,,,# so ;111 is
10
0
=−=−+−⋅⋅⋅=−+−+⋅⋅
=−+==
==−===
=+←
I
X#X#c
Ib
Ia
cbaIXXI
Instruction Coding Example 02
Let I be [A1] X1 X1+1← . What is #(I)?
Instruction Coding Example 02
[ ]( ) ( ) ( )
( )( )[ ]( ) .211112111112222
111,1221,1,1
11#,1,1##
,111 1 is Since
111
1
=−⋅=−+−+⋅
=−+⋅=
=−=
+←
XAI
XXAI
Sub-task 3: Mapping Programs to Numbers
● We know how to do sub-task 1 of mapping labels and variables into numbers through label and variable orderings
● We know how to do sub-task 2 of mapping individual primitive L instructions into numbers
● What is left? We need to do sub-task 3, i.e., mapping finite sequences of primitive L instructions into numbers
Sub-task 3: Definition of #(P)
( ) ( ) ( ) ( )[ ] .1#,...,#,##
thenns,instructio primitive are ,...,, and
0 where,,...,, of consists that program a be Let
21
21
21
−=
≥
k
k
k
IIIP
LIII
kIIIP
Example of #(P)
[ ]1 GOTO 0 1 IF
111 1
program? thisofnumber theisWhat
AX
XXA
≠+←
Example of #(P)
[ ]
( ) ( ) ( )[ ] 1#,##
Thus,
1 GOTO 0 1 IF is
111 1 is
21
2
1
−=
≠+←
IIP
AXI
XXAI
Example of #(P)
( ) [ ]( )
( )
( )( ) 21111,1221,1,1 So,
11211# .3
1 2.
11# .1
where
,,,111 1## example, previous aBy
1
1
=−+=
=−=−==
==
=+←=
Xc
b
Aa
cbaXXAI
Example of #(P)
( ) ( )
( )( )
( ) ( )
( )
( ) .46147112321
111112222
111,3221,3,0# Thus,
.111# .3
;32121# .2
;0 .1
where,,,1 GOTO 0! 1 IF##
30
02
2
=−=−+⋅
=−
+
−+⋅
=−+==
=−==+=+=
=
===
I
Xc
Ab
a
cbaAXI
Example of #(P)
( )( )
( ) ( ) ( )[ ] [ ]32969420735054819781858692840132
146,211#,##
46#
21#
4621
21
2
1
=−⋅
=−=−=
==
IIP
I
I
Back to The Big Picture
GÖDEL Coder
GÖDEL Decoder
L Program Number Universal Program
Number
???
The red arrows show the implemented tasks (program compilation): we now know how to map L programs to numbers.
Gödel Decoder
Gödel Decoder
● Sub-Task 1: Obtain the source code number● Sub-Task 2: Extract from the source code
number the numbers for individual instructions● Sub-Task 3: Extract the source from each
individual instruction number
GÖDEL Decoder: Sub-Task 1
( ) ( ) ( ) ( )[ ]
( ) ( ) ( )[ ] ( ) .1##,...,#,#
isnumber code source theThus,
.1#,...,#,##
21
21
+=
−=
PIII
IIIP
k
k
Side Note: A Prime Factorization Algorithm
// n is a natural number > 0, i is the prime counter, initially// set to 1. We need this algorithm to solve sub-task 2 of // Gödel DecodingFactors(n, i)
If n is prime then n is the factorization;If n is composite thenIf pi divides n then
add pi to the list of factors;Factors((n / pi), i);
If pi does not divide n ThenFactors(n, i+1)
Prime Factorization Algorithm: Example
( )
( )
2] 2, [2,return
2] [2,return
[2]return so prime a is 2
1) factors(2, to2 add so
4|2
1) factors(4, to2 add so
8|2
1)factors(8,
1
1
=
=
p
p
GÖDEL Decoder: Sub-Task 2
( ) ( ) ( ) ( )[ ]( ) ( ) ( )[ ]
( )
number-G the
i.e., powers, prime oflist obtain the factors, prime oflist theFrom .3
factors prime oflist obtain the to,1,1nfactorsRun 2.
#,...,#,#1 isnumber code source theSo,
1#,...,#,# be Let 1.
21
21
+=+
−=
k
k
IIIn
IIIP#n
Examples of Source Code Extraction
lyrespective 1, and 1,0,0, into compile that nsinstructiofour of consists P .3
1475321] 0, 0, [1,number -G the toconverted is [2,7] 2.
[2,7] returns ,1)factors(14 then 13, into compiles P If .1
3number into compilesn that instructio one of consists code source sP' .5
82[3]number -G the toconverted is ]2,2,2[ 4.
2] 2, [2, returns 1)factors(8, 3.
8 is Pfor number code source The 2.
7number into compiled is P program some Suppose 1.
1001
3
=⋅⋅⋅=
==
GÖDEL Decoder: Sub-Task 3
( )( )
( )( )( )
( )( ) ordering. variablein the 1 isposition
whose variableis in used variable theThus, . 3.
ordering. label in the 2 isposition
whoselabel is L whereL GOTO 0 ! V IF typeof is 2.4.
;2 if 1 - V V typeof is 2.3.
;1 if 1 V V typeof is 2.2.
;0 if V V typeof is 2.1.
Then . 2.
ordering. label in the
number label has Otherwise, unlabeled. is ,0 If . 1.
.,,#Let
number.-G in then instructioeach of code source eextract th
toneed we2,task -sub fromnumber -G thehave that weNow
c
Iqrrc
b-
I
b I
b I
b I
qrlb
ql
IIaqla
cbaqI
+=
==←=+←=←
=
==
==
#(P) P: GÖDEL Decoding Algorithm
( ) ( ) ( ) ( )[ ]( ) ( ) ( )[ ]
( )
recursive primitive are operations All 5.
3 stepin obtained
number-G theofelement each from code source eExtract th 4.
powers prime of sequence the
get i.e. factors, prime oflist thefromnumber -G Obtain the .3
1,1nFactorsRun 2.
#,...,#,#1 isnumber code source theSo,
;1#,...,#,# be Let 1.
21
21
+=+
−=
k
k
IIIn
IIIP#n
GÖDEL Decoding Algorithm: Example1. Let #(P) = 199.2. The source code number = 199+1=200.3. The prime factorization of 200 is 233052. Thus, the G-number = [3,0,2].4. 3 = <2, <0, 0>>. So the label is B1, the instruction variable is Y and the type of instruction is self-assignment. Thus, first instruction is [B1] Y Y.←5. 0 = <0, <0, 0>>. Thus, second instruction is Y Y.←6. 2 = <0, <1, 0>>. Thus, third instruction is Y Y + 1.←
GÖDEL Decoding Algorithm: Example
Let #(P) = 199. The source code number = 199+1=200. So, the source code of P is
[B1] Y ← Y
Y ← Y
Y ← Y + 1
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
Reading Suggestions
● Chapters 3 & 4, Computability, Complexity, and Languages, 2nd Edition, by Davis, Weyuker, Sigal
● http://en.wikipedia.org/wiki/Kurt_Gödel