disp is under heavy development and much of the codebase is AI-written — explore this website at your peril :)

Two-way programs, compiled into a net

The same programs as on the two-way page, but compiled: each part of the program becomes one agent (a gate for each move, a node for a sequence, a tri, a map, a self), and the value goes through them. There is no walker reading the program as a tree: the value's top node is the run, and a move is done where the value meets its gate.

on
topself▼
running · 1 run going
0 rounds0 meetings0 erasing29 agents now29 most agents
point at a node or a run
the run, forward the run, backward head or reader, mid-step a node's principal port on the way down to a run leaf stem fork about to meet

Every node faces the run

The program stays a tree, and so does the whole net. Each program node's principal port (the port it can meet on, marked with a dot) points the way the run is: up while the run is above it or has finished with it, down into the part it is running while the run is inside. The nodes on the way down to a run all point at it (drawn thick), so a node's facing is all it knows about where the run is. Nothing counts, and nothing is reversed but which port is principal.

A run going down meets a node; going up, it meets the node it came out of. A sequence sends it into its next part, a tri chooses a part by the value's left and puts it back after, a map sends a fork's two halves into its two parts at once and joins them when both are back. A gate does its move and sends the run back up: swap, stem and unit in one meeting, since they only rearrange the top; cross, sink and unit† read a level down in a second. Going down into part of the value (stem†, map, tri) takes a head: one more meeting, to make that part the new top.

Flip, where the run stands

Flip (⇆) turns each run around in time: a root swaps its two wires and its direction, and goes back the way it came, undoing as it goes. A head or reader in the middle of a step finishes the step and comes out turned; a map holding one finished half sends it back down. That is the whole of it: no step has to land first, no other node changes, and nothing is kept on the side. Running back is the net's own rules, the same ones that ran it forward. The check (npm run twoway-compiled-check) flips after every round of every example and of a few thousand small programs, runs back, and gets the net exactly as it was loaded.

Recursion unrolls, and rolls back up

self is the one part that cannot be laid out ahead of time. When a run reaches a self, the self becomes a copy of its fix's body, made in that one meeting (drawn dashed, with the fix's name). When the run comes back out of the copy, either way, the copy is at rest again, the fix's body as written, so it is erased back into a self: erasers take it apart beside the run, off its way, and a run that comes back unrolls it again. Erasing loses nothing, since what goes is program text, not anything the run worked out. Nothing is copied node by node and nothing is merged back, which on the walker net was four meetings in five.

Against the other two

examplethis netits erasing, beside the runthe walker netthe counter machine
not23 meetings, 23 rounds0134, 1347 rounds
add 3 + 240, 4036537, 15218
mirror88, 4460802, 14524
notw, 8 bits269, 462555,611, 22223
copy312, 28957637,824, 1,44195
addw, 4 bits5,617, 2,3968,4781,782,875, 10,517894

The net takes about two rounds for each step of the counter machine: one meeting to do a part and one to move on to the next. Erasing the copies of fix bodies costs about as many meetings again, but they happen beside the run and never hold it up; keeping the copies instead would cost nothing to erase and leave the 4-bit adder holding 8,514 agents at the end rather than 36. Its parallelism is still map's, the same as the counter machine's, since a program that runs back cannot be used up as it runs.

Not done

eval is not compiled: running a program held in the value would need it compiled while the run waits, and put back after. A self's copy is made in one meeting however big the body is, which is cheap here but not what a net with small rules would do.