summaryrefslogtreecommitdiff
path: root/src/primes.ml
blob: e0eeed78d38d013e0e257d20767685fc4b245af2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module Primes (K : Kahn.S) = struct
  module K = K
  module Lib = Kahn.Lib(K)
  open K
  open Lib

  let integers nmax (qo : int out_port) : unit process =
    let rec loop n =
      if n > nmax then
        put (-1) qo
      else
        (put n qo) >>= (fun () -> loop (n+1))
    in
    loop 2

  let filter n (qi : int in_port) (qo : int out_port) : unit process =
    let rec loop () =
      (get qi) >>= (fun v ->
        if v <> -1 then
          (if v mod n = 0 then return () else put v qo) >>= loop
        else
          put v qo)
    in loop()

  let rec primes (qi : int in_port) : unit process =
    (get qi) >>= (fun v ->
      if v <> -1 then begin
        Format.printf "%d@." v;
        (delay new_channel ()) >>=
        (fun (qi2, qo2) -> doco [ filter v qi qo2 ; primes qi2 ])
      end else return ())

  let main : unit process =
    (delay new_channel ()) >>=
    (fun (q_in, q_out) -> doco [ integers 2000 q_out ; primes q_in ])

end

module Eng = Kahn.Pipe
module P = Primes(Eng)

let () = P.K.run P.main