Theory of Computation (Fall 2013): Access Functions for Gödel Numbers Lengths of Natural Numbers Formal Foundations of Program Compilation & Interpretation/Execution: Gödel Coder & Gödel Decoder
Theory of Computation (Fall 2013): Access Functions for Gödel Numbers Lengths of Natural Numbers Formal Foundations of Program Compilation & Interpretation/Execution: Gödel Coder & Gödel Decoder
Theory of Computation (Fall 2013): Access Functions for Gödel Numbers Lengths of Natural Numbers Formal Foundations of Program Compilation & Interpretation/Execution: Gödel Coder & Gödel Decoder
1.
Theory of Computation
Access Functions for Gödel Numbers
Lengths of Natural Numbers
Formal Foundations of Program Compilation &
Interpretation/Execution:
Gödel Coder & Gödel Decoder
Vladimir Kulyukin
www.vkedco.blogspot.com
2.
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
4.
Review: Pairing Functions
•
x, y = 2 ( 2 y + 1) −1
x
•
2 ( 2 y + 1) −1 ≥ 0
x
x, y + 1 = 2 ( 2 y + 1)
x
5.
Review: Equation 8.1 (Ch. 3)
If z is a a natural number, there is a unique
solution for x, y to the following equation :
x, y = z
6.
Review: Splitting Natural Numbers
Equation 8.1 defines two functions : l ( z ) and r ( z ) , z ∈ N .
l ( z ) = l ( x, y ) = x.
r ( z ) = r ( x, y ) = y.
Examples :
l (10 ) = l ( 0,5 ) = 0; r (10 ) = r ( 0,5 ) = 5.
l (19 ) = r ( 2,2 ) = 2; r (19 ) = r ( 2,2 ) = 2.
7.
Review: l(z) and r(z) are primitive recursive
If z ∈ N , then z = x, y , x ≤ z , y ≤ z. Thus,
l ( z ) = min[( ∃y ) ≤ z [ z = x, y
x≤z
r ( z ) = min[( ∃x ) ≤ z [ z =
y≤z
]]
x, y ] ]
8.
Review: Gödel Numbers
Let ( a1 ,..., an ) be a sequence of numbers.
n
We define [ a1 ,..., an ] = ∏ p , where pi is the i - th prime.
i =1
[ a1 ,..., an ]
ai
i
is the Gödel number
(G-number) of this sequence.
9.
Review: Example
Suppose we have the following sequence (1,3,2 ).
The G - number of this sequence is
[1,3,2] = p
1
1
⋅ p ⋅ p = 2 ⋅3 ⋅5 .
3
2
2
3
1
3
2
10.
Lemma
For each n, [ a1 ,..., a n ] is primitive
recursive
11.
Proof
pi is primitive recursive.
ai
i
y
x is primitive recursive so p is primitive recursive.
n
∏p
i =1
ai
i
is primitive recursive, because the bounded product
of a primitive recursive function is primitive recursive, by
Corollary 6.2, Ch. 3.
12.
Note 1 on Theorem 8.2 (Ch. 3)
In general, we can add any number of zero' s to the right of
the sequence without changing its G - number. For example,
[ a1 ,..., a n ] = [ a1 ,..., a n ,0,0], because [ a1 ,..., a n ] =
p ⋅ ... ⋅ p
a1
1
an
n
= p ⋅ ... ⋅ p ⋅ 1 ⋅ 1 = p ⋅ ... ⋅ p ⋅ p
a1
1
an
n
a1
1
an
n
0
n +1
⋅p
0
n+2
.
13.
Note 2 on Theorem 8.2 (Ch. 3)
The Godel number of the empty sequence () is defined
to be 1, because
1 = 2 = 2 3 = 2 3 5 = 2 3 5 7 = ...
0
0 0
0 0 0
0 0 0
0
14.
Note 2 on Theorem 8.2 (Ch. 3)
We cannot add zero' s to the left of the sequence
without changing its G - number. In general,
[ a1 ,..., an ] ≠ [ 0, a1 ,..., an ], because
p ⋅ ... ⋅ p
a1
1
an
n
≠ p ⋅ p ⋅ ... ⋅ p
For example,
0
1
a1
2
an
n +1
.
[1,2] ≠ [ 0,1,2], because
1
2
0
1
2
[1,2] = 2 ⋅ 3 = 18 ≠ [ 0,1,2] = 2 ⋅ 3 ⋅ 5 = 75.
16.
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
17.
Access Function for Gödel Numbers
Let x = [ a1 ,..., a n ]. We want to define a primitive recursive
function ( x ) i = a i , 1 ≤ i ≤ n.
For example, if x = [ 2,4,3], then
( x ) 1 = 2;
( x ) 2 = 4;
( x ) 3 = 3.
18.
Access Function for Gödel Numbers
( x ) i = min[¬( p
t ≤x
t +1
i
|x
Note that
1. ( x ) 0 = 0, for all x;
2. ( 0 ) i = 0, for all i.
)]
19.
Example 01
Let 18 = [1,2] = p ⋅ p = 2 ⋅ 3 . Then
1
1
2
2
(18) 1 = min[¬ ( p
t ≤18
1
2
)]
t +1
1
| 18 = 1, because p1 = 2; and
( | 18), so t = 1.
(18) = min[¬ ( p | 18)] = 2, because p = 3; and
3 | 18; 3 | 18; 3 | 18; but ¬ ( 3 | 18), so t = 2.
2
t =0
| 18; 2
2
t =0
t =1
t ≤18
t =1
| 18; but ¬ 2
t =2
t +1
2
t =2
2
t =3
20.
Example 02
Let 108 = [ 2,3] = p ⋅ p = 2 ⋅ 3 = 4 ⋅ 27. Then
1
1
(108) 1 = 2
(108) 2 = 3
2
2
2
3
21.
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;
}
}
22.
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
24.
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
25.
Length of a Natural Number: Definition
Lt ( x ) = min ( ( x ) i ≠ 0 & ( ∀j ) ≤ x ( j ≤ i ∨ ( x ) j = 0 ) ).
i≤ x
In other words, i is the index of the last non - zero
prime power in a G - number.
By definition, Lt ( 0 ) = Lt (1) = 0.
26.
Example of Lt()
Lt ( 20 ) = ? .
x = 20 = 2 ⋅ 3 ⋅ 5 = [ 2,0,1].
2
0
1
( x ) 3 = 1.
( x ) 4 = ( x ) 5 = ... = ( x ) 20 = 0.
Lt ( 20 ) = 3.
27.
Theorem 8.3 (Ch. 3)
Let x > 1. Then
1. ( [ a1 ,..., a n ] ) i
a i if 1 ≤ i ≤ n
=
0 otherwise
2. [ ( x ) 1 ,..., ( x ) n ] = x if Lt ( x ) ≤ n
28.
Proof
Part 1 follows from the definition of the G - number.
For part 2, we observe that, if x > 1 and Lt ( x ) = n > 0, then p n|x,
but no prime greater than p n divides x, because Lt ( [ a1 ,...,a n ] ) = n
iff a n is the last non - zero prime power. Thus, if x > 1 and
Lt ( x ) ≤ n, then n > 0 and [ ( x ) 1 ,..., ( x ) n ] = x.
29.
Formal Foundations
of
Program Compilation & Execution/Interpretation
30.
The Big Picture
Number
GÖDEL Coder
L Program
Number
Universal Program
GÖDEL Decoder
GÖDEL Coder
= Compiler
GÖDEL Decoder = Reverse Compiler
Universal Program = Operating System/VM/Interpreter
???
31.
The Software/Hardware Duality Principle
Co-Dependency
Formalism
Hardware Device
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
32.
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
34.
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
●
35.
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 A 3 …
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
36.
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
37.
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
38.
Sub-Task 2: Assigning Numbers to Instruction Types
V ←V
V ← V +1
V ← V -1
// This will be instruction type 0
// This will be instruction type 1
// This will be instruction type 2
IF V ≠ 0 GOTO L // This will be instruction type # ( L ) + 2
39.
Sub-Task 2: Mapping Instructions to Numbers
Let I be a primitive L instruction. Then # ( I ) = a, b, c , where
1. a is the label number
2. b is the instruction type number
3. c is the number of the variable used in the instruction
40.
Sub-Task 2: Mapping Instructions to Numbers
Let I be a primitive L instruction. Then # ( I ) = a, b, c , where
1. if I is unlabeled, then a = 0;
2. if I is labeled with some label L, then a = # ( L );
3. if a variable V is used in I , then c = # (V ) − 1;
4. if I is V ← V , then b = 0;
5. if I is V ← V + 1, then b = 1;
6. if I is V ← V - 1, then b = 2;
7. if I is IF V != 0 GOTO L, then b = # ( L ) + 2. Note that in this case
b = # ( L ) + 2 > 2.
41.
Instruction Coding Example 01
Let I be X1 ← X1 + 1. What is #(I)?
42.
Instruction Coding Example 01
I is X 1 ← X 1 + 1; so # ( I ) = a, b, c , where
1. a = 0, because I is unlabeled;
2. b = 1, because I is addition;
3. c = # ( X 1) − 1 = 1, because # ( X 1) = 2;
4. Thus,
# ( I ) = 0, 1,1 = 2 0 ( 2 1,1 + 1) − 1 =
[
]
2 ( 2 ⋅ 2 ( 2 ⋅ 1 + 1) − 1 + 1) − 1 =
1 ⋅ ( 2 ⋅ [ 2 ⋅ 3 − 1] + 1) − 1 = 11 − 1 = 10.
0
1
43.
Instruction Coding Example 02
Let I be [A1] X1 ← X1+1. What is #(I)?
45.
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
46.
Sub-task 3: Definition of #(P)
Let P be a program that consists of I1 , I 2 ,..., I k , where k ≥ 0
and I1 , I 2 ,..., I k are primitive L instructions, then
# ( P ) = [ # ( I1 ) , # ( I 2 ) ,..., # ( I k ) ] − 1.
47.
Example of #(P)
What is the number of this program?
[ A1]
X 1 ← X 1 +1
IF X 1 ≠ 0 GOTO A1
48.
Example of #(P)
I1 is [ A1] X 1 ← X 1 + 1
I 2 is IF X 1 ≠ 0 GOTO A1
Thus,
# ( P ) = [ # ( I1 ) , # ( I 2 ) ] − 1
49.
Example of #(P)
By a previous example, # ( I1 ) =# ( [ A1] X 1 ← X 1 + 1) = a, b, c ,
where
1. a =# ( A1) = 1
2. b = 1
3. c =# ( X 1) − 1 = 2 − 1 = 1
So, 1, 1,1 = 2 ( 2 1,1 + 1) − 1 = 21
1
50.
Example of #(P)
# ( I 2 ) =# ( IF X 1!= 0 GOTO A1) = a, b, c , where
1. a = 0;
2. b =# ( A1) + 2 = 1 + 2 = 3;
3. c =# ( X 1) − 1 = 1.
Thus, # ( I 2 ) = 0, 3,1 = 20 ( 2 3,1 + 1) − 1 =
3
2 2 2 ( 2 ⋅1 + 1) − 1 + 1 − 1 =
1( 2 ⋅ 23 + 1) − 1 = 47 − 1 = 46.
0
51.
Example of #(P)
# ( I 1 ) = 21
# ( I 2 ) = 46
# ( P ) = [ # ( I 1 ) , # ( I 2 ) ] − 1 = [ 21,46] − 1 =
2 21 ⋅ 3 46 − 1 = 18586928403505481978329694207
52.
Back to The Big Picture
Number
GÖDEL Coder
L Program
Number
Universal Program
GÖDEL Decoder
The red arrows show the implemented tasks (program compilation):
we now know how to map L programs to numbers.
???
54.
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
55.
GÖDEL Decoder: Sub-Task 1
# ( P ) = [ # ( I1 ) , # ( I 2 ) ,..., # ( I k ) ] − 1.
Thus, the source code number is
[ # ( I1 ) , # ( I 2 ) ,..., # ( I k ) ] =# ( P ) + 1.
56.
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 Decoding
Factors(n, i)
If n is prime then n is the factorization;
If n is composite then
If pi divides n then
add pi to the list of factors;
Factors((n / pi), i);
If pi does not divide n Then
Factors(n, i+1)
57.
Prime Factorization Algorithm: Example
factors(8,1)
( p1 = 2) | 8
so add 2 to factors(4, 1)
( p1 = 2) | 4
so add 2 to factors(2, 1)
2 is a prime so return [2]
return [2, 2]
return [2, 2, 2]
58.
GÖDEL Decoder: Sub-Task 2
1. Let n be # ( P ) = [ # ( I 1 ) , # ( I 2 ) ,..., # ( I k ) ] − 1
So, the source code number is n + 1 = [ # ( I 1 ) , # ( I 2 ) ,..., # ( I k ) ]
2. Run factors( n + 1,1) , to obtain the list of prime factors
3. From the list of prime factors, obtain the list of prime powers, i.e.,
the G - number
59.
Examples of Source Code Extraction
1. Suppose some program P is compiled into number 7
2. The source code number for P is 8
3. factors(8,1) returns [2, 2, 2]
4. [2,2,2] is converted to the G - number [3] = 2 3 = 8
5. P' s source code consists of one instruction that compiles into number 3
1. If P compiles into 13, then factors(14,1) returns [2,7]
2. [2,7] is converted to the G - number [1, 0, 0, 1] = 21 ⋅ 30 ⋅ 5 0 ⋅ 71 = 14
3. P consists of four instructions that compile into 1,0,0, and 1, respectively
60.
GÖDEL Decoder: Sub-Task 3
Now that we have the G - number from sub - task 2, we need to
extract the source code of each instruction in the G - number.
Let # ( I ) = q = a, b, c .
1. a = l ( q ). If a = 0, I is unlabeled. Otherwise, I has label number
l ( q ) in the label ordering.
2. b = l ( r ( q ) ). Then
2.1. I is of type V ← V
if b = 0;
2.2. I is of type V ← V + 1 if b = 1;
2.3. I is of type V ← V - 1 if b = 2;
2.4. I is of type IF V != 0 GOTO L where L is label whose
position is b- 2 in the label ordering.
3. c = r ( r ( q ) ). Thus, the variable used in I is variable whose
position is c + 1 in the variable ordering.
61.
#(P) P: GÖDEL Decoding Algorithm
1. Let n be # ( P ) = [ # ( I1 ) , # ( I 2 ) ,..., # ( I k ) ] −1;
So, the source code number is n + 1 = [ # ( I1 ) , # ( I 2 ) ,..., # ( I k ) ]
2. Run Factors( n + 1,1)
3. Obtain the G - number from the list of prime factors, i.e. get
the sequence of prime powers
4. Extract the source code from each element of the G - number
obtained in step 3
5. All operations are primitive recursive
62.
GÖDEL Decoding Algorithm: Example
1. 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 selfassignment. 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.
63.
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
64.
Back To The Big Picture
Number
GÖDEL Coder
L Program
Number
Universal Program
GÖDEL Decoder
GÖDEL Coder
= Compiler
GÖDEL Decoder = Reverse Compiler
Universal Program = Operating System/VM/Interpreter
???
65.
Reading Suggestions
●
●
Chapters 3 & 4, Computability, Complexity, and
Languages, 2nd Edition, by Davis, Weyuker, Sigal
http://en.wikipedia.org/wiki/Kurt_Gödel
Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.
Be the first to comment