Take a machine whose command system is exactly the same as in the language of brainfuck, but which runs on a tape, in the cells of which you can put any integers. Overflow in arithmetic operations does not occur, the "+" command applied to a positive number will always give a positive result, etc. The question is: is it possible to work on such a machine, what problems will arise and how to get around them?
The advantages are obvious: since there are no overflows, then there is no need for long arithmetic, you can work equally with arrays of any length, etc. But rather quickly, we notice that our favorite way of cleaning the cell ("[-]") does not work: if the cell had a negative value, then the program loops. Similarly, we cannot freely use the copy command "[-> + <]" - it also works only for non-negative numbers.
It turns out that when programming, we need to carefully monitor the signs of the contents of the cells (the easiest way is to prevent the appearance of negative numbers in general), and if a number appears whose sign is unknown, work with it in a special way
')
Here we will consider two tasks: first, we will program the operator “if (a> b) C; else D; ”where a and b are non-negative, and C and D are some actions, and secondly, we will learn to reset, copy and determine the sign of an arbitrary number.
Conditional operator.
So, two non-negative numbers a and b are written on the tape (in which cells we choose ourselves). We need to compare these numbers and perform, depending on the result, one of the actions C or D. All cells used (including those in which a and b were located) should be zeroed out.
Rewrite the operator in this form:
h = 1;
while (h) {
if (a == 0) {D; a = b = 1; h = 0; }
if (b == 0) {C; a = b = 1; h = 0; }
a--; b--;
}
If a and b were non-negative, then negative numbers in such a program cannot appear. To implement the operator if (a == 0) B; create a block of 4 cells “a 0 1 0” on the ribbon and execute the commands “[>] >> [B>]”. If at the beginning the carriage was on the first cell of the block, then at the end it will be on its 4th cell in any embodiment.
The program will look like this (we assume that the caret and the number “a” are in cell 0, and the number “b” is in cell 4):
>> + [
<< [>] >> [C - << + >>>> [-] + <]
> [<] << [D - >> + <<<< [-] +>]
<- >>>> - <<]
The unit h from the “a 0 1 0 b” block is used as the variable h - all the same, after its zeroing, there will be no effective comparisons.
You can slightly simplify the program by writing it in the form
h = 1;
while (a) {
if (b == 0) {C; a = b = 1; h = 0; }
a--; b--;
}
if (h) {D; h = b = 0; }
Will turn out
>>> + <<<
[> [>] >> [C - << + <[-] + >>>>]
<<< - <-]
> [-] >> [D-]
The program operation time is linear from max (a, b).
Number with an unknown sign
Let the tape be a number a, about which we do not know whether it is positive or negative. We need it: (1) to zero, (2) to copy to another cell and (3) to perform the action F in case a> 0. We will solve all three tasks together.
How to solve them? In the same way as we are looking for a nonzero cell on the tape of the Turing machine, run left and right until we stumble on 0:
if (a) {
x = 1;
while (x) {
y = 2 * x;
while (x) {
x--; a ++; b--;
if (a == 0) x = y = 0;
}
x = 2 * y;
while (y) {
y--; a--; b ++;
if (a == 0) {F; x = y = 0; }
}
}
}
The variable a will be copied to b, and actions F are performed only if the initial value of a was positive. Translation on bf:
[>> + <<<<< +
[[- <++ >> + <]>
[-> -> + [>] >>
[- <<<< [-] << [-]]
<<<<<]
<< [-> ++> + <<] >>
[-> +> - [>] >>
[F - <<<< [-] <[-]]
<<<<<]
<]]
The running time is also linear from abs (a). Here it should be noted that in the blocks if (a == 0) x = y = 0; the carriage goes far away from its place, and remains there. But this is not very important, since the cycles at this moment end.
What? Anything that could be done on an 8-bit BF machine can be done here? Alas. There is one task that a machine with infinite bit is too tough. Namely, cleaning the tape section. If we are not given a piece that is guaranteed to be filled with zeros, we will not be able to create it. Although I can not prove it: (