I/D machine Turing-completeness proof

This page contains a proof by User:ais523 that the I/D machine is Turing-complete. It does this by introducing an intermediate language, "ErrorBucket", then gives a compiler from cyclic tag to ErrorBucket and ErrorBucket to the I/D machine.

ErrorBucket
ErrorBucket is a (very special-purpose, and probably not usable outside this page) esoteric programming language. It uses the following data model:


 * There are five queue elements (each of which is an atom / enum value, i.e. the only thing you can do with queue elements is compare them to see if they're equal): inactive data (or ), active data (or  ), inactive bucket (or  ), active bucket (or  ), and inactive error (or  ). There's no such thing as an active error; attempting to activate an inactive error is undefined behaviour (i.e. an erroneous thing to do). There's an infinite supply of each of these queue elements (i.e. a queue can contain multiple copies of the same element, if necessary).
 * There are two queues: the data queue and the bit bucket. The data queue is read/write, supporting the queue operations "push" (i.e. adding an element to the back of the queue), "peek" (i.e. looking at the element at the front of the queue, without changing the queue), "activate" (which replaces the first element of the queue with an active version, and thus requires it to not be an inactive error; this is undefined behaviour if the element is active or an inactive error), and "shift" (i.e. removing the front element of the queue without inspecting it). The bit bucket is write-only; it only supports pushes, not peeks or shifts. Both queues can contain queue elements, but nothing else.
 * At any given time, there may or may not be a selected queue. It's possible either for one of the queues to be selected, or for neither queue to be selected; it is never possible for both queues to be selected at once. (This is a small amount of additional state beyond that stored in the queues.)

The initial state upon running any program is as follows:


 * The active queue contains two elements: active data at the front, inactive data at the back (i.e. ).
 * The bit bucket is initially empty (although its initial state is not really observable anyway).
 * Neither queue is selected.

An ErrorBucket program consists of a sequence of commands, which are run in a loop forever. No command takes an argument, i.e. each command makes a small discrete modification to the program's state. Here are the commands:


 * Activate
 * This command is undefined behaviour if a queue is selected. It activates the first element of the data queue; this will also be undefined behaviour, unless the element in question is inactive data or inactive bucket. An activate command must always be followed by a select command; a program is syntactically incorrect if this rule is not followed.


 * Push Bucket
 * This command is undefined behaviour unless a queue is selected. It pushes an inactive bucket element to the end of the selected queue, then deselects the queue.


 * Continue
 * This command is undefined behaviour if a queue is selected. It shifts and discards the first element of the data queue. This is undefined behaviour unless the data queue contains at least two elements at the time (the data queue is never supposed to become completely empty).


 * Push Data
 * This command is undefined behaviour unless a queue is selected. It pushes an inactive data element to the end of the selected queue, then deselects the queue.


 * Push Error
 * This command is undefined behaviour unless a queue is selected. It pushes an inactive error element to the end of the selected queue. Unlike the other "push" commands, it does not deselect the queue.


 * Select
 * This command is undefined behaviour if a queue is selected. It peeks at the first element of the data queue; if this is inactive, it causes undefined behaviour; if it's active, it selects the corresponding queue (i.e. if the element is active data, the data queue is selected, if the element is active bucket, the bit bucket is selected).

As a special restriction (beyond the fact that each  must be followed by an  ), the last few commands of the program (before it loops) must be. (A program that does not end with this sequence of seven commands is syntactically invalid.) Of course, the programmer will need to write the program in such a way that this mandatory sequence of commands fits into the flow of their program, not causing any undefined behaviour or other unwanted effects on the program state.

Compiling cyclic tag to ErrorBucket
We use the following definition of cyclic tag programs: we have a command list that is a sequence of strings of Booleans, and a storage, a queue of Booleans. (The program itself simply defines the initial states of the command list and of the storage.) Execution proceeds by performing the following steps in a loop:


 * 1) Shift the storage (i.e. remove its head element).
 * 2) If "true" was shifted this way, append a copy of the first string in the program to the tail of the storage. (If "false" was shifted, no append is done.)
 * 3) Move the first string in the program to the end.

This is well-known to be a Turing-complete construction (and is equivalent to that in Bitwise Cyclic Tag, although Bitwise Cyclic Tag has a lower-level concept of how the appending happens). We'll also add the (without-loss-of-generality) restrictions that the storage never becomes shorter than 2 elements, and that the initial value of the storage starts with "true" (you can easily cause this situation to come about by simply running the program until a state in which the storage starts with "true"; there has to be one somewhere in the initial storage or the program has no way to append to it, causing the storage to empty).

