anchor.diff:    }
   
     let run n =
  -    printf "Async:        "
  +    printf "Async:         "
       use cancelSource = new System.Threading.CancellationTokenSource ()
       let timer = Stopwatch.StartNew ()
       let ps = Async.RunSynchronously (primes n, -1, cancelSource.Token)
  
positive.code:     result
  
    let run n =
      printf "HopacCh:      "
      let timer = Stopwatch.StartNew ()
      let ps = run (primes n)
      let d = timer.Elapsed
  
negative.code:     result
  
    let run n =
      printf "Sequential:    "
      let timer = Stopwatch.StartNew ()
      let ps = primes n
      let d = timer.Elapsed
      printf "%7d - %fs\n" ps.[ps.Length-1] d.TotalSeconds
      d
  
  /////////////////////////////////////////////////////////////////////////
  
  module HopacPromise =
    type [<NoComparison>] Stream<'a> = {Value: 'a; Next: Promise<Stream<'a>>}
  
    let rec iterate (step: 'a -> 'a) (init: 'a) : Job<Stream<_>> = Job.thunk <| fun () ->
      {Value = init;
       Next = Promise.Now.delay (iterate step (step init))}
  
    let rec filter (pred: 'a -> bool) (xs: Stream<'a>) : Job<Stream<'a>> =
      let next = xs.Next >>= filter pred
      if pred xs.Value then
        Job.result {Value = xs.Value; Next = Promise.Now.delay next}
      else
        next
  
    let sieve : Job<Stream<_>> =
      let rec sieve nats = Job.thunk <| fun () ->
        let prime = nats.Value
        {Value = prime;
         Next =
          Promise.Now.delay (nats.Next >>= filter (fun x -> x % prime <> 0) >>= sieve)}
      iterate (fun x -> x+1) 2 >>= sieve
  
    let primes n = Job.delay <| fun () ->
      let before = GC.GetTotalMemory true
      let result = Array.zeroCreate n
      let rec loop i primes =
        if i < n then
          result.[i] <- primes.Value
          primes.Next >>= loop (i+1)
        else
          printf "%5d b/p " (max 0L (GC.GetTotalMemory true - before) / int64 n)
          Job.result result
      sieve >>= loop 0
  
    let run n =
      printf "PromiseStream: "
      let timer = Stopwatch.StartNew ()
      let ps = run (primes n)
      let d = timer.Elapsed
      printf "%7d - %fs\n" ps.[ps.Length-1] d.TotalSeconds
      d
  