State Code Function is Primitive Recursive

Theorem
Let $k \in \N^*$.

Let $e = \gamma \left({P}\right)$ be the code number of a URM program $P$.

Let $\left({n_1, n_2, \ldots, n_k}\right)$ be the input of $P$.

Let $S_k: \N^{k+2} \to \N$ be the function defined as:
 * $S_k \left({e, n_1, n_2, \ldots, n_k, t}\right)$ is the state code for $P$ at stage $t$ of computation of $P$.

If $e$ does not code a URM Program then $S_k = 0$.

Also, if $P$ terminates at stage $t_0$, then we put:
 * $\forall t > t_0: S_k \left({e, n_1, n_2, \ldots, n_k, t}\right) = S_k \left({e, n_1, n_2, \ldots, n_k, t_0}\right)$.

Then for all $k \ge 1$, the function $S_k$ is primitive recursive.

Proof
It can easily be seen that $S_k$ is a total function.

Suppose $e = \gamma \left({P}\right)$ for some URM program $P$.

At stage $0$, we are about to carry out instruction $1$ with the input $\left({n_1, n_2, \ldots, n_k}\right)$.

So we have:
 * $S_k \left({e, n_1, n_2, \ldots, n_k, 0}\right) = \begin{cases}

2^1 3^{n_1} 5^{n_2} \cdots p_{k+1}^{n_k} & : e \in \operatorname{Prog} \\ 0 & : \text{otherwise} \end{cases}$ where $\operatorname{Prog}$ is the set of code numbers of all URM programs.

We see that $S_k \left({e, n_1, n_2, \ldots, n_k, 0}\right)$ does not actually depend upon the actual program being run, beyond the fact that it matters whether it actually is a program or not.

Now $\operatorname{Prog}$ is a primitive recursive set.

So from results about primitive recursive functions, the relations defining the cases are primitive recursive.

We can also deduce from various results about primitive recursive functions that the functions given by the formulas $2^1 3^{n_1} 5^{n_2} \cdots p_{k+1}^{n_k}$ and $0$ are primitive recursive.

In particular, we use the results:
 * Multiplication is Primitive Recursive;
 * Exponentiation is Primitive Recursive;
 * Prime Enumeration Function is Primitive Recursive.

So from Definition by Cases is Primitive Recursive, $S_k \left({e, n_1, n_2, \ldots, n_k, 0}\right)$ is primitive recursive.

Now we want to be able to express $S_k \left({e, n_1, n_2, \ldots, n_k, t+1}\right)$ in terms of $e, \left({n_1, n_2, \ldots, n_k}\right), t$ and $S_k \left({e, n_1, n_2, \ldots, n_k, t}\right)$ using known primitive recursive functions.

We need to consider a number of cases:
 * 1) $e$ does not code a URM program;
 * 2) $e = \gamma \left({P}\right)$ and the computation halts at stage $t$ or earlier;
 * 3) $e = \gamma \left({P}\right)$ and the instruction to be carried out at stage $t$ is a Zero instruction;
 * 4) $e = \gamma \left({P}\right)$ and the instruction to be carried out at stage $t$ is a Successor instruction;
 * 5) $e = \gamma \left({P}\right)$ and the instruction to be carried out at stage $t$ is a Copy instruction;
 * 6) $e = \gamma \left({P}\right)$ and the instruction to be carried out at stage $t$ is a Jump instruction.

These cases are clearly mutually exclusive and exhaustive.

First we need to check that each case corresponds to a primitive recursive relation.


 * The set $\operatorname{Prog}$ is primitive recursive so its complement is also primitive recursive.

So 1. is a primitive recursive relation.


 * So we have that $e$ codes a URM program. Call that program $P$.

From the definition of state code, we see that if a computation halts at stage $t$ or earlier, then the number of the instruction to be carried out at stage $t$ is greater than the number of instructions in $P$.

From the definition of the code number of $P$, the number of instructions in $P$ is $\operatorname{len} \left({e}\right)$ where $\operatorname{len} \left({e}\right)$ is the length of $e$, which is primitive recursive.

Now let $r = S_k \left({e, n_1, n_2, \ldots, n_k, t}\right)$.

Let $\left({r}\right)_j$ be defined as the prime exponent function.

By the definition of the state code, the number of the instruction to be carried out at stage $t$ is $\left({r}\right)_1$, which is primitive recursive.

So 2. can be expressed as:
 * $e \in \operatorname{Prog} \text { and } \left({S_k \left({e, n_1, n_2, \ldots, n_k, t}\right)}\right)_1 > \operatorname{len} \left({e}\right)$