First, giving the translation without explanation (a proof that this is correct will be given subsequently):


 * The very first part of the ErrorBucket program is a fixed.
 * The next part of the ErrorBucket program represents the initial storage (minus its leading 1), encoded as follows:
 * "true" becomes
 * "false" becomes
 * Next comes another fixed part, a single.
 * This is followed by the representation of the command list. Each string within the command list is written in order, represented as follows:
 * An empty string becomes . If the string is nonempty, its translation instead consists of , followed by the Booleans that make up the string, followed by  . (Note that the translation of a Boolean always starts with  , fulfilling the requirement that   is always followed by  .)
 * Each Boolean in the string is then encoded the same way as above:
 * "true" becomes
 * "false" becomes
 * Immediately after each command comes an additional, unless it's the last command in the program, in which case   is used instead (to fulfil the requirement that the program must end with  ; the leading   will be present because the encoding of every command ends in  ).

So how does this work? First, we consider program initialisation. At the very start of the program, the data queue is. So  (where x is   or  ) will select the data queue, push   or   accordingly onto it, and then deselect the queue, leaving everything in the same state apart from the newly pushed element. Finally, the  at the end of the initialisation removes the leading. In other words, the eventual state of the data queue will be equal to the initial storage of the cyclic tag system, expressed as  for true or   for false. The bit bucket's state is of course irrelevant, and no queue is selected.

We can then specify our invariant for mapping between a cyclic tag state and an ErrorBucket state; just before each cyclic tag command runs, the ErrorBucket state consists of no selected queue, with the data queue containing the cyclic tag's storage encoded using /. This is clearly true post-initialisation. Meanwhile, just after the command runs, the encoding is slightly different; it consists of a leading, followed by the same encoding as before. (This makes the purpose of the linking  that comes after each command other than the last clear; it deletes the leading , putting the data queue back into the expected format.)

Next, we check that the compiled version of each cyclic tag command retains this invariant:
 * If the command is empty, it doesn't matter what the head of the storage is; we're either appending the empty string to it or not appending the empty string to it, and both of these choices have the same result. So the only effect of the cyclic tag command is to remove the head of the storage. We do this using ; the first element of the storage corresponds to   or   in the data queue, and   removes half of this element, leaving it as just  . So we've gone from the encoding of the storage using the no-  encoding to the encoding of the shifted version of the storage with a leading , just as required.
 * If the command is not empty, we start with, activating the first element of the data queue (it's now   or  ; note that this activation is defined because we know that the queue was nonempty and thus must have started with   or  ). If the element happens to be  , then just like at the start of the program,   will push   onto the data queue (the equivalent of pushing   onto the storage), and likewise   will push   and thus emulate pushing  . So in that case – where the first element of the storage was   – a copy of the command will get appended to the storage, just as required. Meanwhile, if the first element of the storage was  , meaning that we have a   at the start of the data queue, in order to emulate cyclic tag semantics we need to not append the command to the storage. The ErrorBucket program handles that by pushing all the  s and  s onto the bit bucket instead, which clearly has no useful effect (this is because the   will now select the bitbucket, with the subsequent   or   uselessly pushing onto it and then deselecting the bit bucket). Once we're done, the final   of the command will remove the activated   or  , leaving us with the encoding of the shifted storage preceded by  , as required.

Finally, what happens at the end of the ErrorBucket program, when it loops back round? Due to our invariant, the queue at this stage is encoded as  followed by the encoding of the storage. We then run a mandatory ; the   converts the leading   to  ; then   will push   onto the bit bucket (which of course has no effect). Then the preamble at the start of the program will run; there's still a  at the start of the data queue, so all the   and   will push   or   respectively onto the bit bucket, again doing nothing. The only remaining character before the encoding of the first command of the cyclic tag program is a ; that will remove the initial , restoring our invariant. So the cyclic tag commands will run in sequence forever (with all the characters that aren't part of an encoding of a command doing nothing but shifting off stray  or   characters or pushing onto the bit bucket), meaning that the ErrorBucket program emulates the cyclic tag program.

