User:IssaRice/Computability and logic/Decrement command in loop programs

From Machinelearning

(started typing this up as a question for math SE, but I think I got it)

Herbert Enderton’s Computability Theory: An Introduction to Recursion Theory defines a loop program as one made up of the following commands:

  1. The clear command Xn0 that sets the value of the variable Xn to zero.
  2. The increment command XnXn+1 that increases the value of Xn by one.
  3. The copy command XnXm that sets the value of Xn to that of Xm and leaves the value of Xm unchanged.
  4. The loop command loopXn;P;endloopXn that takes a program P and executes it Xn times. Here the value of Xn may be changed inside the loop, but this will not affect the number of times P is executed. (This ensures the overall program always terminates, assuming P does.)

The output of a program is always stored in X0. A function f:NkN is loop-computable if there is a loop program such that for every xNk the program started with (X1,,Xk)=x (and zero in all the other variables) halts with f(x) in X0.

The book then states (without proof, as far as I can tell) that a function taking values in N is loop-computable if and only if it is primitive recursive.

I am confused that the above definition of a loop program does not have a decrement command. When I look at other definitions I see that they have a decrement command.

I have noticed that I can implement a decrement program given if-else conditionals, as follows:

loop X₁
    if counter != 0
        X₀ ← X₀ + 1
    endif
    counter ← counter + 1
endloop X₁

n0n1, so we just need a program that checks if the input is at least 1:

loop X₁
     X₀ ← 1
endloop X₁

If the input is at least 1, the program enters the loop, setting X0 to one each time, resulting in 1 as the final output. If the input is 0, the loop is skipped ("loop zero times") and we return the default value of 0.

Now we need to figure out how to write a general conditional program like:

if condition:
    A
else:
    B

We can do this as follows:

X₂ ← χ(condition)
X₃ ← χ(¬condition)
loop X₂
    A
endloop X₂
loop X₃
    B
endloop X₃

Python code:

def at_least_one(x):
    result = 0
    for _ in range(x):
        result = 1
    return result

def decrement(n):
    result = 0
    counter = 0
    for _ in range(n):
        for _ in range(at_least_one(counter)):
            result += 1
        counter += 1
    return result