Self-modifying Turing machine
The states, the current state, the data pointer, the input and the output all live on one tape of bits.
Hakerh400, 2020 · Turing complete: unknown · ●●● hard · esolangs.org
Tested with 3 known programs · Test method
Run it
The idea
Hakerh400 made this machine in 2020. It is a Turing machine without a separate program. The source is a string of bits at the start of memory, the input follows it, and the machine parses everything it needs from memory again before each step. The table of states, the current state and the data pointer are bits like any other, so a step can rewrite them.
The machine
Memory starts with the source. Each input bit follows as a pair, a 1 and then the bit. The machine halts as soon as bit 0 is 1. Bit 1 says whether the structures are read from the even or the odd bits after it.
| Structure | Bits |
|---|---|
| State index size n | A run of 1s and the 0 after it, counted together |
| Current state | n bits |
| States | 2^n states, each with a part for a pointed bit of 0 and a part for 1 |
| Data pointer size | Written the way n is |
| Data pointer | That many bits, an address anywhere in memory |
A part holds xor, move, direction and the next state. A step moves the data pointer if move is 1 and writes it back, writes the next state, and flips the pointed bit if xor is 1. When the machine halts, the output is read the way the input was written, starting at the pair after the data pointer.
Example
The cat.
The cat. State 0 flips bit 0, so the machine halts after one step, and the input pairs are still where the output is read.
Flip the first bit. The data pointer is 6 bits wide and points at bit 63, the first input bit. State 0 flips it and moves right, and 63 + 1 wraps to 0. State 1 then flips bit 0, which halts the machine.
The longer sample has 8 states and a 5-bit data pointer that points at bit 3. None of its states flips a bit, so it never halts; it goes from state 7 to state 5 and back.
Patterns it teaches
- Self-modifying code: the table of states is data the machine can flip.
- Code as data: program, registers and data share one tape.
See also
Self-modifying
Write your own in Run Convert a program from Self-modifying Turing machine
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: self-modifying-turing-machine.
Command line
shib run self-modifying-turing-machine -e '000000100000000000000000000000000000000000' --input '00000000110'shib manual self-modifying-turing-machineSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/self-modifying-turing-machine.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org