Computable Functions
In this part of the course, we study several formal definitions of algorithm. These definitions are equivalent, providing different ways of describing the notion ofcomputable function.
Slide 1
Algorithms, informally
People tried to find an algorithm to solve Hilbert’s Entscheidungsproblem, without success.
A natural question was then to ask whether it was possible toprove
that such an algorithm did not exist. To ask this question properly, it was necessary to provide aformaldefinition of algorithm.
Common features of the (historical) examples of algorithms:
•
finitedescription of the procedure in terms of elementaryoperations;
•
deterministic, next step is uniquely determined if there is one;•
procedure may not terminate on some input data, but we canrecognise when it does terminate andwhattheresultwill be.
So far, we have studied the operational and denotational semantics of a sim-ple while language, While. The denotational semantics ofWhilegives math-ematical meaning to while programs as special functions (actually, the so-called continuous functions) over special sets (domains). We have given an indication of this world of denotational semantics, by providing an interpreta-tion (meaning) of while programs as state transformers
ST
= [Σ
→
Σ
⊥]
where
Σ
is the set of all states. However, although the denotational seman-tics provides a fundamental mathematical meaning to programs, it does not provide us with a direct definition of what it means to compute something. Meanwhile, the operational semantics ofWhiledoes provide us with a formal description of what it means to compute something. Given an initial state, we know precisely how a while program computes: it either computes for ever, gets stuck, or terminates yielding a final state (the result). Although the commands inWhileare important programming constructs, the language does not seem right as a fundamental definition of algorithm. Instead, wea simple description of a computing machine. We then give a definition of Tur-ing machine, which is more complicated description of a computTur-ing machine. Although more complicated, every computer scientist should know about Tur-ing machines! Finally, we give a definition of Church’s lambda-calculus. This provides a completely different way of describing computation which is much nearer to the notion of function rather than the notion of computing machine. What is amazing is that these different definitions are actually equivalent. In the Compilers course, you have seen the translation of a while language into register machines. Each course uses slightly different definitions, be-cause the different courses are emphasising different points. Despite this, ideas from each course transfer. Can you define a translation from While to the register machines described in this course?
Slide 2
Algorithms as Special Functions
Turing and Church’s equivalent definitions of algorithm capture the notion ofcomputable function: an algorithm expects some input, does some calculation and, if it terminates, returns a unique result.
We first study register machines, which provide a simple definition of algorithm. We describe theuniversal register machineand
introduce thehalting problem, which is probably the most famous example of a problem that is not computable.
We then move toTuring machines andChurch’s
λ
-calculus.Register Machines
The register machine gets its name from its one or more uniquely addressed registers, each of which holds a natural number. There are several versions of register machines. We are using the Minski register machines. The work
on register machines occurred in the 1950s and 1960s. One motivation was that people were trying to show that Hilbert’s 10th problem on Diophantine equations was undecidable. This was finally cracked in 1970s using Turing machines, and a simpler proof was given in the 1980s using register ma-chines. Register machines are (apparently) particularly suited to constructing Diophantine equations [Matiyasevich].
Slide 3
Register Machines, informally
Register machines operate on natural numbers
N
=
{
0,
1,
2, . . .
}
stored in (idealized) registers using the following “elementaryoperations”:
•
add1
to the contents of a register•
test whether the contents of a register is0
•
subtract1
from the contents of a register if it is non-zero•
jumps (“goto”)Slide 4
Register Machines
DefinitionAregister machine(sometimes abbreviated to RM) is specified by:
•
finitely manyregistersR
0,R
1, . . . ,R
n, each capable of storinga natural number;
•
aprogramconsisting of a finite list of instructions of the formlabel
:
body
where, fori
= 0,
1,
2, . . .
, the(i
+ 1)
th instruction has labelL
i. The instructionbodytakes the form:R
+L
′ add1to contents of registerRand jump to instruction labelledL′R
−L
′, L
′′ if contents ofRis >0, then subtract1and jump toL′, else jump toL′′HALT
stop executing instructionsSlide 5
Example
Registers
R
0R
1R
2Program
L
0:
R
1−L
1,
L
2L
1:
R
+ 0L
0L
2:
R
2−L
3,
L
4L
3:
R
+ 0L
2L
4:
HALT
Example ComputationL
iR
0R
1R
20
0
1
2
1
0
0
2
0
1
0
2
2
1
0
2
3
1
0
1
2
2
0
1
3
2
0
0
2
3
0
0
4
3
0
0
Exercise Consider the following program, acting on registers
R
0,
R
1,
R
2,
R
3:Program
L
0:
R
1−L
1,
L
6L
1:
R
2−L
2,
L
4L
2:
R
0+L
3L
3:
R
+ 3L
1L
4:
R
− 3L
5,
L
0L
5:
R
2+L
4L
6:
HALT
Give the example computation starting from initial configuration
(0,
2,
3,
0)
.Slide 6
Register Machine Configuration
A register machineconfigurationhas the form:
c
= (ℓ, r
0, . . . , r
n)
where
ℓ
= current label andr
i = current contents ofR
i.Notation“
R
i=
x
[in configurationc
]” meansc
= (ℓ, r
0, . . . , r
n)
with
r
i=
x
.Initial configurations
c
0= (0, r
0, . . . , r
n)
Slide 7
Register Machine Computation
Acomputationof a RM is a (finite or infinite) sequence of configurations
c
0, c
1, c
2, . . .
where
•
c
0= (0, r
0, . . . , r
n)
is an initial configuration;•
eachc
= (ℓ, r
0, . . . , r
n)
in the sequence determines the next configuration in the sequence (if any) by carrying out the program instruction labelledL
ℓ with registers containingr
0,. . . ,r
n.Slide 8
Halting Computations
For a finite computation
c
0, c
1, . . . , c
m, the last configurationc
m= (ℓ, r, . . .)
is ahaltingconfiguration: that is, the instructionlabelled
L
ℓ iseither
HALT
(a ‘ proper halt’)or
R
+L
, orR
−L, L
′ withR >
0
, orR
−L
′, L
withR
= 0
and there is no instruction labelledL
in the program (an‘erroneous halt’)
For example, the program
L
0:
R
+1
L
2L
1:
HALT
Notice that it is always possible to modify programs (without affecting their computations) to turn all erroneous halts into proper halts by adding extra
HALT
instructions to the list with appropriate labels.Slide 9
Non-halting Computations
There are computations which never halt. For example, the program
L
0:
R
1+L
0L
1:
HALT
only has infinite computation sequences(0, r),
(0, r
+ 1),
(0, r
+ 2), . . .
Slide 10
Graphical representation
•
One node in the graph for each instructionlabel
:
body
, with the node labelled by the register of the instruction body; notation[L]
denotes the register of the body of labelL
•
Arcs represent jumps between instructions•
Initial instructionSTART
.Instruction Representation R+L R+
/
/
[L] R− L, L′ [L] R−4
4
✐
✐
✐
✐
✐
✐
*
*
*
*
❯
❯
❯
❯
❯
❯
[L′] HALT HALT L0 START/
/
[L0] Slide 11Example
Registers
R
0R
1R
2Program
L
0:
R
−1L
1,
L
2L
1:
R
+0L
0L
2:
R
−2L
3,
L
4L
3:
R
+0L
2L
4:
HALT
Graphical RepresentationSTART
R
− 1*
*
R
+ 0j
j
R
− 2*
*
R
+ 0j
j
HALT
Claim: starting from initial configuration
(0,
0, x, y)
, this machine’s computation halts with configuration(4, x
+
y,
0,
0)
.The graphical representation is a bit confusing. There is one node in the graph for each instruction
label
:
body
. However, the nodes are only la-belled with the registers of the instruction bodies. For example, in slide 10, we have two nodes labelledR
+0. The top node corresponds to the
instruc-tion
L
1:
R
0+L
0, and the bottom node toL
3:
R
0+L
2. The initialin-struction
START
is essential, as the graphical representation looses the sequential ordering of instructions.ExerciseRecall the following program acting on registers
R
0,
R
1,
R
2,
R
3:Program
L
0:
R
1−L
1,
L
6L
1:
R
2−L
2,
L
4L
2:
R
+ 0L
3L
3:
R
+ 3L
1L
4:
R
3−L
5,
L
0L
5:
R
2+L
4L
6:
HALT
What is the graphical representation of this program?
Slide 12
Partial functions
Register machine computation isdeterministic: in any non-halting configuration, the next configuration is uniquely determined by the program.
So the relation between initial and final register contents defined by a register machine program is apartial function. . .
Definition Apartial functionfrom a set
X
to a setY
is specified by any subsetf
⊆
X
×
Y
satisfyingSlide 13
Partial Functions
Notation•
“f
(x) =
y
” means(x, y)
∈
f
•
“f
(x)
↓
” means∃
y
∈
Y
(f(x) =
y)
•
“f
(x)
↑
” means¬∃
y
∈
Y
(f
(x) =
y)
•
X
⇀Y
= set of all partial functions fromX
toY
X
Y
= set of all (total) functions fromX
toY
Definition. Apartial functionfrom a set
X
to a setY
istotalif it satisfiesf
(x)
↓
for all
x
∈
X
.Slide 14
Computable functions
Definition. The partial function
f
∈
N
n⇀N
is(register machine)computableif there is a register machine
M
with at leastn
+ 1
registersR
0,R
1, . . . ,R
n (and maybe more) such that for all(x
1, . . . , x
n)
∈
N
nand ally
∈
N
,the computation of
M
starting withR
0= 0
,R
1=
x
1, . . . ,R
n=
x
n and all other registers set to0
, halts withR
0=
y
The I/O convention is somewhat arbitrary: in the initial configuration, reg-isters
R
1, . . . ,R
n store the function’s arguments (with all others zeroed);in the halting configuration, register
R
0 stores it’s value (if any). Notice that there may be many different register machines that compute the same partial functionf
. Slide 15Example
Registers
R
0R
1R
2Program
L
0:
R
−1L
1,
L
2L
1:
R
+0L
0L
2:
R
−2L
3,
L
4L
3:
R
+0L
2L
4:
HALT
Graphical RepresentationSTART
R
− 1*
*
R
+ 0j
j
R
− 2*
*
R
+ 0j
j
HALT
If the machine starts with registers
(
R
0,
R
1,
R
2) = (0, x, y)
, then ithalts with registers
(
R
0,
R
1,
R
2) = (x
+
y,
0,
0)
.The notation is a little confusing. The slide states that, if the machine starts with registers
(
R
0,
R
1,
R
2) = (0, x, y)
, then it halts withregis-ters
(
R
0,
R
1,
R
2) = (x
+
y,
0,
0)
. This description focusses on regis-ters, and demonstrates thatf
(x, y)
,
x
+
y
is computable. (The no-tationf
(x, y)
,
x
+
y
means thatf(x, y)
‘is defined to be equal to’x
+
y
.) Compare this description with the description using configurations in slide 11: starting from initial configuration(0,
0, x, y)
, this machine’s com-putation halts with configuration(4, x
+
y,
0,
0)
. This description also gives information about the initial and final labels. The configuration(0,
0, x, y)
means that the first component is the initial label0
, the second component is initially set to zero and will eventually give the final answer when the compu-tation halts, and the third and fourth components provide the two input values of the function. From configuration(0,
0, x, y)
, this machine’s computation halts with configuration(4, x
+
y,
0,
0)
.Slide 16
Multiplication
f
(
x, y
)
,
xy
is computable
START
R
+ 3~
~⑥⑥
⑥⑥
⑥⑥
⑥⑥
R
− 1/
/
R
− 2/
/
R
+ 0`
`
❆❆
❆❆
❆❆
❆❆
HALT
R
− 3c
c
c
c
●●
●●
●●
●●
●
,
,
R
+ 2l
l
If the machine starts with registers
(
R
0,
R
1,
R
2,
R
3) = (0, x, y,
0)
, then it halts with registers(
R
0,
R
1,
R
2,
R
3) = (xy,
0, y,
0)
.Exercise Construct a register machine that computes the function
f
(x, y)
,
x
+
y
.The following arithmetic functions are all computable. The proofs are left as exercises. 1. Projection:
p(x, y)
,
x
2. Constant:c(x)
,
n
3. Truncated subtraction:x
−
·
y
,
(
x
−
y
ify
≤
x
0
ify > x
4. Integer division:x
div
y
,
(
integer part of
x/y
ify >
0
0
ify
= 0
5. Integer remainder:
x
mod
y
,
x
−
·
y(x
div
y)
6. Exponentiation base 2:e(x)
,
2
x7. Logarithm base 2:
log
2(x)
,
(
greatest
y
such that2
y≤
x
ifx >
0
Coding Programs as Numbers
So far, we have only seen how to write simple arithmetical operations as register-machine programs. The Turing/Church solution of the Entschei-dungsproblem and the Halting problem uses the fundamentally important idea that (formal descriptions of) algorithms can be the data on which al-gorithms act. Recall the following slide from lecture 1.
Slide 17
The Halting Problem
The Halting Problem is the decision problem with
•
the setS
of all pairs(A, D)
, whereA
is an algorithm andD
is some input datum on which the algorithm is designed to operate;•
the propertyA(D)
↓
holds for(A, D)
∈
S
if algorithmA
when applied toD
eventually produces a result: that is, eventually halts. Turing and Church’s work shows that the Halting Problem isunsolvable (undecidable): that is, there is no algorithm
H
such that, for all(A, D)
∈
S
,H
(A, D) = 1
A(D)
↓
= 0
otherwiseTo realise this idea of algorithms being used as input data in the context of Register Machines, we have to be able to code register-machine programs as numbers. (In general, such codings are often calledG ¨odel numberings.) To do this, first we have to code pairs of numbers and lists of numbers as numbers. There are many ways to do this. We fix upon one way.
Slide 18
Numerical Coding of Pairs
Definition Forx, y
∈
N
, define
hh
x, y
ii
,
2
x(2y
+ 1)
h
x, y
i
,
2
x(2y
+ 1)
−
1
Example27 = 0
b
11011 =
hh
0,
13
ii
=
h
2,
3
i
Resulthh−
,
−ii
gives a bijection betweenN
×
N
andN
+=
{
n
∈
N
|
n
6
= 0
}
.h−
,
−i
gives a bijection betweenN
×
N
andN
.Recall the definition of bijection from discrete maths.
The notation
0
b
11011
is sometimes used to emphasise that the number, in this case11011
, is in binary. We will also use the notation0
b
x
forx
∈
N
to denote the binary number ofx
. We investigate a few examples ofhh
x, y
ii
for small examples ofx
andy
:hh
0,
0
ii
= 1
hh
1,
0
ii
= 2
hh
2,
0
ii
= 4
hh
3,
0
ii
= 8
hh
0,
1
ii
= 3
hh
1,
1
ii
= 6
hh
2,
1
ii
= 12
. . .
hh
0,
2
ii
= 5
hh
1,
2
ii
= 10
hh
2,
2
ii
= 20
. . .
Slide 19
Numerical Coding of Pairs
Definition Forx, y
∈
N
, define
hh
x, y
ii
,
2
x(2y
+ 1)
h
x, y
i
,
2
x(2y
+ 1)
−
1
Sketch Proof of Result
It is enough to observe that
0
b
hh
x, y
ii
= 0
b
y
1 0
· · ·
0
x
number of0
s0
b
h
x, y
i
= 0
b
y
0 1
· · ·
1
x
number of1
swhere
0
b
x
,
x
in binary.,
means ‘is defined to be’.To show that
0
b
hh
x, y
ii
=
0
b
y
1 0
| {z }
· · ·
0
x0s
observe that
hh
x, y
ii
,
2
x(2y
+ 1) = 2
x+1y
+ 2
x.To show
hh−
,
−ii
:
N
×
N
→
N
+ is one-to-one, assume thathh
x
1, y
1ii
=
hh
x
2, y
2ii
, and eitherx
16
=
x
2 ory
16
=
y
2 or both. Sincehh
x
1, y
1ii
=
hh
x
2, y
2ii
, we have0
b
hh
x
1, y
1ii
= 0
b
hh
x
2, y
2ii
and hence0
b
y
11 0
· · ·
0
| {z }
x1 0s
=
0
b
y
21 0
| {z }
· · ·
0
x2 0s which cannot hold as either
x
16
=
x
2 ory
16
=
y
2or both.To show
hh−
,
−ii
:
N
×
N
→
N
+ is onto, assume not. We know thathh
0,
0
ii
= 1
. Hence, there must be a smallestn
∈
N
+ such thatn
=
hh
u, v
ii
for someu, v
∈
N
andn
+ 1
6
=
hh
x, y
ii
for allx, y
∈
N
. So,n
= 0
b
v
1 0
· · ·
0
| {z }
u0s and0
b
(n
+ 1) = 0
b
(n) + 1 = 0
b
v
1 0
· · ·
0
| {z }
u0s+1
. Ifu
6
= 0
, thenn
+ 1 =
hh
0, w
ii
where0
b
w
= 0
b
v
1 0
· · ·
0
| {z }
u−1 0s . Ifu
= 0
,number after the first one.
Here’s another proof! To prove
hh−
,
−ii
is one-to-one, assumehh
x
1, y
1ii
=
hh
x
2, y
2ii
: that is,2
x1(2y
1
+ 1) = 2
x2(2y
2+ 1)
If
x
1> x
2, then2
x1−x2(2y
1
+ 1) = 2y
2+ 1
which is impossible. Asimilar argument shows that
x
1< x
2 is impossible. Hence,x
1=
x
2 and2y
1+ 1 = 2y
2+ 1
, which implies thaty
1=
y
2 andhh−
,
−ii
is one-to-one.To prove
hh−
,
−ii
is onto, assume for contradiction that there is a smallestn
∈
N
such that there is nox, y
∈
N
withhh
x, y
ii
=
n
. Ifn
is even, thenn
= 2m
withm < n
. Hence,m
= 2
x1(2y
1
+ 1)
for somex
1, y
1∈
N
.Then
n
= 2
x1+1(2y
1
+ 1)
. Ifn
is odd, thenn
= 2m
+ 1 =
hh
0, m
ii
.Slide 20
Numerical Coding of Lists
Let
List
N
be the set of all finite lists of natural numbers, defined by:•
empty list:[ ]
•
list cons:x
::
ℓ
∈
List
N
ifx
∈
N
andℓ
∈
List
N
Slide 21
Numerical Coding of Lists
Let
List
N
be the set of all finite lists of natural numbers.For
ℓ
∈
List
N
, definep
ℓ
q
∈
N
by induction on the length of the listℓ
:
p
[]
q ,
0
p
x
::
ℓ
q ,
hh
x,
p
ℓ
q
ii
= 2
x(2
·
p
ℓ
q
+ 1)
Thus,p
[x
1, x
2, . . . , x
n]
q
=
hh
x
1,
hh
x
2,
· · · hh
x
n,
0
ii · · ·iiii
Slide 22Numerical Coding of Lists
Let
List
N
be the set of all finite lists of natural numbers.For
ℓ
∈
List
N
, definep
ℓ
q
∈
N
by induction on the length of the listℓ
:
p
[]
q ,
0
p
x
::
ℓ
q ,
hh
x,
p
ℓ
q
ii
= 2
x(2
·
p
ℓ
q
+ 1)
Examplesp
[3]
q
=
p
3 :: []
q
=
hh
3,
0
ii
= 2
3(2
·
0 + 1) = 8
p
[1,
3]
q
=
hh
1,
p
[3]
q
ii
=
hh
1,
8
ii
= 34
p
[2,
1,
3]
q
=
hh
2,
p
[1,
3]
q
ii
=
hh
2,
34
ii
= 276
Slide 23
Numerical Coding of Lists
Let
List
N
be the set of all finite lists of natural numbers.For
ℓ
∈
List
N
, definep
ℓ
q
∈
N
by induction on the length of the listℓ
:
p
[]
q ,
0
p
x
::
ℓ
q ,
hh
x,
p
ℓ
q
ii
= 2
x(2
·
p
ℓ
q
+ 1)
ResultThe function
ℓ
7→
p
ℓ
q
gives a bijection fromList
N
toN
.Slide 24
Numerical Coding of Lists
Let
List
N
be the set of all finite lists of natural numbers.For
ℓ
∈
List
N
, definep
ℓ
q
∈
N
by induction on the length of the listℓ
:
p
[]
q ,
0
p
x
::
ℓ
q ,
hh
x,
p
ℓ
q
ii
= 2
x(2
·
p
ℓ
q
+ 1)
ResultThe function
ℓ
7→
p
ℓ
q
gives a bijection fromList
N
toN
.Sketch Proof
The proof follows by observing that
0
b
p
[x
1, x
2, . . . , x
n]
q
= 1 0
| {z }
· · ·
0
xn0s1 0
· · ·
0
| {z }
xn −10s ···1 0
| {z }
· · ·
0
x10sTo prove
0
b
p
[x
1, x
2, . . . , x
n]
q
= 1 0
· · ·
0
1 0
· · ·
0
···1 0
· · ·
0
, weuse induction on the structure of
L
= [x
1, . . . , x
n]
. Base Case This is trivial as0
b
p
[]
q
= 0
.Inductive stepAssume
0
b
p
[x
1, x
2, . . . , x
k]
q
= 1 0
· · ·
0 1 0
· · ·
0
···1 0
· · ·
0
By the definitions, we have
0
b
p
[x, x
1, x
2, . . . , x
k]
q
= 0
b
hh
x,
p
[x
1, . . . , x
k]
q
ii
= 0
b
p
[x
1, . . . , x
n]
q
1 0
. . .
0
| {z }
x0s
.
The induction hypothesis now gives the result. Using this result,
p
L
q
is clearly one-to-one and onto. To convince yourself of this, choose a few binary numbersn
and give the corresponding listL
n such that0
b
p
L
nq
=
n
.Slide 25
Recall Register Machines
DefinitionAregister machine(sometimes abbreviated to RM) is specified by:
•
finitely manyregistersR
0,R
1, . . . ,R
n, each capable of storinga natural number;
•
aprogramconsisting of a finite list of instructions of the formlabel
:
body
where, fori
= 0,
1,
2, . . .
, the(i
+ 1)
th instruction has labelL
i. The instructionbodytakes the form:R
+L
′ add1to contents of registerRand jump to instruction labelledL′R
−L
′, L
′′ if contents ofRis >0, then subtract1and jump toL′, else jump toL′′HALT
stop executing instructionsSlide 26
Numerical Coding of Programs
If
P
is the RM programL
0:
body
0L
1:
body
1 . . .L
n:
body
nthen its numerical code is
p
P
q , p
[
p
body
0q
, . . . ,
p
body
nq
]
q
where the numerical code
p
body
q
of an instruction body is definedby:
p
R
+ iL
jq ,
hh
2i, j
ii
p
R
− iL
j,
L
kq ,
hh
2i
+ 1,
h
j, k
iii
p
HALT
q ,
0
Since
hh−
,
−ii
:
N
×
N
→
N
+,h−
,
−i
:
N
×
N
→
N
andp
−
q
:
List
N
→
N
are bijections, the functionsp
−
q
from bodies to natural numbers andp
−
q
from RM programs toN
are bijections.Slide 27
Recall Addition
f
(
x, y
)
,
x
+
y
is Computable
Registers
R
0R
1R
2Program
L
0:
R
−1L
1,
L
2L
1:
R
+0L
0L
2:
R
−2L
3,
L
4L
3:
R
+0L
2L
4:
HALT
Graphical RepresentationSTART
R
− 1*
*
R
+ 0j
j
R
− 2*
*
R
+ 0j
j
HALT
If the machine starts with registers
(
R
0,
R
1,
R
2) = (0, x, y)
, it halts with registers(
R
0,
R
1,
R
2) = (x
+
y,
0,
0)
.Slide 28
Coding of the RM for Addition
p
P
q , p
[
p
B
0q
, . . . ,
p
B
4q
]
q
wherep
B
0q
=
p
R
1−L
1,
L
2q
=
hh
(2
×
1) + 1,
h
1,
2
iii
=
hh
3,
9
ii
= 8
×
(18 + 1) = 152
p
B
1q
=
p
R
0+L
0q
=
hh
2
×
0,
0
ii
= 1
p
B
2q
=
p
R
2−L
3,
L
4q
=
hh
(2
×
2) + 1,
h
3,
4
iii
=
hh
5,
(8
×
9)
−
1
ii
=
hh
5,
71
ii
= 2
5×
((2
×
71) + 1) = 32
×
143 = 4576
p
B
3q
=
p
R
0+L
2q
=
hh
2
×
0,
2
ii
= 5
p
B
4q
=
p
HALT
q
= 0
starting with
R
0= 0
,R
1=
e
(the code of the program),R
2=
a
(code of the list of arguments), and all other registerszeroed:
•
decodee
as a RM programP
•
decodea
as a list of register valuesa
1, . . . , a
n•
carry out the computation of the RM programP
starting withR
0= 0
,R
1=
a
1, . . . , R
n=
a
n(and any otherregisters occurring in
P
set to0
).It is therefore important for you to understand what it means for a number
x
∈
N
to decode to a unique instructionbody
(x)
, and for a numbere
∈
N
to decode to a unique programprog
(e)
.Slide 29
Decoding Numbers as Bodies and Programs
Any
x
∈
N
decodes to a unique instructionbody
(x)
: ifx
= 0
thenbody
(x)
isHALT
,else (
x >
0
and) letx
=
hh
y, z
ii
inif
y
= 2i
is even, thenbody
(x)
isR
i+L
z,else
y
= 2i
+ 1
is odd, letz
=
h
j, k
i
inbody
(x)
isR
−i
L
j,
L
kSo any
e
∈
N
decodes to a unique programprog
(e)
, called the register machine program with indexe
:prog
(e)
,
L
0:
body
(x
0)
. . .L
n:
body
(x
n)
wheree
=
p
[x
0, . . . , x
n]
q
Slide 30
Example of
prog
(
e
)
•
786432 = 2
19+ 2
18= 0
b
110
. . .
0
| {z }
18 ”0”s=
p
[18,
0]
q
•
18 = 0
b
10010 =
hh
1,
4
ii
=
hh
1,
h
0,
2
iii
=
p
R
0−L
0,
L
2q
•
0 =
p
HALT
q
Soprog
(786432) =
L
0:
R
− 0L
0,
L
2L
1:
HALT
Notice that, when
e
= 0
, we have0 =
p
[]
q
soprog
(0)
is the program with an empty list of instructions, which by convention we regard as a RM that does nothing (i.e. that halts immediately). Also, notice in slide 26 the jump to a label with no body (an erroneous halt). Again, choose some numbers and see what the register-machine programs they correspond to.