Compiling ErrorBucket to the I/D machine
The mechanical translation from ErrorBucket to I/D machine is first given without explanation, and then proved correct. The translation is mostly given in RLE format for readability, although the occasional translated command ends with  and thus needs to be spelled out. (It's trivial to convert the translated program into either format via run-length encoding or decoding.)

Here's the translation: Before performing the translation, the program is rotated to put the  sequence that was at the end of the program to the start of the program instead. (That is, the program will always start, i.e.  .)

And now for the proof. First, we look at the  with which the program starts, and simply observe the effect it has at the start of the program (i.e. starting with empty RAM and the pointer at 0) by executing it:

Address 0  1  2  3  4  5  6  7  8  9 Command -- [0] 0 0  0  0  0  0  0  0  0    3     3  0  0 [0] 0  0  0  0  0  0    2     3  0 [0] 2  0  0  0  0  0  0    3     3  0  3 [2] 0  0  0  0  0  0    5     3  0  3  7  0  0  0 [0] 0  0    0    [3] 0  3  7  0  0  0  0  0  0    0     3  0  3 [7] 0  0  0  0  0  0    0     3  0  3  7  0  0  0 [0] 0  0    6     3  0  3  7  0  0 [0] 6  0  0    5     3  0  3  7  0 [0] 5  6  0  0    0    [3] 0  3  7  0  0  5  6  0  0

Next, we state how an I/D machine state corresponds to the ErrorBucket state:


 * The I/D machine pointer corresponds to the selected ErrorBucket queue: it points to 0 if neither queue is selected, 3 if the bit bucket is selected, 7 if the data queue is selected.
 * Each ErrorBucket queue element has a corresponding number in the I/D machine state:
 * Inactive data in ErrorBucket corresponds to a value of   in the I/D machine's RAM;
 * Active data in ErrorBucket corresponds to a value of   in the I/D machine's RAM;
 * Inactive bucket in ErrorBucket corresponds to a value of   in the I/D machine's RAM;
 * Active bucket in ErrorBucket corresponds to a value of   in the I/D machine's RAM;
 * Inactive error in ErrorBucket corresponds to a value of   in the I/D machine's RAM.
 * The elements of the bit bucket are stored at RAM elements 10, 13, 16, 19, etc. (going up by 3 each time). RAM address 3 tracks the size of the bit bucket, always pointing to 3 cells before the first unused cell (i.e. once the bit bucket becomes nonempty, it'll point to the most recently pushed bit bucket cell). Cells which exist in this arithmetic sequence, but are "beyond the end" of the bit bucket, contain 0. The bit bucket is ordered in memory so that lower-numbered cells are towards the front of the queue.
 * The elements of the data queue are stored in cells whose addresses are divisible by 3. The first cell being used for the data queue (i.e. the cell containing its head) is pointed to by RAM cell 0. The last cell being used for the data queue (i.e. the cell containing its tail) is pointed to by RAM cell 7. Cells in the sequence 6, 9, 12, 15, etc. that are to the left of the data queue can contain any value. Cells in this sequence that are to the right of the data queue always contain 0. The data queue is ordered in memory so that lower-numbered cells are towards the front of the queue.
 * The remaining RAM elements are 1, 2, 4, 5, and the sequence 8, 11, 14, 17, etc.. These each have fixed values; RAM cell 2 always contains 3, the rest always contain 0.

We can see that the RAM array that exists after startup,, corresponds to a valid ErrorBucket state: no queue is selected (the pointer is at 0), the bit bucket is empty (because RAM element 3 points to 7), and the data queue (which goes from 3 to 6, as indicated by RAM elements 0 and 7 respectively) contains  , i.e. active data, inactive data (or  ). This is in fact the same state in which ErrorBucket programs need to start. As such, by the time we reach the start of the ErrorBucket program (the  is at the end, so after running its translation , the two programs will be in the same place), the two programs have the same state.

Confirming the translation is now a simple matter of confirming that the translation of each command maintains the correspondence between the states of the two languages:


 * In ErrorBucket,  is defined only if the data queue starts with an inactive element (other than an error) and no queue is selected; it makes the element active, then selects the corresponding queue. So in the I/D machine program, we'll always start with the pointer at 0, and a 5 or 1 in the cell it points to. Running   will point the pointer at the start of the data queue; then   will add 2 to that (changing 5=  to 7=, or 1=  to 3= ) and move the pointer to 7 or 3 accordingly, thus maintaining the state correspondence.
 * In ErrorBucket,  is defined only if the data queue starts with an inactive element (other than an error) and no queue is selected; it makes the element active, then selects the corresponding queue. So in the I/D machine program, we'll always start with the pointer at 0, and a 5 or 1 in the cell it points to. Running   will point the pointer at the start of the data queue; then   will add 2 to that (changing 5=  to 7=, or 1=  to 3= ) and move the pointer to 7 or 3 accordingly, thus maintaining the state correspondence.


 * In ErrorBucket,  requires a queue to be selected. So the I/D machine program has the pointer pointing to the tail of that queue. The first   command will point the tail three squares further right (to a 0, due to our invariant that cells beyond the right end of a queue are always zero), and then point the I/D machine pointer at the newly added element. Then the   will change that element to a 1 (i.e. we just pushed a   to the end of the queue in question, as required), and point the I/D machine at cell 1. Finally,   will dereference cell 1; it always contains 0 due to our invariant, so the I/D machine pointer moves back to cell 0, corresponding to a deselection of the queue (as required).
 * In ErrorBucket,  requires a queue to be selected. So the I/D machine program has the pointer pointing to the tail of that queue. The first   command will point the tail three squares further right (to a 0, due to our invariant that cells beyond the right end of a queue are always zero), and then point the I/D machine pointer at the newly added element. Then the   will change that element to a 1 (i.e. we just pushed a   to the end of the queue in question, as required), and point the I/D machine at cell 1. Finally,   will dereference cell 1; it always contains 0 due to our invariant, so the I/D machine pointer moves back to cell 0, corresponding to a deselection of the queue (as required).


 * The way our correspondence is defined means that increasing cell 0 by 3 is performs the operation of shifting the data queue entirely by itself.  is only defined behaviour in ErrorBucket if no queue is selected (i.e. the I/D machine data pointer is at 0), so we know the pointer is at cell 0, and   will increase it by 3 with no other effects.
 * The way our correspondence is defined means that increasing cell 0 by 3 is performs the operation of shifting the data queue entirely by itself.  is only defined behaviour in ErrorBucket if no queue is selected (i.e. the I/D machine data pointer is at 0), so we know the pointer is at cell 0, and   will increase it by 3 with no other effects.


 * This is entirely analogous for the case for ; the only difference is that we're using 5 (i.e.  ), not 1 (i.e.  ). Cell 5 is always maintained at 0, just like cell 1 is, so we get the pointer back to cell 0 without any trouble.
 * This is entirely analogous for the case for ; the only difference is that we're using 5 (i.e.  ), not 1 (i.e.  ). Cell 5 is always maintained at 0, just like cell 1 is, so we get the pointer back to cell 0 without any trouble.


 * The  command in ErrorBucket pushes   onto the selected queue, without deselecting it.   corresponds to 0, and cells to the right of the tail of a queue are always 0. Because   is only defined on a selected queue, this means that the I/D machine pointer will be pointing to a "tail of the queue" storage element; moving the tail three cells to the right (and doing nothing else) will cause a 0 that was just beyond the right-hand end of the queue to now be considered part of the queue, effectively pushing a 0 onto the queue. We can do that with.
 * The  command in ErrorBucket pushes   onto the selected queue, without deselecting it.   corresponds to 0, and cells to the right of the tail of a queue are always 0. Because   is only defined on a selected queue, this means that the I/D machine pointer will be pointing to a "tail of the queue" storage element; moving the tail three cells to the right (and doing nothing else) will cause a 0 that was just beyond the right-hand end of the queue to now be considered part of the queue, effectively pushing a 0 onto the queue. We can do that with.


 * is very similar to, except that it leaves the first element of the queue alone rather than activating it. So the construction is the same as for  , except that we don't do the two increments on the head of the queue while we're there; we just dereference it immediately without altering it.   is only defined in ErrorBucket if the queue starts with an active element (i.e. 7 or 3 after being translated), so the pointer will end up pointing to 7 or 3 accordingly, thus selecting the appropriate queue.
 * is very similar to, except that it leaves the first element of the queue alone rather than activating it. So the construction is the same as for  , except that we don't do the two increments on the head of the queue while we're there; we just dereference it immediately without altering it.   is only defined in ErrorBucket if the queue starts with an active element (i.e. 7 or 3 after being translated), so the pointer will end up pointing to 7 or 3 accordingly, thus selecting the appropriate queue.