blob: a95fb646bda31317bfd300817d33a4fa8206a233 (
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
44
45
46
47
48
49
50
|
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
K.output @@ Format.sprintf "%d@." v;
(delay new_channel ()) >>=
(fun (qi2, qo2) -> doco [ filter v qi qo2 ; primes qi2 ])
end
else return ())
let main : int process =
(delay new_channel ()) >>=
(fun (q_in, q_out) -> doco [ integers 500 q_out ; primes q_in ])
>>= (fun () -> return 42)
end
module Eng = Kahn_stdio.ProtoKahn
module P = Primes(Eng)
let () =
let r = P.K.run P.main in
assert (r = 42);
Format.eprintf "Primes finished (%d \\o/).@." r
|