Sockets for states. Jumpers for transitions. A tape that keeps making room.
Turingtosh is a small Python DSL for defining and running deterministic, binary Turing machines. States are represented by Sockets, transitions by Jumpers, and the execution trace is rendered as a compact table with a visual tape. The notation stays close to the machine itself, without making you write a miniature interpreter just to describe one.
What it does · Get started · A quick example · How it fits together · Run a machine · Inspect the trace · Reference · Canonical style · Examples · Good to know · Design notes
- Defines named states with
AthroughZ, or with customSocketobjects. - Connects state-and-symbol pairs using
>>or<<to createJumpertransitions. - Reads and writes binary tape symbols:
0and1. - Supports stationary, forward, backward, and custom head actions.
- Starts at
Aautomatically whenAis part of the machine, or at an explicitly selected socket. - Expands the tape in either direction as the head moves beyond its current ends.
- Provides
HALTfor an intentional finish andVOIDfor an explicit dead-end branch. - Records execution snapshots and renders them with aligned transitions, a visible head, and optional movement arrows.
- Offers zero-based indexing, slicing, iteration, and Busy Beaver milestone markers.
Put turingtosh.py somewhere on your Python import path, then import the objects you need:
from turingtosh import A, B, HALT, VOID, MachineTuringtosh uses the Python standard library and requires Python 3.10 or newer. A machine is built from one or more Jumpers. Each Jumper describes what happens for one state and one tape symbol.
There is no implicit execution limit. Give the machine a non-negative integer limit when you run it:
machine(42)The number is an upper bound on the number of transitions, not a promise that all of them will run. HALT or VOID can end execution earlier.
This machine starts in A on a blank tape. It reads 0, writes 1, and enters B. The next transition finishes the machine. The other declared input cases are routed explicitly, so there are no dangling branches.
from turingtosh import A, B, HALT, VOID, Machine
machine = Machine(
A(0) >> B(1),
A(1) >> HALT,
B(0) >> VOID,
B(1) >> HALT,
)
trace = machine(42)
print(trace)Output:
|0| A(0) >> B(1) |
░░ ░░ ░░ ◉ ░░ ░░ ░░
BB1 --------------
|1| B(1) >> HALT |
░░ ░░ ░░ ◉ ░░ ░░ ░░
The first transition writes 1, so the head marker becomes ◉. The second transition enters HALT. The requested limit was 42, but the machine had other plans and finished after two transitions.
A Socket names a machine state. Turingtosh provides the single-letter sockets A through Z:
A
B
CCalling a socket with 0 or 1 attaches a tape link to it:
A(0)
A(1)
B(0)
B(1)The bare socket is a state name. The linked socket describes that state's behavior for one symbol:
A(0)means: the machine is in stateAand the current tape cell contains0.A(1)means: the machine is in stateAand the current tape cell contains1.
A socket can have at most one outgoing transition for each link. In other words, A(0) and A(1) are two different cases, and each case gets its own Jumper.
You are not limited to the alphabet. Create a custom socket with Socket:
from turingtosh import Socket
SCAN = Socket("SCAN")
WRITE = Socket("WRITE")Use custom sockets in exactly the same way:
SCAN(0) >> WRITE(1)The name is just the state label. Keep it non-empty and avoid leading or trailing whitespace; the interpreter has enough to keep track of already.
A Jumper connects one linked source socket to a destination socket. The source link is the symbol being read. The destination link is the symbol to write.
A(0) >> B(1)This reads 0 while in state A, writes 1 on the current tape cell, and enters state B. The head stays where it is unless an action is attached.
The two operators express which side of the expression contains the source:
| Syntax | Source | Destination |
|---|---|---|
A(0) >> B(1) |
A(0) |
B(1) |
A(0) << B(1) |
B(1) |
A(0) |
So these two declarations describe the same transition:
B(1) >> A(0)
A(0) << B(1)>> reads naturally from source to destination, so it is the recommended direction for ordinary state tables. << is perfectly valid; just avoid mixing directions without a reason.
Important
>> and << do not move the tape head. They describe the orientation of the Jumper expression. Head movement is controlled separately by forward, backward, or a custom action.
The tape is binary. Each cell contains either 0 or 1, and every cell starts at 0.
| Property | Behavior |
|---|---|
| Initial displayed width | 7 cells |
| Initial head position | Position 0, centered in the initial tape |
| Cell values | 0 or 1 |
| Expansion | Automatic, in blocks of 7 cells |
| Direction | Expands to the left and to the right |
The head begins at tape position 0. The initial seven cells cover positions -3 through 3. Moving beyond either end expands the tape automatically; there is no fixed edge to run into.
Both terminal sockets stop execution as soon as the machine enters them. Neither accepts a link, writes a tape value, moves the head, nor acts as a source for another Jumper.
Use them to express different intent:
| Socket | Use it when... |
|---|---|
HALT |
The computation has reached its intended finish. |
VOID |
This branch is intentionally a dead end or an otherwise unused case. |
For example:
B(0) >> VOID
B(1) >> HALTThis explicitly handles both possible symbols in state B. Reading 0 abandons that branch; reading 1 finishes normally.
VOID is not a state that waits for another transition. It is a terminal sink: entering it ends the run without changing the tape. Its purpose is to make an intentionally unused branch explicit, rather than leaving a transition undefined or labelling every dead end as a successful finish.
Caution
A missing Jumper is still an error if execution reaches it. VOID does not silently catch missing transitions. Declare the branch that should lead to it, for example B(0) >> VOID.
Pass one or more Jumpers to Machine:
from turingtosh import A, B, C, HALT, VOID, Machine
machine = Machine(
A(0) >> B(1),
A(1) >> C(0),
B(0) >> VOID,
B(1) >> HALT,
C(0) >> HALT,
C(1) >> VOID,
)Each (state, read-symbol) pair must be unique. Duplicate Jumpers for the same pair are rejected.
If A is one of the machine's declared states, this is enough:
machine(42)Turingtosh starts at the bare socket A automatically. No extra start declaration is needed.
To start elsewhere, supply the bare socket and the limit:
machine(B, 42)The starting socket must belong to the machine and must be unlinked. Use B, not B(0): a linked socket describes a transition case, not a starting state.
Custom sockets work too:
machine(SCAN, 1337)The call signature is always one of these:
machine(limit)
machine(starting_socket, limit)The limit is mandatory and must be a non-negative integer. Each call starts a fresh run with a blank tape and the head back at position 0; calling the machine again does not resume the previous run.
The limit is the maximum number of transitions to execute. A terminal socket can end the run before the limit is reached.
machine(37)
machine(42)
machine(1337)The printed transition indexes are zero-based. A run of 42 transitions is labelled 0 through 41, not 1 through 42.
A machine call returns an Info object. The machine also keeps that run's trace available through indexing:
trace = machine(42)
print(trace)
print(machine[...])machine[...] selects the complete recorded run. It returns Info, just like an integer index or a slice.
machine[0]
machine[3]
machine[-1]Indexes are zero-based. Negative indexes count from the end, so machine[-1] is the last recorded transition.
Slices follow normal Python conventions: the start is included and the stop is excluded.
machine[3:7]
machine[:5]
machine[10:]
machine[4:2]machine[3:7] selects transition indexes 3, 4, 5, and 6. machine[4:2] is an empty selection, because the stop comes before the start. Printing an empty selection gives:
No transitions executed.
Tip
Use machine[3:7] to inspect a small window of a long run instead of printing the entire trace. The tape may be infinite in spirit, but your terminal is not.
An Info selection can be printed directly or queried through its properties:
| Property | What it describes |
|---|---|
step |
Zero-based transition index |
home |
Source state name |
into |
Destination state name |
read |
Symbol read by the transition |
puts |
Symbol present after the transition's write behavior |
move |
Action associated with the Jumper |
transition |
Formatted Jumper declaration |
arrow |
Actual head-movement direction, if it moved |
tape |
Tape snapshot after the transition |
head |
Head index within that tape snapshot |
origin |
Tape position represented by the first snapshot cell |
position |
Absolute tape position of the head |
A single-step selection gives scalar values for these properties. A multi-step selection gives tuples of values or tape snapshots.
Info selections can also be indexed, sliced, iterated, and measured:
first = machine[0]
window = machine[3:7]
all_steps = machine[...]
print(first.transition)
print(first.tape)
print(len(window))
for step in window:
print(step.transition)The machine itself can be iterated after a run as well:
for step in machine:
print(step.transition)Each transition is printed above the tape snapshot produced by that transition.
| Display | Meaning |
|---|---|
░░ |
Cell contains 0 |
██ |
Cell contains 1 |
◎ |
Head is over a cell containing 0 |
◉ |
Head is over a cell containing 1 |
-> |
Head actually moved right during this transition |
<- |
Head actually moved left during this transition |
The movement arrow appears only when the head moved. A transition using >> can still leave the head in place, and one using << can move it right if its attached action says so. The tape is shown after the write and movement have taken place.
| Form | Meaning |
|---|---|
A |
Unlinked socket representing state A |
A(0) |
State A when the current cell reads 0 |
A(1) |
State A when the current cell reads 1 |
A(0) >> B(1) |
Read 0 in A, write 1, enter B |
A(0) << B(1) |
Read 1 in B, write 0, enter A |
B(0) >> HALT |
Enter the terminal HALT socket without writing or moving |
B(0) >> VOID |
Enter the terminal VOID socket without writing or moving |
The source socket must have a link. A normal destination socket must also have a link. Terminal destinations (HALT and VOID) are the exception: they never take a link.
A Jumper defaults to stayhere; no action needs to be written for a stationary transition.
| Action | Behavior |
|---|---|
stayhere |
Leave the head at its current position |
forward |
Move the head one cell right |
backward |
Move the head one cell left |
Attach an action to a Jumper with &:
from turingtosh import A, B, HALT, Machine, forward
machine = Machine(
(A(0) >> B(1)) & forward,
B(0) >> HALT,
B(1) >> HALT,
)The write described by the destination link happens first. The attached action then runs as part of the transition, handling its movement or extra tape work. Terminal destinations must use the default stayhere action.
A custom action is a callable that accepts the running Machine and returns None. It can move the head or write to the tape using the machine's public methods:
def hop(machine):
machine.move_head(2)
machine.write(0)
machine = Machine(
(A(0) >> B(1)) & hop,
B(0) >> HALT,
B(1) >> HALT,
)
machine(37)move_head(distance) moves by an integer distance. write(value) writes 0 or 1 at the head's current position; an optional position can be supplied to write to target another tape cell. Keep custom actions focused: the Jumper already describes the read, write, and destination state, so the action is best used for the extra movement or tape operation that does not fit in that declaration.
Important
Custom actions must return None. Returning another value is treated as an invalid action result. An action attached to a transition into HALT or VOID is not allowed; terminal transitions do not move or write.
Turingtosh provides five named execution limits:
| Constant | Transition count | Separator appears after transition index |
|---|---|---|
BB1 |
1 | 0 |
BB2 |
6 | 5 |
BB3 |
21 | 20 |
BB4 |
107 | 106 |
BB5 |
47,176,870 | 47,176,869 |
Use them directly as limits:
from turingtosh import BB3, Machine
trace = machine(BB3)The separator is a visual checkpoint, not a stop instruction. Reaching BB3, for example, prints the BB3 line and execution continues until the machine enters a terminal socket or reaches its supplied limit. Since the printed indexes are zero-based, a checkpoint for N transitions appears after row N - 1.
The syntax allows a fair amount of freedom. A few conventions make a machine definition much easier to check by eye.
Keep each state's two possible read cases together, and list 0 before 1:
machine = Machine(
A(0) >> B(1),
A(1) >> C(0),
B(0) >> VOID,
B(1) >> A(0),
C(0) >> HALT,
C(1) >> VOID,
)This makes the transition table scan like a truth table: state first, tape symbol second. Missing cases become much easier to spot.
Use >> consistently so the source is always on the left and the destination is always on the right:
A(0) >> B(1)
B(1) >> C(0)
C(0) >> HALTUsing only << is also valid, but then the source is always on the right. Both conventions work. Mixing them makes the state table harder to audit, and the tape head will not care which convention you picked.
If a reachable state has an input case that is intentionally not part of the computation, say so explicitly:
B(0) >> VOID
B(1) >> A(0)Do not leave a reachable (state, symbol) pair undefined, and do not send every unused branch to HALT just because it is terminal. Reserve HALT for the intended completion of the computation; use VOID for an explicitly abandoned branch.
stayhere is the default, so this:
A(0) >> B(1)is preferable to spelling out the no-op action. Add forward, backward, or a custom action only when the head needs to move or perform additional work.
A machine is a transition table. Give each row its own line, use trailing commas, and keep the source cases grouped:
machine = Machine(
A(0) >> B(1),
A(1) >> C(0),
B(0) >> VOID,
B(1) >> A(0),
C(0) >> HALT,
C(1) >> VOID,
)The extra lines are cheaper than debugging a transition table written as punctuation soup.
This machine alternates between A and B, changing the symbol it reads on every pass. All four state-and-symbol cases are defined, so the run can continue until its limit.
from turingtosh import A, B, Machine
machine = Machine(
A(0) >> B(1),
A(1) >> B(0),
B(0) >> A(1),
B(1) >> A(0),
)
trace = machine(42)
print(machine[3:7])The slice prints only transition indexes 3 through 6. It is a small window into the run, not a new execution.
A custom socket is useful when the built-in letters stop being descriptive:
from turingtosh import HALT, Socket, Machine
SCAN = Socket("SCAN")
machine = Machine(
SCAN(0) >> HALT,
SCAN(1) >> HALT,
)
machine(SCAN, 1337)The start is explicit because the state is named SCAN, not A.
Note
A transition describes one read case. It does not mean “do this whenever the machine is in state A.” A(0) and A(1) are separate cases, and a machine may need both.
- The tape starts blank, with every cell set to
0. - The head starts at absolute tape position
0. - Tape expansion happens automatically in blocks of seven cells, to either side.
- Each machine call starts a fresh run; it does not continue from the previous call's final tape.
- A run limit is mandatory. It must be a non-negative integer.
- A terminal transition into
HALTorVOIDdoes not write a symbol or move the head. - A missing transition raises
Erroronly when execution reaches that state-and-symbol pair. - A machine cannot declare two Jumpers for the same source state and read symbol.
- The printed transition indexes start at
0, while the run limit counts transitions. A limit of 42 permits indexes0through41. machine[4:2]is an empty slice, andmachine[...]selects the whole recorded trace.
Turingtosh keeps the machine description close to the classic parts: a finite set of named states, a binary tape, and explicit transitions. Sockets make the read cases visible. Jumpers make the source, write value, and destination visible in one expression. Actions handle the occasional head movement without turning the declaration into a pile of implementation details.
The default path is intentionally small: define the Jumpers, call the machine with a limit, inspect the Info trace. The extra conveniences are there when they help, not because every machine needs them. A transition table should be easier to reason about than the problem it is trying to solve. That is the whole idea.