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.
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
| example | this net | its erasing, beside the run | the walker net | the counter machine |
|---|---|---|---|---|
not | 23 meetings, 23 rounds | 0 | 134, 134 | 7 rounds |
add 3 + 2 | 40, 40 | 36 | 537, 152 | 18 |
mirror | 88, 44 | 60 | 802, 145 | 24 |
notw, 8 bits | 269, 46 | 255 | 5,611, 222 | 23 |
copy | 312, 289 | 576 | 37,824, 1,441 | 95 |
addw, 4 bits | 5,617, 2,396 | 8,478 | 1,782,875, 10,517 | 894 |
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.