Two-way programs, step by step
A program here is a tree, put together from five moves that rearrange a tree and a few ways to combine programs. A run is a tree of dots, each a program on a value, and there are two things you can do to it: fire makes one small move at one dot, and flip turns a dot around without computing anything, so that firing runs it back. Dots that do not wait on each other fire at once.
swap cross sink stem unit eval id, a ; b, a†, tri(a, b, c), map(a, b, c), fix(b) with self inside, the shorthands first(f) second(f) par(f, g) when(f), the library's names, or any tree as {△△△}. A value: △ △v (u, v) T F, numbers, a word 0b1011, registers <0b10, 0b01>, or a program
as a tree, [add].One move: cross trades the right parts of a fork and its left half, ((u, v), w) becomes ((u, w), v). Every node is kept; only the links change. Done twice it is undone, so it is its own inverse.
This program as one tree (4 nodes)
cross
△△△△
How to read the tree
Each dot is a program on a value: the purple tree on its left, the value on its
right. A dot runs the parts of its program's sequence one after another, and its count of how many have run shows in the program: the parts the value has been
through fade, and the part the dot is at wears a ring. A live dot (the dashed ring) wears the move it
will make, and a move fires in place. When a combinator or a call starts, it takes the value apart
and hands the parts to new dots, and the parts of its program that run travel up into them, each
leaving a dotted slot in the program. map and tri hold
on to the node they opened, which stays where the dot was inside a blue (map) or amber (tri) ring;
the others stay a dashed bud, the way the tree-calculus visualizer draws an application waiting on
the ones inside it. A part that has run comes back into its slot, and when every dot above a ring or
bud has run, it closes, putting back what it held, and the count goes on. A dot whose count is at
the end has run, and its ring turns solid.
▸ fires every move that can be made right now (one round), or just the next one with parallel off; ▶ keeps firing, and ◂ takes the last action back. ⇆ flips the whole run. Clicking a live dot fires it, clicking a ring or a waiting bud plays the part under it until that part has run, and clicking a dot that has run flips just that dot. A program with a † on top runs backward, and its dot is drawn blue. The moves and big named parts inside a program are pods, which open when they reach a dot of their own; click any part to fold it, and a pod to open it.
The pieces
A program is read by its top shape, the way tree calculus reads one: a leaf, a stem or a fork, and for a fork, the shape of its left part.
| tree | written | on a value x |
|---|---|---|
△ | id | x as it is |
(△, b) | b† | b run backward |
(△a, b) | a ; b | a, then b on what a gave |
((a, b), c) | tri(a, b, c) | x is a fork (s, y), and s's shape picks: a on y for a leaf, b on (u, y) for a stem △u, c on (p, (q, y)) for a fork (p, q); then s goes back together |
△((a, b), c) | map(a, b, c) | a on a stem's inside, or b and c on a fork's two halves, at once |
△(△, b) | fix(b) | b, where self runs b again |
△△ | self | the innermost fix's b, again |
△△△ | swap | (u, v) becomes (v, u) |
△△△△ | cross | ((u, v), w) becomes ((u, w), v) |
△△△△△ | sink | △(u, v) becomes (△u, v), and (△u, v) becomes △(u, v) |
△△△△△△ | stem | x becomes △x |
△△△△△△△ | unit | x becomes (△, x) |
△△△△△△△△ | eval | (q, z) becomes (q, q z): the left part run as a program |
swap, cross and sink leave a tree they do not fit as it is,
so each is its own inverse, and the others build on them: rot, which makes
((u, v), w) into (u, (v, w)), is first(swap) ; cross ; swap. Any other tree is not a
program, and running it gets stuck. So does a move with nothing to undo: backward, stem needs a stem and unit a (△, x). first(f), second(f) and par(f, g) are short for map(id, f, id), map(id, id, f) and map(id, f, g), and when(f) for tri(id, tri(f, id, id), id): when the left part is T, f runs on the right.
Even not is built from the moves: unit ; swap ; tri(stem, id, id) ; swap ; tri(stem, id, id)† ; swap ; unit†. It
makes an empty slot beside the bit, sets the slot where the bit is F, clears the bit where the slot
stayed F, and drops the cleared bit. Then cnot is when(not), and
flipping every bit of a word is fix(map(not, self, self)).
Fire and flip
A dot reads its program as a sequence of parts, with every sequence inside opened up and a † on a
sequence reversing the order of its parts and turning each one around. Its program is never written
to while it runs: where the dot is in it is one number, the count. Fire rewrites one dot and looks
no further than the dots just under it: one of the five moves rearranges its value and the count
goes up one; a combinator or a call opens dots for its parts (a map or tri whose parts are single moves does them in the same step); or, once those dots have
all run, it closes and the count goes up. The same rules run a program backward: a move runs as its
inverse (stem as stem†, unit as unit†, and the
other three as themselves), and the parts run in the other order. No move copies or drops a tree:
the five moves only relink, add or take away the nodes they name, and a dot with a part in flight
keeps every node it takes apart until it puts it back. Copying is a program like any other
(copy below), and it walks the tree it copies a node at a time.
Flip computes nothing. On a dot, it turns the way the dot goes and counts from the other end: two
parts into a seven-part sequence, flipped, is five parts into it run backward. A dot with a part in
flight flips the dots under it too. Flip is its own inverse. Undoing a move is flip, fire, flip,
which is why there is no table
of inverse rules, and why going back needs no record of the run: as in Bennett's reversible machines, nothing was thrown away. Firing add on (3, 2) ends with (3, 5), its count at the
end. Flipped, it is add run backward from (3, 5), its count at the start; firing it
ends with (3, 2), and flipped again it is add on (3, 2), where it started.
What runs in parallel
map starts both halves of a fork at once, and nothing else waits on them until they
are done. A word is a balanced tree of bits, each in a stem, so notw reaches all of
its bits together: 8 bits take 23 rounds and 64 bits take 35, four more each time the word
doubles. The work grows with the number of bits; the rounds
grow with the depth of the tree.
Adding is harder, because a carry can travel the whole word. addw adds in place, (a, b) → (a, a + b), with nothing left over, the way a carry-lookahead adder does: a
status for every part of the word goes up the tree (does it kill a carry, pass one on, or make
one), the carries come down it, every b becomes its bit of the sum, and then the carries are taken
back out, which works because the carries of a + not(a + b) are the carries of a + b. It is a long program in small moves, and how long depends a little on the
bits: 730 to 940 rounds at 4 bits, 990 to 1,230 at 8, and 1,850 to 2,060 at 64. Its rounds,
too, grow with the depth of the tree and not its width.
Programs are trees
A program is a tree, so a program can be a value. unit puts (△, ·) on top of a tree,
which turns a program into its dagger, the program that runs it backward, and eval runs the left part of a fork on the right part and keeps it. So first(unit) ; eval,
given ([add], (2, 7)), subtracts. Tree calculus reads a program the same way, by its top shape. Its triage, which picks by
the shape of a value and forgets it, comes back here as tri and map, which
put the shape back. Its K and S, which drop and copy their argument, have no counterpart: nothing is
dropped, and copying is a program.
The library
Everything in the examples is built from the pieces above, nothing else. A name marked part is a piece of a bigger program, and its self is that program's.
Bits
not | unit ; swap ; tri(stem, id, id) ; swap ; tri(stem, id, id)† ; swap ; unit†F ⇄ T: an empty slot is set where the bit is F, the bit is cleared where the slot stayed F, and dropped |
cnot | when(not)(T, t) → (T, not t): when the left is T, not runs on the right |
toffoli | tri(id, id, when(cnot))((T, T), t) → ((T, T), not t) |
Trees and numbers
rot | first(swap) ; cross ; swap((u, v), w) ⇄ (u, (v, w)): three moves, each its own inverse |
xch | rot† ; first(swap) ; rot(u, (v, w)) ⇄ (v, (u, w)) |
transpose | rot ; second(xch) ; rot†((a, b), (c, d)) ⇄ ((a, c), (b, d)) |
add | fix(tri(id, self ; second(stem), id))(a, b) → (a, a + b), numbers counted in stems |
dup | fix(tri(id, self ; second(stem), second(second(unit)) ; rot† ; transpose ; par(self, self) ; transpose ; rot))(x, △) → (x, x): fills an empty place with a copy of x |
copy | unit ; swap ; dupx → (x, x) |
mirror | fix(map(self, self, self) ; swap)swaps the halves of every fork |
Words, all bits at once
notw | fix(map(not, self, self))flips every bit of a word, all at once |
cnotw | fix(map(cnot, self, self))<a, b> → <a, b xor a>, bit by bit |
The adder
prep | fix(map(unit, self, self))gives each bit pair (a, b) an empty carry: (△, (a, b)) |
negb | fix(map(second(second(not)), self, self))flips every b |
sumbitpart | second(cnot) ; xch ; second(cnot) ; xch(c, (a, b)) → (c, (a, b xor a xor c)) |
sumz | fix(map(sumbit, self, self))every b becomes its bit of the sum |
flatpart | rot ; second(rot)((c, (a, b)), s) ⇄ (c, (a, (b, s))) |
leafspart | flat ; second(second(when(stem)) ; xch ; second(when(stem)) ; xch) ; flat†a bit pair's status s = a + b: 0 kills a carry, 1 passes it on, 2 makes one |
pickpart | cross ; first(dup) ; cross((s, x), △) → ((s, x), s) |
nodespart | rot ; tri(id, tri(second(pick), second(second(second(stem ; stem))), id), id) ; rot†a half-pair's status: the high half's, unless it passes, then the low half's |
annpart | unit ; swap ; tri(id, leafs, nodes) ; swapx → (status, x), once the parts of x have theirs |
up | fix(map(id, self, self) ; ann)every part x of the word becomes (status, x), from the bits up |
copart | tri(id, tri(first(cnot), second(first(second(not))), id), id)(s, ((c, △), x)) → (s, ((c, out), x)): the carry out of a part, given the carry in |
carrypart | rot ; second(swap) ; second(first(unit ; swap)) ; co ; second(first(swap)) ; second(rot) ; xch ; second(second(swap)) ; second(rot†)(low, c) → (carry into high, (low, c)) |
leafdpart | first(swap) ; rot ; second(swap) ; rot† ; first(swap)((△, ab), c) → ((c, ab), △) |
nodedpart | second(carry) ; xch ; rot† ; second(swap) ; par(self, self) ; second(unit ; swap)works out the carry into the high half, then goes down both halves at once |
down | fix(second(ann†) ; swap ; tri(id, leafd, noded) ; swap ; unit†)(carry in, (s, x)) → x: each status comes off once it has been used, and the carries go down into the slots |
carries | up ; unit ; downfills every carry slot |
addw | prep ; carries ; sumz ; negb ; carries† ; negb ; prep†<a, b> → <a, a + b>: the carries of a + not(a + b) are those of a + b, so they come back out |
Where it comes from
Putting reversible programs together from primitive bijections with sequence, a choice and a
parallel step is the design of James and Sabry's language Π, whose three ways to combine programs
are sequential, choice and parallel composition (Carette, James and Sabry). Here the choice is made by a tree's shape instead of a sum type, as tree calculus
makes it, and the result is put back so the choice is never lost. The strand lattice runs a
similar instruction set as agents (evaluators/rust-ic-strands/crate/src/twoway.rs).