Both $\operatorname{Prog}$ and $\left({r}\right)_1$ are primitive recursive, so from Set Operations on Primitive Recursive Relations, 2. is a primitive recursive relation.


 * So, let the number of the instruction to be carried out at stage $t$ be $a = \left({S_k \left({e, n_1, n_2, \ldots, n_k, t}\right)}\right)_1$.

From the definition of the code number of $P$, the code number of this instruction is $\left({e}\right)_a$.

Now from Set of Codes for URM Instructions is Primitive Recursive, each of the sets $\operatorname{Zinstr}$, $\operatorname{Sinstr}$, $\operatorname{Cinstr}$ and $\operatorname{Jinstr}$ are primitive recursive.

So each of 3. to 6. above can be expressed as:
 * $e \in \operatorname{Prog} \text { and } a \le \operatorname{len} \left({e}\right) \text { and } \left({e}\right)_a \in \operatorname{Instr}$

and is a primitive recursive relation.

So relations 1. to 6. are all primitive recursive.

Now we need to show how, in each case, $S_k \left({e, n_1, n_2, \ldots, n_k, t+1}\right)$ can be obtained from $e, \left({n_1, n_2, \ldots, n_k}\right), t$ and $S_k \left({e, n_1, n_2, \ldots, n_k, t}\right)$ using known primitive recursive functions.

First, if $e$ does not code a URM program then $S_k \left({e, n_1, n_2, \ldots, n_k, t+1}\right) = 0$, which is primitive recursive.

Second, we have adopted the convention that if $P$ has halted, then $S_k$ does not change.

So if $P$ halts at or before stage $t$, we have that $S_k \left({e, n_1, n_2, \ldots, n_k, t+1}\right) = S_k \left({e, n_1, n_2, \ldots, n_k, t}\right)$

Next, we look at the individual commands.

As an example we will investigate the Successor command. The others are treated similarly.

Suppose the instruction to be carried out at stage $t$ is a Successor command.

We know that the code number $c$ is given by $c = \left({e}\right)_a$ where $a = \left({S_k \left({e, n_1, n_2, \ldots, n_k, t}\right)}\right)_1$.

Suppose the instruction is $S \left({n}\right)$. Then $c = 6 n$.

So $n = \operatorname{quot} \left({6, n}\right)$ which is recursive from Quotient and Remainder are Primitive Recursive.

This instruction adds $1$ to the number in $R_n$.

This increases the exponent $p_{n+1}$ in the state code by $1$.

This is achieved by multiplying $S_k \left({e, n_1, n_2, \ldots, n_k, t}\right)$ by $p \left({n+1}\right)$, where $p \left({n+1}\right)$ is the prime enumeration function which is primitive recursive.

Since the instruction to be carried out at stage $t$ is a Successor the instruction number at stage $t+1$ is $a+1$ so the factor $2^a$ in $S_k \left({e, n_1, n_2, \ldots, n_k, t}\right)$ is replaced by $2^{a+1}$.

So:
 * $S_k \left({e, n_1, n_2, \ldots, n_k, t+1}\right) = 2 \times p_{n+1} \times S_k \left({e, n_1, n_2, \ldots, n_k, t}\right)$

where $n = \operatorname{quot} \left({6, n}\right)$, $c = \left({e}\right)_a$ and $a = \left({S_k \left({e, n_1, n_2, \ldots, n_k, t}\right)}\right)_1$.

This is the required expression for $S_k \left({e, n_1, n_2, \ldots, n_k, t+1}\right)$ obtained by substitution from primitive recursive functions.

The proofs for $\operatorname{Zinstr}$, $\operatorname{Cinstr}$ and $\operatorname{Jinstr}$ are along the same lines.

In each case, the value of $S_k \left({e, n_1, n_2, \ldots, n_k, t+1}\right)$ can be obtained by substitution from primitive recursive functions (but I'd hate to have to do the calculations on my fingers).

Thus by Definition by Cases is Primitive Recursive, $S_k \left({e, n_1, n_2, \ldots, n_k, t+1}\right)$ is primitive recursive.

Hence $S_k$ is defined by primitive recursion from functions known to be primitive recursive.

Hence the result.

Note
The function $S_k$ starts with an arbitrary sequence of numbers $\left({e, n_1, n_2, \ldots, n_k, t}\right)$.

If it so happens that $e$ is the code of some program $P$, then $\left({n_1, n_2, \ldots, n_k}\right)$ are taken to be the state of $R_1, R_2, \ldots, R_k$ before $P$ starts executing.

Then we determine what would happen when $P$ is left to run for $t$ stages (during which time it may have terminated).

Then we examine the state of $P$ at that point.

Then we calculate the state code for that state, and output it.

Of course, if $e$ does not actually code any program, then we fall at the first fence and output zero.