let Tree.cmd.type = enum(read.node, add.value, walk.tree, kill.tree) let Tree.cmd = record $tree.crad: Tree.cmd.type command Kind new.value sink Kind reply $tree.cmd;
let add.to.tree = ... and walk = ... and die = ...
let node = para proc ( Kind value //value of this node source Tree.crad get.cmd //source of commands ; read.node, add.value, walk.tree, kill.tree) %node: let (l.cmd, r.cmd) = (New.funnel Tree.cmd, New.funnel Tree.cmd)
with (left.child, right.child) = (False, False) using valof ^iterate: let c = 5get.cmd switch command ! c into factions: case read.node: (reply ! c) ! value //send the value of this node
case add.value: let v = new.value ! c //value to be added if v = value do exit actions //value already in tree switch v < value into % left.or.right: case True: //left insert if left.child do Jleft.delegate: (Sink.of l.cmd) ! (add.to.tree ! v) exit actions %lcft.delegate; node ! (v, Source.of l.cmd) iterate <- (True, right.child) case False: //right insert if right.child do fright.delegate: (Sink.of r.cmd) } (add.to.tree ! v) exit actions fright.delegate; node ! (v, Source.of r.cmd) iterate <- (left.child, True) %left.or.right;
case walk.tree: let answer = reply ! c if left.child do Jwalk.left: (Sink.of l.cmd) ! (walk ! answer) Jwalk.left; answer ! value if right.child do $walk.right: (Sink.of r.cmd) ! (walk ! answer) $walk.right;
case kill.tree: if left.child do $kill.left: (Sink.of l.cmd) ! die Jkill.left; if right.child do Jkill.right: (Sink.of r.cmd) ! die $kill.right; exit node //commit suicide factions; iterate <- (left.child, right.child) //changed only by add.value ^iterate; do skip //all work done in using clause /Cnode;
Note that although walking the tree requires every node to be visited, the reply from each node can be sent directly to the interested party, without having to be passed back up the tree. (For simplicity, we may suppose that the tree contains a recognisable maximal element, so that it is not necessary to send an end-of-walk signal.)
References:
Hunt, J. G. (1978) Hunt, J. G. "An introduction to LIMP: an experimental language for the implementation of messages and processes" pp110-120
in (1978) 5 Fachtagung über Programmiersprachen 1978