# LOOP (programming language)

LOOP is a simple register language that precisely captures the primitive recursive functions. The language is derived from the counter-machine model. Like the counter machines the LOOP language comprises a set of one or more unbounded registers, each of which can hold a single non-negative integer. A few arithmetic instructions (like 'CleaR', 'INCrement', 'DECrement', 'CoPY', ...) operate on the registers. The only control flow instruction is 'LOOP x DO ... END'. It causes the instructions within its scope to be repeated x times. (Changes of the content of register x during the execution of the loop do not affect the number of passes.)

## History

The LOOP language was formulated in a 1967 paper by Albert R. Meyer and Dennis M. Ritchie. They showed the correspondence between the LOOP language and primitive recursive functions.
The language also was the topic of the unpublished PhD thesis of Ritchie.
It was also presented by Uwe Schöning, along with GOTO and WHILE.

## Design philosophy and features

In contrast to GOTO programs and WHILE programs, LOOP programs always terminate. Therefore, the set of functions computable by LOOP-programs is a proper subset of computable functions (and thus a subset of the computable by WHILE and GOTO program functions).
Meyer & Ritchie proved that each primitive recursive function is LOOP-computable and vice versa.
An example of a total computable function that is not LOOP computable is the Ackermann function.

## Formal definition
### Syntax

LOOP-programs consist of the symbols LOOP, DO, END, :=, + and ; as well as any number of variables and constants. LOOP-programs have the following syntax in modified Backus–Naur form:
    ${\begin{array}{lrl}P&::=&x_{i}:=0\\&|&x_{i}:=x_{i}+1\\&|&P;P\\&|&{\mathtt {LOOP}}\,x_{i}\,{\mathtt {DO}}\,P\,{\mathtt {END}}\end{array}}$
Here, $Var:=\{x_{0},x_{1},...\}$ are variable names and $c\in \mathbb {N}$ are constants.

### Semantics

If P is a LOOP program, P is equivalent to a function $f:\mathbb {N} ^{k}\rightarrow \mathbb {N}$. The variables $x_{1}$ through $x_{k}$ in a LOOP program correspond to the arguments of the function $f$, and are initialized before program execution with the appropriate values. All other variables are given the initial value zero. The variable $x_{0}$ corresponds to the value that $f$ takes when given the argument values from $x_{1}$ through $x_{k}$.
A statement of the form
means the value of the variable $x_{i}$ is set to 0.
A statement of the form
means the value of the variable $x_{i}$ is incremented by 1.
A statement of the form
represents the sequential execution of sub-programs $P_{1}$ and $P_{2}$, in that order.
A statement of the form
means the repeated execution of the partial program $P$ a total of $x$ times, where the value that $x$ has at the beginning of the execution of the statement is used. Even if $P$ changes the value of $x$, it won't affect how many times $P$ is executed in the loop. If $x$ has the value zero, then $P$ is not executed inside the LOOP statement. This allows for branches in LOOP programs, where the conditional execution of a partial program depends on whether a variable has value zero or one.

### Creating "convenience instructions"

From the base syntax one create "convenience instructions". These will not be subroutines in the conventional sense but rather LOOP programs created from the base syntax and given a mnemonic. In a formal sense, to use these programs one needs to either (i) "expand" them into the code – they will require the use of temporary or "auxiliary" variables so this must be taken into account, or (ii) design the syntax with the instructions 'built in'.

#### Example

The k-ary projection function $U_{i}^{k}(x_{1},...,x_{i},...,x_{k})=x_{i}$ extracts the i-th coordinate from an ordered k-tuple.
In their seminal paper Meyer & Ritchie made the assignment $x_{j}:=x_{i}$ a basic statement. As the example shows the assignment can be derived from the list of basic statements.
To create the $\operatorname {assign}$ instruction use the block of code below. $x_{j}:=\operatorname {assign} (x_{i})$ =$_{equiv}$
Again, all of this is for convenience only; none of this increases the model's intrinsic power.

## Example Programs
### Addition

Addition is recursively defined as:
    ${\begin{aligned}a+0&=a,&{\textrm {(1)}}\\a+S(b)&=S(a+b).&{\textrm {(2)}}\end{aligned}}$
Here, S should be read as "successor".
In the hyperoperater sequence it is the function $\operatorname {H_{1}}$
$\operatorname {H_{1}} (x_{1},x_{2})$ can be implemented by the LOOP program ADD( x$_1$, x$_2$)

### Multiplication

Multiplication is the hyperoperation function $\operatorname {H_{2}}$
$\operatorname {H_{2}} (x_{1},x_{2})$ can be implemented by the LOOP program MULT( x$_1$, x$_2$ )
The program uses the ADD program as a "convenience instruction". Expanded, the MULT program is a LOOP-program with two nested LOOP instructions. ADD counts for one.

### More hyperoperators

Given a LOOP program for a hyperoperation function $\operatorname {H_{i}}$, one can construct a LOOP program for the next level
$\operatorname {H_{3}} (x_{1},x_{2})$ for instance (which stands for exponentiation) can be implemented by the LOOP program POWER( x$_1$, x$_2$ )
The exponentiation program, expanded, has three nested LOOP instructions.

### Predecessor

The predecessor function is defined as
    $\operatorname {pred} (n)={\begin{cases}0&{\mbox{if }}n=0,\\n-1&{\mbox{otherwise}}\end{cases}}$.
This function can be computed by the following LOOP program, which sets the variable $x_{0}$ to $pred(x_{1})$.
Expanded, this is the program
This program can be used as a subroutine in other LOOP programs. The LOOP syntax can be extended with the following statement, equivalent to calling the above as a subroutine:
Remark: Again one has to mind the side effects. The predecessor program changes the variable x$_2$, which might be in use elsewhere. To expand the statement x$_0$ := x$_1$ ∸ 1, one could initialize the variables x$_n$, x$_{n+1}$ and x$_{n+2}$ (for a big enough n) to 0, x$_1$ and 0 respectively, run the code on these variables and copy the result (x$_n$) to x$_0$. A compiler can do this.

### Cut-off subtraction

If in the 'addition' program above the second loop decrements x$_0$ instead of incrementing, the program computes the difference (cut off at 0) of the variables $x_{1}$ and $x_{2}$.
Like before we can extend the LOOP syntax with the statement:

### If then else

An if-then-else statement with if x$_1$ > x$_2$ then P1 else P2: