LOOP (programming language)

From HandWiki
Short description: Programming language


LOOP is a simple register language that precisely captures the primitive recursive functions.[1] 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.[2] 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.[3][4]

It was also presented by Uwe Schöning, along with GOTO and WHILE.[5]

Design philosophy and features

In contrast to GOTO programs and WHILE programs, LOOP programs always terminate.[6] 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).[7]

Meyer & Ritchie proved that each primitive recursive function is LOOP-computable and vice versa.[2][5]

An example of a total computable function that is not LOOP computable is the Ackermann function.[8]

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:

[math]\displaystyle{ \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} }[/math]

Here, [math]\displaystyle{ Var := \{ x_0, x_1, ... \} }[/math] are variable names and [math]\displaystyle{ c \in \mathbb{N} }[/math] are constants.

Semantics

If P is a LOOP program, P is equivalent to a function [math]\displaystyle{ f: \mathbb{N}^k \rightarrow \mathbb{N} }[/math]. The variables [math]\displaystyle{ x_1 }[/math] through [math]\displaystyle{ x_k }[/math] in a LOOP program correspond to the arguments of the function [math]\displaystyle{ f }[/math], and are initialized before program execution with the appropriate values. All other variables are given the initial value zero. The variable [math]\displaystyle{ x_0 }[/math] corresponds to the value that [math]\displaystyle{ f }[/math] takes when given the argument values from [math]\displaystyle{ x_1 }[/math] through [math]\displaystyle{ x_k }[/math].

A statement of the form

xi := 0

means the value of the variable [math]\displaystyle{ x_i }[/math] is set to 0.

A statement of the form

xi := xi + 1

means the value of the variable [math]\displaystyle{ x_i }[/math] is incremented by 1.

A statement of the form

P1; P2

represents the sequential execution of sub-programs [math]\displaystyle{ P_1 }[/math] and [math]\displaystyle{ P_2 }[/math], in that order.

A statement of the form

LOOP x DO P END

means the repeated execution of the partial program [math]\displaystyle{ P }[/math] a total of [math]\displaystyle{ x }[/math] times, where the value that [math]\displaystyle{ x }[/math] has at the beginning of the execution of the statement is used. Even if [math]\displaystyle{ P }[/math] changes the value of [math]\displaystyle{ x }[/math], it won't affect how many times [math]\displaystyle{ P }[/math] is executed in the loop. If [math]\displaystyle{ x }[/math] has the value zero, then [math]\displaystyle{ P }[/math] 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 [math]\displaystyle{ U_i^k (x_1, ..., x_i, ..., x_k) = x_i }[/math] extracts the i-th coordinate from an ordered k-tuple.

In their seminal paper [2] Meyer & Ritchie made the assignment [math]\displaystyle{ x_j := x_i }[/math] a basic statement. As the example shows the assignment can be derived from the list of basic statements.

To create the [math]\displaystyle{ \operatorname{assign} }[/math] instruction use the block of code below. [math]\displaystyle{ x_j := \operatorname{assign}(x_i) }[/math] =equiv

xj := 0;
LOOP xi DO
  xj := xj + 1
END

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:

[math]\displaystyle{ \begin{align} a + 0 &= a , & \textrm{(1)}\\ a + S (b) &= S (a + b). & \textrm{(2)} \end{align} }[/math]

Here, S should be read as "successor".

In the hyperoperater sequence it is the function [math]\displaystyle{ \operatorname{H_1} }[/math]

[math]\displaystyle{ \operatorname{H_1}(x_1,x_2) }[/math] can be implemented by the LOOP program ADD( x1, x2)

LOOP x1 DO
  x0 := x0 + 1
END;
LOOP x2 DO
  x0 := x0 + 1
END

Multiplication

Multiplication is the hyperoperation function [math]\displaystyle{ \operatorname{H_2} }[/math]

[math]\displaystyle{ \operatorname{H_2}(x_1,x_2 ) }[/math] can be implemented by the LOOP program MULT( x1, x2 )

x0 := 0;
LOOP x2 DO
  x0 := ADD( x1, x0)
END

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 [math]\displaystyle{ \operatorname{H_i} }[/math], one can construct a LOOP program for the next level

[math]\displaystyle{ \operatorname{H_3}(x_1,x_2) }[/math] for instance (which stands for exponentiation) can be implemented by the LOOP program POWER( x1, x2 )

x0 := 1;
LOOP x2 DO
  x0 := MULT( x1, x0 )
END

The exponentiation program, expanded, has three nested LOOP instructions.

Predecessor

The predecessor function is defined as

[math]\displaystyle{ \operatorname{pred}(n) = \begin{cases} 0 & \mbox{if }n=0, \\ n-1 & \mbox{otherwise}\end{cases} }[/math].

This function can be computed by the following LOOP program, which sets the variable [math]\displaystyle{ x_0 }[/math] to [math]\displaystyle{ pred(x_1) }[/math].

/* precondition: x2 = 0 */
LOOP x1 DO
  x0 := x2;
  x2 := x2 + 1
END

Expanded, this is the program

/* precondition: x2 = 0 */
LOOP x1 DO
  x0 := 0;
  LOOP x2 DO
    x0 := x0 + 1
  END;
  x2 := x2 + 1
END

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:

x0 := x1 ∸ 1

Remark: Again one has to mind the side effects. The predecessor program changes the variable x2, which might be in use elsewhere. To expand the statement x0 := x1 ∸ 1, one could initialize the variables xn, xn+1 and xn+2 (for a big enough n) to 0, x1 and 0 respectively, run the code on these variables and copy the result (xn) to x0. A compiler can do this.

Cut-off subtraction

If in the 'addition' program above the second loop decrements x0 instead of incrementing, the program computes the difference (cut off at 0) of the variables [math]\displaystyle{ x_1 }[/math] and [math]\displaystyle{ x_2 }[/math].

x0 := x1
LOOP x2 DO
  x0 := x0 ∸ 1
END

Like before we can extend the LOOP syntax with the statement:

x0 := x1 ∸ x2

If then else

An if-then-else statement with if x1 > x2 then P1 else P2:

xn1 := x1 ∸ x2;
xn2 := 0;
xn3 := 1;
LOOP xn1 DO
  xn2 := 1;
  xn3 := 0
END;
LOOP xn2 DO
  P1
END;
LOOP xn3 DO
  P2
END;

See also

Notes and references

Bibliography