resource sieve()
  type Wrets = rec(feed: cap(num: int); done: cap())
  op worker() returns c: Wrets

  begin # initial code to start up pipeline
    var n: int, c : Wrets
    writes("find primes 2 ... n? "); read(n)
    c := worker()
    fa i := 2 to n -> send c.feed(i) af
    send c.done()
  end

  # Each worker receives a stream of integers.
  # It keeps the first, discards multiples, and
  # passes the rest onto the next worker, its `child'.
  proc worker() returns c
    op filter(x: int), done(); var n: int
    c.feed := filter; c.done := done
    reply
    receive filter(n)
    write(n)
    var child: Wrets, child_exists := false
    do true ->
      in filter(y) ->
        if y % n != 0 ->
          if not child_exists ->
            child := worker(); child_exists := true
          fi
          send child.feed(y)
        fi
      [] done() ->
        exit
      ni
    od
    if child_exists -> send child.done() fi
  end
end
